This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
forked from fable-network/wardrobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateComponent.js
64 lines (48 loc) · 1.76 KB
/
generateComponent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* eslint no-console: 0 */
const fs = require('fs');
const { logInfo, logError, isComponentDirectory } = require('./scriptUtils');
const COMPONENTS_PATH = './src/components';
const currentComponents = fs
.readdirSync(COMPONENTS_PATH)
.map(name => name)
.filter(isComponentDirectory(COMPONENTS_PATH));
const componentName = process.argv[process.argv.indexOf('--name') + 1];
if (currentComponents.indexOf(componentName) > -1) {
logError(`A component with the name "${componentName}" already exists!`);
return;
}
const componentPath = `${COMPONENTS_PATH}/${componentName}`;
const stylesBoilterplate = "import styled from 'styled-components';";
const ComponentBoilerplate = `import React from 'react';
import PropTypes from 'prop-types';
${stylesBoilterplate}
const Wrapper = styled.span\`\`;
const ${componentName} = ({ name }) => (<Wrapper>Hello, I am {name}</Wrapper>);
${componentName}.defaultProps = {
name: '${componentName}',
};
${componentName}.propTypes = {
// Please remove unused prop types
name: PropTypes.string,
};
export default ${componentName};
`;
const ComponentDocumentationBoilerplate = `${componentName} component:
\`\`\`jsx
<${componentName} />
\`\`\`
`;
// Create new folder.
fs.mkdirSync(componentPath);
// Create JS file.
fs.writeFileSync(`${componentPath}/${componentName}.js`, ComponentBoilerplate);
// Create index file.
fs.writeFileSync(`${componentPath}/index.js`, `export default from './${componentName}';\n`);
// Create markdown file.
fs.writeFileSync(`${componentPath}/${componentName}.md`, ComponentDocumentationBoilerplate);
// Add export in src/components/index.js
fs.appendFileSync(
'./src/index.js',
`export ${componentName} from './components/${componentName}';\n`
);
logInfo(`Your component can be found in ${componentPath}`);