(srcDir, copyEntries)
| 40 | * @returns {ReturnType<typeof createTask>} |
| 41 | */ |
| 42 | export function createCopyTask(srcDir, copyEntries) { |
| 43 | const paths = copyEntries.map((entry) => entry.path).map((path) => `${srcDir}/${path}`); |
| 44 | |
| 45 | /** @type {(path: string) => string} */ |
| 46 | const getCwdPath = (srcPath) => { |
| 47 | return srcPath.substring(srcDir.length + 1); |
| 48 | }; |
| 49 | |
| 50 | /** @type {(path: string, options: {debug: boolean; platform: any}) => Promise<void>} */ |
| 51 | const copyEntry = async (path, {debug, platform}) => { |
| 52 | const cwdPath = getCwdPath(path); |
| 53 | const destDir = getDestDir({debug, platform}); |
| 54 | const src = `${srcDir}/${cwdPath}`; |
| 55 | const dest = `${destDir}/${cwdPath}`; |
| 56 | await copyFile(src, dest); |
| 57 | }; |
| 58 | |
| 59 | const copyAll = async ({platforms, debug}) => { |
| 60 | const promises = []; |
| 61 | const enabledPlatforms = Object.values(PLATFORM).filter((platform) => platform !== PLATFORM.API && platforms[platform]); |
| 62 | for (const entry of copyEntries) { |
| 63 | if (entry.platforms && !entry.platforms.some((platform) => platforms[platform])) { |
| 64 | continue; |
| 65 | } |
| 66 | const files = await getPaths(`${srcDir}/${entry.path}`); |
| 67 | for (const file of files) { |
| 68 | for (const platform of enabledPlatforms) { |
| 69 | if (entry.platforms === undefined || entry.platforms.includes(platform)) { |
| 70 | promises.push(copyEntry(file, {debug, platform})); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | await Promise.all(promises); |
| 76 | }; |
| 77 | |
| 78 | /** @type {(changedFiles: string[], watcher: FSWatcher, platforms: any) => Promise<void>} */ |
| 79 | const onChange = async (changedFiles, _, platforms) => { |
| 80 | for (const file of changedFiles) { |
| 81 | if (await pathExists(file)) { |
| 82 | for (const platform of Object.values(PLATFORM).filter((platform) => platforms[platform])) { |
| 83 | await copyEntry(file, {debug: true, platform}); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | reload.reload({type: reload.FULL}); |
| 88 | }; |
| 89 | |
| 90 | return createTask( |
| 91 | 'copy', |
| 92 | copyAll, |
| 93 | ).addWatcher( |
| 94 | paths, |
| 95 | onChange, |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | export default createCopyTask('src', copyEntries); |
no test coverage detected