Skip to content

Commit 66e91f0

Browse files
Copilothotlong
andcommitted
fix: address code review - use execFileSync and TypeScript API for fallback
- Replace execSync with execFileSync to avoid shell injection risks - Log tsc errors instead of silently swallowing them - Use TypeScript compiler API (ts.transpileModule) as fallback instead of fragile regex-based type stripping Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 77464c8 commit 66e91f0

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

scripts/patch-console-plugin.cjs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Node.js cannot execute .ts files natively, so we transpile it to
66
* JavaScript and update the package exports before deployment.
77
*/
8-
const { execSync } = require('child_process');
8+
const { execFileSync } = require('child_process');
99
const fs = require('fs');
1010
const path = require('path');
1111

@@ -30,22 +30,34 @@ if (fs.existsSync(pluginJs)) {
3030
console.log('[patch-console-plugin] Transpiling plugin.ts → plugin.js ...');
3131

3232
try {
33-
// Try using tsc via npx first
34-
execSync(
35-
`npx tsc --outDir "${consolePkgDir}" --declaration false --module nodenext --moduleResolution nodenext --target es2022 --esModuleInterop true --skipLibCheck true "${pluginTs}"`,
36-
{ stdio: 'inherit' }
37-
);
38-
} catch {
39-
// Fallback: manual transpilation (the file has no complex TS features)
40-
console.log('[patch-console-plugin] tsc failed, using manual transpilation ...');
41-
let source = fs.readFileSync(pluginTs, 'utf-8');
42-
// Remove type annotations: `: any`, `: string`, `: Record<...>`, `as const`
43-
source = source
44-
.replace(/:\s*Record<string,\s*string>\s*=/g, ' =')
45-
.replace(/\bas const\b/g, '')
46-
.replace(/:\s*any/g, '')
47-
.replace(/:\s*string/g, '');
48-
fs.writeFileSync(pluginJs, source, 'utf-8');
33+
// Use tsc to transpile the single file (paths passed as array args, not shell string)
34+
execFileSync('npx', [
35+
'tsc', '--outDir', consolePkgDir,
36+
'--declaration', 'false',
37+
'--module', 'nodenext',
38+
'--moduleResolution', 'nodenext',
39+
'--target', 'es2022',
40+
'--esModuleInterop', 'true',
41+
'--skipLibCheck', 'true',
42+
pluginTs,
43+
], { stdio: 'inherit' });
44+
} catch (err) {
45+
console.warn('[patch-console-plugin] tsc failed:', err.message);
46+
console.warn('[patch-console-plugin] Falling back to TypeScript API transpilation ...');
47+
// Fallback: use TypeScript compiler API for reliable transpilation
48+
const ts = require('typescript');
49+
const source = fs.readFileSync(pluginTs, 'utf-8');
50+
const result = ts.transpileModule(source, {
51+
compilerOptions: {
52+
module: ts.ModuleKind.NodeNext,
53+
moduleResolution: ts.ModuleResolutionKind.NodeNext,
54+
target: ts.ScriptTarget.ES2022,
55+
esModuleInterop: true,
56+
declaration: false,
57+
},
58+
fileName: 'plugin.ts',
59+
});
60+
fs.writeFileSync(pluginJs, result.outputText, 'utf-8');
4961
}
5062

5163
// 2. Update package.json to point to plugin.js

0 commit comments

Comments
 (0)