Skip to content

Commit 558e49b

Browse files
authored
build(devs-infra): improve type generation script (#2492)
1 parent 693245c commit 558e49b

9 files changed

+390
-609
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ website/src/**/*.js
88
presets/index.d.ts
99
*.config.js
1010
.eslintrc.js
11-
src/generated-raw-compiler-options.d.ts

e2e/__tests__/module-kinds/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const runTestForOptions = (options: TestOptions, preset: AllPresetType = AllPres
5858
}
5959

6060
// eslint-disable-next-line jest/no-export
61-
export const runTestCases = (moduleKind: string, preset: AllPresetType = AllPreset.DEFAULT): void => {
61+
export const runTestCases = (moduleKind: RawCompilerOptions['module'], preset: AllPresetType = AllPreset.DEFAULT): void => {
6262
runTestForOptions({ module: moduleKind }, preset)
6363
runTestForOptions({ module: moduleKind, allowSyntheticDefaultImports: false }, preset)
6464
runTestForOptions({ module: moduleKind, allowSyntheticDefaultImports: true }, preset)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"test:external-repos": "npm run test:external external-repos",
3030
"lint": "node_modules/.bin/eslint --ext .js,.ts .",
3131
"lint:fix": "node_modules/.bin/eslint --fix --ext .js,.ts .",
32-
"lint:prettier": "prettier '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'CONTRIBUTING.md' 'README.md' 'TROUBLESHOOTING.md' --write --ignore-path .gitignore",
33-
"lint:prettier-ci": "prettier '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'CONTRIBUTING.md' 'README.md' 'TROUBLESHOOTING.md' --check --ignore-path .gitignore",
32+
"lint:prettier": "prettier 'scripts/**' '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'CONTRIBUTING.md' 'README.md' 'TROUBLESHOOTING.md' --write --ignore-path .gitignore",
33+
"lint:prettier-ci": "prettier 'scripts/**' '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'CONTRIBUTING.md' 'README.md' 'TROUBLESHOOTING.md' --check --ignore-path .gitignore",
3434
"doc": "cd website && npm run start",
3535
"doc:build": "cd website && npm run build",
3636
"changelog": "node_modules/.bin/conventional-changelog -p angular -i CHANGELOG.md -s -r 1",

scripts/generate-raw-compiler-options.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@ const fs = require('fs')
22

33
const { compile } = require('json-schema-to-typescript')
44
const fetch = require('node-fetch')
5+
const execa = require('execa')
56

6-
const { generatedPath } = require('./lib/paths')
7+
const { generatedPath, rawCompilerOptionsFileName } = require('./lib/paths')
78

89
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
910
const generateRawTsCompilerOptions = async () => {
1011
const response = await fetch('https://json.schemastore.org/tsconfig')
1112
const tsconfigSchema = await response.json()
12-
13-
return compile(
13+
const resultString = await compile(
1414
{
1515
title: 'RawCompilerOptions',
1616
type: 'object',
1717
properties: tsconfigSchema.definitions.compilerOptionsDefinition.properties,
1818
additionalProperties: false,
1919
},
20-
'generated-raw-compiler-options'
20+
'raw-compiler-options'
21+
)
22+
23+
return (
24+
resultString
25+
.substring(resultString.indexOf('compilerOptions?:'), resultString.lastIndexOf('}'))
26+
.replace('compilerOptions?:', 'export interface RawCompilerOptions')
27+
// Allow to specify string value like `module: 'amd'` besides valid value `module: 'AMD'`
28+
.replace(/&\n {6}string/g, '| string')
29+
// Remove invalid type `[k: string]: unknown`
30+
.replace(/\| {\n {10}\[k: string]: unknown;\n {8}}/g, '')
2131
)
2232
}
2333

24-
void generateRawTsCompilerOptions().then((rawCompilerOptions) => {
25-
fs.writeFileSync(generatedPath, rawCompilerOptions)
34+
void generateRawTsCompilerOptions().then((resultString) => {
35+
fs.writeFileSync(generatedPath, resultString)
36+
execa.sync('eslint', [rawCompilerOptionsFileName, '--fix'])
2637
})
2738

2839
module.exports = {

scripts/lib/paths.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare const testsRootDir: string
1212
declare const e2eTestsDir: string
1313
declare const projectsToRun: string[]
1414
declare const generatedPath: string
15+
declare const rawCompilerOptionsFileName: string
1516

1617
export {
1718
rootDir,
@@ -28,4 +29,5 @@ export {
2829
e2eTestsDir,
2930
projectsToRun,
3031
generatedPath,
32+
rawCompilerOptionsFileName,
3133
}

scripts/lib/paths.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const projectsToRun = [
2424
`${e2eExternalRepoDir}/simple-project-references`,
2525
`${e2eExternalRepoDir}/yarn-workspace-composite`,
2626
]
27-
const generatedPath = path.join(process.cwd(), 'src', 'generated-raw-compiler-options.d.ts')
27+
const rawCompilerOptionsFileName = path.join('src', 'raw-compiler-options.ts')
28+
const generatedPath = path.join(process.cwd(), rawCompilerOptionsFileName)
2829

2930
module.exports = {
3031
pkgDigestFile,
@@ -41,4 +42,5 @@ module.exports = {
4142
e2eTestsDir,
4243
projectsToRun,
4344
generatedPath,
45+
rawCompilerOptionsFileName,
4446
}

scripts/post-build.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
const fs = require('fs')
2-
3-
const { generateRawTsCompilerOptions } = require('./generate-raw-compiler-options')
41
const { computePackageDigest } = require('./lib/bundle')
5-
const { generatedPath } = require('./lib/paths')
2+
const execa = require('execa')
3+
const { generatedPath, rawCompilerOptionsFileName } = require('./lib/paths')
64

7-
void (async () => {
8-
const rawCompilerOptions = await generateRawTsCompilerOptions()
9-
if (rawCompilerOptions !== fs.readFileSync(generatedPath, 'utf-8')) {
10-
throw new Error('Tsconfig options have changed. The generated file should be regenerated')
11-
}
12-
})()
5+
if (execa.sync('git', ['diff-index', '--name-only', 'HEAD']).stdout.includes(rawCompilerOptionsFileName)) {
6+
throw new Error(
7+
`Tsconfig options have changed. Please check the modified generated ${generatedPath} and commit the change`
8+
)
9+
}
1310

1411
computePackageDigest()

0 commit comments

Comments
 (0)