-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
249dda9
commit 614e3c5
Showing
3 changed files
with
50 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters