-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
164 lines (149 loc) · 6.09 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
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
const fs = require('fs-extra');
const path = require('path');
const Zip = require('adm-zip');
const v3Process = require('./v3Maker');
const process = require('process');
const readline = require('readline');
// Before starting, make sure that the other folders don't exist.
var chromeDir = '../PlayerTube-Chrome';
var firefoxDir = '../PlayerTube-Firefox';
if (fs.existsSync(chromeDir)) {
console.log(`Deleting past Chrome folder`);
fs.rmSync(chromeDir, { recursive: true });
console.log(`Deleted past Chrome folder`);
}
if (fs.existsSync(firefoxDir)) {
console.log(`Deleting past Firefox folder`);
fs.rmSync(firefoxDir, { recursive: true });
console.log(`Deleted past Firefox folder`);
}
// Function to copy all the dirs but not REALLY all of them.
async function copyDir(sourceDir, newDir) {
// Get dirs in the project folder.
var dirs = fs.readdirSync(sourceDir, { withFileTypes: true });
fs.mkdirSync(newDir);
// now we finna do something with all of these dirs
for (let entry of dirs) {
// Folders & files that we AIN'T ALLOWIN'!
if (
entry.name === '.git' ||
entry.name === '.github' ||
entry.name === 'psds' ||
entry.name === 'node_modules' ||
entry.name === 'build.js' ||
entry.name === 'v3Maker.js' ||
entry.name === 'v3elmnts.json' ||
entry.name === 'package-lock.json' ||
entry.name === 'package.json'
) continue;
// If the files passed the vibe check, we go.
const sourcePath = path.join(sourceDir, entry.name);
const newPath = path.join(newDir, entry.name);
if (entry.isDirectory()) {
await copyDir(sourcePath, newPath);
} else {
fs.copyFileSync(sourcePath, newPath);
}
}
}
console.log(`-------------`);
// Here's we build.
// So first, let's copy this folder for Chrome.
copyDir('./', chromeDir).then(async () => {
console.log(`(Re)made the Chrome folder`);
// If the zip already exists...
if (fs.existsSync('../PlayerTube-Chrome.zip')) {
console.log("Deleting old Chrome zip");
fs.unlinkSync('../PlayerTube-Chrome.zip');
console.log("Deleted old Chrome zip");
}
console.log("Zipping Chrome version...");
// Try to zip up the extension
try {
const zip = new Zip();
const outputDir = "../PlayerTube-Chrome.zip";
zip.addLocalFolder("../PlayerTube-Chrome");
zip.writeZip(outputDir);
} catch (e) {
console.log(`WHAT THE FRICK! ${e}`);
}
console.log(`Zipped Chrome version into ../PlayerTube-Chrome.zip`);
console.log(`-------------`);
});
// Then we copy the same folder for Firefox.
copyDir('./', firefoxDir).then(async () => {
console.log(`(Re)made the Firefox folder`);
// Then we modify the Firefox extension a bit cuz no
// browser developer can come up with extension
// manifest standards like WHY.
var firefoxManifest = JSON.parse(fs.readFileSync('../PlayerTube-Firefox/manifest.json', { encoding: 'utf8' }));
firefoxManifest.manifest_version = 2;
firefoxManifest.background = {
"scripts": [
"src/pt-background.js"
],
"persistent": false,
"type": "module"
}; // Add this so Firefox extension can work
firefoxManifest.browser_specific_settings = {
"gecko": {
"id": "{925b0516-774d-469d-bb0e-2b94f199b29a}"
}
};
firefoxManifest.browser_action = {
"default_popup": "html/popup.html",
"default_icon": "img/playertube/icon.png"
}; // Popups
firefoxManifest.web_accessible_resources = [
"css/*"
],
delete firefoxManifest.action; // Manifest v2 moment
delete firefoxManifest.background.service_worker; // This is only for Chrome, Firefox will freak out if this isn't deleted lol.
// Write the manifest file for Firefox
fs.writeFileSync('../PlayerTube-Firefox/manifest.json', JSON.stringify(firefoxManifest, null, 2));
// Since v1.6, we also have to replace all the "chrome-extension://" with "moz-extension://"
// WHY CAN'T IT JUST BE "extension://" OR SOMETHING??????
console.log('Replacing all css files that include "chrome-extension://" with "moz-extension://"');
// For each CSS file, do the replacing moment for both vanilla css and v3 css
async function replaceChromewithMoz(cssFiles) {
let fsCssFiles = fs.readdirSync(cssFiles);
fsCssFiles.forEach(cssFileName => {
let cssPath = `../PlayerTube-Firefox/css/${cssFileName}`;
// Make sure cssPath has an extension
if (cssPath.endsWith('.css')) {
process.stdout.write(`> ${cssPath}`);
// Read the CSS file
let cssFile = fs.readFileSync(cssPath, { encoding: 'utf8' });
// Replace all chrome-extension:// with moz-extension://
cssFile = cssFile.replaceAll('chrome-extension://', 'moz-extension://');
// Then write the CSS file with "cssFile"
fs.writeFileSync(cssPath, cssFile);
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0, null);
process.stdout.write(`✓ ${cssPath}\n`);
}
});
};
await replaceChromewithMoz('../PlayerTube-Firefox/css/');
await replaceChromewithMoz('../PlayerTube-Firefox/css/v3/');
console.log(`Replace complete.`);
// If the zip already exists...
if (fs.existsSync('../PlayerTube-Firefox.zip')) {
console.log("Deleting old Firefox zip");
fs.unlinkSync('../PlayerTube-Firefox.zip');
console.log("Deleted old Firefox zip");
}
console.log("Zipping Firefox version...");
// Try to zip up the extension
try {
const zip = new Zip();
const outputDir = '../PlayerTube-Firefox.zip';
zip.addLocalFolder("../PlayerTube-Firefox");
zip.writeZip(outputDir);
} catch (e) {
console.log(`WHAT THE FRICK! ${e}`);
}
console.log(`Zipped Firefox version into ../PlayerTube-Firefox.zip`);
// End
console.log(`-------------`);
});