grunt-sync-pkg
Version:
Minimalist Grunt plugin to sync only basic properties between package.json and bower.json
59 lines (45 loc) • 1.7 kB
JavaScript
/**
* grunt-sync-pkg
* http://github.com/jonschlinkert/grunt-sync-pkg
*
* Copyright (c) 2013 Jon Schlinkert, contributors
* Licensed under the MIT license.
*/
;
module.exports = function (grunt) {
grunt.registerTask('sync', 'Sync manifests.', function () {
// If bower.json doesn't exist yet, add one.
if (!grunt.file.exists('bower.json')) {
grunt.file.write('bower.json', "{}");
}
var bower = grunt.file.readJSON('bower.json');
var pkg = grunt.file.readJSON('package.json');
var _ = require('lodash');
var options = this.options({
name: pkg.name,
version: pkg.version,
main: [pkg.main],
include: []
});
// Sync any properties listed in the "include" array.
options.include.forEach(function(key) {
options[key] = pkg[key];
});
// Format the 'main' property as an array for bower.json
if (pkg.main || options.main) {
options.main = _.union([], [pkg.main], (options.main || ['']));
}
var bowerProps = _.extend(bower, options);
bowerProps = _.omit(bowerProps, options.exclude, ['exclude', 'include', 'alt']);
if (!_.isString(pkg.main)) {
grunt.fail.warn('>>'.yellow + ' The "main" property is missing in package.json.'.bold);
}
grunt.file.write('bower.json', JSON.stringify(bowerProps, null, 2));
if(options.alt) {
var alt = grunt.file.readJSON(options.alt);
var altProps = _.extend(alt, options);
altProps = _.omit(altProps, options.exclude, ['exclude', 'include', 'alt', 'main']);
grunt.file.write(options.alt, JSON.stringify(altProps, null, 2));
}
});
};