forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy.js
More file actions
99 lines (90 loc) · 3.13 KB
/
Copy pathcopy.js
File metadata and controls
99 lines (90 loc) · 3.13 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
// @ts-check
import {getDestDir} from './paths.js';
import {PLATFORM} from './platform.js';
import * as reload from './reload.js';
import {createTask} from './task.js';
import {pathExists, copyFile, getPaths} from './utils.js';
/** @typedef {import('chokidar').FSWatcher} FSWatcher */
/** @typedef {import('./types').CopyEntry} CopyEntry */
/** @type {CopyEntry[]} */
const copyEntries = [
{
path: 'config/**/*.{config,drconf}',
reloadType: reload.FULL,
},
{
path: 'icons/**/*.*',
reloadType: reload.FULL,
},
{
path: 'ui/assets/**/*.*',
reloadType: reload.UI,
},
{
path: 'ui/popup/compatibility.js',
reloadType: reload.UI,
platforms: [PLATFORM.CHROMIUM_MV2],
},
{
path: 'plus/assets/**/*.*',
reloadType: reload.UI,
platforms: [PLATFORM.CHROMIUM_MV2_PLUS],
},
];
/**
* @param {string} srcDir
* @param {CopyEntry[]} copyEntries
* @returns {ReturnType<typeof createTask>}
*/
export function createCopyTask(srcDir, copyEntries) {
const paths = copyEntries.map((entry) => entry.path).map((path) => `${srcDir}/${path}`);
/** @type {(path: string) => string} */
const getCwdPath = (srcPath) => {
return srcPath.substring(srcDir.length + 1);
};
/** @type {(path: string, options: {debug: boolean; platform: any}) => Promise<void>} */
const copyEntry = async (path, {debug, platform}) => {
const cwdPath = getCwdPath(path);
const destDir = getDestDir({debug, platform});
const src = `${srcDir}/${cwdPath}`;
const dest = `${destDir}/${cwdPath}`;
await copyFile(src, dest);
};
const copyAll = async ({platforms, debug}) => {
const promises = [];
const enabledPlatforms = Object.values(PLATFORM).filter((platform) => platform !== PLATFORM.API && platforms[platform]);
for (const entry of copyEntries) {
if (entry.platforms && !entry.platforms.some((platform) => platforms[platform])) {
continue;
}
const files = await getPaths(`${srcDir}/${entry.path}`);
for (const file of files) {
for (const platform of enabledPlatforms) {
if (entry.platforms === undefined || entry.platforms.includes(platform)) {
promises.push(copyEntry(file, {debug, platform}));
}
}
}
}
await Promise.all(promises);
};
/** @type {(changedFiles: string[], watcher: FSWatcher, platforms: any) => Promise<void>} */
const onChange = async (changedFiles, _, platforms) => {
for (const file of changedFiles) {
if (await pathExists(file)) {
for (const platform of Object.values(PLATFORM).filter((platform) => platforms[platform])) {
await copyEntry(file, {debug: true, platform});
}
}
}
reload.reload({type: reload.FULL});
};
return createTask(
'copy',
copyAll,
).addWatcher(
paths,
onChange,
);
}
export default createCopyTask('src', copyEntries);