packages/toolkit/scripts/build/generateConfigurationAttributes.ts (58 lines of code) (raw):
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as fs from 'fs' // eslint-disable-line no-restricted-imports
import * as path from 'path'
import { JSONSchema4 } from 'json-schema'
import { compile } from 'json-schema-to-typescript'
import * as nlsJson from '../../package.nls.json'
const config = [
{
debugger: 'aws-sam',
requestType: 'direct-invoke' as const,
// writes to core-lib, because that is where the code that uses the definitions lives
// TODO: Drop down all code that uses definitions to packages/toolkit and stop writing definitions to core lib.
outputFile: '../core/src/shared/sam/debugger/awsSamDebugConfiguration.gen.ts',
imports: ["import * as vscode from 'vscode'"],
topLevelClass: 'AwsSamDebuggerConfiguration',
},
]
const docRegex = /%([a-zA-Z.]{1,})%/g
const replacer: (match: string, key: string) => string = (match, key) =>
(nlsJson as { [key: string]: string })[key].replace(/\n/g, '\n* ') ?? match
function addBaseClass(generated: string, topLevelClass: string): string {
return generated.replace(topLevelClass, `${topLevelClass} extends vscode.DebugConfiguration`)
}
async function generateConfigurationAttributes(): Promise<void> {
const packageJson = JSON.parse(fs.readFileSync('./package.json', { encoding: 'utf-8' }))
for (const debugConfiguration of packageJson.contributes.debuggers) {
const debuggerConfig = config.find((cfg) => {
return cfg.debugger === debugConfiguration.type
})
if (debuggerConfig === undefined) {
continue
}
if (!fs.existsSync(path.dirname(debuggerConfig.outputFile))) {
throw new Error(`Output folder of ${debuggerConfig.outputFile} does not exist.`)
}
const header = `
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* This file is not generated from the core library. Please regenerate from
* 'packages/toolkit', e.g. \`npm run generateConfigurationAttributes -w packages/toolkit\`
*/
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/
import * as vscode from 'vscode'
`
// JSONSchema4 impl doesn't like properties with type undefined, but the compilation works correctly
const schema = debugConfiguration.configurationAttributes[debuggerConfig.requestType] as any as JSONSchema4
await compile(schema, 'DirectInvoke', { bannerComment: header })
.then((ts) => addBaseClass(ts, debuggerConfig.topLevelClass))
.then((ts) => ts.replace(docRegex, replacer))
.then((ts) => fs.writeFileSync(debuggerConfig.outputFile, ts))
}
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
await generateConfigurationAttributes()
})()