nx/packages/linter/src/migrations/update-10-3-0/add-json-ext-to-eslintrc.ts
James Henry e339ece224
feat(linter): add explicit file extension to config files (#3847)
* feat(linter): add explicit file extension to config files

* feat(linter): update references to .eslintrc for new projects

* fix(linter): fix quotes in global eslint config
2020-10-01 18:59:45 -04:00

65 lines
1.7 KiB
TypeScript

import { basename } from '@angular-devkit/core';
import { chain, Tree } from '@angular-devkit/schematics';
import {
formatFiles,
readJsonInTree,
serializeJson,
updateWorkspace,
visitNotIgnoredFiles,
} from '@nrwl/workspace';
function updateESLintConfigReferencesInWorkspace() {
return updateWorkspace((workspace) => {
workspace.projects.forEach((project) => {
const lintTarget = project.targets.get('lint');
if (
lintTarget?.builder !== '@nrwl/linter:eslint' &&
(lintTarget?.builder !== '@nrwl/linter:lint' ||
lintTarget?.options?.linter === 'tslint')
) {
return;
}
if (lintTarget.builder === '@nrwl/linter:eslint') {
if (!lintTarget.options.eslintConfig) {
return;
}
lintTarget.options.eslintConfig = `${lintTarget.options.eslintConfig}.json`;
return;
}
if (lintTarget.builder === '@nrwl/linter:lint') {
if (!lintTarget.options.config) {
return;
}
lintTarget.options.config = `${lintTarget.options.config}.json`;
return;
}
});
});
}
function renameESLintConfigFiles() {
return visitNotIgnoredFiles((file, host, context) => {
if (basename(file) !== '.eslintrc') {
return;
}
// Using .eslintrc without an explicit file extension is deprecated
const newFilePath = `${file}.json`;
context.logger.info(`Renaming ${file} to ${newFilePath}`);
try {
return host.rename(file, newFilePath);
} catch (e) {
context.logger.error(e);
}
});
}
export default function () {
return chain([
renameESLintConfigFiles,
updateESLintConfigReferencesInWorkspace,
formatFiles(),
]);
}