Skip to content

Commit

Permalink
version to v1.1.0
Browse files Browse the repository at this point in the history
- fix: add validation for argument of <componentName>
- feat: add option --withProps to generate component with props
- feat: add option --withImportReact to generate "Import React"
- update license to MIT
  • Loading branch information
snowden-fu committed May 27, 2024
1 parent 249dda9 commit 614e3c5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
9 changes: 4 additions & 5 deletions ComponentFileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class ComponentFileContent {
}
// generate the part of "import React from 'react'";
#generateImportReact() {
return this.isImportReact ? `import React from 'react';\n` : "";
return this.#hasImportReact ? `import React from 'react';\n` : "";
}
// generate the part of props, considering if it is a ts file and if it is with props
#generateProps() {
if (this.style === "ts" && this.isWithProps) {
if (this.style === "ts" && this.#hasProps) {
return `interface Props {}\n`;
}
return "";
Expand All @@ -60,11 +60,10 @@ class ComponentFileContent {
const importReactContent = this.#generateImportReact();
const propsObjectContent = this.#generateProps();
let propsParamContent = "";
if (this.isWithProps) {
if (this.#hasProps) {
propsParamContent = this.style === "ts" ? "{}:Props" : "props";
}
this.#content = `
${importReactContent}
this.#content = `${importReactContent}
${propsObjectContent}
function ${this.componentName}(${propsParamContent}) {
return (
Expand Down
61 changes: 44 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,82 @@
#!/usr/bin/env node

const commander = require('commander');
const fs = require('fs');
const path = require('path');
const commander = require("commander");
const fs = require("fs");
const path = require("path");

const program = new commander.Command();

program
.name('create-new-react-component')
.usage('<componentName> [options]')
.version('1.0.0')
.name("create-new-react-component")
.usage("<componentName> [options]")
.version("1.1.0")
.description(
"Create a new React component with an optional CSS file. " +
"The component will be created in a new directory with the same name as the component."
"The component will be created in a new directory with the same name as the component."
)
.arguments('<componentName>', 'Name of the component to create')
.option('--withStyles', 'Create a CSS file for the component')
.option('--lang <lang>', 'Choose the file style (js or ts)', 'js')
.action(createComponent);
.arguments("[componentName]", "Name of the component to create")
.option("--withStyles", "Create a CSS file for the component")
.option("--lang <lang>", "Choose the file style (js or ts)", "js")
.option("--withProps", "Create a component with props")
.option(
"--withImportReact",
"Create a component with `import React from react`"
)
.action((componentName, options) => {
createComponent(componentName, options);
});

function createComponent(componentName, options) {
if (!componentName) {
console.error('Error: <componentName> is required.');
process.exit(1); // Exit the process with an error code
}
const componentDir = path.join(process.cwd(), componentName);
const indexFilePath = path.join(componentDir, `index.${options.lang}`);
const componentFilePath = path.join(componentDir, `${componentName}.${options.lang}x`);
const componentFilePath = path.join(
componentDir,
`${componentName}.${options.lang}x`
);
const stylesFilePath = path.join(componentDir, `${componentName}.css`);

const indexFileContent = `export { default } from './${componentName}';`;

const ComponentFileContent = require('./ComponentFileContent');
const componentFileContent = new ComponentFileContent(componentName, options.withStyles, options.lang);
const componentFileContentContent = componentFileContent.generateComponentContent();
const ComponentFileContent = require("./ComponentFileContent");
const componentFileContent = new ComponentFileContent(
componentName,
options.withStyles,
options.lang,
options.withProps,
options.withImportReact
);
const componentFileContentContent =
componentFileContent.generateComponentContent();

const stylesFileContent = `/* Add your component styles here */
.${componentName} {
}
`;

if (fs.existsSync(componentDir)) {
console.error(`Component ${componentName} already exists`);
return;
}

try {
fs.mkdirSync(componentDir);
fs.writeFileSync(indexFilePath, indexFileContent);
fs.writeFileSync(componentFilePath, componentFileContentContent);
if (options.withStyles) {
fs.writeFileSync(stylesFilePath, stylesFileContent);
}
console.log(`Component ${componentName} created successfully${options.withStyles ? ' with language' : ''} (${options.lang})`);
console.log(
`Component ${componentName} created successfully${
options.withStyles ? " with language" : ""
} (${options.lang})`
);
} catch (err) {
console.error(`Error creating component ${componentName}:`, err);
}
}

program.parse(process.argv);
program.parse(process.argv);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-new-react-component",
"version": "1.0.7",
"version": "1.1.0",
"description": "an utility to create a new react component with a single command",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,7 +30,7 @@
"tool",
"generator"
],
"license": "ISC",
"license": "MIT",
"devDependencies": {
"jest": "^29.7.0"
}
Expand Down

0 comments on commit 614e3c5

Please sign in to comment.