-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite-plugin-package-extensions.ts
191 lines (160 loc) · 5.44 KB
/
vite-plugin-package-extensions.ts
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
import { PluginOption } from 'vite';
import fs from 'fs';
import path from 'path';
import JSZip from 'jszip';
import getUuid from 'uuid-by-string';
import childProcess from 'node:child_process';
import packageJson from './package.json';
import gherSay from 'ghersay';
export default function packageExtensions(): PluginOption {
const inDir = 'dist';
const outDir = 'extensions';
const baseBranch = 'production';
const { version } = packageJson;
function addFilesToZipArchive(zip: JSZip | null, inDir: string) {
const listOfFiles = fs.readdirSync(inDir);
listOfFiles.forEach((fileName) => {
const filePath = path.join(inDir, fileName);
const file = fs.statSync(filePath);
if (file?.isDirectory()) {
const dir = zip!.folder(fileName);
addFilesToZipArchive(dir, filePath);
} else {
zip!.file(fileName, fs.readFileSync(filePath));
}
});
}
function createZipArchive(zip: JSZip, outFileName: string) {
zip
.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: {
level: 9,
},
})
.then((file) => {
const fileName = path.join(outDir, outFileName);
if (fs.existsSync(fileName)) {
fs.unlinkSync(fileName);
}
fs.writeFileSync(fileName, file);
});
}
function modifyManifest(inDir: string) {
const data = fs.readFileSync(path.join(inDir, 'manifest.json'), {
encoding: 'utf-8',
});
const jsonData = JSON.parse(data);
// having use_dynamic_url prevents extension being installed in firefox since its an invalid value there
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/web_accessible_resources
// https://bugzilla.mozilla.org/show_bug.cgi?id=1713196
delete jsonData['web_accessible_resources'][0]['use_dynamic_url'];
const bgData = jsonData['background'];
jsonData['background'] = {
scripts: [bgData["service_worker"]],
type: "module"
}
// All Manifest V3 extensions need an add-on ID in their manifest.json when submitted to AMO.
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/browser_specific_settings#description
jsonData.browser_specific_settings = {
gecko: {
id: `{${getUuid('pert-with-wings')}}`,
strict_min_version: '58.0',
},
};
fs.writeFileSync(
path.join(inDir, 'manifest.json'),
JSON.stringify(jsonData, null, 2),
{ encoding: 'utf-8' }
);
return data;
}
return {
name: 'vite-plugin-package-extensions',
apply: 'build',
closeBundle() {
try {
gherSay(`Let's build v${version}`);
console.log(
' %s \x1b[42m\x1b[30m\033[1m %s \x1b[0m\x1b[32m\033[1m %s \x1b[0m%s',
'🧩',
'PERT WITH WINGS',
`v${version}`,
`started packaging extensions:`
);
console.log(' ');
if (fs.existsSync(inDir)) {
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir);
}
const chromeZip = new JSZip();
console.log(' - Preparing files for Chrome extension.');
addFilesToZipArchive(chromeZip, inDir);
console.log(' - Creating Chrome extension package.');
createZipArchive(
chromeZip,
`chrome-PERT-with-wings-package-${version}.zip`
);
console.log(
'\x1b[32m\033[1m%s\x1b[0m',
' ✓ Chrome extension packaged.'
);
const firefoxZip = new JSZip();
console.log(' - Preparing files for Firefox extension.');
const originalManifestData = modifyManifest(inDir);
addFilesToZipArchive(firefoxZip, inDir);
console.log(' - Creating Firefox extension package.');
createZipArchive(
firefoxZip,
`firefox-PERT-with-wings-package-${version}.zip`
);
console.log(
'\x1b[32m\033[1m%s\x1b[0m',
' ✓ Firefox extension packaged.'
);
console.log(' - Creating source code zip file.');
const gitArchive = childProcess.exec(
`git archive --format zip --output extensions/pert-extension-source-code-${version}.zip ${baseBranch}`
);
gitArchive.stdout.on('close', () => {
console.log(
'\x1b[32m\033[1m%s\x1b[0m',
' ✓ Source code zip file created.'
);
console.log(' ');
console.log(
'\x1b[36m%s\x1b[0m',
' Packages successfully created in /extensions'
);
console.log(' ');
console.log(
' - Reverting manifest to original.'
);
fs.writeFileSync(
path.join(inDir, 'manifest.json'),
originalManifestData,
{ encoding: 'utf-8' }
);
console.log(
'\x1b[32m\033[1m%s\x1b[0m',
' ✓ Reverted manifest to original.'
);
console.log(' ');
console.log(' ');
});
} else {
console.log(
'\x1b[31m%s\x1b[0m',
` × "${inDir}" folder does not exist!`
);
}
} catch (error) {
console.log(
'\x1b[31m%s\x1b[0m',
' × Something went wrong while building packages!'
);
}
},
};
}