From b6e9361bf6823d39e7380e06331a80bb063d685e Mon Sep 17 00:00:00 2001 From: Michael Krotscheck Date: Thu, 26 May 2016 07:56:20 -0700 Subject: [PATCH] Script commands may now be added to a package. This adds the pkgBuilder.addCommand() method, which permits adding a command (such as 'eslint':'eslint ./') to the npm scripts list. Change-Id: I0ebfa9cbc0a526363cd2c8d1a0b697f5748e0771 --- generators/app/lib/pkg_builder.js | 21 ++++++++++++++++++++- spec/app/lib/pkg_builder.js | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/generators/app/lib/pkg_builder.js b/generators/app/lib/pkg_builder.js index a20a13b..7a494b1 100644 --- a/generators/app/lib/pkg_builder.js +++ b/generators/app/lib/pkg_builder.js @@ -120,12 +120,31 @@ return defaultValue || undefined; } + /** + * Create a specific NPM script command, invoked by 'npm run ' or 'npm '. + * + * @param {String} name The name of the script. + * @param {String} command The command to invoke. + * @returns {void} + */ + function addCommand (name, command) { + + // sanity check, does 'scripts' exist? + if (!pkgContent.hasOwnProperty('scripts')) { + pkgContent.scripts = {}; + } + + // Save/Override the command. + pkgContent.scripts[name] = command; + } + module.exports = { fromJSON: readPackage, toJSON: writePackage, setValues: setValues, getValues: getValues, getValue: getValue, - addDependencies: addDependencies + addDependencies: addDependencies, + addCommand: addCommand }; })(); diff --git a/spec/app/lib/pkg_builder.js b/spec/app/lib/pkg_builder.js index 48c11fe..1264cb0 100644 --- a/spec/app/lib/pkg_builder.js +++ b/spec/app/lib/pkg_builder.js @@ -187,5 +187,25 @@ expect(builder.getValues()).toEqual({dependencies: {}}); }); }); + + describe('addCommand', function () { + it('should add a command', function () { + builder.fromJSON('{"scripts":{}}'); + builder.addCommand('foo', 'bar'); + expect(builder.getValue('scripts').foo).toBe('bar'); + }); + + it('should overwrite an existing command', function () { + builder.fromJSON('{"scripts":{"foo":"bar"}}'); + builder.addCommand('foo', 'lol'); + expect(builder.getValue('scripts').foo).toBe('lol'); + }); + + it('should create the scripts hash if it doesn\'t exist', function () { + builder.fromJSON('{}'); + builder.addCommand('foo', 'bar'); + expect(builder.getValue('scripts')).toBeDefined(); + }); + }); }); })();