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
This commit is contained in:
Michael Krotscheck
2016-05-26 07:56:20 -07:00
parent 4c3e27d6b7
commit b6e9361bf6
2 changed files with 40 additions and 1 deletions

View File

@@ -120,12 +120,31 @@
return defaultValue || undefined;
}
/**
* Create a specific NPM script command, invoked by 'npm run <name>' or 'npm <name>'.
*
* @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
};
})();

View File

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