mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 15:42:39 +08:00
d871e57e44
* add extra dest file * delete old .d.ts
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
const { spawnSync } = require('child_process');
|
|
const { join, extname, basename, dirname } = require('path');
|
|
const { readFileSync, existsSync, unlinkSync } = require('fs');
|
|
const ts = require('typescript');
|
|
|
|
const tscExecutableName = process.platform === 'win32' ? 'tsc.cmd' : 'tsc';
|
|
const tscExecutablePath = join(__dirname, '..', 'node_modules', '.bin', tscExecutableName);
|
|
const tssConfigDir = join(__dirname, '..');
|
|
const tscConfigPath = join(tssConfigDir, 'tsconfig-gendecls.json');
|
|
const extraDestFiles = [
|
|
join(__dirname, './embedded-cocos-3d.d.ts'),
|
|
];
|
|
|
|
function generate () {
|
|
const tscConfig = ts.readConfigFile(tscConfigPath, (path) => readFileSync(path).toString());
|
|
if (tscConfig.error) {
|
|
console.error(`Bad tsconfig.json: ${tscConfig.error.messageText}`);
|
|
return undefined;
|
|
}
|
|
|
|
const outputJSPath = join(tssConfigDir, tscConfig.config.compilerOptions.outFile);
|
|
console.log(outputJSPath);
|
|
|
|
const extName = extname(outputJSPath);
|
|
if (extName !== '.js') {
|
|
console.error(`Unexpected output extension ${extName}, please check it.`);
|
|
return undefined;
|
|
}
|
|
|
|
const dirName = dirname(outputJSPath);
|
|
const baseName = basename(outputJSPath, extName);
|
|
const destExtensions = [
|
|
'.d.ts',
|
|
'.d.ts.map',
|
|
];
|
|
|
|
const destFiles = destExtensions.map((destExtension) => {
|
|
const destFile = join(dirName, baseName + destExtension);
|
|
if (existsSync(destFile)) {
|
|
console.log(`Delete old ${destFile}.`);
|
|
unlinkSync(destFile);
|
|
}
|
|
|
|
return destFile;
|
|
});
|
|
|
|
console.log(`Generating...`);
|
|
|
|
spawnSync(tscExecutablePath, [
|
|
'-p',
|
|
tscConfigPath,
|
|
], {
|
|
stdio: [ null, process.stdout, process.stderr ]
|
|
});
|
|
|
|
return destFiles.concat(extraDestFiles);
|
|
}
|
|
|
|
module.exports = { generate };
|