-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprepare.js
More file actions
87 lines (74 loc) · 2.91 KB
/
Copy pathprepare.js
File metadata and controls
87 lines (74 loc) · 2.91 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
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const rimraf = require('rimraf');
function prepareExtension() {
// 1. Read version from manifest.json
const manifestPath = path.join(__dirname, 'src', 'manifest.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
const version = manifest.version;
// 2. Create temp folders
const tempChromePath = path.join(__dirname, 'temp-chrome');
const tempFirefoxPath = path.join(__dirname, 'temp-firefox');
fs.mkdirSync(tempChromePath);
fs.mkdirSync(tempFirefoxPath);
copyFolderRecursiveSync(path.join(__dirname, 'src'), tempChromePath);
copyFolderRecursiveSync(path.join(__dirname, 'src'), tempFirefoxPath);
// 3. Combine manifest files in temp-firefox
const firefoxManifestTemplate = JSON.parse(fs.readFileSync(path.join(tempFirefoxPath, 'manifest.firefox.json'), 'utf8'));
const firefoxManifest = combineManifests(manifest, firefoxManifestTemplate);
delete firefoxManifest.action;
fs.writeFileSync(path.join(tempFirefoxPath, 'manifest.json'), JSON.stringify(firefoxManifest, null, 2));
// 4. Delete manifest.firefox.json
fs.unlinkSync(path.join(tempChromePath, 'manifest.firefox.json'));
fs.unlinkSync(path.join(tempFirefoxPath, 'manifest.firefox.json'));
// 5. Replace external API
const apiUrl = process.env.APIURL;
replaceTokens(tempChromePath, apiUrl);
replaceTokens(tempFirefoxPath, apiUrl);
// 6. Archive extensions
archiveExtension(tempChromePath, `src-chrome-${version}.zip`, 'src');
archiveExtension(tempFirefoxPath, `src-firefox-${version}.zip`);
}
function combineManifests(target, source) {
const output = { ...target };
for (const key in source) {
if (source.hasOwnProperty(key)) {
output[key] = source[key];
}
}
return output;
}
function copyFolderRecursiveSync(srcPath, destPath) {
const entries = fs.readdirSync(srcPath, { withFileTypes: true });
fs.mkdirSync(destPath, { recursive: true });
entries.forEach((entry) => {
const src = path.join(srcPath, entry.name);
const dest = path.join(destPath, entry.name);
if (entry.isDirectory()) {
copyFolderRecursiveSync(src, dest);
} else {
fs.copyFileSync(src, dest);
}
});
}
function archiveExtension(dirPath, name, dir = false) {
const zipPath = path.join(__dirname, name);
const zip = archiver('zip', { zlib: { level: 9 } });
const zipStream = fs.createWriteStream(zipPath);
zip.pipe(zipStream);
zip.directory(dirPath, dir);
zip.finalize();
zipStream.on('close', () => {
console.log(`Created ${zipPath}`);
rimraf.sync(dirPath);
});
}
function replaceTokens(dirPath, apiUrl) {
const utilsFilePath = path.join(dirPath, 'scripts', 'utils.js');
const utilsContent = fs.readFileSync(utilsFilePath, 'utf8');
const replacedContent = utilsContent
.replace(/%APIURL%/g, apiUrl)
fs.writeFileSync(utilsFilePath, replacedContent);
}
prepareExtension();