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(); + }); + }); }); })();