UNPKG

@clipboard-health/nx-plugin

Version:

An Nx plugin with generators to manage libraries and applications.

54 lines 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = generate; const tslib_1 = require("tslib"); const node_path_1 = tslib_1.__importDefault(require("node:path")); const devkit_1 = require("@nx/devkit"); const get_import_path_1 = require("@nx/js/src/utils/get-import-path"); const ROOT_TS_CONFIG = "tsconfig.base.json"; function normalizeOptions(tree, options) { const name = (0, devkit_1.names)(options.name).fileName; const projectDirectory = name; return { ...options, importPath: (0, get_import_path_1.getImportPath)(tree, projectDirectory), projectDirectory, projectName: name.replaceAll("/", "-"), projectRoot: `${(0, devkit_1.getWorkspaceLayout)(tree).libsDir}/${name}`, publishConfigAccess: /* istanbul ignore next */ options.publishPublicly === true ? "public" : "restricted", }; } function getRelativePathToRootTsConfig(targetPath) { return `${(0, devkit_1.offsetFromRoot)(targetPath)}${ROOT_TS_CONFIG}`; } function addFiles(tree, options) { (0, devkit_1.generateFiles)(tree, node_path_1.default.join(__dirname, "files"), options.projectRoot, { ...options, ...(0, devkit_1.names)(options.name), offsetFromRoot: (0, devkit_1.offsetFromRoot)(options.projectRoot), rootTsConfigPath: getRelativePathToRootTsConfig(options.projectRoot), template: "", }); } function updateRootTsConfig(host, options) { (0, devkit_1.updateJson)(host, ROOT_TS_CONFIG, (json) => { const c = json.compilerOptions; c.paths ||= {}; // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete c.paths[options.name]; /* istanbul ignore next */ if (c.paths[options.importPath]) { throw new Error(`There is already a library with import path "${options.importPath}".`); } c.paths[options.importPath] = [(0, devkit_1.joinPathFragments)(options.projectRoot, "./src", "index.ts")]; return json; }); } async function generate(tree, options) { const normalizedOptions = normalizeOptions(tree, options); addFiles(tree, normalizedOptions); updateRootTsConfig(tree, normalizedOptions); await (0, devkit_1.formatFiles)(tree); } //# sourceMappingURL=generator.js.map