|
| 1 | +import path from 'path'; |
| 2 | +import util from 'util'; |
| 3 | +import fs from 'fs-extra'; |
| 4 | +import isObject from 'is-plain-object'; |
| 5 | +import globby from 'globby'; |
| 6 | +import { green, bold, yellow } from 'colorette'; |
| 7 | + |
| 8 | +function stringify(value) { |
| 9 | + return util.inspect(value, { |
| 10 | + breakLength: Infinity |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +async function isFile(filePath) { |
| 15 | + const fileStats = await fs.stat(filePath); |
| 16 | + return fileStats.isFile(); |
| 17 | +} |
| 18 | + |
| 19 | +function renameTarget(target, rename, src) { |
| 20 | + const parsedPath = path.parse(target); |
| 21 | + return typeof rename === 'string' ? rename : rename(parsedPath.name, parsedPath.ext.replace('.', ''), src); |
| 22 | +} |
| 23 | + |
| 24 | +async function generateCopyTarget(src, dest, { |
| 25 | + flatten, |
| 26 | + rename, |
| 27 | + transform |
| 28 | +}) { |
| 29 | + if (transform && !(await isFile(src))) { |
| 30 | + throw new Error(`"transform" option works only on files: '${src}' must be a file`); |
| 31 | + } |
| 32 | + |
| 33 | + const { |
| 34 | + base, |
| 35 | + dir |
| 36 | + } = path.parse(src); |
| 37 | + const destinationFolder = flatten || !flatten && !dir ? dest : dir.replace(dir.split('/')[0], dest); |
| 38 | + return { |
| 39 | + src, |
| 40 | + dest: path.join(destinationFolder, rename ? renameTarget(base, rename, src) : base), |
| 41 | + ...(transform && { |
| 42 | + contents: await transform(await fs.readFile(src), base) |
| 43 | + }), |
| 44 | + renamed: rename, |
| 45 | + transformed: transform |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function copy(options = {}) { |
| 50 | + const { |
| 51 | + copyOnce = false, |
| 52 | + flatten = true, |
| 53 | + hook = 'buildEnd', |
| 54 | + targets = [], |
| 55 | + verbose = false, |
| 56 | + ...restPluginOptions |
| 57 | + } = options; |
| 58 | + let copied = false; |
| 59 | + return { |
| 60 | + name: 'copy', |
| 61 | + [hook]: async () => { |
| 62 | + if (copyOnce && copied) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + setTimeout(async () => { |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + const copyTargets = []; |
| 71 | + |
| 72 | + if (Array.isArray(targets) && targets.length) { |
| 73 | + for (const target of targets) { |
| 74 | + if (!isObject(target)) { |
| 75 | + throw new Error(`${stringify(target)} target must be an object`); |
| 76 | + } |
| 77 | + |
| 78 | + const { |
| 79 | + dest, |
| 80 | + rename, |
| 81 | + src, |
| 82 | + transform, |
| 83 | + ...restTargetOptions |
| 84 | + } = target; |
| 85 | + |
| 86 | + if (!src || !dest) { |
| 87 | + throw new Error(`${stringify(target)} target must have "src" and "dest" properties`); |
| 88 | + } |
| 89 | + |
| 90 | + if (rename && typeof rename !== 'string' && typeof rename !== 'function') { |
| 91 | + throw new Error(`${stringify(target)} target's "rename" property must be a string or a function`); |
| 92 | + } |
| 93 | + |
| 94 | + const matchedPaths = await globby(src, { |
| 95 | + expandDirectories: false, |
| 96 | + onlyFiles: false, |
| 97 | + ...restPluginOptions, |
| 98 | + ...restTargetOptions |
| 99 | + }); |
| 100 | + |
| 101 | + if (matchedPaths.length) { |
| 102 | + for (const matchedPath of matchedPaths) { |
| 103 | + const generatedCopyTargets = Array.isArray(dest) ? await Promise.all(dest.map(destination => generateCopyTarget(matchedPath, destination, { |
| 104 | + flatten, |
| 105 | + rename, |
| 106 | + transform |
| 107 | + }))) : [await generateCopyTarget(matchedPath, dest, { |
| 108 | + flatten, |
| 109 | + rename, |
| 110 | + transform |
| 111 | + })]; |
| 112 | + copyTargets.push(...generatedCopyTargets); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (copyTargets.length) { |
| 119 | + if (verbose) { |
| 120 | + console.log(green('copied:')); |
| 121 | + } |
| 122 | + |
| 123 | + for (const copyTarget of copyTargets) { |
| 124 | + const { |
| 125 | + contents, |
| 126 | + dest, |
| 127 | + src, |
| 128 | + transformed |
| 129 | + } = copyTarget; |
| 130 | + |
| 131 | + if (transformed) { |
| 132 | + await fs.outputFile(dest, contents, restPluginOptions); |
| 133 | + } else { |
| 134 | + await fs.copy(src, dest, restPluginOptions); |
| 135 | + } |
| 136 | + |
| 137 | + if (verbose) { |
| 138 | + let message = green(` ${bold(src)} → ${bold(dest)}`); |
| 139 | + const flags = Object.entries(copyTarget).filter(([key, value]) => ['renamed', 'transformed'].includes(key) && value).map(([key]) => key.charAt(0).toUpperCase()); |
| 140 | + |
| 141 | + if (flags.length) { |
| 142 | + message = `${message} ${yellow(`[${flags.join(', ')}]`)}`; |
| 143 | + } |
| 144 | + |
| 145 | + console.log(message); |
| 146 | + } |
| 147 | + } |
| 148 | + } else if (verbose) { |
| 149 | + console.log(yellow('no items to copy')); |
| 150 | + } |
| 151 | + |
| 152 | + copied = true; |
| 153 | + }) |
| 154 | + } |
| 155 | + }; |
| 156 | +} |
| 157 | + |
| 158 | +export default copy; |
0 commit comments