Skip to content

Commit

Permalink
build(devs-infra): improve type generation script (#2492)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Apr 5, 2021
1 parent 693245c commit 558e49b
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 609 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Expand Up @@ -8,4 +8,3 @@ website/src/**/*.js
presets/index.d.ts
*.config.js
.eslintrc.js
src/generated-raw-compiler-options.d.ts
2 changes: 1 addition & 1 deletion e2e/__tests__/module-kinds/helpers.ts
Expand Up @@ -58,7 +58,7 @@ const runTestForOptions = (options: TestOptions, preset: AllPresetType = AllPres
}

// eslint-disable-next-line jest/no-export
export const runTestCases = (moduleKind: string, preset: AllPresetType = AllPreset.DEFAULT): void => {
export const runTestCases = (moduleKind: RawCompilerOptions['module'], preset: AllPresetType = AllPreset.DEFAULT): void => {
runTestForOptions({ module: moduleKind }, preset)
runTestForOptions({ module: moduleKind, allowSyntheticDefaultImports: false }, preset)
runTestForOptions({ module: moduleKind, allowSyntheticDefaultImports: true }, preset)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -29,8 +29,8 @@
"test:external-repos": "npm run test:external external-repos",
"lint": "node_modules/.bin/eslint --ext .js,.ts .",
"lint:fix": "node_modules/.bin/eslint --fix --ext .js,.ts .",
"lint:prettier": "prettier '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'CONTRIBUTING.md' 'README.md' 'TROUBLESHOOTING.md' --write --ignore-path .gitignore",
"lint:prettier-ci": "prettier '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'CONTRIBUTING.md' 'README.md' 'TROUBLESHOOTING.md' --check --ignore-path .gitignore",
"lint:prettier": "prettier 'scripts/**' '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'CONTRIBUTING.md' 'README.md' 'TROUBLESHOOTING.md' --write --ignore-path .gitignore",
"lint:prettier-ci": "prettier 'scripts/**' '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'CONTRIBUTING.md' 'README.md' 'TROUBLESHOOTING.md' --check --ignore-path .gitignore",
"doc": "cd website && npm run start",
"doc:build": "cd website && npm run build",
"changelog": "node_modules/.bin/conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
Expand Down
23 changes: 17 additions & 6 deletions scripts/generate-raw-compiler-options.js
Expand Up @@ -2,27 +2,38 @@ const fs = require('fs')

const { compile } = require('json-schema-to-typescript')
const fetch = require('node-fetch')
const execa = require('execa')

const { generatedPath } = require('./lib/paths')
const { generatedPath, rawCompilerOptionsFileName } = require('./lib/paths')

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
const generateRawTsCompilerOptions = async () => {
const response = await fetch('https://json.schemastore.org/tsconfig')
const tsconfigSchema = await response.json()

return compile(
const resultString = await compile(
{
title: 'RawCompilerOptions',
type: 'object',
properties: tsconfigSchema.definitions.compilerOptionsDefinition.properties,
additionalProperties: false,
},
'generated-raw-compiler-options'
'raw-compiler-options'
)

return (
resultString
.substring(resultString.indexOf('compilerOptions?:'), resultString.lastIndexOf('}'))
.replace('compilerOptions?:', 'export interface RawCompilerOptions')
// Allow to specify string value like `module: 'amd'` besides valid value `module: 'AMD'`
.replace(/&\n {6}string/g, '| string')
// Remove invalid type `[k: string]: unknown`
.replace(/\| {\n {10}\[k: string]: unknown;\n {8}}/g, '')
)
}

void generateRawTsCompilerOptions().then((rawCompilerOptions) => {
fs.writeFileSync(generatedPath, rawCompilerOptions)
void generateRawTsCompilerOptions().then((resultString) => {
fs.writeFileSync(generatedPath, resultString)
execa.sync('eslint', [rawCompilerOptionsFileName, '--fix'])
})

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions scripts/lib/paths.d.ts
Expand Up @@ -12,6 +12,7 @@ declare const testsRootDir: string
declare const e2eTestsDir: string
declare const projectsToRun: string[]
declare const generatedPath: string
declare const rawCompilerOptionsFileName: string

export {
rootDir,
Expand All @@ -28,4 +29,5 @@ export {
e2eTestsDir,
projectsToRun,
generatedPath,
rawCompilerOptionsFileName,
}
4 changes: 3 additions & 1 deletion scripts/lib/paths.js
Expand Up @@ -24,7 +24,8 @@ const projectsToRun = [
`${e2eExternalRepoDir}/simple-project-references`,
`${e2eExternalRepoDir}/yarn-workspace-composite`,
]
const generatedPath = path.join(process.cwd(), 'src', 'generated-raw-compiler-options.d.ts')
const rawCompilerOptionsFileName = path.join('src', 'raw-compiler-options.ts')
const generatedPath = path.join(process.cwd(), rawCompilerOptionsFileName)

module.exports = {
pkgDigestFile,
Expand All @@ -41,4 +42,5 @@ module.exports = {
e2eTestsDir,
projectsToRun,
generatedPath,
rawCompilerOptionsFileName,
}
17 changes: 7 additions & 10 deletions scripts/post-build.js
@@ -1,14 +1,11 @@
const fs = require('fs')

const { generateRawTsCompilerOptions } = require('./generate-raw-compiler-options')
const { computePackageDigest } = require('./lib/bundle')
const { generatedPath } = require('./lib/paths')
const execa = require('execa')
const { generatedPath, rawCompilerOptionsFileName } = require('./lib/paths')

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

computePackageDigest()

0 comments on commit 558e49b

Please sign in to comment.