Repository navigation
Expand file tree
/
Copy pathbundle-extension.js
More file actions
187 lines (153 loc) · 5.41 KB
/
Copy pathbundle-extension.js
File metadata and controls
187 lines (153 loc) · 5.41 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
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
import { exec } from "child_process";
import { copy } from "fs-extra";
import { copyFile, rm, writeFile } from "fs/promises";
import process from "process";
import readline from "readline";
import zipper from "zip-local";
import { MANIFEST_CHROME, MANIFEST_FIREFOX } from "./extension-manifests.js";
const runCommand = (command, yes) =>
new Promise((resolve, reject) => {
exec(yes ? `echo "y" | ${command}` : command, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve({ stdout, stderr });
}
});
});
const bundle = async (manifest, bundleDirectory) => {
try {
// Remove old bundle directory
await rm(bundleDirectory, { recursive: true, force: true }); // requires node 14+
console.log(`🧹 Cleaned up \`${bundleDirectory}\` directory.`);
// Run both build scripts
const runBuildScript = (directory) => {
return new Promise(async (resolve, reject) => {
let intervalId;
let spinner = "\\";
const startBuilding = () => {
let P = ["\\", "|", "/", "-"];
intervalId = setInterval(() => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
spinner = P[P.indexOf(spinner) + 1] || P[0];
process.stdout.write(
`${spinner} Building popup and content scripts...`
);
}, 250);
};
startBuilding();
try {
await runCommand(`pnpm --dir ./${directory} build`);
clearInterval(intervalId);
resolve();
} catch (error) {
clearInterval(intervalId);
console.error(
`Error running build script for ${directory}: ${error}`
);
reject(error);
}
});
};
await runBuildScript("popup");
await runBuildScript("content-scripts");
process.stdout.clearLine();
process.stdout.cursorTo(0);
console.log("🔥 Built popup and content scripts.");
// Bundle popup Next.js export
await copy("popup/out", `${bundleDirectory}`);
console.log(`🚗 Moved export to bundle.`);
// Bundle content-scripts
await copy("content-scripts/dist", `${bundleDirectory}/dist`);
console.log(`🚗 Moved content_scripts to bundle.`);
// Bundle background.js
await copyFile("background.js", `${bundleDirectory}/background.js`);
console.log(`🚗 Moved background.js to bundle.`);
// Bundle css
await copy("css", `${bundleDirectory}/css`);
console.log(`🚗 Moved css to bundle.`);
// Bundle fonts
await copy("fonts", `${bundleDirectory}/fonts`);
console.log(`🚗 Moved fonts to bundle.`);
// Bundle images
await copy("images", `${bundleDirectory}/images`);
console.log(`🚗 Moved images to bundle.`);
// Create manifest
await writeFile(
`${bundleDirectory}/manifest.json`,
Buffer.from(JSON.stringify(manifest, null, 2)),
"utf8"
);
// Done.
console.log(`📦 Bundled \`${bundleDirectory}\`.`);
// Zip the directory
zipper.sync
.zip(`./${bundleDirectory}`)
.compress()
.save(`./bundle/${bundleDirectory.replace("bundle/", "")}.zip`);
console.log(
`🧬 Zipped \`${bundleDirectory}\` to \`bundle/${bundleDirectory.replace(
"bundle/",
""
)}.zip\`.`
);
} catch (error) {
console.error(error);
}
};
const bundleAll = async () => {
await bundle(MANIFEST_CHROME, "bundle/chrome");
await bundle(MANIFEST_FIREFOX, "bundle/firefox");
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"Which browser would you like to bundle for? [All / Chrome / Firefox / Safari] ",
async (browser) => {
switch (browser) {
case "Chrome":
await bundle(MANIFEST_CHROME, "bundle/chrome");
break;
case "Firefox":
await bundle(MANIFEST_FIREFOX, "bundle/firefox");
break;
case "Safari":
await bundle(MANIFEST_FIREFOX, "bundle/firefox");
let intervalId;
let spinner = "\\";
const startBuilding = () => {
let P = ["\\", "|", "/", "-"];
intervalId = setInterval(() => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
spinner = P[P.indexOf(spinner) + 1] || P[0];
process.stdout.write(`${spinner} Bundling Safari...`);
}, 250);
};
startBuilding();
await runCommand(generateSafariProjectCommand, true);
await runCommand(fixBundleIdentifierCommand, true);
clearInterval(intervalId);
break;
case "All":
await bundleAll();
break;
default:
await bundleAll();
}
rl.close();
}
);
rl.on("close", () => {
process.exit(0);
});
const generateSafariProjectCommand = `xcrun safari-web-extension-converter bundle/firefox --project-location bundle/safari --app-name 'Minimal Twitter' --bundle-identifier 'com.typefully.minimal-twitter'`;
// The first command currently ignores the full --bundle-identifier flag (it still take the company name), so a replace is required to make sure it matches our bundle identifier
const fixBundleIdentifierCommand = `find "bundle/safari/Minimal Twitter" \\( -name "*.swift" -or -name "*.pbxproj" \\) -type f -exec sed -i '' 's/com.typefully.Minimal-Twitter/com.typefully.minimal-twitter/g' {} +`;
/*--- Bundle without prompting
await bundleAll();
process.exit(0);
---*/