Skip to content

Commit

Permalink
feat(changelog): added changelog schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
nongrata081 committed Apr 9, 2019
1 parent f516028 commit 4d017a1
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/changelog/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';


// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function changelog(_options: any): Rule {
return (tree: Tree, _context: SchematicContext) => {

const packageJsonBuffer = tree.read("package.json");
if (!packageJsonBuffer) {
throw new SchematicsException("Not an npm project. Couldn't find package.json");
}

const packageJson = JSON.parse(packageJsonBuffer.toString());

const scripts = "scripts";
const changelogScript = "changelog";
const changelogScriptCmd = "./node_modules/.bin/conventional-changelog -p angular -i CHANGELOG.md -s -r 0";
if (!packageJson[scripts]) {
packageJson[scripts] = {};
}
packageJson[scripts][changelogScript] = changelogScriptCmd;

const devDeps = "devDependencies";
const changelog = "conventional-changelog-cli";
const version = "latest";
if (!packageJson[devDeps]) {
packageJson[devDeps] = {};
}
if (!packageJson[devDeps][changelog]) {
packageJson[devDeps][changelog] = version;
}

tree.overwrite('package.json', JSON.stringify(packageJson, null, 2));
_context.logger.log('info', `Added "${changelogScript}" to "${scripts}" in package.json`);
_context.logger.log('info', `Added "${changelog}@${version}" to ${devDeps}`);
_context.addTask(new NodePackageInstallTask());

return tree;
};
}

0 comments on commit 4d017a1

Please # to comment.