Skip to content

Commit edc6393

Browse files
committed
fix(cli): safely parse existing TS configs
1 parent f6a0301 commit edc6393

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

packages/cli/src/commands/sync-project-references/update-ts-configs.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { readFile, writeFile } from 'node:fs/promises';
33
import { notNil } from '@apitree.cz/ts-utils';
44
import type { Object as ObjectType } from 'ts-toolbelt';
55

6+
import { logger } from '../../utils.js';
7+
68
import { getExistingTsConfigPath } from './get-existing-ts-config-path.js';
79
import type { getReferences } from './get-references.js';
810
import type { SyncProjectReferencesTsConfigs, WorkspacePackageProps } from './types.js';
@@ -29,17 +31,26 @@ export const updateTsConfigs = async ({ references, tsConfigs, workspacePackage
2931
tsConfig: configFile,
3032
workspacePackage,
3133
});
34+
3235
if (tsConfigPath) {
33-
const tsConfigJson = JSON.parse(await readFile(tsConfigPath, 'utf8')) as {
34-
references?: { path: string }[];
35-
};
36+
let tsConfigJson = {} as { references?: { path: string }[] };
37+
38+
try {
39+
tsConfigJson = JSON.parse(await readFile(tsConfigPath, 'utf8')) as typeof tsConfigJson;
40+
} catch (error) {
41+
logger.error(`Error reading TS config file: ${tsConfigPath}`, error);
42+
return;
43+
}
44+
3645
tsConfigJson.references = references.map(
3746
(reference) => reference[configType] ?? reference.build ?? reference.default,
3847
);
48+
3949
await writeFile(tsConfigPath, JSON.stringify(tsConfigJson).replaceAll('\r\n', '\n'));
4050
return tsConfigPath;
4151
}
4252
}),
4353
);
54+
4455
return paths.filter(notNil);
4556
};

packages/cli/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const exec = promisify(childProcess.exec);
77

88
/* eslint-disable no-console */
99
export const logger = {
10+
error: console.error,
1011
info: console.info,
1112
warn: console.warn,
1213
};

packages/eslint-config/src/base/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const config: ConfigArray = tsEslint.config(
3131
'**/node_modules/**/*',
3232
'**/out/**/*',
3333
'**/public/**/*',
34+
'**/storybook-static/**/*',
3435
],
3536
},
3637
eslint.configs.recommended,

0 commit comments

Comments
 (0)