Skip to content

Instantly share code, notes, and snippets.

@btaillon-coveo
Created March 13, 2023 18:26
Show Gist options
  • Save btaillon-coveo/99fb9dd59f8c65381f4eaa868d70096e to your computer and use it in GitHub Desktop.
Save btaillon-coveo/99fb9dd59f8c65381f4eaa868d70096e to your computer and use it in GitHub Desktop.
assets copier
/**
* @typedef Assets
* @property {string} [baseSrcPath]
* @property {string | string[]} globs
*/
/**
* @typedef Package
* @property {string} packageName
* @property {Assets | Assets[]} assets
*/
/** @type {Package[]} */
const packages = [
{
packageName: "react",
assets: { baseSrcPath: "umd", globs: "react.production.min.js" },
},
{
packageName: "react-dom",
assets: {
baseSrcPath: "umd",
globs: [
"react-dom.production.min.js",
"react-dom-server.browser.production.min.js",
],
},
},
{
packageName: "@coveo/atomic",
assets: { baseSrcPath: "dist/atomic", globs: "**" },
},
{
packageName: "@coveo/atomic-react",
assets: { baseSrcPath: "dist/iife", globs: "atomic-react.min.js" },
},
];
/**
* @template T
* @param {T | T[]} value
* @returns {T[]}
*/
function toArray(value) {
return Array.isArray(value) ? value : [value];
}
function main() {
for (const { packageName, assets } of packages) {
const packagePath = dirname(resolve.sync(`${packageName}/package.json`));
for (const { baseSrcPath, globs } of toArray(assets)) {
const fullBasePath = resolvePath(packagePath, baseSrcPath);
const sourcePaths = toArray(globs)
.flatMap((glob) => fastGlob.sync(glob, { cwd: fullBasePath }))
.map((relativePath) => resolvePath(fullBasePath, relativePath));
if (!sourcePaths.length) {
throw `No assets found for:\n${toArray(globs)
.map((glob) => `- ${fullBasePath}/${glob}`)
.join("\n")}`;
}
console.log(
`Copying ${sourcePaths.length} files from ${packageName} (${baseSrcPath}).`
);
for (const sourcePath of sourcePaths) {
const output = resolvePath(
"www",
"deps",
...packageName.split("/"),
relative(fullBasePath, sourcePath)
);
mkdirSync(dirname(output), { recursive: true });
copyFileSync(sourcePath, output);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment