Created
July 30, 2015 13:29
-
-
Save okunishinishi/5ed5f14bdac2b168458c to your computer and use it in GitHub Desktop.
bowerから必要なファイルだけとってきて必要な場所に配置するようにする ref: http://qiita.com/okunishinishi@github/items/7be00d641930481ba376
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bower install jquery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bower_components | |
└─ jquery | |
├─ dist | |
| └─ ... | |
├─ src | |
| └─ ... | |
├─ bower.json | |
├─ .bower.json | |
├─ bower.json | |
└─ MIT-LICENSE.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# jqueryの最新版と、angularのv1.4.3を"app/public/vendor"ディレクトリに配置する | |
$ handybower jquery angular#1.4.3 -d app/public/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var handybower = require('handybower'); | |
handybower([ | |
"jquery", | |
"angular#1.4.3" | |
], { | |
dest: "app/public/vendor" | |
}, function (err) { | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bower.commands | |
.install(['jquery'], { save: true }, { /* custom config */ }) | |
.on('end', function (installed) { | |
console.log(installed); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Install bower components and collect main files | |
* @function handyBower | |
* @param {string|string[]} name - Module name to install. | |
* @param {object} options - Optional settings. | |
* @param {string} [options.dest='handybower_components'] - Destination directory path | |
* @param {boolean} [options.verbose=false] - Log verbose. | |
* @param {string[]} [options.main] - Name of main files. Default is `main` attribuite in installed bower.json. | |
* @param {function} callback - Callback when done. | |
*/ | |
"use strict"; | |
var argx = require('argx'), | |
writeout = require('writeout'), | |
fs = require('fs'), | |
path = require('path'), | |
glob = require('glob'), | |
async = require('async'), | |
bower = require('bower'), | |
Colorprint = require('colorprint/lib/colorprint'); | |
function handyBower(names, options, callback) { | |
var args = argx(arguments); | |
callback = args.pop('function') || argx.noop; | |
options = args.pop('object'); | |
names = args.remain().reduce(_concat, []).map(function (name) { | |
if (typeof(name) === 'object') { | |
var data = name; | |
return Object.keys(data).map(function (name) { | |
return [name, data[name]].join('#'); | |
}); | |
} else { | |
return String(name); | |
} | |
}).reduce(_concat, []); | |
var logger = new Colorprint({ | |
PREFIX: '[handybower] ' | |
}); | |
var verbose = !!options.verbose, | |
main = [].concat(options.main || '') | |
.reduce(_concat, []) | |
.filter(_notEmpty); | |
if (verbose) { | |
logger.trace('options:\n', options); | |
} | |
var destDir = options.dest || 'handybower_components'; | |
if (!destDir) { | |
callback(new Error('dest is required.')); | |
return; | |
} | |
var aborted = false; | |
var components = {}; | |
logger.info('Installing started...'); | |
bower.commands | |
.install(names, { | |
force: true | |
}, { | |
interactive: false | |
}) | |
.on('error', function (err) { | |
callback(err); | |
aborted = true; | |
}) | |
.on('log', function (packet) { | |
var data = packet.data || {}; | |
var endpoint = data.endpoint || {}; | |
var name = endpoint.name; | |
components[name] = components[name] || data.canonicalDir; | |
}) | |
.on('end', function (installed) { | |
if (aborted) { | |
return; | |
} | |
Object.keys(installed).forEach(function (name) { | |
components[name] = components[name] || installed[name]['canonicalDir']; | |
}); | |
var dirnames = Object.keys(components).map(function (name) { | |
return components[name]; | |
}); | |
async.eachSeries(dirnames, function (dirname, callback) { | |
var data = require(path.resolve(dirname, 'bower.json')); | |
var found = [].concat(main && main.length ? main : data.main).map(function (pattern) { | |
return glob.sync(pattern, {cwd: dirname}); | |
}).reduce(_concat, []); | |
if (verbose) { | |
logger.trace('main files:\n%s', found); | |
} | |
async.eachSeries(found, function (filename, callback) { | |
var src = path.resolve(dirname, filename), | |
dest = path.resolve(destDir, path.basename(filename)); | |
async.waterfall([ | |
function (callback) { | |
fs.readFile(src, callback); | |
}, | |
function (content, callback) { | |
writeout(dest, String(content), { | |
mkdirp: true | |
}, callback); | |
}, | |
function (result, callback) { | |
if (!result.skipped) { | |
logger.debug('File generated:', dest); | |
} | |
callback(null); | |
} | |
], callback); | |
}, callback); | |
}, function (err) { | |
if (err) { | |
console.error(err); | |
logger.error('...installing failed!'); | |
} else { | |
logger.info('...installing done!'); | |
} | |
callback(err); | |
}); | |
}); | |
} | |
function _concat(a, b) { | |
return a.concat(b); | |
} | |
function _notEmpty(val) { | |
return !!val; | |
} | |
module.exports = handyBower; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* @file Bins script for handybower. | |
*/ | |
"use strict"; | |
var program = require('commander'), | |
pkg = require('../package'), | |
handybower = require('../handybower'); | |
program | |
.usage('[options] [name ...]') | |
.version(pkg['version']) | |
.option('-d, --dest <dest>', "Destination directory path.") | |
.option('-v, --verbose', "Show verbose log.") | |
.parse(process.argv); | |
var moduleNames = program.args; | |
handybower(moduleNames, { | |
verbose: program.verbose, | |
dest: program.dest, | |
main: program.main && (program.main).split(/,/g) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install handybower -g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment