forked from Rob--W/crxviewer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
make.js
executable file
·222 lines (204 loc) · 6.17 KB
/
make.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env node
/**
* (c) 2013 Rob Wu <rob@robwu.nl>
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Build tool, adapted from PDF.js
/* jshint node:true */
/* globals cd, cp, echo, exec, find, grep, mkdir, rm, target, test */
'use strict';
require('shelljs/make');
var builder = require('./external/builder');
var ROOT_DIR = __dirname + '/';
var BUILD_DIR = ROOT_DIR + 'dist/';
var SRC_DIR = ROOT_DIR + 'src/';
var CHROME_BUILD_DIR = BUILD_DIR + 'chrome/';
var OPERA_BUILD_DIR = BUILD_DIR + 'opera/';
var FIREFOX_BUILD_DIR = BUILD_DIR + 'firefox/';
var WEB_BUILD_DIR = BUILD_DIR + 'web/';
var ALLOWED_FILES = [
'manifest.json',
'.css',
'.html',
'.js',
'.png',
];
function getVersionString() {
cd(ROOT_DIR);
var gitHash = exec('git log --format=%H -n1', {silent: true}).stdout.trim();
return gitHash || '(unknown version)';
}
function getBuildConfig(options) {
var dest_dir = options.build_dir;
var setup = {
defines: {
CHROME: false,
FIREFOX: false,
OPERA: false,
WEB: false,
VERSION: getVersionString(),
},
mkdirs: [
dest_dir + 'icons',
],
copy: [
[SRC_DIR + 'lib', dest_dir],
[SRC_DIR + 'icons/*.png', dest_dir + 'icons']
],
preprocess: [
[SRC_DIR + '*.html', dest_dir],
[SRC_DIR + '*.js', dest_dir],
[SRC_DIR + 'lib/crx-to-zip.js', dest_dir + 'lib'],
]
};
if (options.defines)
for (var key in options.defines)
setup.defines[key] = options.defines[key];
if (options.copy) [].push.apply(setup.copy, options.copy);
if (options.preprocess) [].push.apply(setup.preprocess, options.preprocess);
return setup;
}
function cleanDirectory(dir) {
if (test('-d', dir))
rm('-rf', dir + '*'); // Append wildcard to preserve the directory
else
mkdir('-p', dir);
}
function build(setup, output_root_dir) {
cleanDirectory(output_root_dir);
builder.build(setup);
exec(ROOT_DIR + '/node_modules/.bin/lessc --strict-math=on "' + SRC_DIR + 'crxviewer.less" "' + output_root_dir + 'crxviewer.css"');
}
function removeFirefoxSpecificFiles() {
rm('domain-fronter.js');
}
function removeTestFiles() {
rm('lib/prettify/test-prism-language-detect.js');
rm('lib/prettify/test-prism-li-lines.js');
rm('lib/prettify/test-prism-source-extensions.html');
rm('test-asn1.html');
}
function lintDir(dest_dir) {
var warningCount = 0;
find(dest_dir).forEach(function(filepath) {
if (/\/\.[^\/]*$/.test(filepath)) {
echo('WARNING: Found dot file: ' + filepath);
++warningCount;
return;
}
if (test('-d', filepath)) {
return;
}
var unprocessed = grep('//#', filepath).trim();
if (unprocessed) {
echo('WARNING: unprocessed text in' + filepath);
echo(unprocessed);
++warningCount;
}
if (!ALLOWED_FILES.some(function(p) {
return p.test ? p.test(filepath) : filepath.endsWith(p);
})) {
echo('WARNING: Unrecognized file: ' + filepath);
++warningCount;
}
});
if (warningCount) {
process.nextTick(function() {
echo('WARNING: ' + warningCount + ' warnings in ' + dest_dir);
});
}
}
target.all = function() {
target.chrome();
target.opera();
target.firefox();
target.web();
};
target.chrome = function() {
echo();
echo('Building Chrome extension...');
var setup = getBuildConfig({
build_dir: CHROME_BUILD_DIR,
defines: {
CHROME: true
},
copy: [
[SRC_DIR + 'manifest.json', CHROME_BUILD_DIR]
]
});
build(setup, CHROME_BUILD_DIR);
cd(CHROME_BUILD_DIR);
removeFirefoxSpecificFiles();
removeTestFiles();
rm('-f', '../crxviewer.zip');
exec('7z a ../crxviewer.zip * -tzip');
lintDir(CHROME_BUILD_DIR);
};
target.opera = function() {
echo();
echo('Building Opera extension...');
var setup = getBuildConfig({
build_dir: OPERA_BUILD_DIR,
defines: {
OPERA: true
}
});
build(setup, OPERA_BUILD_DIR);
cp(SRC_DIR + 'manifest_opera.json', OPERA_BUILD_DIR + 'manifest.json');
cd(OPERA_BUILD_DIR);
removeFirefoxSpecificFiles();
removeTestFiles();
rm('-f', '../crxviewer_opera.zip');
exec('7z a ../crxviewer_opera.zip * -tzip');
lintDir(OPERA_BUILD_DIR);
};
target.firefox = function() {
echo();
echo('Building Firefox addon...');
var setup = getBuildConfig({
build_dir: FIREFOX_BUILD_DIR,
defines: {
FIREFOX: true
}
});
build(setup, FIREFOX_BUILD_DIR);
cp(SRC_DIR + 'manifest_firefox.json', FIREFOX_BUILD_DIR + 'manifest.json');
cd(FIREFOX_BUILD_DIR);
removeTestFiles();
// Split incognito is not supported in Firefox.
rm('incognito-events.js');
rm('-f', '../crxviewer_firefox.zip');
exec('7z a ../crxviewer_firefox.zip * -tzip');
lintDir(FIREFOX_BUILD_DIR);
};
target.web = function() {
echo();
echo('Building online demo...');
var dest_dir = WEB_BUILD_DIR;
var setup = {
defines: {
CHROME: false,
FIREFOX: false,
OPERA: false,
WEB: true,
VERSION: getVersionString(),
},
copy: [
[SRC_DIR + 'search-worker.js', dest_dir],
[SRC_DIR + 'search-tools.js', dest_dir],
[SRC_DIR + 'lib', dest_dir],
[SRC_DIR + 'chrome-platform-info.js', dest_dir],
[SRC_DIR + 'asn1lite.js', dest_dir],
[SRC_DIR + 'mozcose.js', dest_dir],
],
preprocess: [
[SRC_DIR + 'crxviewer.html', dest_dir],
[SRC_DIR + 'crxviewer.js', dest_dir],
[SRC_DIR + 'cws_pattern.js', dest_dir],
[SRC_DIR + 'lib/crx-to-zip.js', dest_dir + 'lib'],
]
};
build(setup, WEB_BUILD_DIR);
lintDir(WEB_BUILD_DIR);
};