UNPKG

@roots/bud-api

Version:

bud.js core module

49 lines (48 loc) 1.88 kB
import { isAbsolute } from 'node:path'; import { BudError } from '@roots/bud-support/errors'; import isString from '@roots/bud-support/isString'; export const copyDir = async function copyDir(request, context, overrides = {}) { const makePatternObjectFromString = fromStringFactory(this, overrides); const makePatternObjectFromTuple = fromTupleFactory(this, overrides); if (context && !isString(context)) { throw BudError.normalize(`bud.copyDir: Parameter 2 must be a string.`, { details: `The second parameter, context, must be a string. Received ${typeof context}.`, docs: new URL(`https://bud.js.org/reference/bud.copyDir`), thrownBy: import.meta.url, }); } if (!context) context = this.path(`@src`); if (!isAbsolute(context)) context = this.path(context); const result = isString(request) ? makePatternObjectFromString(request, context) : makePatternObjectFromTuple(...request, context); this.extensions .get(`@roots/bud-extensions/copy-webpack-plugin`) .setPatterns((patterns = []) => [...patterns, result]); this.api.logger.log(`bud.copyDir`, `asset pattern added`).info(result); return this; }; /** * Take an input string and return a {@link CopyPlugin.ObjectPattern} */ export const fromStringFactory = (app, overrides) => (from, context) => ({ context, from: app.relPath(from), globOptions: { dot: false }, noErrorOnMissing: true, to: app.relPath(from, `@file`), ...overrides, }); /** * Take an input [from, to] tuple and return a {@link CopyPlugin.ObjectPattern} */ export const fromTupleFactory = (app, overrides) => (from, to, context) => ({ context, from: app.relPath(from), globOptions: { dot: false }, noErrorOnMissing: true, to: app.relPath(to, `@file`), ...overrides, });