Skip to content

Commit

Permalink
feat(generators): add addCliDependency to app.nativescript (#104)
Browse files Browse the repository at this point in the history
This will control whether the schematics will add {N} CLI as a devDependency or not

close #101
  • Loading branch information
dungahk authored and NathanWalker committed Mar 18, 2019
1 parent 8446670 commit 1dbe95a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/app.nativescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
schematic,
noop,
} from '@angular-devkit/schematics';
import { stringUtils, prerun, getNpmScope, getPrefix, addRootDeps, updatePackageScripts, updateAngularProjects, updateNxProjects, applyAppNamingConvention, getGroupByName, getAppName, missingArgument } from '../utils';
import { stringUtils, prerun, getNpmScope, getPrefix, addRootDeps, updatePackageScripts, updateAngularProjects, updateNxProjects, applyAppNamingConvention, getGroupByName, getAppName, missingArgument, getJsonFromFile, setDependency, updateJsonFile } from '../utils';
import { Schema as ApplicationOptions } from './schema';

export default function (options: ApplicationOptions) {
Expand Down Expand Up @@ -43,6 +43,7 @@ export default function (options: ApplicationOptions) {
})(tree, context),
// add root package dependencies
(tree: Tree) => addRootDeps(tree, {nativescript: true}),
addNsCli(options.addCliDependency),
// add start/clean scripts
(tree: Tree) => {
const scripts = {};
Expand Down Expand Up @@ -99,3 +100,32 @@ function addAppFiles(options: ApplicationOptions, appPath: string, extra: string
]))
);
}

/**
* Add {N} CLI to devDependencies
*/
function addNsCli(add: boolean): Rule {
if (!add) {
return noop();
}

return (tree: Tree) => {
const packagePath = "package.json";
const packageJson: { devDependencies: { [packageName: string]: string } } = getJsonFromFile(tree, packagePath);

if (!packageJson) {
return tree;
}

packageJson.devDependencies = setDependency(
packageJson.devDependencies,
{
name: 'nativescript',
version: "^5.0.0",
type: "devDependency"
}
);

return updateJsonFile(tree, packagePath, packageJson);
}
}
4 changes: 4 additions & 0 deletions src/app.nativescript/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ export interface Schema {
* Skip installing dependencies
*/
skipInstall?: boolean;
/**
* Add {N} CLI to devDependencies
*/
addCliDependency?: boolean;
}
5 changes: 5 additions & 0 deletions src/app.nativescript/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"type": "boolean",
"description": "Skip installing dependencies.",
"default": false
},
"addCliDependency": {
"type": "boolean",
"description": "Add {N} CLI to devDependencies",
"default": false
}
},
"required": []
Expand Down
2 changes: 1 addition & 1 deletion src/utils/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export const copy = (tree: Tree, from: string, to: string) => {
tree.create(to, file.content);
};

const setDependency = (
export const setDependency = (
dependenciesMap: { [key: string]: string },
{ name, version }: NodeDependency
) => Object.assign(dependenciesMap, { [name]: version });
Expand Down

0 comments on commit 1dbe95a

Please sign in to comment.