Skip to content

Commit 2207c83

Browse files
committed
refactor: use latest version of cpy
1 parent c7f4753 commit 2207c83

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

src/bundler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
*/
99

1010
import slash from 'slash'
11-
import copyfiles from 'cpy'
1211
import fs from 'node:fs/promises'
1312
import type tsStatic from 'typescript'
1413
import { fileURLToPath } from 'node:url'
1514
import { join, relative } from 'node:path'
1615
import { cliui, type Logger } from '@poppinss/cliui'
1716

18-
import { run, parseConfig } from './helpers.js'
1917
import type { BundlerOptions } from './types.js'
18+
import { run, parseConfig, copyFiles } from './helpers.js'
2019

2120
/**
2221
* Instance of CLIUI
@@ -101,7 +100,7 @@ export class Bundler {
101100
*/
102101
async #copyFiles(files: string[], outDir: string) {
103102
try {
104-
await copyfiles(files, outDir, { parents: true, cwd: this.#cwdPath })
103+
await copyFiles(files, this.#cwdPath, outDir)
105104
} catch (error) {
106105
if (!error.message.includes("the file doesn't exist")) {
107106
throw error

src/debug.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { debuglog } from 'node:util'
11+
12+
export default debuglog('adonisjs:assembler')

src/helpers.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import cpy from 'cpy'
11+
import { isNotJunk } from 'junk'
12+
import fastGlob from 'fast-glob'
1013
import getRandomPort from 'get-port'
1114
import type tsStatic from 'typescript'
1215
import { fileURLToPath } from 'node:url'
1316
import { execaNode, execa } from 'execa'
17+
import { isAbsolute, relative } from 'node:path'
1418
import { EnvLoader, EnvParser } from '@adonisjs/env'
1519
import { ConfigParser, Watcher } from '@poppinss/chokidar-ts'
1620

1721
import type { RunOptions, WatchOptions } from './types.js'
22+
import debug from './debug.js'
1823

1924
/**
2025
* Default set of args to pass in order to run TypeScript
@@ -160,3 +165,45 @@ export async function getPort(cwd: URL): Promise<number> {
160165
*/
161166
return getRandomPort({ port: 3333 })
162167
}
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+
}

tests/watch.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import ts from 'typescript'
1111
import { test } from '@japa/runner'
1212
import { watch } from '../src/helpers.js'
1313

14-
test.group('Watcher', () => {
14+
test.group('Watcher', (group) => {
15+
group.tap((t) => t.disableTimeout())
16+
1517
test('watch files included by the tsconfig.json', async ({ fs, assert, cleanup }, done) => {
1618
assert.plan(1)
1719

0 commit comments

Comments
 (0)