Skip to content

Commit

Permalink
refactor: clean up gen script using promises (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ojeytonwilliams authored Apr 26, 2024
1 parent e021cb2 commit e021ad0
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions utils/gen-component-script.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fs from "fs";
import path from "path";
import { promisify } from "util";

import { component, story, test, barrel, type } from "./gen-component-template";

const writeFile = promisify(fs.writeFile);

// Grab component name from terminal argument
const [name] = process.argv.slice(2);
if (!name) {
Expand Down Expand Up @@ -30,41 +34,19 @@ if (fs.existsSync(dir)) {
// Create the folder
fs.mkdirSync(dir);

const writeFileErrorHandler = (err: Error | null) => {
if (err) {
throw err;
}
};

// Create the component file - my-component.tsx
fs.writeFile(
`${dir}/${kebabCasedName}.tsx`,
component(name),
writeFileErrorHandler,
);
writeFile(`${dir}/${kebabCasedName}.tsx`, component(name));

// Create the type file - types.ts
fs.writeFile(`${dir}/types.ts`, type(name), writeFileErrorHandler);
writeFile(`${dir}/types.ts`, type(name));

// Create the test file - my-component.test.tsx
fs.writeFile(
`${dir}/${kebabCasedName}.test.tsx`,
test(name),
writeFileErrorHandler,
);
writeFile(`${dir}/${kebabCasedName}.test.tsx`, test(name));

// Create the Storybook file - my-component.stories.tsx
fs.writeFile(
`${dir}/${kebabCasedName}.stories.tsx`,
story(name),
writeFileErrorHandler,
);
writeFile(`${dir}/${kebabCasedName}.stories.tsx`, story(name));

// Create the barrel file - index.ts
fs.writeFile(
`${dir}/index.ts`,
barrel(name, kebabCasedName),
writeFileErrorHandler,
);
writeFile(`${dir}/index.ts`, barrel(name, kebabCasedName));

console.log(`The ${name} component has been created successfully! 🎉`);

0 comments on commit e021ad0

Please sign in to comment.