-
Notifications
You must be signed in to change notification settings - Fork 0
/
do-build.ts
121 lines (105 loc) · 4.4 KB
/
do-build.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
import type { PackageJson } from '@npm/types';
import { build } from 'esbuild';
import { copyFile, mkdir, readFile, rm, writeFile } from 'fs/promises';
import * as TypeScriptJsonSchema from 'typescript-json-schema';
import type { BuilderSources } from './build-scripts/builder-sources.interface';
import { getBuilderSources } from './build-scripts/get-builder-sources.script';
import { getDirectories } from './build-scripts/get-directories.script';
type PackageJsonKey = keyof PackageJson | string;
const descriptionByBuilderName: Map<string, string> = new Map<string, string>([
['circular-dependencies-linter', 'Linter for circular dependencies'],
]);
const distPath: string = `${__dirname}/dist`;
rm(distPath, { force: true, recursive: true })
.then(() => mkdir(distPath))
.then(() => getDirectories(`${__dirname}/packages`))
.then((builderSourceDirectories: string[]) =>
Promise.all(
builderSourceDirectories.map(async (sourceDirectory: string) => await getBuilderSources(sourceDirectory))
)
)
.then(async (builderSources: BuilderSources[]) => {
builderSources.forEach(async (sources: BuilderSources) => {
const { entryPointPath, name }: BuilderSources = sources;
const outDirPath: string = `${__dirname}/dist/${name}`;
await build({
chunkNames: 'chunks/[name]-[hash]',
bundle: true,
splitting: false,
external: ['@angular-devkit/architect', '@angular-devkit/core', 'pnpapi', 'rxjs'],
minify: true,
platform: 'node',
sourcemap: false,
target: 'node16',
format: 'cjs',
treeShaking: true,
tsconfig: './tsconfig.builders.json',
mainFields: ['module', 'main', 'browser'],
color: true,
metafile: true,
legalComments: 'linked',
outdir: outDirPath,
entryPoints: [entryPointPath],
});
await generateBuilderSchema(sources, outDirPath);
});
await generateBuildersJson(builderSources);
await generatePackageJson();
});
async function generateBuilderSchema({ name, optionsPath }: BuilderSources, outDirPath: string): Promise<void> {
const program: TypeScriptJsonSchema.Program = TypeScriptJsonSchema.getProgramFromFiles([optionsPath]);
const builderSchema: TypeScriptJsonSchema.Definition | null = TypeScriptJsonSchema.generateSchema(
program,
'BuilderOptions'
);
if (builderSchema === null) {
throw new Error(`Builder schema creation for "${name}" failed.`);
}
await writeFile(`${outDirPath}/schema.json`, JSON.stringify(builderSchema));
}
async function generateBuildersJson(builderSources: BuilderSources[]): Promise<void> {
const buildersJsonContent: object = builderSources.reduce((accumulatedData: object, { name }: BuilderSources) => {
return {
...accumulatedData,
[name]: {
description: descriptionByBuilderName.get(name),
schema: `./${name}/schema.json`,
implementation: `./${name}/builder.js`,
},
};
}, {});
await writeFile(`${__dirname}/dist/builders.json`, JSON.stringify({ builders: buildersJsonContent }));
}
async function generatePackageJson(): Promise<void> {
const rootPackageJsonData: Record<PackageJsonKey, unknown> = await readFile(
`${__dirname}/package.json`,
'utf-8'
).then((fileData: string) => JSON.parse(fileData));
const allowedFields: Set<PackageJsonKey> = new Set<PackageJsonKey>([
'dependencies',
'name',
'version',
'license',
'author',
'bugs',
'description',
'homepage',
'keywords',
'repository',
'icon',
]);
const sanitizedPackageJsonEntries = Object.entries(rootPackageJsonData).filter(([key, value]: [string, unknown]) => {
const stringifiedValue: string = JSON.stringify(value);
const valueIsEmpty: boolean = stringifiedValue === '' || stringifiedValue === '[]' || stringifiedValue === '{}';
return allowedFields.has(key) && !valueIsEmpty;
});
const resultPackageJsonContent: Record<PackageJsonKey, unknown> = Object.fromEntries([
...sanitizedPackageJsonEntries,
['builders', './builders.json'],
]);
await writeFile(`${__dirname}/dist/package.json`, JSON.stringify(resultPackageJsonContent));
await mkdir(`${__dirname}/dist/assets`);
await copyFile(`${__dirname}/assets/icon.png`, `${__dirname}/dist/assets/icon.png`);
await copyFile(`${__dirname}/LICENSE.md`, `${__dirname}/dist/LICENSE.md`);
await copyFile(`${__dirname}/README.md`, `${__dirname}/dist/README.md`);
}