-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure
More file actions
executable file
·133 lines (119 loc) · 4.67 KB
/
Copy pathconfigure
File metadata and controls
executable file
·133 lines (119 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
// Given a number of files, scans them and generates package.config declarations
// for the packages in those files. Also takes into account any configuration
// information declared explicitly in them.
//
// Run like this -
//
// node configure.js file1.js file2.js ... > pkgmap.js
// node build.js pkgmap.js main.js > concatted.js
// minify concatted.js > main.min.js
function package(name) {
package.__configurations__[name] = {path: package.__PATH__};
}
package.__configurations__ = {}
package.config = function (cfg) {
Object.keys(cfg).forEach(function (k) {
// path configurations override non-path configurations
// (i.e. urls and externals). This lets you give preference
// to local files over what is fetched from the net. Otherwise,
// we flag a "conflict" and report to user.
var conflict = package.__configurations__.hasOwnProperty(k)
&& !(cfg[k].path && !package.__configurations__[k].path)
&& JSON.stringify(package.__configurations__[k]) !== JSON.stringify(cfg[k]);
if (conflict) {
console.error("Package [" + k + "] has conflicting configuration!");
console.error("\tPrevious: " + JSON.stringify(package.__configurations__[k]));
console.error("\tNew: " + JSON.stringify(cfg[k]));
throw "Aborted";
} else {
package.__configurations__[k] = cfg[k];
}
});
};
package.fetch = function (url, callback) {
callback(package, url, 'undefined'); // Dummy
};
package.declare = function () {
// no op
};
package.external = function (pkgname, exportedName, url, dependsOn, depNames) {
var cfg = {};
cfg[pkgname] = { external: {
url: url,
dependsOn: dependsOn || [],
depNames: depNames || dependsOn || [],
name: exportedName
}};
package.config(cfg);
};
function fetchKnownPackageConfigs(next) {
var urlp = require('url').parse('http://sriku.org/lib/package_registry/packages.js');
urlp.headers = {'Accept-Encoding': 'identity'};
require(urlp.protocol.split(':')[0]).get(urlp, function (res) {
var source = "";
res.setEncoding('utf8');
res.on('data', function (chunk) {
source += chunk;
});
res.on('end', function () {
eval('(function (package) {\n' + source + '\n})')(package);
require('fs').writeFileSync('./.packages.js', source, 'utf8');
next && next();
});
res.on('error', function (err) {
next && next();
});
}).on('error', function (err) {
next && next();
});
}
if (process.argv.length <= 2) {
process.stderr.write('Usage: configure file1.js file2.js ... > build-config.js\n');
process.stderr.write("Will generate a package.config() declaration for all the packages\n");
process.stderr.write("found in the given files to stdout. The order of the files is irrelevant,\n");
process.stderr.write("so you don't need to know the package dependencies in advance. Just make\n");
process.stderr.write("the generated configuration the first file you provide to the build.js.\n");
process.stderr.write("\nThe '-r' or '--registry' flag can be used to fetch the registry\n");
process.stderr.write("of known packages.\n\n");
}
function main() {
var slicedArgs = process.argv.slice(2);
var i = 0, arg;
function iter() {
if (i < slicedArgs.length) {
arg = slicedArgs[i++];
switch (arg) {
case '-r':
case '--registry':
fetchKnownPackageConfigs(iter);
break;
default:
package.__PATH__ = arg;
eval('(function (package) {\n' + require('fs').readFileSync(arg, 'utf8') + '\n})')(package);
process.nextTick(iter);
}
} else {
process.stdout.write('package.config({\n');
Object.keys(package.__configurations__).forEach(function (c, i, keys) {
process.stdout.write(' ' + JSON.stringify(c) + ': ');
var cfg = package.__configurations__[c];
if (cfg && cfg.constructor === Function) {
process.stdout.write('(' + cfg.toString() + ')');
} else {
process.stdout.write(JSON.stringify(cfg));
}
if (i + 1 < keys.length) {
process.stdout.write(',\n');
} else {
process.stdout.write('\n');
}
});
process.stdout.write('});\n');
}
}
if (slicedArgs.length > 0) {
iter();
}
}
main();