Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use rollup's addWatchFile API to watch target sources #38

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 37 additions & 47 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,84 +57,74 @@ export default function copy(options = {}) {

return {
name: 'copy',
[hook]: async () => {
if (copyOnce && copied) {
return
}

const copyTargets = []

if (Array.isArray(targets) && targets.length) {
buildStart() {
if (Array.isArray(targets)) {
for (const target of targets) {
if (!isObject(target)) {
throw new Error(`${stringify(target)} target must be an object`)
throw new Error(`${stringify(target)} target must be an object`);
}

const { dest, rename, src, transform, ...restTargetOptions } = target
const {
src,
dest,
rename,
...restTargetOptions
} = target;

if (!src || !dest) {
throw new Error(`${stringify(target)} target must have "src" and "dest" properties`)
throw new Error(`${stringify(target)} target must have "src" and "dest" properties`);
}

if (rename && typeof rename !== 'string' && typeof rename !== 'function') {
throw new Error(`${stringify(target)} target's "rename" property must be a string or a function`)
throw new Error(`${stringify(target)} target's "rename" property must be a string or a function`);
}

const matchedPaths = await globby(src, {
const matchedPaths = globby.sync(src, {
expandDirectories: false,
onlyFiles: false,
...restPluginOptions,
...restTargetOptions
})
});

if (matchedPaths.length) {
for (const matchedPath of matchedPaths) {
const generatedCopyTargets = Array.isArray(dest)
? await Promise.all(dest.map((destination) => generateCopyTarget(
matchedPath,
destination,
{ flatten, rename, transform }
)))
: [await generateCopyTarget(matchedPath, dest, { flatten, rename, transform })]

copyTargets.push(...generatedCopyTargets)
}
matchedPaths.forEach(matchedPath => {
const generatedCopyTargets = Array.isArray(dest) ? dest.map(destination => generateCopyTarget(matchedPath, destination, rename)) : [generateCopyTarget(matchedPath, dest, rename)];
copyTargets.push(...generatedCopyTargets);
});
}
}
}

const targetSources = copyTargets.map(({ src }) => path.resolve(src));
targetSources.forEach(target => {
this.addWatchFile(target);
});
},

[hook]: async () => {
if (copyOnce && copied) {
return;
}

if (copyTargets.length) {
if (verbose) {
console.log(green('copied:'))
console.log(green('copied:'));
}

for (const copyTarget of copyTargets) {
const { contents, dest, src, transformed } = copyTarget

if (transformed) {
await fs.outputFile(dest, contents, restPluginOptions)
} else {
await fs.copy(src, dest, restPluginOptions)
}
for (const {
src,
dest
} of copyTargets) {
await fs.copy(src, dest, restPluginOptions);

if (verbose) {
let message = green(` ${bold(src)} → ${bold(dest)}`)
const flags = Object.entries(copyTarget)
.filter(([key, value]) => ['renamed', 'transformed'].includes(key) && value)
.map(([key]) => key.charAt(0).toUpperCase())

if (flags.length) {
message = `${message} ${yellow(`[${flags.join(', ')}]`)}`
}

console.log(message)
console.log(green(` ${bold(src)} → ${bold(dest)}`));
}
}
} else if (verbose) {
console.log(yellow('no items to copy'))
console.log(yellow('no items to copy'));
}

copied = true
copied = true;
}
}
}