Skip to content

Commit

Permalink
fix(env): fixed @Env when project was mbt build for production ready
Browse files Browse the repository at this point in the history
The `mbt build` was given an error during `npm install` due to newly
`@Env` decorator.
  • Loading branch information
dragolea committed Nov 12, 2024
1 parent 58f1bfa commit 4e148eb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 51 deletions.
42 changes: 20 additions & 22 deletions dist/postinstall/PostInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var GenerateEnv = class {
export ${typeDefinitions}`);
}
compileEnvFile() {
this.executeShellCommand("tsc", [
this.executeShellCommand("npx tsc", [
this.envFilePath,
"--outDir",
this.dispatcherFolderPath
Expand All @@ -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) {
Expand Down
54 changes: 25 additions & 29 deletions postinstall/util/GenerateEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,42 @@ export class GenerateEnv {
}

private compileEnvFile() {
this.executeShellCommand('tsc', [this.envFilePath, '--outDir', this.dispatcherFolderPath]);
this.executeShellCommand('npx tsc', [this.envFilePath, '--outDir', this.dispatcherFolderPath]);
}

private addToGitignore() {
this.appendLineIfAbsent(this.gitignoreFilePath, '@dispatcher');
}

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));
}
}
}

Expand Down

0 comments on commit 4e148eb

Please sign in to comment.