-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkcomponent.js
41 lines (32 loc) · 1.09 KB
/
mkcomponent.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
const { existsSync, writeFileSync } = require('fs');
const path = require('path');
const mkdirp = require('./mkdirp');
const indexTemplate = componentName =>
`export { ${componentName} } from './${componentName}';
`;
const componentTemplate = componentName =>
`import React from 'react';
import { View, Text } from 'react-native';
export function ${componentName}(props) {
return (
<View>
<Text>hello, world</Text>
</View>
);
}
`;
module.exports = function mkcomponent({ baseDir, name, type }) {
const destDir = path.join(baseDir, name);
// Note that 'index' is excluded too
if (name[0].toLowerCase() === name[0]) {
const capitalized = name[0].toUpperCase() + name.slice(1);
return console.error(`Invalid component name, use ${capitalized} instead`);
}
if (existsSync(destDir)) {
return console.error(`"${name}" ${type} already exists`);
}
mkdirp(destDir);
writeFileSync(path.join(destDir, 'index.js'), indexTemplate(name));
writeFileSync(path.join(destDir, `${name}.js`), componentTemplate(name));
console.log(`"${name}" ${type} was created successfully`);
};