Skip to content

Commit

Permalink
added unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Jun 17, 2024
1 parent 1e73ed2 commit 67dfd40
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ typings/
.next
tests/to
dist
tests/from/z
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"main": "./dist/index.cjs",
"exports": {
"import": "./src/index.js",
"require": "./dist/index.cjs"
"require": "./dist/index.cjs",
"types": "./types/index.d.ts"
},
"files": [
"src",
Expand Down
31 changes: 26 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,33 @@ export default function copyPlugin (options) {
const jobs = new Set()
const { paths, ignorePaths } = parseUserPaths(userPaths, absWorkingDir, defaultTo)

const onFile = (srcFile) => {
const fullPath = path.resolve(absWorkingDir, srcFile)
const pathOptions = paths.find(pathOptions => pathOptions.valid(srcFile, fullPath))
const getPaths = (srcFile) => {
const srcPath = path.resolve(absWorkingDir, srcFile)
const pathOptions = paths.find(pathOptions => pathOptions.valid(srcFile, srcPath))

if (!pathOptions) return
const destFile = path.resolve(pathOptions.to, fullPath.replace(`${pathOptions.parent}${path.sep}`, ''))
let subPath = srcPath.replace(pathOptions.parent, '')
if (subPath.startsWith(path.sep)) {
subPath = subPath.slice(1)
}
const destPath = path.resolve(pathOptions.to, subPath)
return { srcPath, destPath }
}

const onFile = (srcFile) => {
const { srcPath, destPath } = getPaths(srcFile)
const job = copy(srcPath, destPath)
jobs.add(job)
job.finally(() => {
jobs.delete(job)
})
}

const job = copy(fullPath, destFile)
const onUnlink = recursive => (srcFile) => {
const { destPath } = getPaths(srcFile)
const job = fs.rm(destPath, {
recursive
}).catch(() => {})
jobs.add(job)
job.finally(() => {
jobs.delete(job)
Expand All @@ -148,6 +167,8 @@ export default function copyPlugin (options) {

watcher.on('add', onFile)
watcher.on('change', onFile)
watcher.on('unlink', onUnlink(false))
watcher.on('unlinkDir', onUnlink(true))
await new Promise(resolve => watcher.once('ready', resolve))
await checkFinish()
})
Expand Down
43 changes: 39 additions & 4 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test('basic', async () => {
})

test('watch', async () => {
await del(['tests/to'])
await del(['tests/to', 'tests/from/z'])

const ctx = await esbuild.context({
entryPoints: ['from/index.js'],
Expand All @@ -57,7 +57,8 @@ test('watch', async () => {
{ from: 'from/a', to: 'a' },
{ from: 'from/b/*.txt', to: 'b' },
{ from: 'from/c/**', to: 'c' },
{ from: ['from/d/**', '!from/d/ignore.txt'], to: 'd' }
{ from: ['from/d/**', '!from/d/ignore.txt'], to: 'd' },
{ from: 'from/z/**', to: 'z' }
]
})
]
Expand All @@ -66,13 +67,47 @@ test('watch', async () => {
try {
await ctx.watch()

await new Promise(resolve => setTimeout(resolve, 2 * 1000))
await new Promise(resolve => setTimeout(resolve, 2_000))

let files = ['to/a/file-a0.txt', 'to/b/file-b0.txt', 'to/c/file-c0.txt', 'to/c/cc/file-cc0.txt', 'to/d/include.txt', 'to/z/test-0.txt', 'to/z/test-1.txt']

await fs.mkdir('./tests/from/z')

await Promise.all([
fs.writeFile('./tests/from/z/test-0.txt', 'content'),
fs.writeFile('./tests/from/z/test-1.txt', 'content')
])

await new Promise(resolve => setTimeout(resolve, 2_000))

for (const file of files) {
assert.ok(await fs.pathExists(join(__dirname, file)), `Exists: ${join(__dirname, file)}`)
}

// delete the z/test file

const files = ['to/a/file-a0.txt', 'to/b/file-b0.txt', 'to/c/file-c0.txt', 'to/c/cc/file-cc0.txt', 'to/d/include.txt']
await fs.unlink('./tests/from/z/test-0.txt')
await new Promise(resolve => setTimeout(resolve, 2_000))

files = files.filter(file => file !== 'to/z/test-0.txt')
for (const file of files) {
assert.ok(await fs.pathExists(join(__dirname, file)), `Exists: ${join(__dirname, file)}`)
}

assert.not.ok(await fs.pathExists(join(__dirname, 'to/z/test-0.txt')))

await fs.rm('./tests/from/z', {
recursive: true
})
await new Promise(resolve => setTimeout(resolve, 2_000))

files = files.filter(file => file !== 'to/z/test-1.txt')
for (const file of files) {
assert.ok(await fs.pathExists(join(__dirname, file)), `Exists: ${join(__dirname, file)}`)
}

assert.not.ok(await fs.pathExists(join(__dirname, 'to/z/test-1.txt')))
assert.not.ok(await fs.pathExists(join(__dirname, 'to/z')))
} finally {
await ctx.dispose()
}
Expand Down

0 comments on commit 67dfd40

Please sign in to comment.