|
7 | 7 | * file that was distributed with this source code.
|
8 | 8 | */
|
9 | 9 |
|
| 10 | +import cpy from 'cpy' |
| 11 | +import { isNotJunk } from 'junk' |
| 12 | +import fastGlob from 'fast-glob' |
10 | 13 | import getRandomPort from 'get-port'
|
11 | 14 | import type tsStatic from 'typescript'
|
12 | 15 | import { fileURLToPath } from 'node:url'
|
13 | 16 | import { execaNode, execa } from 'execa'
|
| 17 | +import { isAbsolute, relative } from 'node:path' |
14 | 18 | import { EnvLoader, EnvParser } from '@adonisjs/env'
|
15 | 19 | import { ConfigParser, Watcher } from '@poppinss/chokidar-ts'
|
16 | 20 |
|
17 | 21 | import type { RunOptions, WatchOptions } from './types.js'
|
| 22 | +import debug from './debug.js' |
18 | 23 |
|
19 | 24 | /**
|
20 | 25 | * Default set of args to pass in order to run TypeScript
|
@@ -160,3 +165,45 @@ export async function getPort(cwd: URL): Promise<number> {
|
160 | 165 | */
|
161 | 166 | return getRandomPort({ port: 3333 })
|
162 | 167 | }
|
| 168 | + |
| 169 | +/** |
| 170 | + * Helper function to copy files from relative paths or glob |
| 171 | + * patterns |
| 172 | + */ |
| 173 | +export async function copyFiles(files: string[], cwd: string, outDir: string) { |
| 174 | + /** |
| 175 | + * Looping over files and create a new collection with paths |
| 176 | + * and glob patterns |
| 177 | + */ |
| 178 | + const { paths, patterns } = files.reduce<{ patterns: string[]; paths: string[] }>( |
| 179 | + (result, file) => { |
| 180 | + if (fastGlob.isDynamicPattern(file)) { |
| 181 | + result.patterns.push(file) |
| 182 | + } else { |
| 183 | + result.paths.push(file) |
| 184 | + } |
| 185 | + |
| 186 | + return result |
| 187 | + }, |
| 188 | + { patterns: [], paths: [] } |
| 189 | + ) |
| 190 | + |
| 191 | + debug('copyFiles inputs: %O, paths: %O, patterns: %O', files, paths, patterns) |
| 192 | + |
| 193 | + /** |
| 194 | + * Getting list of relative paths from glob patterns |
| 195 | + */ |
| 196 | + const filePaths = paths.concat(await fastGlob(patterns, { cwd })) |
| 197 | + |
| 198 | + /** |
| 199 | + * Computing relative destination. This is because, cpy is buggy when |
| 200 | + * outDir is an absolute path. |
| 201 | + */ |
| 202 | + const destination = isAbsolute(outDir) ? relative(cwd, outDir) : outDir |
| 203 | + debug('copying files %O to destination "%s"', filePaths, destination) |
| 204 | + |
| 205 | + return cpy(filePaths.filter(isNotJunk), destination, { |
| 206 | + cwd: cwd, |
| 207 | + flat: false, |
| 208 | + }) |
| 209 | +} |
0 commit comments