@nstudio/schematics
Version:
Cross-platform (xplat) tools for Nx workspaces.
209 lines • 8.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
// import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
const utils_1 = require("../utils");
let platformArg;
function default_1(options) {
const targetPlatforms = {};
platformArg = options.platforms || '';
if (platformArg === 'all') {
// conveniently add support for all supported platforms
for (const platform of utils_1.supportedPlatforms) {
targetPlatforms[platform] = true;
}
platformArg = utils_1.supportedPlatforms.join(",");
}
else {
const platforms = utils_1.sanitizeCommaDelimitedArg(platformArg);
if (platforms.length === 0) {
throw new Error(utils_1.noPlatformError());
}
else {
for (const platform of platforms) {
if (utils_1.supportedPlatforms.includes(platform)) {
targetPlatforms[platform] = true;
}
else {
throw new Error(utils_1.unsupportedPlatformError(platform));
}
}
}
}
// console.log(`Generating xplat support for: ${platforms.toString()}`);
const sample = options.sample;
return schematics_1.chain([
utils_1.prerun(options, true),
// update gitignore to support xplat
utils_1.updateGitIgnore(),
// add references to support xplat
utils_1.addReferences(),
// libs
(tree, context) => addLibFiles(tree, options)(tree, context),
// libs w/sample feature
(tree, context) => sample
? addLibFiles(tree, options, "sample")(tree, context)
: schematics_1.noop()(tree, context),
// nativescript
(tree, context) => targetPlatforms.nativescript
? addPlatformFiles(tree, options, "nativescript")(tree, context)
: schematics_1.noop()(tree, context),
// nativescript w/sample feature
(tree, context) => sample
? addPlatformFiles(tree, options, "nativescript", "sample")(tree, context)
: schematics_1.noop()(tree, context),
// web
(tree, context) =>
// always generate web if ionic or electron is specified since they depend on it
utils_1.hasWebPlatform(targetPlatforms)
? addPlatformFiles(tree, options, "web")(tree, context)
: schematics_1.noop()(tree, context),
// web w/sample feature
(tree, context) => sample
? addPlatformFiles(tree, options, "web", "sample")(tree, context)
: schematics_1.noop()(tree, context),
// ionic
(tree, context) => targetPlatforms.ionic
? addPlatformFiles(tree, options, "ionic")(tree, context)
: schematics_1.noop()(tree, context),
// electron
(tree, context) => targetPlatforms.electron
? addPlatformFiles(tree, options, "electron")(tree, context)
: schematics_1.noop()(tree, context),
// ssr (TODO)
(tree, context) => targetPlatforms.ssr
? addPlatformFiles(tree, options, "ssr")(tree, context)
: schematics_1.noop()(tree, context),
// testing
(tree, context) => utils_1.addTestingFiles(tree, options)(tree, context),
updateTestingConfig,
updateLint,
// update tsconfig files to support xplat
(tree, context) => schematics_1.schematic("ts-config", {
platforms: platformArg
})(tree, context),
utils_1.formatFiles(options),
// update root package for xplat configuration
(tree) => utils_1.updatePackageForXplat(tree, targetPlatforms),
// clean shared code script ({N} build artifacts that may need to be cleaned up at times)
(tree) => {
const scripts = {};
scripts[`clean.shared`] = `cd libs/ && git clean -dfX && cd ../xplat/ && git clean -dfX`;
return utils_1.updatePackageScripts(tree, scripts);
},
// update IDE settings
(tree) => utils_1.updateIDESettings(tree, platformArg)
]);
}
exports.default = default_1;
const addPlatformFiles = (tree, options, platform, sample = "") => {
if (!sample && tree.exists(`xplat/${platform}/core/index.ts`)) {
return schematics_1.noop();
}
sample = sample ? `${sample}_` : "";
return schematics_1.branchAndMerge(schematics_1.mergeWith(schematics_1.apply(schematics_1.url(`./_${platform}_${sample}files`), [
schematics_1.template(Object.assign({}, options, { npmScope: utils_1.getNpmScope(), prefix: utils_1.getPrefix(), dot: ".", utils: utils_1.stringUtils })),
schematics_1.move(`xplat/${platform}`)
])));
};
const addLibFiles = (tree, options, sample = "") => {
sample = sample ? `${sample}_` : "";
if (!sample) {
if (tree.exists(`libs/core/base/base-component.ts`) ||
tree.exists(`libs/features/index.ts`)) {
return schematics_1.noop();
}
}
return schematics_1.branchAndMerge(schematics_1.mergeWith(schematics_1.apply(schematics_1.url(`./_lib_${sample}files`), [
schematics_1.template(Object.assign({}, options, { npmScope: utils_1.getNpmScope(), prefix: utils_1.getPrefix(), dot: ".", utils: utils_1.stringUtils })),
schematics_1.move("libs")
])));
};
function updateTestingConfig(tree, context) {
const angularConfigPath = `angular.json`;
const nxConfigPath = `nx.json`;
const angularJson = utils_1.getJsonFromFile(tree, angularConfigPath);
const nxJson = utils_1.getJsonFromFile(tree, nxConfigPath);
const prefix = utils_1.getPrefix();
// console.log('prefix:', prefix);
// update libs and xplat config
if (angularJson && angularJson.projects) {
angularJson.projects["libs"] = {
root: "libs",
sourceRoot: "libs",
projectType: "library",
prefix: prefix,
architect: {
test: {
builder: "@angular-devkit/build-angular:karma",
options: {
main: "testing/test.libs.ts",
tsConfig: "testing/tsconfig.libs.spec.json",
karmaConfig: "testing/karma.conf.js"
}
},
lint: {
builder: "@angular-devkit/build-angular:tslint",
options: {
tsConfig: [
"testing/tsconfig.libs.json",
"testing/tsconfig.libs.spec.json"
],
exclude: ["**/node_modules/**"]
}
}
}
};
angularJson.projects["xplat"] = {
root: "xplat",
sourceRoot: "xplat",
projectType: "library",
prefix: prefix,
architect: {
test: {
builder: "@angular-devkit/build-angular:karma",
options: {
main: "testing/test.xplat.ts",
tsConfig: "testing/tsconfig.xplat.spec.json",
karmaConfig: "testing/karma.conf.js"
}
},
lint: {
builder: "@angular-devkit/build-angular:tslint",
options: {
tsConfig: [
"testing/tsconfig.xplat.json",
"testing/tsconfig.xplat.spec.json"
],
exclude: ["**/node_modules/**"]
}
}
}
};
}
if (nxJson && nxJson.projects) {
nxJson.projects["libs"] = {
tags: []
};
nxJson.projects["xplat"] = {
tags: []
};
}
tree = utils_1.updateJsonFile(tree, angularConfigPath, angularJson);
tree = utils_1.updateJsonFile(tree, nxConfigPath, nxJson);
return tree;
}
function updateLint(host, context) {
const prefix = utils_1.getPrefix();
return utils_1.updateJsonInTree("tslint.json", json => {
json.rules = json.rules || {};
// remove forin rule as collides with LogService
delete json.rules["forin"];
// adjust console rules to work with LogService
json.rules["no-console"] = [true, "debug", "time", "timeEnd", "trace"];
json.rules["directive-selector"] = [true, "attribute", prefix, "camelCase"];
json.rules["component-selector"] = [true, "element", prefix, "kebab-case"];
return json;
})(host, context);
}
//# sourceMappingURL=index.js.map