UNPKG

@nstudio/schematics

Version:

Cross-platform (xplat) tools for Nx workspaces.

152 lines 7.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const schematics_1 = require("@angular-devkit/schematics"); const utils_1 = require("../utils"); let featureName; let projectNames; let platforms = []; function default_1(options) { if (!options.name) { throw new Error(utils_1.generateOptionError("component")); } // reset module globals options.needsIndex = false; projectNames = null; platforms = []; if (options.feature) { featureName = options.feature.toLowerCase(); } const projects = options.projects; if (projects) { options.onlyProject = true; if (!featureName) { // no feature targeted, default to shared featureName = "shared"; } // building feature in shared code and in projects projectNames = projects.split(","); for (const name of projectNames) { const platPrefix = name.split("-")[0]; if (utils_1.supportedPlatforms.includes(platPrefix) && !platforms.includes(platPrefix)) { // if project name is prefixed with supported platform and not already added platforms.push(platPrefix); } } } else if (options.platforms) { if (!featureName) { // no feature targeted, default to ui featureName = "ui"; } // building feature in shared code only platforms = options.platforms.split(","); } if (platforms.length === 0) { let error = projects ? utils_1.platformAppPrefixError() : utils_1.generatorError("component"); throw new Error(utils_1.optionsMissingError(error)); } const targetPlatforms = {}; for (const t of platforms) { if (utils_1.supportedPlatforms.includes(t)) { targetPlatforms[t] = true; } else { throw new Error(utils_1.unsupportedPlatformError(t)); } } const projectChains = []; if (options.onlyProject) { for (const projectName of projectNames) { const platPrefix = projectName.split("-")[0]; let srcDir = platPrefix !== "nativescript" ? "src/" : ""; const prefixPath = `apps/${projectName}/${srcDir}app`; const featurePath = `${prefixPath}/features/${featureName}`; const featureModulePath = `${featurePath}/${featureName}.module.ts`; const barrelIndex = `${featurePath}/components/index.ts`; // console.log('will adjustProject:', projectName); projectChains.push((tree, context) => { if (!tree.exists(featureModulePath)) { throw new Error(utils_1.needFeatureModuleError(featureModulePath, featureName, projectName, true)); } return utils_1.addToFeature("component", options, prefixPath, tree, `_${platPrefix}`)(tree, context); }); projectChains.push((tree, context) => { return utils_1.adjustBarrelIndex("component", options, barrelIndex, true)(tree, context); }); projectChains.push((tree, context) => { return utils_1.addToFeature("component", options, prefixPath, tree, "_index")(tree, context); }); } } else { projectChains.push(schematics_1.noop()); } const platformChains = []; for (const platform of utils_1.supportedPlatforms) { if (targetPlatforms[platform]) { if (!options.onlyProject) { // add component platformChains.push((tree, context) => { return utils_1.addToFeature("component", options, `xplat/${platform}`, tree, `_${platform}`, true)(tree, context); }); if (options.subFolder) { // adjust components barrel for subFolder platformChains.push((tree, context) => { return utils_1.adjustBarrelIndex("component", options, `xplat/${platform}/features/${featureName}/components/${options.subFolder}/index.ts`, true)(tree, context); }); platformChains.push((tree, context) => { return options.needsIndex ? utils_1.addToFeature("component", options, `xplat/${platform}`, tree, "_index", true)(tree, context) : schematics_1.noop()(tree, context); }); } // adjust overall components barrel platformChains.push((tree, context) => { return utils_1.adjustBarrelIndex("component", options, `xplat/${platform}/features/${featureName}/components/index.ts`, true, false, true)(tree, context); }); platformChains.push((tree, context) => { return options.needsIndex ? utils_1.addToFeature("component", options, `xplat/${platform}`, tree, "_index")(tree, context) : schematics_1.noop()(tree, context); }); } } } if (platformChains.length === 0) { // none specified, insert noop platformChains.push(schematics_1.noop()); } return schematics_1.chain([ utils_1.prerun(), // add component for base libs feature (tree, context) => !options.onlyProject && options.createBase ? utils_1.addToFeature("component", options, "libs", tree, "_base", true)(tree, context) : schematics_1.noop()(tree, context), // adjust libs barrel for subFolder (tree, context) => options.subFolder && !options.onlyProject && options.createBase ? utils_1.adjustBarrelIndex("component", options, `libs/features/${featureName}/base/${options.subFolder}/index.ts`, false, true)(tree, context) : schematics_1.noop()(tree, context), // add index barrel if needed for subFolder (tree, context) => options.needsIndex ? utils_1.addToFeature("component", options, "libs", tree, "_base_index", true)(tree, context) : schematics_1.noop()(tree, context), // adjust libs barrel (tree, context) => !options.onlyProject && options.createBase ? utils_1.adjustBarrelIndex("component", options, `libs/features/${featureName}/base/index.ts`, false, true)(tree, context) : schematics_1.noop()(tree, context), // add index barrel if needed (tree, context) => options.needsIndex ? utils_1.addToFeature("component", options, "libs", tree, "_base_index")(tree, context) : schematics_1.noop()(tree, context), // add platform chains ...platformChains, // project handling ...projectChains, options.skipFormat ? schematics_1.noop() : utils_1.formatFiles(options) ]); } exports.default = default_1; //# sourceMappingURL=index.js.map