1 | 'use strict';
|
2 |
|
3 | var glob = require('glob')
|
4 | , path = require('path')
|
5 | , fs = require('fs')
|
6 | , runnel = require('runnel')
|
7 |
|
8 | function 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 |
|
15 |
|
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 |
|
26 | function 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 |
|
33 | var go = module.exports =
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function 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 |
|
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 |
|
74 | module.exports.sync =
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function 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 | }
|