-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·126 lines (106 loc) · 3.15 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const fx = require('mkdir-recursive');
const path = require('path');
const prompt = require('prompt');
const args = process.argv.slice(2);
const suggestedName = args[0];
if (suggestedName == '' || suggestedName == null || suggestedName == undefined) {
console.log("ERROR: You must provide a component name as first argument.");
console.log("Example: rg relative/path/to/YourComponentName");
process.exit(0);
}
let configFilePath = path.join(__dirname, '.config');
let userDefaultPath = fs.existsSync(configFilePath) && fs.readFileSync(configFilePath, { encoding: 'utf-8' });
let dirPath = '';
let componentName = suggestedName;
let filePath = path.parse(suggestedName);
if (filePath.dir !== '') {
// User provided file path
dirPath = path.join(filePath.dir, filePath.name);
componentName = filePath.name;
prompt.start();
console.log(`\n* Do you want to save the path '${filePath.dir}' as your default directory?`)
prompt.message = `* Answer 'y' and press Enter to save default path. `;
prompt.delimiter = '';
prompt.get({
properties: {
defaultPath: {
description: 'y/n?'
}
}
}, (err, result) => {
console.log('* Saving default path: ' + filePath.dir);
fs.writeFileSync(configFilePath, filePath.dir, { encoding: 'utf-8' });
createComponent();
});
} else {
if (userDefaultPath) {
dirPath = path.join(userDefaultPath, suggestedName);
} else {
dirPath = suggestedName;
}
createComponent();
}
function createComponent() {
if (componentName[0].toUpperCase() !== componentName[0]) {
componentName = componentName[0].toUpperCase() + componentName.substring(1);
}
const indexFileContent = `import ${componentName} from './${componentName}'
export default ${componentName}
`
const compFileContent = `import React, { Component } from 'react'
import './${componentName}.css'
class ${componentName} extends Component {
render() {
return (
<div>
</div>
)
}
}
export default ${componentName}
`
const files = [
{
name: 'index.js',
content: indexFileContent,
},
{
name: componentName + '.js',
content: compFileContent,
},
{
name: componentName + '.scss',
content: ''
},
{
name: componentName + '.css',
content: ''
},
];
// Create folder
fx.mkdir(dirPath, function(err) {
if (err) throw err;
console.log('Created dir ' + dirPath)
let filesCreated = 0;
// Create files
for (let i in files) {
let file = files[i];
let filePath = path.join(dirPath, file.name);
if (fs.existsSync(filePath)) {
console.log(`- ERROR: The file ${filePath} already exists. Delete it and run the command again or choose another name for your component.`);
break;
}
fs.writeFile(filePath, file.content, (err) => {
if (err) throw err;
console.log(`Created ${filePath}`);
filesCreated++;
if (filesCreated == files.length) {
console.log(`React component ${componentName} successfully created.`)
}
})
}
});
}