UNPKG

@o3r/core

Version:
71 lines (69 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ngGenerateUpdate = void 0; const node_fs_1 = require("node:fs"); const path = require("node:path"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@o3r/schematics"); const otter_environment_1 = require("../rule-factories/otter-environment"); /** * add a new ngUpdate function * @param options */ function ngGenerateUpdateFn(options) { const generateFiles = (tree, _context) => { const destination = (0, schematics_2.getDestinationPath)('@o3r/core:schematics-update', options.path, tree, options.projectName); const sanitizedVersion = options.version.replace('.', '_'); const baseVersion = `${options.version}${options.version.includes('.') ? '' : '.0'}.0-alpha.0`; const updateFunction = `updateV${sanitizedVersion}`; const barrelPath = path.join(destination, 'schematics', 'ng-update', 'index.ts'); let migrationFilePath = JSON.parse((0, node_fs_1.readFileSync)(path.resolve(destination, '..', 'package.json'), { encoding: 'utf8' }))['ng-update']; migrationFilePath = migrationFilePath && migrationFilePath.migrations; migrationFilePath = migrationFilePath && path.join(destination, '..', migrationFilePath); if (tree.exists(barrelPath)) { const currentUpdateIndexBuffer = tree.read(barrelPath); let currentUpdateIndex = currentUpdateIndexBuffer ? currentUpdateIndexBuffer.toString() : ''; if (!(new RegExp(updateFunction).test(currentUpdateIndex))) { currentUpdateIndex += ` /** * update of Otter library V${options.version}.x */ export function ${updateFunction}(): Rule { return (tree: Tree, context: SchematicContext) => { const updateRules: Rule[] = [ // TODO: add custom function to run on upgrade to ${options.version}.* ]; return chain(updateRules)(tree, context); }; }`; tree.overwrite(barrelPath, currentUpdateIndex); } } if (tree.exists(migrationFilePath)) { const currentMigrationBuffer = tree.read(migrationFilePath); let currentMigration = JSON.parse(currentMigrationBuffer ? currentMigrationBuffer.toString() : '{}'); if (!currentMigration || !currentMigration.schematics || !currentMigration.schematics[`migration-v${sanitizedVersion}`]) { currentMigration = currentMigration || {}; currentMigration.schematics = currentMigration.schematics || {}; currentMigration.schematics[`migration-v${sanitizedVersion}`] = { version: baseVersion, description: `Updates of the Otter Library to v${options.version}.x`, factory: `./schematics/ng-update/index#${updateFunction}` }; tree.overwrite(migrationFilePath, JSON.stringify(currentMigration, null, 2)); } } return tree; }; return (0, schematics_1.chain)([ (0, otter_environment_1.updateOtterEnvironmentAdapter)(options, __dirname), generateFiles, options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)() ]); } /** * add a new ngUpdate function * @param options */ exports.ngGenerateUpdate = (0, schematics_2.createOtterSchematic)(ngGenerateUpdateFn); //# sourceMappingURL=index.js.map