ads-icons-react
Version:
ADS icons for React usage
47 lines (42 loc) • 1.79 kB
JavaScript
const fs = require("fs").promises;
const path = require("path");
const { rimraf } = require("rimraf");
/**
* Recreates a directory at the specified path. If the directory already exists,
* no action is taken. If the directory does not exist, it is created with all
* necessary parent directories.
*
* @async
* @param {string} componentPath - The path where the directory will be recreated.
* @returns {Promise<boolean>} A promise that resolves to true if the directory
* is successfully checked and possibly created.
* @throws {Error} Throws an error if there is an issue accessing or creating the directory.
*/
const recreateIconsDir = async (componentPath) => {
try {
await fs.access(componentPath);
// If the directory exists, no further action is needed
} catch (error) {
// If the directory does not exist, it will throw an error, then we create the directory
await fs.mkdir(componentPath, { recursive: true });
}
return true; // This return value is not strictly needed unless you use it for a condition
};
/**
* Cleans and recreates the icons directory within the "src/components" path
* relative to the current working directory. This function first attempts to
* remove the existing icons directory and then recreates it.
*
* @async
* @returns {Promise<void>} A promise that resolves when the icons directory has
* been successfully cleaned and recreated.
* @throws {Error} Throws an error if cleaning or recreating the directory fails.
*/
const cleanRecreateIconsDirs = async () => {
const componentPath = path.join(process.cwd(), "src/components");
// Clean the directory first
await rimraf(componentPath);
// Then recreate the directory
await recreateIconsDir(componentPath);
};
module.exports = cleanRecreateIconsDirs;