fix(core): warn for invalid eslintrc

This commit is contained in:
Jason Jean 2020-07-31 16:31:41 -04:00 committed by Victor Savkin
parent d6b2201a21
commit 4d4749c9b1
2 changed files with 28 additions and 14 deletions

View File

@ -50,4 +50,9 @@ describe('Eslintrc Migration', () => {
const eslintrc = readJsonInTree(result, 'project2/.eslintrc');
expect(eslintrc.parserOptions.project).toEqual('./tsconfig.json');
});
it('should not fail for a non-json eslintrc', async () => {
tree.overwrite('project2/.eslintrc', 'not-json');
await runMigration('migrate-eslintrc-tsconfig', {}, tree);
});
});

View File

@ -10,21 +10,30 @@ export default function (schema: any): Rule {
return;
}
return updateJsonInTree(file, (json) => {
const tsconfig = json?.parserOptions?.project;
if (tsconfig) {
const tsconfigPath = join(dirname(file), tsconfig);
if (tsconfigPath === 'tsconfig.json') {
json.parserOptions.project = json.parserOptions.project.replace(
/tsconfig.json$/,
'tsconfig.base.json'
);
}
return json;
} else {
return json;
return (host, context) => {
try {
updateJsonInTree(file, (json) => {
const tsconfig = json?.parserOptions?.project;
if (tsconfig) {
const tsconfigPath = join(dirname(file), tsconfig);
if (tsconfigPath === 'tsconfig.json') {
json.parserOptions.project = json.parserOptions.project.replace(
/tsconfig.json$/,
'tsconfig.base.json'
);
}
return json;
} else {
return json;
}
})(host, context);
} catch (e) {
context.logger.warn(
`${file} could not be migrated because it is not valid JSON`
);
context.logger.error(e);
}
});
};
}),
formatFiles(),
]);