forked from Rob--W/browser-action-jplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·63 lines (55 loc) · 1.79 KB
/
build.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
#!/usr/bin/env node
/* jshint node:true */
'use strict';
var fs = require('fs');
var path = require('path');
// npm install mime
var mime = require('mime');
var baseDir = __dirname;
var dataDir = path.join(baseDir, 'data');
var outputFile = path.join(baseDir, 'lib/browser-action-jplib-data.js');
var encodedFiles = {};
// Assume that there are no subdirectories in the data dir
fs.readdirSync(dataDir)
.forEach(function(filename) {
if (filename.charAt(0) === '.') {
// Skip hidden / temporary files
return;
}
var pathToFile = path.join(dataDir, filename);
var stat = fs.statSync(pathToFile);
var mimeType = mime.lookup(filename);
var data = fs.readFileSync(pathToFile);
var base64encoded = new Buffer(data).toString('base64');
var dataURI = 'data:' + mimeType + ';base64,' + base64encoded;
encodedFiles[filename] = dataURI;
});
var moduleContent =
[
'/**',
' * (c) 2013 Rob Wu <[email protected]>',
' * Released under the MIT license.',
' *',
' * Auto-generated from "data" dir by build.js',
' * Because the Add-on SDK does not support loading assets',
' * from the data dir for third-party modules.',
' **/',
'',
'\'use strict\';',
'',
'const base64 = require(\'sdk/base64\');',
'',
'function uri(path) {',
' path = (\'\'+path).split(/[#?]/, 1)[0];',
' if (!dataFiles[path]) throw new Error(\'Resource not found: \' + path);',
' return dataFiles[path];',
'}',
'exports.url = uri;',
'exports.load = function(path) {',
' var fileAsBase64 = uri(path).split(\',\')[1];',
' return base64.decode(fileAsBase64);',
'};',
'',
'const dataFiles = ' + JSON.stringify(encodedFiles, null, 4) + ';'
].join('\n');
fs.writeFileSync(outputFile, moduleContent);