-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathcopy.js
220 lines (191 loc) · 5.85 KB
/
copy.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* eslint no-console: off */
import fs from 'fs'
import path from 'path'
import { IS_DEV, ADDON_PATH, treeToList, watch, log, logOk, VUE_DIST, logErr } from './utils.js'
const COPY = {
'./src/manifest.json': {
path: `${ADDON_PATH}/`,
handler: handleManifest,
},
'./src/_locales/dict.browser.json': {
path: `${ADDON_PATH}/_locales/`,
handler: handleLocales,
},
'./src/assets/logo-native-dark.svg': `${ADDON_PATH}/assets/`,
'./src/assets/logo-native-light.svg': `${ADDON_PATH}/assets/`,
'./src/assets/logo-native.svg': `${ADDON_PATH}/assets/`,
'./src/assets/logo.svg': `${ADDON_PATH}/assets/`,
'./src/assets/group-page-favicon.svg': `${ADDON_PATH}/assets/`,
'./src/assets/snapshot-native.svg': `${ADDON_PATH}/assets/`,
'./src/assets/proxy-native.svg': `${ADDON_PATH}/assets/`,
[`./node_modules/vue/dist/${VUE_DIST}`]: `${ADDON_PATH}/vendor/`,
}
/**
* ...
*/
async function build() {
const entries = await parseEntries()
await copyAllEntries(entries)
}
/**
* ...
*/
async function copyAndWatch() {
const entries = await parseEntries()
await copyAllEntries(entries)
const tasks = entries
.filter(e => !e.srcIsDir)
.map(e => {
e.files = [e.src]
return e
})
watch(
tasks,
affectedTasks => changeHandler(affectedTasks),
(task, file) => {
log(`Copy: File ${file} was renamed, restart this script`)
tasks.forEach(t => t.watchers.forEach(w => w.close()))
}
)
}
/**
* ...
*/
async function changeHandler(changedFiles) {
for (const info of changedFiles) {
log(`Copy: Changed source: ${info.src}`)
await fs.promises.copyFile(info.src, info.dst)
}
}
/**
* ...
*/
async function parseEntries() {
const entriesInfo = []
for (const src of Object.keys(COPY)) {
const srcStats = await fs.promises.stat(src)
const info = { src, srcIsDir: srcStats.isDirectory() }
const dst = COPY[src]
let dstPath
if (typeof dst === 'string') dstPath = dst
else {
dstPath = dst.path
if (dst.handler) info.srcHandler = dst.handler
}
if (dstPath) {
info.dst = path.resolve(dstPath)
if (dstPath.endsWith('/')) {
info.destDir = info.dst
if (!info.srcIsDir) info.dst = path.join(info.dst, path.basename(src))
} else {
info.destDir = path.dirname(info.dst)
}
}
entriesInfo.push(info)
}
return entriesInfo
}
/**
* ...
*/
async function copyAllEntries(entries) {
for (const info of entries) {
await copyEntry(info)
}
}
/**
* ...
*/
async function copyEntry(info) {
await fs.promises.mkdir(info.destDir, { recursive: true })
const normSrc = path.normalize(info.src)
if (info.srcIsDir) {
for (const f of await treeToList(normSrc)) {
const destDir = path.normalize(f.dir.replace(normSrc, info.dst + path.sep))
if (f.file) {
const srcPath = path.join(f.dir, f.file)
const dstPath = path.join(destDir, f.file)
if (info.srcHandler) await info.srcHandler(srcPath, dstPath)
else await fs.promises.copyFile(srcPath, dstPath)
} else await fs.promises.mkdir(destDir, { recursive: true })
}
} else {
if (info.srcHandler) await info.srcHandler(info.src, info.dst)
else await fs.promises.copyFile(info.src, info.dst)
}
}
/**
* Main
*/
function main() {
log('Copy: Copying')
if (IS_DEV) {
copyAndWatch()
logOk('Copy: Watching')
} else {
build()
logOk('Copy: Done')
}
}
main()
async function handleManifest(srcPath, dstPath) {
const forChromium = process.argv.includes('--chromium')
// Parse and patch manifest for chromium-based browser
if (forChromium) {
const srcData = await fs.promises.readFile(srcPath, 'utf-8')
const data = JSON.parse(srcData)
// Remove unsupported keys
delete data.page_action
delete data.browser_specific_settings
// Reset commands
for (const key of Object.keys(data.commands)) {
const cmd = data.commands[key]
if (key === '_execute_sidebar_action') {
cmd.suggested_key.windows = cmd.suggested_key.default
} else {
delete cmd.suggested_key
}
}
// Clean up permissions
const contextualIdentitiesIndex = data.permissions.indexOf('contextualIdentities')
if (contextualIdentitiesIndex !== -1) data.permissions.splice(contextualIdentitiesIndex, 1)
const menusIndex = data.permissions.indexOf('menus')
if (menusIndex !== -1) data.permissions.splice(menusIndex, 1)
const menusOverrideContextIndex = data.permissions.indexOf('menus.overrideContext')
if (menusOverrideContextIndex !== -1) data.permissions.splice(menusOverrideContextIndex, 1)
const tabHideIndex = data.permissions.indexOf('tabHide')
if (tabHideIndex !== -1) data.permissions.splice(tabHideIndex, 1)
const proxyIndex = data.optional_permissions.indexOf('proxy')
if (proxyIndex !== -1) data.optional_permissions.splice(proxyIndex, 1)
data.permissions.push('proxy')
const dstData = JSON.stringify(data)
await fs.promises.writeFile(dstPath, dstData)
}
// Copy
else {
return fs.promises.copyFile(srcPath, dstPath)
}
}
async function handleLocales(srcPath, dstPath) {
const dirPath = path.dirname(dstPath)
const srcData = await fs.promises.readFile(srcPath, 'utf-8')
const jsonData = JSON.parse(srcData)
const langs = {}
for (const key of Object.keys(jsonData)) {
const dict = jsonData[key]
if (!dict || typeof dict !== 'object') {
logErr(`Copy: Locales: No dictionary for: ${key}`)
break
}
for (const lang of Object.keys(dict)) {
if (!langs[lang]) langs[lang] = {}
langs[lang][key] = { message: dict[lang] }
}
}
for (const lang of Object.keys(langs)) {
const dict = langs[lang]
const jsonStr = JSON.stringify(dict)
await fs.promises.mkdir(path.join(dirPath, lang), { recursive: true })
await fs.promises.writeFile(path.join(dirPath, lang, 'messages.json'), jsonStr)
}
}