Skip to content

Instantly share code, notes, and snippets.

@disgusting-dev
Last active June 16, 2021 18:58
Show Gist options
  • Save disgusting-dev/4ceb0e488483aca340aa8a93a2720a05 to your computer and use it in GitHub Desktop.
Save disgusting-dev/4ceb0e488483aca340aa8a93a2720a05 to your computer and use it in GitHub Desktop.
// Usage: node createComponent button - Creates folder Button with set of files with extensions from json
const path = require('path');
const {
promises: {
writeFile,
},
stat,
mkdir,
} = require('fs');
const componentName = process.argv[2] || '';
if (!componentName) {
throw new Error('Please provide the component name');
}
const FOLDER_CONFIG = require('./folderConfig.json');
const STATIC_PATH = path.join(__dirname, '../src/components/');
const capitalizedName = componentName.charAt(0).toUpperCase() + componentName.slice(1);
const dir = `${STATIC_PATH}${capitalizedName}`;
stat(dir, generateDir)
function generateDir(error, status) {
if (status && status.isDirectory()) {
generateFile(error, status);
} else {
mkdir(dir, { recursive: true }, generateFile);
}
}
async function generateFile(error, result) {
if (result) {
FOLDER_CONFIG.files.forEach(async ({ fileName, extension }) => {
try {
await writeFile(`${dir}/${fileName || capitalizedName}.${extension}`, '');
} catch (e) {
console.log(e);
}
});
}
}
{
"files": [
{
"extension": "tsx"
},
{
"extension": "css"
},
{
"extension": "stories.tsx"
},
{
"fileName": "index",
"extension": "ts"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment