@stihl-design-system/components
Version:
Welcome to the STIHL Design System react component library.
99 lines (87 loc) • 3.29 kB
JavaScript
import fs from 'fs';
import path from 'path';
import {
NEXT_WITH_APP_ROUTER_MODE,
SUPPORTED_PRODUCTS,
} from './assets.const.js';
const copyFile = (source, destination) => {
try {
fs.copyFileSync(source, destination);
console.log(`Copied asset ${path.basename(source)} to ${destination}`);
} catch (error) {
console.error(`Error copying ${path.basename(source)}: ${error}`);
}
};
export const copyAssets = (copyDirectory, mode, product) => {
if (product !== 'default' && !SUPPORTED_PRODUCTS.includes(product)) {
throw new Error(
`[STIHL Design System] - [Meta Icons]: You passed the product name "${product}" as an argument, which is not supported. Supported product name arguments are: [${SUPPORTED_PRODUCTS.join(', ')}]. Please check our Meta Icons Partial documentation: https://main--63440bbb95889041542a5ba3.chromatic.com/?path=/docs/utilities-partials-meta-icons--documentation`
);
}
// Define the source and destination directories
const sourceDir = path.resolve(
'node_modules',
'@stihl-design-system',
'components',
'partials'
);
const destDir = path.resolve(
copyDirectory,
mode === NEXT_WITH_APP_ROUTER_MODE ? '' : 'public'
);
// Array of file names to copy
const filesToCopy = [
`${product}-apple-icon.png`,
`${product}-favicon.ico`,
`${product}-icon-192.png`,
`${product}-icon-512.png`,
`${product}-icon.svg`,
mode === NEXT_WITH_APP_ROUTER_MODE
? 'manifest.json'
: 'manifest.webmanifest',
];
// Create the destination directory if it doesn't exist
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true });
}
console.log('\x1b[36m%s\x1b[0m', 'Copying Meta Icon Assets...');
// Copy each file
filesToCopy.forEach((file) => {
const sourceFile = path.join(sourceDir, file);
let fileName = file;
// adjustments for next with app router
if (mode === NEXT_WITH_APP_ROUTER_MODE) {
if (file === `${product}-icon-192.png`) {
fileName = 'icon2.png';
} else if (file === `${product}-icon-512.png`) {
fileName = 'icon1.png';
} else if (file === 'manifest.json') {
fileName = 'manifest.webmanifest';
}
}
// remove product from final file name
const destFile = path.join(destDir, fileName.replace(`${product}-`, ''));
if (fs.existsSync(sourceFile)) {
copyFile(sourceFile, destFile);
console.log(`Copied ${file} to ${destDir}`);
} else {
throw Error(`Source file ${sourceFile} does not exist.`);
}
});
console.log('\x1b[32m%s\x1b[0m', 'SUCCESS - Copying Meta Icon Assets');
};
// Get the arguments
const args = process.argv;
// Extract directory argument
const dirArg = args.find((arg) => arg.startsWith('--dir='));
const copyDirectory = dirArg ? dirArg.replace('--dir=', '') : './';
// Extract product argument
const productArg = args.find((arg) => arg.startsWith('--product='));
const product = productArg ? productArg.replace('--product=', '') : 'default';
const modeArg = args.find((arg) => arg.startsWith('--mode='));
const mode = modeArg ? modeArg.replace('--mode=', '') : undefined;
try {
copyAssets(copyDirectory, mode, product);
} catch (error) {
console.error('ERROR - Copying Meta Icon Assets:', error);
}