@nstudio/schematics
Version:
Cross-platform (xplat) tools for Nx workspaces.
89 lines • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const utils_1 = require("../utils");
function default_1(options) {
if (!options.name) {
throw new schematics_1.SchematicsException(utils_1.missingArgument('name', 'Provide a name for your Nest app.', 'ng g app.nest sample'));
}
const appPath = `nest-${options.name}`;
return schematics_1.chain([
utils_1.prerun(options),
// adjust naming convention
utils_1.applyAppNamingConvention(options, 'nest'),
options.angularJson ? (tree) => updateAngularJson(options, tree) : schematics_1.noop(),
// create app files
(tree, context) => addAppFiles(options, appPath)(tree, context),
// add root package dependencies
(tree) => utils_1.addRootDeps(tree, { nest: true }),
// add npm scripts
(tree) => {
const platformApp = options.name.replace('-', '.');
const packageConfig = utils_1.getJsonFromFile(tree, "package.json");
const scripts = packageConfig.scripts || {};
const tsConfig = `tsconfig${options.angularJson ? ".app" : ""}.json`;
scripts[`serve.${platformApp}`] = `ts-node -P apps/${options.name}/${tsConfig} apps/${options.name}/src/main.ts`;
scripts[`start.${platformApp}`] = `npm-run-all -p serve.${platformApp}`;
scripts[`build.${platformApp}`] = `tsc -p apps/${options.name}/${tsConfig}`;
scripts[`test.${platformApp}`] = `jest --config=apps/${options.name}/jest.json`;
scripts[`test.${platformApp}.coverage`] = `jest --config=apps/${options.name}/jest.json --coverage --coverageDirectory=coverage`;
scripts[`test.${platformApp}.watch`] = `jest --config=apps/${options.name}/jest.json --watch`;
scripts[`test.${platformApp}.e2e`] = `jest --config=apps/${options.name}/e2e/jest-e2e.json --forceExit`;
scripts[`test.${platformApp}.e2e.watch`] = `jest --config=apps/${options.name}/e2e/jest-e2e.json --watch`;
return utils_1.updatePackageScripts(tree, scripts);
},
// nx.json
(tree) => {
const projects = {};
projects[`${options.name}`] = {
tags: []
};
return utils_1.updateNxProjects(tree, projects);
},
utils_1.addInstall,
utils_1.addPostinstallers(),
options.skipFormat ? schematics_1.noop() : utils_1.formatFiles(options)
]);
}
exports.default = default_1;
function addAppFiles(options, appPath, sample = "") {
sample = "";
const appname = utils_1.getAppName(options, 'nest');
return schematics_1.branchAndMerge(schematics_1.mergeWith(schematics_1.apply(schematics_1.url(`./_${sample}files`), [
schematics_1.template(Object.assign({}, options, { appname, utils: utils_1.stringUtils, npmScope: utils_1.getNpmScope(), prefix: utils_1.getPrefix(), dot: ".", ext: options.angularJson ? ".app" : "" })),
schematics_1.move(`apps/${appPath}`)
])));
}
/**
* Remove assets option (not created), test target because it does not work
* and spec tsConfig.
* @todo fix the test target
* @param nestProject Project configuration from angular.json
*/
function tweakNxNestArchitect(nestProject) {
delete nestProject.architect.build.options.assets;
delete nestProject.architect.test;
nestProject.architect.lint.options.tsConfig.pop();
return nestProject;
}
/**
* Add Nest project to angular.json along with the architects targets
* @param options
* @param host
*/
function updateAngularJson(options, host) {
let nestProject;
return schematics_1.chain([
schematics_1.externalSchematic("@nrwl/schematics", "node-application", Object.assign({}, options, { skipInstall: true, skipFormat: true, skipPackageJson: true, framework: "nestjs" })),
(tree) => {
const nxAngular = JSON.parse(tree.read("/angular.json").toString());
nestProject = tweakNxNestArchitect(nxAngular.projects[options.name]);
return host;
},
utils_1.updateJsonInTree("angular.json", angularJson => {
angularJson.projects[options.name] = nestProject;
return angularJson;
})
]);
}
//# sourceMappingURL=index.js.map