@nstudio/schematics
Version:
Cross-platform (xplat) tools for Nx workspaces.
129 lines • 5.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const utils_1 = require("../utils");
// Helpers
const applitools_1 = require("./applitools");
const imports_1 = require("./imports");
// Mapping config of what each helper supports
const helperSupportConfig = {
imports: {
platforms: ["nativescript"],
moveTo: function (platform, target) {
return `xplat/${platform}/utils`;
},
additionalSupport: function (platform, helperChains, options) {
switch (platform) {
case "nativescript":
return imports_1.supportImports_NativeScript(helperChains, options);
}
}
},
applitools: {
platforms: ["web"],
requiresTarget: true,
additionalSupport: function (platform, helperChains, options) {
switch (platform) {
case "web":
return applitools_1.supportApplitools_Web(helperChains, options);
}
}
}
};
let helpers = [];
let platforms = [];
function default_1(options) {
if (!options.name) {
throw new schematics_1.SchematicsException(utils_1.missingArgument('name', 'Provide a comma delimited list of helpers to generate.', 'ng g xplat-helper imports'));
}
helpers = options.name.split(",");
platforms = ((options.platforms ? options.platforms.split(",") : []));
const helperChains = [];
const processHelpers = (platform) => {
for (const helper of helpers) {
if (utils_1.supportedHelpers.includes(helper)) {
// get helper support config
const supportConfig = helperSupportConfig[helper];
if (!platform) {
// when using targeting the platforms argument can be ommitted
// however when doing so it relies on platform being in the target name
if (supportConfig.requiresTarget && options.target) {
for (const p of utils_1.supportedPlatforms) {
const match = options.target.match(p);
if (match) {
platform = match[0];
break;
}
}
}
}
// if platform is still falsey, error out
if (!platform) {
throw new schematics_1.SchematicsException(utils_1.helperMissingPlatforms());
}
// throw is target required and it's missing
if (supportConfig.requiresTarget && !options.target) {
throw new schematics_1.SchematicsException(utils_1.helperTargetError(helper));
}
if (supportConfig.platforms.includes(platform)) {
if (supportConfig.moveTo) {
// add files for the helper
const moveTo = supportConfig.moveTo(platform, options.target);
helperChains.push((tree, context) => {
return addHelperFiles(options, platform, helper, moveTo)(tree, context);
});
}
if (supportConfig.additionalSupport) {
// process additional support modifications
helperChains.push((tree, context) => {
return supportConfig.additionalSupport(platform, helperChains, options)(tree, context);
});
}
}
}
else {
throw new schematics_1.SchematicsException(utils_1.unsupportedHelperError(helper));
}
}
};
if (platforms.length) {
for (const platform of platforms) {
if (utils_1.supportedPlatforms.includes(platform)) {
processHelpers(platform);
}
else {
throw new schematics_1.SchematicsException(utils_1.unsupportedPlatformError(platform));
}
}
}
else {
processHelpers();
}
return schematics_1.chain([
utils_1.prerun(options),
// add helper chains
...helperChains,
// log additional notes
(tree) => {
logNotes(options, helpers);
return schematics_1.noop();
}
]);
}
exports.default = default_1;
function addHelperFiles(options, platform, helper, moveTo) {
return schematics_1.branchAndMerge(schematics_1.mergeWith(schematics_1.apply(schematics_1.url(`./${helper}/${platform}/_files`), [
schematics_1.template(Object.assign({}, options, { utils: utils_1.stringUtils, npmScope: utils_1.getNpmScope(), prefix: utils_1.getPrefix(), dot: "." })),
schematics_1.move(moveTo)
])));
}
function logNotes(options, helpers) {
for (const helper of helpers) {
switch (helper) {
case "applitools":
applitools_1.applitools_logNote(options);
break;
}
}
}
//# sourceMappingURL=index.js.map