UNPKG

@roots/bud-api

Version:

bud.js core module

37 lines (36 loc) 1.29 kB
import { dirname, isAbsolute } from 'node:path'; export const copyFile = async function copyFile(request, context, overrides = {}) { const app = this; const makePatternObjectFromString = fromStringFactory(app, overrides); const makePatternObjectFromTuple = fromTupleFactory(app, overrides); if (!context) context = app.path(`@src`); if (!isAbsolute(context)) context = app.path(context); const result = typeof request === `string` ? makePatternObjectFromString(request, context) : makePatternObjectFromTuple(...request, context); app.extensions .get(`@roots/bud-extensions/copy-webpack-plugin`) .set(`patterns`, (patterns = []) => [...patterns, result]); app.api.logger.log(`bud.copyDir: asset pattern added`); return app; }; /** * Take an input string and return a {@link CopyPlugin.ObjectPattern} */ export const fromStringFactory = (app, overrides) => (from, context) => ({ context, from, to: app.relPath(dirname(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, to: app.relPath(to), ...overrides, });