Files
tsdeno/ts/tsdeno.classes.tsdeno.ts

60 lines
1.8 KiB
TypeScript

import * as plugins from './plugins.js';
const shell = new plugins.smartshell.Smartshell({
executor: 'bash',
});
export class TsDeno {
public cwd: string;
constructor(cwdArg?: string) {
this.cwd = cwdArg || process.cwd();
}
/**
* Wraps `deno compile` with package.json isolation.
* Temporarily removes package.json so Deno doesn't resolve devDependencies
* into the compiled binary, then restores it afterwards.
*/
public async compile(args: string[]): Promise<void> {
const packageJsonPath = plugins.path.join(this.cwd, 'package.json');
const backupPath = plugins.path.join(this.cwd, 'package.json.bak');
let hasPackageJson = false;
try {
await plugins.fs.access(packageJsonPath);
hasPackageJson = true;
} catch {
hasPackageJson = false;
}
// Ensure --node-modules-dir=none is present
if (!args.some((arg) => arg.startsWith('--node-modules-dir'))) {
args = ['--node-modules-dir=none', ...args];
}
// Temporarily hide package.json from Deno
if (hasPackageJson) {
console.log('tsdeno: temporarily hiding package.json to exclude devDependencies from bundle');
await plugins.fs.rename(packageJsonPath, backupPath);
}
try {
const shellCommand = `cd ${this.cwd} && deno compile ${args.join(' ')}`;
console.log(`tsdeno: running deno compile ${args.join(' ')}`);
const result = await shell.execPassthrough(shellCommand);
if (result.exitCode !== 0) {
console.error(`tsdeno: deno compile exited with code ${result.exitCode}`);
process.exit(result.exitCode);
}
} finally {
// Always restore package.json
if (hasPackageJson) {
await plugins.fs.rename(backupPath, packageJsonPath);
console.log('tsdeno: restored package.json');
}
}
}
}