UNPKG

2.99 kBJavaScriptView Raw
1'use strict';
2
3var glob = require('glob')
4 , path = require('path')
5 , fs = require('fs')
6 , runnel = require('runnel')
7
8function addTransform(front, transform, packfile) {
9 var pack = require(packfile);
10 if (!pack.browserify) pack.browserify = {};
11 if (!pack.browserify.transform) pack.browserify.transform = [];
12
13 transform.forEach(function (tx) {
14 // remove previously injected transform (i.e. may have been added at the end, but now needs to be in front)
15 // other option would be to only add it if it's not there already, but then front/back switching wouldn' work
16 var idx = pack.browserify.transform.indexOf(tx);
17 if (~idx) pack.browserify.transform.splice(idx, 1);
18 });
19
20 if (front) pack.browserify.transform = transform.concat(pack.browserify.transform);
21 else pack.browserify.transform = pack.browserify.transform.concat(transform);
22
23 return { file: packfile, pack: pack };
24}
25
26function packsWithTransforms(root, transform, front, relPaths) {
27 return relPaths
28 .map(function (x) { return path.resolve(root, x) })
29 .map(addTransform.bind(null, front, transform));
30}
31
32
33var go = module.exports =
34
35/**
36 * Injects the given transform(s) into the `browserify.transform` field of all `package.json`s
37 * at and below the given `root`.
38 *
39 * @name viralify
40 * @function
41 * @param {String} root of the package
42 * @param {Array.<String>} transform one or more transforms to be added to the transform field
43 * @param {Boolean=} front if set transforms are added to the front of the transform field so they run first
44 * @param {Function(Error)} cb called when the transform injection is complete
45 */
46function viralify(root, transform, front, cb) {
47 if (!Array.isArray(transform)) transform = [ transform ];
48
49 if (typeof front === 'function') {
50 cb = front;
51 front = false;
52 }
53
54 glob('**/package.json', { cwd: root }, function (err, relPaths) {
55 if (err) return cb(err);
56
57 // nothing to do
58 if (!relPaths.length) return cb();
59
60 var packs = packsWithTransforms(root, transform, front, relPaths);
61
62 var tasks = packs
63 .map(function (p) {
64 return function (cb) {
65 fs.writeFile(p.file, JSON.stringify(p.pack, null, 2), 'utf8', cb);
66 }
67 })
68 .concat(cb);
69
70 runnel(tasks);
71 });
72};
73
74module.exports.sync =
75
76/**
77 * Same as `viralify` but performed synchronously.
78 *
79 * @name viralivy.sync
80 * @function
81 * @param {String} root of the package
82 * @param {Array.<String>} transform one or more transforms to be added to the transform field
83 * @param {Boolean=} front if set transforms are added to the front of the transform field so they run first
84 */
85function sync(root, transform, front) {
86 if (!Array.isArray(transform)) transform = [ transform ];
87
88 var relPaths = glob.sync('**/package.json', { cwd: root })
89 var packs = packsWithTransforms(root, transform, front, relPaths);
90 packs.forEach(function (p) {
91 fs.writeFileSync(p.file, JSON.stringify(p.pack, null, 2), 'utf8');
92 })
93}