forked from webextensions/live-css-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip-extension.js
executable file
·110 lines (96 loc) · 3.91 KB
/
zip-extension.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
#!/usr/bin/env node
/* eslint-env node */
var fs = require('fs'),
cpFile = require('cp-file'),
del = require('del'),
archiver = require('archiver'),
chalk = require('chalk');
var processCommand = process.title + ' ' + require('path').relative(__dirname, process.argv[1]);
console.log(chalk.gray([
'',
'Format: ' + processCommand + ' [chrome/edge/firefox/opera]',
'Examples: ' + processCommand,
' ' + processCommand + ' chrome',
' ' + processCommand + ' edge',
''
].join('\n')));
var whichBrowser = process.argv[2] || 'chrome';
var zipFileName = null;
switch (whichBrowser) {
case 'chrome': zipFileName = 'extension-chrome.zip'; break;
case 'edge': zipFileName = 'extension-edge.zip'; break;
case 'firefox': zipFileName = 'extension-firefox.zip'; break;
case 'opera': zipFileName = 'extension-opera.zip'; break;
default:
console.log(chalk.red('Error: Please pass a supported browser name as parameter (or no parameters)'));
process.exit(1);
}
if (fs.readFileSync(__dirname + '/extension/manifest.json', 'utf8') !== fs.readFileSync(__dirname + '/extension/manifest-chrome.json', 'utf8')) {
console.log(chalk.yellow('Warning: extension/manifest.json & extension/manifest-chrome.json do not match.\nThey must have same contents before zipping the extension with this script. Exiting.'));
process.exit(1);
}
// create a file to stream archive data to.
var output = fs.createWriteStream(__dirname + '/' + zipFileName);
var archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
var warnUserToCheckManifestFile = function (e) {
console.log(chalk.yellow('\nWarning: Due to this error, the file extension/manifest.json might have been modified/deleted. Please check manually.\n'));
throw e;
};
// listen for all archive data to be written
output.on('close', function() {
try {
del.sync(['extension/manifest.json']);
cpFile.sync('extension/manifest-chrome.json', 'extension/manifest.json', {overwrite: false});
console.log(chalk.green('The extension has been zipped as: ' + zipFileName + ' (' + archive.pointer() + ' bytes)'));
} catch (e) {
warnUserToCheckManifestFile(e);
}
});
// good practice to catch error explicitly
archive.on('error', function(e) {
console.log(chalk.red('Error: An unexpected error occurred in zipping the extension.'));
warnUserToCheckManifestFile(e);
});
// pipe archive data to the file
archive.pipe(output);
try {
del.sync(['extension/manifest.json']);
cpFile.sync(
'extension/' +
(function () {
switch (whichBrowser) {
case 'chrome': return 'manifest-chrome.json';
case 'edge': return 'manifest-edge.json';
case 'firefox': return 'manifest-firefox.json';
case 'opera': return 'manifest-opera.json';
default: return 'manifest-chrome.json';
}
}()),
'extension/manifest.json',
{overwrite: false}
);
archive.glob('**/*', {
cwd: __dirname + '/extension',
ignore: (function () {
var pathsToIgnore = [
'manifest-generator.js',
'manifest-chrome.json',
'manifest-edge.json',
'manifest-firefox.json',
'manifest-opera.json',
'ui-images/**/*.*', // Exclude files in "ui-images" folder
'ui-images' // Avoid "ui-images" folder from getting created
];
if (whichBrowser !== 'opera') {
pathsToIgnore.push('scripts/3rdparty/sass/**');
}
return pathsToIgnore;
}())
}, {});
// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();
} catch (e) {
warnUserToCheckManifestFile(e);
}