Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up gen script using promises #102

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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! 🎉`);