Skip to content

Commit

Permalink
Feat/nvmrc schematic (#72)
Browse files Browse the repository at this point in the history
* refactor: add new functions to modify package.json

* feat: add new schematic to generate nvmrc and include node version in package.json
  • Loading branch information
neodmy authored Oct 31, 2023
1 parent 6507a77 commit ced573b
Show file tree
Hide file tree
Showing 8 changed files with 5,903 additions and 6,145 deletions.
11,950 changes: 5,811 additions & 6,139 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/@guidesmiths/cuckoojs-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guidesmiths/cuckoojs-cli",
"version": "0.0.3",
"version": "0.0.4",
"description": "CuckooJS CLI",
"keywords": [
"schematics",
Expand Down Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"@angular-devkit/schematics-cli": "^14.2.3",
"@guidesmiths/cuckoojs-schematics": "^0.0.3",
"@guidesmiths/cuckoojs-schematics": "^0.0.4",
"@nestjs/schematics": "^9.0.3",
"@types/node": "^18.11.17",
"commander": "^9.4.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/@guidesmiths/cuckoojs-schematics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guidesmiths/cuckoojs-schematics",
"version": "0.0.3",
"version": "0.0.4",
"description": "A collection of CuckooJS schematics",
"keywords": [
"schematics",
Expand Down
5 changes: 5 additions & 0 deletions packages/@guidesmiths/cuckoojs-schematics/src/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
"description": "Add Husky to the project",
"factory": "./husky/husky.factory#main",
"schema": "./husky/schema.json"
},
"nvmrc": {
"description": "Add nvmrc to the project",
"factory": "./nvmrc/nvmrc.factory#main",
"schema": "./nvmrc/schema.json"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%=nodeVersion%>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type {
Rule,
SchematicContext,
Tree} from '@angular-devkit/schematics';
import {
apply,
MergeStrategy,
mergeWith, move,
template,
url,
chain,
} from '@angular-devkit/schematics';
import {normalize} from '@angular-devkit/core';
import {join} from 'path';
import {PackageJsonUtils} from '../utils/package-json.utils';

type Options = {
directory: string;
nodeVersion: number;
};

export function main(options: Options): Rule {
return (_tree: Tree, context: SchematicContext): Rule => {
context.logger.info('Creating .nvmrc file and updating package.json...');

const templateSource = apply(url('./files'), [
template({...options}),
move(normalize(options.directory)),
]);

return chain([
mergeWith(templateSource, MergeStrategy.Overwrite),
updatePackageJson(options),
]);
};
}

function updatePackageJson(options: Options): Rule {
return (tree: Tree) => {
const path = join(options.directory, 'package.json');

const packageJsonUtils = new PackageJsonUtils(tree, path);
packageJsonUtils.addWithPath(['engines', 'node'], `>=${options.nodeVersion}`);

return tree;
};
}
21 changes: 21 additions & 0 deletions packages/@guidesmiths/cuckoojs-schematics/src/nvmrc/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "SchematicsNvmrc",
"title": "Nvmrc schematic schema",
"type": "object",
"properties": {
"directory": {
"type": "string",
"description": "nvmrc file destination directory.",
"default": ".",
"x-prompt": "Where is the root folder of your project?"
},
"nodeVersion": {
"type": "number",
"description": "node version to use in nvmrc file",
"x-prompt": "What node version would you like for your project?",
"default": 18
}
},
"required": ["directory", "nodeVersion"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,26 @@ export class PackageJsonUtils {
this.checkFileContent();
}

// @TODO: deprecate and replace all usages with addDependency or addDevDependency
addPackage(name: string, version: string, dev = false) {
const section = dev ? 'devDependencies' : 'dependencies';
this.content = this.insertDependency([section, name], version, {ordered: true, overwrite: true});
this.host.overwrite(this.path, this.content);
this.addWithPath([section, name], version, {ordered: true, overwrite: true});
}

addDependency(name: string, version: string) {
this.addWithPath(['dependencies', name], version, {ordered: true, overwrite: true});
}

addDevDependency(name: string, version: string) {
this.addWithPath(['devDependencies', name], version, {ordered: true, overwrite: true});
}

addScript(name: string, value: string) {
this.content = this.insertDependency(['scripts', name], value);
this.addWithPath(['scripts', name], value);
}

addWithPath(path: string[], value: string, options: TInsertDependencyOptions = {}) {
this.content = this.insertDependency(path, value, options);
this.host.overwrite(this.path, this.content);
}

Expand Down

0 comments on commit ced573b

Please sign in to comment.