From 4e148ebd94fa122cd8576f83de7a2ef210344a0b Mon Sep 17 00:00:00 2001 From: dragolea Date: Tue, 12 Nov 2024 14:04:20 +0200 Subject: [PATCH] fix(env): fixed `@Env` when project was `mbt build` for production ready The `mbt build` was given an error during `npm install` due to newly `@Env` decorator. --- dist/postinstall/PostInstall.js | 42 ++++++++++++------------- postinstall/util/GenerateEnv.ts | 54 +++++++++++++++------------------ 2 files changed, 45 insertions(+), 51 deletions(-) diff --git a/dist/postinstall/PostInstall.js b/dist/postinstall/PostInstall.js index dafe5bf..61019fa 100644 --- a/dist/postinstall/PostInstall.js +++ b/dist/postinstall/PostInstall.js @@ -73,7 +73,7 @@ var GenerateEnv = class { export ${typeDefinitions}`); } compileEnvFile() { - this.executeShellCommand("tsc", [ + this.executeShellCommand("npx tsc", [ this.envFilePath, "--outDir", this.dispatcherFolderPath @@ -83,30 +83,28 @@ export ${typeDefinitions}`); this.appendLineIfAbsent(this.gitignoreFilePath, "@dispatcher"); } updatePackageJsonImports() { - if (!(0, import_fs.existsSync)(this.packageJsonFilePath)) { - throw new Error("Could not find package.json"); - } - const packageJson = JSON.parse((0, import_fs.readFileSync)(this.packageJsonFilePath, "utf8")); - packageJson.imports = packageJson.imports || {}; - if (!packageJson.imports["#dispatcher"]) { - packageJson.imports["#dispatcher"] = "./@dispatcher/index.js"; - (0, import_fs.writeFileSync)(this.packageJsonFilePath, JSON.stringify(packageJson, null, 2)); + if ((0, import_fs.existsSync)(this.packageJsonFilePath)) { + const packageJson = JSON.parse((0, import_fs.readFileSync)(this.packageJsonFilePath, "utf8")); + packageJson.imports = packageJson.imports || {}; + if (!packageJson.imports["#dispatcher"]) { + packageJson.imports["#dispatcher"] = "./@dispatcher/index.js"; + (0, import_fs.writeFileSync)(this.packageJsonFilePath, JSON.stringify(packageJson, null, 2)); + } } } updateTsconfigInclude() { - if (!(0, import_fs.existsSync)(this.tsconfigFilePath)) { - throw new Error("Could not find tsconfig.json"); - } - const tsconfigContent = (0, import_fs.readFileSync)(this.tsconfigFilePath, "utf8"); - const errors = []; - const tsconfig = (0, import_jsonc_parser.parse)(tsconfigContent, errors); - if (errors.length > 0) { - throw new Error("tsconfig.json contains comments or invalid JSON format, which is not allowed."); - } - tsconfig.include = tsconfig.include || []; - if (!tsconfig.include.includes("./@dispatcher")) { - tsconfig.include.push("./@dispatcher"); - (0, import_fs.writeFileSync)(this.tsconfigFilePath, JSON.stringify(tsconfig, null, 2)); + if ((0, import_fs.existsSync)(this.tsconfigFilePath)) { + const tsconfigContent = (0, import_fs.readFileSync)(this.tsconfigFilePath, "utf8"); + const errors = []; + const tsconfig = (0, import_jsonc_parser.parse)(tsconfigContent, errors); + if (errors.length > 0) { + throw new Error("tsconfig.json contains comments or invalid JSON format, which is not allowed."); + } + tsconfig.include = tsconfig.include || []; + if (!tsconfig.include.includes("./@dispatcher")) { + tsconfig.include.push("./@dispatcher"); + (0, import_fs.writeFileSync)(this.tsconfigFilePath, JSON.stringify(tsconfig, null, 2)); + } } } appendLineIfAbsent(filePath, line) { diff --git a/postinstall/util/GenerateEnv.ts b/postinstall/util/GenerateEnv.ts index d9f648b..9a6eabc 100644 --- a/postinstall/util/GenerateEnv.ts +++ b/postinstall/util/GenerateEnv.ts @@ -46,7 +46,7 @@ export class GenerateEnv { } private compileEnvFile() { - this.executeShellCommand('tsc', [this.envFilePath, '--outDir', this.dispatcherFolderPath]); + this.executeShellCommand('npx tsc', [this.envFilePath, '--outDir', this.dispatcherFolderPath]); } private addToGitignore() { @@ -54,38 +54,34 @@ export class GenerateEnv { } private updatePackageJsonImports() { - if (!existsSync(this.packageJsonFilePath)) { - throw new Error('Could not find package.json'); - } - - const packageJson = JSON.parse(readFileSync(this.packageJsonFilePath, 'utf8')); - packageJson.imports = packageJson.imports || {}; - - if (!packageJson.imports['#dispatcher']) { - packageJson.imports['#dispatcher'] = './@dispatcher/index.js'; - writeFileSync(this.packageJsonFilePath, JSON.stringify(packageJson, null, 2)); + if (existsSync(this.packageJsonFilePath)) { + const packageJson = JSON.parse(readFileSync(this.packageJsonFilePath, 'utf8')); + packageJson.imports = packageJson.imports || {}; + + if (!packageJson.imports['#dispatcher']) { + packageJson.imports['#dispatcher'] = './@dispatcher/index.js'; + writeFileSync(this.packageJsonFilePath, JSON.stringify(packageJson, null, 2)); + } } } private updateTsconfigInclude() { - if (!existsSync(this.tsconfigFilePath)) { - throw new Error('Could not find tsconfig.json'); - } - - const tsconfigContent = readFileSync(this.tsconfigFilePath, 'utf8'); - const errors: ParseError[] = []; - - const tsconfig = parseJsonc(tsconfigContent, errors); - - if (errors.length > 0) { - throw new Error('tsconfig.json contains comments or invalid JSON format, which is not allowed.'); - } - - // Ensure `include` property is present and update if needed - tsconfig.include = tsconfig.include || []; - if (!tsconfig.include.includes('./@dispatcher')) { - tsconfig.include.push('./@dispatcher'); - writeFileSync(this.tsconfigFilePath, JSON.stringify(tsconfig, null, 2)); + if (existsSync(this.tsconfigFilePath)) { + const tsconfigContent = readFileSync(this.tsconfigFilePath, 'utf8'); + const errors: ParseError[] = []; + + const tsconfig = parseJsonc(tsconfigContent, errors); + + if (errors.length > 0) { + throw new Error('tsconfig.json contains comments or invalid JSON format, which is not allowed.'); + } + + // Ensure `include` property is present and update if needed + tsconfig.include = tsconfig.include || []; + if (!tsconfig.include.includes('./@dispatcher')) { + tsconfig.include.push('./@dispatcher'); + writeFileSync(this.tsconfigFilePath, JSON.stringify(tsconfig, null, 2)); + } } }