What about scripts to generate code ? #126
yoannfleurydev
started this conversation in
Ideas
Replies: 2 comments 2 replies
-
So I did this script at the moment that is working as expected as a solution to that problem. import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';
const INDEX_PATH = resolve(
process.cwd(),
'./scripts/templates/new-component/index.tsx.txt'
);
const STORY_PATH = resolve(
process.cwd(),
'./scripts/templates/new-component/docs.stories.tsx.txt'
);
function readFileAndAdaptToComponentName(file, componentName) {
return readFile(file, 'utf8')
.then((content) => content.replace(/{{COMPONENT_NAME}}/g, componentName))
.catch((err) => console.error(err));
}
async function main() {
// Get inputs the component name from the command line arguments
const [, , componentName] = process.argv;
if (!componentName) {
console.log('Please provide a component name.');
process.exit(1);
}
const component = await readFileAndAdaptToComponentName(
INDEX_PATH,
componentName
);
const story = await readFileAndAdaptToComponentName(
STORY_PATH,
componentName
);
const componentFolder = resolve(
process.cwd(),
`./src/components/${componentName}`
);
mkdir(componentFolder, {
recursive: true,
});
writeFile(resolve(componentFolder, 'index.tsx'), component, 'utf8');
writeFile(resolve(componentFolder, 'docs.stories.tsx'), story, 'utf8');
}
main(); |
Beta Was this translation helpful? Give feedback.
2 replies
-
https://plopjs.com/ might be a good solution (just saw that package in zag.js repository) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The problem
I often find myself doing the same tasks over and over again:
docs.stories.tsx
for that componentThe solution
What about a script that can generate a simple boilerplate to create a new component ?
That will generate a file
src/components/MyComponent/index.tsx
And a file
src/components/MyComponent/docs.stories.tsx
Beta Was this translation helpful? Give feedback.
All reactions