-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateComponent.js
More file actions
70 lines (50 loc) · 1.36 KB
/
generateComponent.js
File metadata and controls
70 lines (50 loc) · 1.36 KB
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
65
66
67
68
69
70
const fs = require('fs');
const name = process.argv[2];
const nameCamelCase = name.replace(/^[A-Z]/, l => l.toLowerCase());
const nameSnakeCase = name.replace(/[A-Z]/g, l => `-${l.toLowerCase()}`).replace(/^-/, '');
if (!name) {
console.error('Missing component name as first and only command argument');
process.exit(1);
} else if (name[0].charCodeAt() < 65 || name[0].charCodeAt() > 90) {
console.error('Component name must be PascalCase and start with a letter');
process.exit(1);
}
const componentTemplate = `\
import React from 'react';
import {} from 'prop-types';
import './style.scss';
const ${name} = () => (
<div
className="${nameSnakeCase}"
>
</div>
);
${name}.propTypes = {};
export default ${name};
`;
const indexTemplate = `\
import ${name} from './${nameCamelCase}';
export default ${name};
`;
const styleTemplate = `\
.${nameSnakeCase} {
}
`;
fs.mkdir(`${__dirname}/app/components/${name}`, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
fs.writeFileSync(
`${__dirname}/app/components/${name}/${nameCamelCase}.jsx`,
componentTemplate
);
fs.writeFileSync(
`${__dirname}/app/components/${name}/index.js`,
indexTemplate
);
fs.writeFileSync(
`${__dirname}/app/components/${name}/style.scss`,
styleTemplate
);
});