Created
October 23, 2018 18:51
-
-
Save bautistaaa/144504c786393d2bc424e3cf9234a750 to your computer and use it in GitHub Desktop.
modify package json via schematics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Rule, SchematicContext, Tree, chain } from '@angular-devkit/schematics'; | |
import { | |
addPackageJsonDependency, | |
NodeDependency, | |
NodeDependencyType, | |
} from '@schematics/angular/utility/dependencies'; | |
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; | |
import { dependencies } from '../dependencies'; | |
function addDependencies(): Rule { | |
return (host: Tree, _context: SchematicContext) => { | |
for (let pkg in dependencies) { | |
const nodeDependency: NodeDependency = _nodeDependencyFactory(pkg, dependencies[pkg]); | |
addPackageJsonDependency(host, nodeDependency); | |
} | |
_context.addTask(new NodePackageInstallTask()); | |
}; | |
} | |
// You don't have to export the function as default. You can also have more than one rule factory | |
// per file. | |
export function mySchematic(_options: any): Rule { | |
return (tree: Tree, _context: SchematicContext) => { | |
return chain([ | |
addDependencies(), | |
])(tree, _context); | |
}; | |
} | |
function _nodeDependencyFactory(packageName: string, version: string): NodeDependency { | |
return { | |
type: NodeDependencyType.Default, | |
name: packageName, | |
version: version, | |
overwrite: true | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment