@nx/plugin
Version:
131 lines (130 loc) • 5.58 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrationGenerator = migrationGenerator;
const devkit_1 = require("@nx/devkit");
const artifact_name_and_directory_utils_1 = require("@nx/devkit/src/generators/artifact-name-and-directory-utils");
const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
const node_path_1 = require("node:path");
const package_json_1 = require("nx/src/utils/package-json");
const paths_1 = require("../../utils/paths");
const versions_1 = require("../../utils/versions");
const generator_1 = require("../lint-checks/generator");
async function normalizeOptions(tree, options) {
const { artifactName: name, directory, fileName, project, } = await (0, artifact_name_and_directory_utils_1.determineArtifactNameAndDirectoryOptions)(tree, {
path: options.path,
name: options.name,
allowedFileExtensions: ['ts'],
fileExtension: 'ts',
});
const { root: projectRoot, sourceRoot: projectSourceRoot } = (0, devkit_1.readProjectConfiguration)(tree, project);
const description = options.description ?? `Migration for v${options.packageVersion}`;
const normalized = {
...options,
directory,
fileName,
project,
name,
description,
projectRoot,
projectSourceRoot: projectSourceRoot ?? (0, node_path_1.join)(projectRoot, 'src'),
isTsSolutionSetup: (0, ts_solution_setup_1.isUsingTsSolutionSetup)(tree),
};
return normalized;
}
function addFiles(host, options) {
(0, devkit_1.generateFiles)(host, (0, node_path_1.join)(__dirname, 'files/migration'), options.directory, {
...options,
tmpl: '',
});
}
function updateMigrationsJson(host, options) {
const configuredMigrationPath = (0, package_json_1.readNxMigrateConfig)((0, devkit_1.readJson)(host, (0, devkit_1.joinPathFragments)(options.projectRoot, 'package.json'))).migrations;
const migrationsPath = (0, devkit_1.joinPathFragments)(options.projectRoot, configuredMigrationPath ?? 'migrations.json');
const migrations = host.exists(migrationsPath)
? (0, devkit_1.readJson)(host, migrationsPath)
: {};
const generators = migrations.generators ?? {};
const dir = (0, paths_1.getArtifactMetadataDirectory)(host, options.project, options.directory, options.isTsSolutionSetup);
generators[options.name] = {
version: options.packageVersion,
description: options.description,
implementation: `${dir}/${options.fileName}`,
};
migrations.generators = generators;
if (options.packageJsonUpdates) {
const packageJsonUpdatesObj = migrations.packageJsonUpdates ?? {};
if (!packageJsonUpdatesObj[options.packageVersion]) {
packageJsonUpdatesObj[options.packageVersion] = {
version: options.packageVersion,
packages: {},
};
}
migrations.packageJsonUpdates = packageJsonUpdatesObj;
}
(0, devkit_1.writeJson)(host, migrationsPath, migrations);
}
function updatePackageJson(host, options) {
(0, devkit_1.updateJson)(host, (0, node_path_1.join)(options.projectRoot, 'package.json'), (json) => {
const addFile = (file) => {
if (options.isTsSolutionSetup) {
const filesSet = new Set(json.files ?? ['dist', '!**/*.tsbuildinfo']);
filesSet.add(file.replace(/^\.\//, ''));
json.files = [...filesSet];
}
};
const migrationKey = json['ng-update'] ? 'ng-update' : 'nx-migrations';
if (typeof json[migrationKey] === 'string') {
addFile(json[migrationKey]);
return json;
}
else if (!json[migrationKey]) {
json[migrationKey] = {
migrations: './migrations.json',
};
}
else if (json[migrationKey].migrations) {
json[migrationKey].migrations = './migrations.json';
}
addFile(json[migrationKey].migrations);
// add dependencies
json.dependencies = {
'@nx/devkit': versions_1.nxVersion,
...json.dependencies,
};
return json;
});
}
function updateProjectConfig(host, options) {
if (options.isTsSolutionSetup) {
return;
}
const project = (0, devkit_1.readProjectConfiguration)(host, options.project);
const assets = project.targets.build?.options?.assets;
if (assets &&
assets.filter((a) => a.glob === 'migrations.json').length === 0) {
project.targets.build.options.assets = [
...assets,
{
input: `./${options.projectRoot}`,
glob: 'migrations.json',
output: '.',
},
];
(0, devkit_1.updateProjectConfiguration)(host, options.project, project);
}
}
async function migrationGenerator(host, schema) {
const options = await normalizeOptions(host, schema);
addFiles(host, options);
updatePackageJson(host, options);
updateMigrationsJson(host, options);
updateProjectConfig(host, options);
if (!host.exists('migrations.json')) {
const packageJsonPath = (0, devkit_1.joinPathFragments)(options.projectRoot, 'package.json');
(0, generator_1.addMigrationJsonChecks)(host, { projectName: options.project }, (0, devkit_1.readJson)(host, packageJsonPath));
}
if (!options.skipFormat) {
await (0, devkit_1.formatFiles)(host);
}
}
exports.default = migrationGenerator;
;