-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
146 lines (130 loc) · 3.67 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
import esMain from 'es-main';
import child_process from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import zipLib from 'zip-lib';
const textFileExtensions = ['.js', '.json', '.html'];
const binaryFileExtensions = ['.png'];
/**
* Package the sources into a user script and a Google Chrome extension.
*/
async function main() {
fs.rmSync('dist', { recursive: true, force: true });
const distFileName = `javadoc_search_frame_${version().replaceAll('.', '_')}`;
// Userscript
copyFile(
'src/userscript/javadoc_search_frame.js',
`dist/userscript/${distFileName}.user.js`,
{
append: [
`var messages = ${readFile('src/common/_locales/en/messages.json')}`,
readFile('src/userscript/lib/Messages.js'),
readFile('src/userscript/lib/Storage.js'),
readFile('src/common/lib/Option.js'),
readFile('src/common/lib/Frames.js'),
readFile('src/userscript/lib/OptionsPage.js'),
readFile('src/common/lib/HttpRequest.js'),
readFile('src/common/lib/OptionsPageGenerator.js'),
readFile('src/common/lib/common.js'),
],
}
);
console.log(`Built dist/userscript/${distFileName}.user.js`);
// Google Chrome extension
copyDir('src/googlechrome', 'dist/googlechrome');
copyFile(
'src/googlechrome/event-page.js',
'dist/googlechrome/event-page.js',
{
append: [readFile('src/common/lib/Frames.js')],
}
);
copyDir('src/common', 'dist/googlechrome');
zipLib.archiveFolder(
'dist/googlechrome',
`dist/googlechrome/${distFileName}.zip`
);
console.log(`Built dist/googlechrome/${distFileName}.zip`);
}
/**
* Get the version from Git.
*/
const version = (() => {
let cachedValue;
return () =>
(cachedValue =
cachedValue ||
child_process
.execFileSync('git', ['describe', '--dirty'], {
encoding: 'utf-8',
})
.trim());
})();
/**
* Read a file to a string.
*
* @param {string} filePath
*/
function readFile(filePath) {
return fs.readFileSync(filePath, { encoding: 'utf-8' });
}
/**
* Copy a single file, replacing #VERSION# placeholders in text files and
* optionally appending other files to the copy.
*
* @param {string} from
* @param {string} to
* @param {{ append?: string[] }} [options]
*/
function copyFile(from, to, options) {
fs.mkdirSync(path.dirname(to), { recursive: true });
const extension = path.extname(from);
if (binaryFileExtensions.includes(extension)) {
if (options?.append?.length) {
throw new Error('Cannot append to binary file: ' + from);
}
fs.copyFileSync(from, to);
return;
}
if (textFileExtensions.includes(extension)) {
let fileContents = readFile(from);
// Append to the end of the file.
if (options?.append?.length) {
fileContents += '\n' + options.append.join('\n');
}
// Replace #VERSION# placeholders.
fileContents = fileContents.replaceAll('#VERSION#', version());
fs.writeFileSync(to, fileContents);
return;
}
throw new Error(`Unrecognized file extension: ${from}`);
}
/**
* Copy a directory, replacing #VERSION# placeholders in text files.
*
* @param {string} from
* @param {string} to
*/
function copyDir(from, to) {
if (!fs.existsSync(to)) {
fs.mkdirSync(to);
}
const dirEntries = fs.readdirSync(from).map((entry) => ({
from: path.join(from, entry),
to: path.join(to, entry),
}));
for (const entry of dirEntries) {
if (fs.lstatSync(entry.from).isFile()) {
copyFile(entry.from, entry.to);
} else {
copyDir(entry.from, entry.to);
}
}
}
// @ts-ignore
if (esMain(import.meta)) {
main().catch((e) => {
console.error(e);
process.exitCode = 1;
});
}