Skip to content

Commit 5465262

Browse files
committed
build: use native ESM and improve templates script performance
This commit refactors the `scripts/templates.mts` file with several improvements: - Uses a native ESM import for `@angular/ng-dev`. - Replaces all synchronous `fs` calls with their `fs.promises` counterparts. - Optimizes file reading by using `Promise.all` to read files concurrently.
1 parent 1c154aa commit 5465262

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

scripts/templates.mts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,42 @@
77
*/
88

99
import lodash from 'lodash';
10-
import * as fs from 'node:fs';
10+
import { readFile, writeFile } from 'node:fs/promises';
1111
import * as path from 'node:path';
1212
import { releasePackages } from './packages.mjs';
1313

1414
const __dirname = import.meta.dirname;
1515

16-
async function _runTemplate(inputPath: string, outputPath: string) {
16+
async function runTemplate(inputPath: string, outputPath: string) {
1717
inputPath = path.resolve(__dirname, inputPath);
1818
outputPath = path.resolve(__dirname, outputPath);
1919

2020
console.info(`Building ${path.relative(path.dirname(__dirname), outputPath)}...`);
2121

22-
// TODO(ESM): Consider making this an actual import statement.
23-
const { COMMIT_TYPES, ScopeRequirement } = await new Function(
24-
`return import('@angular/ng-dev');`,
25-
)();
22+
const { COMMIT_TYPES, ScopeRequirement } = await import('@angular/ng-dev');
2623

27-
const monorepo = JSON.parse(fs.readFileSync('./.monorepo.json', 'utf-8'));
28-
const content = lodash.template(fs.readFileSync(inputPath, 'utf-8'))({
24+
const [monorepoRaw, templateContent] = await Promise.all([
25+
readFile('./.monorepo.json', 'utf-8'),
26+
readFile(inputPath, 'utf-8'),
27+
]);
28+
29+
const monorepo = JSON.parse(monorepoRaw);
30+
const content = lodash.template(templateContent)({
2931
monorepo,
3032
packages: releasePackages.map(({ name }) => name),
31-
encode: (x: string) => global.encodeURIComponent(x),
33+
encode: (x: string) => encodeURIComponent(x),
3234
// Pass-through `ng-dev` ESM commit message information for the `contributing.ejs`
3335
// template. EJS templates using the devkit template cannot use ESM.
3436
COMMIT_TYPES: COMMIT_TYPES,
3537
ScopeRequirement: ScopeRequirement,
3638
});
37-
fs.writeFileSync(outputPath, content, 'utf-8');
39+
await writeFile(outputPath, content, 'utf-8');
3840
}
3941

40-
export default async function (_options: {}): Promise<number> {
42+
export default async function (): Promise<number> {
4143
await Promise.all([
42-
_runTemplate('./templates/readme.ejs', '../README.md'),
43-
_runTemplate('./templates/contributing.ejs', '../CONTRIBUTING.md'),
44+
runTemplate('./templates/readme.ejs', '../README.md'),
45+
runTemplate('./templates/contributing.ejs', '../CONTRIBUTING.md'),
4446
]);
4547

4648
return 0;

0 commit comments

Comments
 (0)