1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var fs = require('fs');
|
11 | var path = require('path');
|
12 | var scope = require('./scope');
|
13 | var colors = require('colors/safe');
|
14 | global.require = require;
|
15 | var defaultProcessors = {
|
16 | "ejs": require('../processor/processor-ejs'),
|
17 | "extract": require('../processor/processor-extract'),
|
18 | "fndep": require('../processor/processor-fndep'),
|
19 | "glob": require('../processor/processor-glob'),
|
20 | "html": require('../processor/processor-html'),
|
21 | "jhtmls": require('../processor/processor-jhtmls'),
|
22 | "jphps": require('../processor/processor-jphps'),
|
23 | "quoted": require('../processor/processor-quoted'),
|
24 | "regex": require('../processor/processor-regex'),
|
25 | "toggle": require('../processor/processor-toggle'),
|
26 | };
|
27 | var defaultTags = {
|
28 | jdists: {
|
29 | encoding: 'original'
|
30 | }
|
31 | };
|
32 | var defaultExclude = [
|
33 | '**/*.+(png|jpeg|jpg|mp3|ogg|gif|eot|ttf|woff)',
|
34 | '**/*.min.+(js|css)'
|
35 | ];
|
36 | var defaultRemove = 'remove,test,debug';
|
37 | var defaultTrigger = 'release';
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function build(filename, argv, hook) {
|
50 |
|
51 | argv = argv || {};
|
52 | argv.trigger = argv.trigger || defaultTrigger;
|
53 | argv.remove = argv.remove || defaultRemove;
|
54 | var scopes = {};
|
55 | var variants = {};
|
56 | var tags = JSON.parse(JSON.stringify(defaultTags));
|
57 | var excludeList = defaultExclude.slice();
|
58 | var removeList = argv.remove.split(/\s*,\s*/);
|
59 | var processors = {};
|
60 | var clean = true;
|
61 | for (var key in defaultProcessors) {
|
62 | processors[key] = defaultProcessors[key];
|
63 | }
|
64 |
|
65 | var configFilename = argv.config || '.jdistsrc';
|
66 | if (fs.existsSync(configFilename)) {
|
67 | var config = JSON.parse(fs.readFileSync(configFilename));
|
68 | if (typeof config.clean !== 'undefined') {
|
69 | clean = config.clean;
|
70 | }
|
71 | if (config.exclude instanceof Array) {
|
72 | excludeList = excludeList.concat(config.exclude);
|
73 | }
|
74 | if (config.processors) {
|
75 | for (var encoding in config.processors) {
|
76 | registerProcessor(encoding, config.processors[encoding]);
|
77 | }
|
78 | }
|
79 | if (config.tags) {
|
80 | for (var name in config.tags) {
|
81 | tags[name] = config.tags[name];
|
82 | }
|
83 | }
|
84 | }
|
85 | else if (configFilename !== '.jdistsrc') {
|
86 | console.error(
|
87 | colors.red('Config file "%s" not exists.'), configFilename
|
88 | );
|
89 | }
|
90 | if (typeof argv.clean !== 'undefined') {
|
91 | clean = argv.clean;
|
92 | }
|
93 | var content;
|
94 | if (argv.fromString) {
|
95 | content = filename;
|
96 | filename = argv.path || 'none';
|
97 | }
|
98 | var rootScope = scope.create({
|
99 | fromString: argv.fromString,
|
100 | content: content,
|
101 | clean: clean,
|
102 | removeList: removeList,
|
103 | excludeList: excludeList,
|
104 | filename: filename,
|
105 | tags: tags,
|
106 | argv: argv,
|
107 | env: process.env,
|
108 | scopes: scopes,
|
109 | variants: variants,
|
110 | processors: processors
|
111 | });
|
112 | if (typeof hook === 'function') {
|
113 | hook(rootScope);
|
114 | }
|
115 | return rootScope.build();
|
116 | }
|
117 | exports.build = build;
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function registerProcessor(encoding, processor) {
|
125 | if (!processor || !encoding) {
|
126 | return;
|
127 | }
|
128 | if (typeof processor === 'function') {
|
129 | defaultProcessors[encoding] = processor;
|
130 | return true;
|
131 | }
|
132 | else if (typeof processor === 'string' && processor) {
|
133 | if (fs.existsSync(processor)) {
|
134 | return registerProcessor(
|
135 | encoding,
|
136 | scope.buildProcessor(fs.readFileSync(processor))
|
137 | );
|
138 | }
|
139 | else {
|
140 | return registerProcessor(
|
141 | encoding,
|
142 | scope.buildProcessor(processor)
|
143 | );
|
144 | }
|
145 | }
|
146 | }
|
147 | exports.registerProcessor = registerProcessor;
|
148 | exports.createScope = scope.create; |
\ | No newline at end of file |