@angular-eslint/schematics
Version:
Angular Schematics for angular-eslint
119 lines (118 loc) • 5.89 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uninstallTSLintAndCodelyzer = exports.ensureESLintPluginsAreInstalled = exports.updateObjPropAndRemoveDuplication = exports.updateArrPropAndRemoveDuplication = void 0;
const tasks_1 = require("@angular-devkit/schematics/tasks");
const assert = __importStar(require("assert"));
const utils_1 = require("../utils");
function updateArrPropAndRemoveDuplication(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
configBeingExtended, arrPropName, deleteIfUltimatelyEmpty) {
json[arrPropName] = json[arrPropName] || [];
configBeingExtended[arrPropName] = configBeingExtended[arrPropName] || [];
json[arrPropName] = json[arrPropName].filter((extended) => !configBeingExtended[arrPropName].includes(extended));
json[arrPropName] = Array.from(new Set(json[arrPropName]));
if (deleteIfUltimatelyEmpty && json[arrPropName].length === 0) {
delete json[arrPropName];
}
}
exports.updateArrPropAndRemoveDuplication = updateArrPropAndRemoveDuplication;
function updateObjPropAndRemoveDuplication(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
json,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
configBeingExtended, objPropName, deleteIfUltimatelyEmpty) {
json[objPropName] = json[objPropName] || {};
configBeingExtended[objPropName] = configBeingExtended[objPropName] || {};
for (const [name, val] of Object.entries(json[objPropName])) {
const valueOfSamePropInExtendedConfig = configBeingExtended[objPropName][name];
try {
assert.deepStrictEqual(val, valueOfSamePropInExtendedConfig);
delete json[objPropName][name];
// eslint-disable-next-line no-empty
}
catch (_a) { }
}
if (deleteIfUltimatelyEmpty && Object.keys(json[objPropName]).length === 0) {
delete json[objPropName];
}
}
exports.updateObjPropAndRemoveDuplication = updateObjPropAndRemoveDuplication;
function ensureESLintPluginsAreInstalled(eslintPluginsToBeInstalled) {
return (host, context) => {
var _a;
if (!(eslintPluginsToBeInstalled === null || eslintPluginsToBeInstalled === void 0 ? void 0 : eslintPluginsToBeInstalled.length)) {
return;
}
if (!host.exists('package.json')) {
throw new Error('Could not find a `package.json` file at the root of your workspace');
}
const projectPackageJSON = host.read('package.json').toString('utf-8');
const json = JSON.parse(projectPackageJSON);
json.devDependencies = json.devDependencies || {};
const pluginsToInstall = [];
for (const pluginName of eslintPluginsToBeInstalled) {
if (!json.devDependencies[pluginName] &&
!((_a = json.dependencies) === null || _a === void 0 ? void 0 : _a[pluginName])) {
json.devDependencies[pluginName] = 'latest';
pluginsToInstall.push(pluginName);
}
}
if (pluginsToInstall.length > 0) {
context.logger.info('\nINFO: To most closely match your tslint.json, the `latest` version of the following eslint plugin(s) have been installed:');
context.logger.info('\n - ' + pluginsToInstall.join('\n - '));
context.logger.info('\nPlease note, you may wish to pin these to a specific version number in your package.json, rather than leaving it open to `latest`.\n');
host.overwrite('package.json', JSON.stringify(json, null, 2));
context.addTask(new tasks_1.NodePackageInstallTask());
}
return host;
};
}
exports.ensureESLintPluginsAreInstalled = ensureESLintPluginsAreInstalled;
function uninstallTSLintAndCodelyzer() {
return (host, context) => {
if (!host.exists('package.json')) {
throw new Error('Could not find a `package.json` file at the root of your workspace');
}
const projectPackageJSON = host.read('package.json').toString('utf-8');
const json = JSON.parse(projectPackageJSON);
if (json.devDependencies) {
delete json.devDependencies['tslint'];
delete json.devDependencies['codelyzer'];
json.devDependencies = (0, utils_1.sortObjectByKeys)(json.devDependencies);
}
if (json.dependencies) {
delete json.dependencies['tslint'];
delete json.dependencies['codelyzer'];
json.dependencies = (0, utils_1.sortObjectByKeys)(json.dependencies);
}
host.overwrite('package.json', JSON.stringify(json, null, 2));
context.addTask(new tasks_1.NodePackageInstallTask());
return host;
};
}
exports.uninstallTSLintAndCodelyzer = uninstallTSLintAndCodelyzer;