-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathutils.ts
223 lines (198 loc) · 6.23 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import execa from 'execa';
import { promises as fs } from 'fs';
import path from 'path';
import { format as prettierFormat } from 'prettier';
import type { Options as PrettierOptions } from 'prettier';
import { MonorepoFiles, Placeholders } from './constants';
import type { FileMap } from './fs-utils';
import { readAllFiles, writeFiles } from './fs-utils';
const PACKAGE_TEMPLATE_DIR = path.join(__dirname, 'package-template');
const REPO_ROOT = path.join(__dirname, '..', '..');
const REPO_TS_CONFIG = path.join(REPO_ROOT, MonorepoFiles.TsConfig);
const REPO_TS_CONFIG_BUILD = path.join(REPO_ROOT, MonorepoFiles.TsConfigBuild);
const REPO_PACKAGE_JSON = path.join(REPO_ROOT, MonorepoFiles.PackageJson);
const PACKAGES_PATH = path.join(REPO_ROOT, 'packages');
const allPlaceholdersRegex = new RegExp(
Object.values(Placeholders).join('|'),
'gu',
);
// Our lint config really hates this, but it works.
// eslint-disable-next-line
const prettierRc = require(path.join(
REPO_ROOT,
'.prettierrc.js',
)) as PrettierOptions;
/**
* The data necessary to create a new package.
*/
export type PackageData = Readonly<{
name: string;
description: string;
directoryName: string;
nodeVersions: string;
currentYear: string;
}>;
/**
* Data parsed from relevant monorepo files.
*/
type MonorepoFileData = {
tsConfig: Tsconfig;
tsConfigBuild: Tsconfig;
nodeVersions: string;
};
/**
* A parsed tsconfig file.
*/
type Tsconfig = {
references: { path: string }[];
[key: string]: unknown;
};
/**
* A parsed package.json file.
*/
type PackageJson = {
engines: { node: string };
[key: string]: unknown;
};
/**
* Reads the monorepo files that need to be parsed or modified.
*
* @returns A map of file paths to file contents.
*/
export async function readMonorepoFiles(): Promise<MonorepoFileData> {
const [tsConfig, tsConfigBuild, packageJson] = await Promise.all([
fs.readFile(REPO_TS_CONFIG, 'utf-8'),
fs.readFile(REPO_TS_CONFIG_BUILD, 'utf-8'),
fs.readFile(REPO_PACKAGE_JSON, 'utf-8'),
]);
return {
tsConfig: JSON.parse(tsConfig) as Tsconfig,
tsConfigBuild: JSON.parse(tsConfigBuild) as Tsconfig,
nodeVersions: (JSON.parse(packageJson) as PackageJson).engines.node,
};
}
/**
* Finalizes package and repo files, writes them to disk, and performs necessary
* postprocessing (e.g. running `yarn install`).
*
* @param packageData - The package data.
* @param monorepoFileData - The monorepo file data.
*/
export async function finalizeAndWriteData(
packageData: PackageData,
monorepoFileData: MonorepoFileData,
) {
const packagePath = path.join(PACKAGES_PATH, packageData.directoryName);
try {
await fs.stat(packagePath);
throw new Error(`The package directory already exists: ${packagePath}`);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
throw error;
}
}
console.log('Writing package and monorepo files...');
// Read and write package files
await writeFiles(packagePath, await processTemplateFiles(packageData));
// Write monorepo files
updateTsConfigs(packageData, monorepoFileData);
await writeJsonFile(
REPO_TS_CONFIG,
JSON.stringify(monorepoFileData.tsConfig),
);
await writeJsonFile(
REPO_TS_CONFIG_BUILD,
JSON.stringify(monorepoFileData.tsConfigBuild),
);
// Postprocess
// Add the new package to the lockfile.
console.log('Running "yarn install"...');
await execa('yarn', ['install'], { cwd: REPO_ROOT });
// Add the new package to the root readme content
console.log('Running "yarn update-readme-content"...');
await execa('yarn', ['update-readme-content'], { cwd: REPO_ROOT });
}
/**
* Formats a JSON file with `prettier` and writes it to disk.
*
* @param filePath - The absolute path of the file to write.
* @param fileContent - The file content to write.
*/
async function writeJsonFile(
filePath: string,
fileContent: string,
): Promise<void> {
await fs.writeFile(
filePath,
await prettierFormat(fileContent, { ...prettierRc, parser: 'json' }),
);
}
/**
* Updates the tsconfig file data in place to include the new package.
*
* @param packageData - = The package data.
* @param monorepoFileData - The monorepo file data.
*/
function updateTsConfigs(
packageData: PackageData,
monorepoFileData: MonorepoFileData,
): void {
const { tsConfig, tsConfigBuild } = monorepoFileData;
tsConfig.references.push({
path: `./${path.basename(PACKAGES_PATH)}/${packageData.directoryName}`,
});
tsConfig.references.sort((a, b) => a.path.localeCompare(b.path));
tsConfigBuild.references.push({
path: `./${path.basename(PACKAGES_PATH)}/${
packageData.directoryName
}/tsconfig.build.json`,
});
tsConfigBuild.references.sort((a, b) => a.path.localeCompare(b.path));
}
/**
* Reads the template files and updates them with the specified package data.
*
* @param packageData - The package data.
* @returns A map of file paths to processed template file contents.
*/
async function processTemplateFiles(
packageData: PackageData,
): Promise<FileMap> {
const result: FileMap = {};
const templateFiles = await readAllFiles(PACKAGE_TEMPLATE_DIR);
for (const [relativePath, content] of Object.entries(templateFiles)) {
result[relativePath] = processTemplateContent(packageData, content);
}
return result;
}
/**
* Processes the template file content by replacing placeholders with relevant values
* from the specified package data.
*
* @param packageData - The package data.
* @param content - The template file content.
* @returns The processed template file content.
*/
function processTemplateContent(
packageData: PackageData,
content: string,
): string {
const { name, description, nodeVersions, currentYear } = packageData;
return content.replace(allPlaceholdersRegex, (match) => {
switch (match) {
case Placeholders.CurrentYear:
return currentYear;
case Placeholders.NodeVersions:
return nodeVersions;
case Placeholders.PackageName:
return name;
case Placeholders.PackageDescription:
return description;
case Placeholders.PackageDirectoryName:
return packageData.directoryName;
/* istanbul ignore next: should be impossible */
default:
throw new Error(`Unknown placeholder: ${match}`);
}
});
}