-
Notifications
You must be signed in to change notification settings - Fork 7
/
trot.js
executable file
·220 lines (180 loc) · 7.21 KB
/
trot.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env node
/* Command Line Parser */
const program = require('commander')
const fs = require('fs')
const readline = require('readline')
const reactComponentEs5 = require('./templates/reactComponentES5')
const reactComponentEs6 = require('./templates/reactComponentES6')
const reactComponentES6Stateless = require('./templates/reactComponentES6Stateless')
const cssTemplate = require('./templates/cssTemplate')
let template = ""
let result = ""
let cssResult = ""
let cssFilename = ""
let dir = './'
let outputName = ""
//let path =""
let nest = (parent, children, options) => {
console.log('Nest components for parent: %s and children: %s', parent, children);
if (options.folder) {
dir = dir + options.folder + "/"
console.log("Folder: ", options.folder)
}
let path = dir + parent + ".js"
//Find Component file in folder
fs.stat(path, function (err, stats) {
if (err) {
console.log("no such file or directory at ", path)
console.log("Please check path and component name")
return
}
//confirm it is a file
if (stats.isFile()) {
//fs.readFile(path,'utf8', (err, data) => {
// if (err) throw err;
// console.log(data.toString());
//})
//create a readstream interface to read line by line
let inputFile = fs.createReadStream(path)
const rl = readline.createInterface({
input: inputFile
});
let newComponent = ''
//read line by line to do the magic
//set flags for include/require each child
//set flags for rendering each child
addedImportOrRequire = false
renderedChild = false
//set Regex's to search for insertion points
reImport = /import/gi
reRequire = /require/gi
reDiv = /(<h1>)/gi
rl.on('line', (input) => {
//check if include has been set for child component
//if it has skip these input lines
//prevents multiple insertions of include files
if (!addedImportOrRequire) {
//assumes first line is always require or import React
newComponent += input + '\n'
addedImportOrRequire = true
if (input.match(reImport)) {
children.map((child) => {
newComponent += "import " + child + " from \'" + child + "\'\n"
})
} else if (input.match(reRequire)) {
children.map((child) => {
newComponent += "var " + child + " = require(\'" + child + "\') " + "\n"
})
}
} else
//search for component's render -> div and render Children
if (input.match(reDiv)) {
newComponent += input + '\n'
children.map((child) => {
newComponent += ' <' + child + ' />\n'
})
} else {
console.log(input)
newComponent += input + '\n'
//console.log(`Received: ${input}`)
}
})
rl.on('close', function () {
fs.writeFile(path, newComponent, 'utf8', function (err) {
if (err) return console.log(err)
console.log("New file created: ", outputName)
});
console.log("New Component:\n", newComponent)
})
}
})
}
program
.command('nest <parent> <children...>')
.description('nests one or more child components into parent component')
.option("-f, --folder [folder]", "containing folder for all files")
.action(nest);
//console.log(reactComponentEs5, typeof(reactComponentEs5))
/* Create Options list from command lines */
program
// .version('0.0.1')
.command('comp')
.description('create components')
.option('-c, --componentName [name]', 'Create Component Named [Layout]', 'layout')
.option('-v, --JSVersion [version]', 'EMCAScript version [6]', '6')
.option('-f, --folder [folder]', 'Folder [./]', './')
.option('-s, --cssFile [cssFile]', 'Create CSS [No]', 'No')
.option('--stateless', 'Stateless component')
.action(function (componentName, options) {
console.log("Composing components...")
//if component flag set (also should be default)
if (componentName) {
console.log('Creating React component: ')
console.log(' - Component Name: %s ', this.componentName)
console.log(' - ECMAScript Version: %s ', this.JSVersion)
console.log(' - Folder: %s ', this.folder)
console.log(' - CSS file created: %s ', this.cssFile)
if (this.JSVersion === '6') console.log(' - Stateless component: %s ', this.stateless ? "Yes" : "No")
// Handle Version Flag "-v"
//select EMCA version 6 as default or version 5 if flagged
if (this.JSVersion === '6') {
// Handle Stateless Flag "--stateless"
//select ES6 Class as default or functional component if flagged
if (this.stateless) {
template = reactComponentES6Stateless
} else {
template = reactComponentEs6
}
} else {
template = reactComponentEs5
}
//Create component file from template, replacing holder text with componentName
//basic replacment regardless of CSS flag
result = template.replace(/\[comp\]/g, this.componentName)
//Handle CSS flag (-s)
//Import CSS, create CSS from Template and add className to Div of component file
if (this.cssFile === 'y' || this.cssFile === 'Y') {
result = result.replace(/\[import-css-file\]/g, ("import \'.\/" + this.componentName + ".css\'"))
result = result.replace(/\[create-css-class\]/g, ("className=\'component-" + this.componentName.toLowerCase() + "\'"))
cssResult = cssTemplate.replace(/\[comp\]/g, this.componentName.toLowerCase())
} else {
//remove CSS references from template
//result = template.replace(/\[comp\]/g, this.componentName)
result = result.replace(/\[import-css-file\]/g, '')
result = result.replace(/\[create-css-class\]/g, '')
}
//build strings for directory and file outputs
if (this.folder) {
dir += this.folder + '/'
cssFilename = dir + this.componentName + ".css"
}
//Check for -f directory and create if doesn't exist
if (!fs.existsSync(dir)) {
console.log("Directory does not exist: " + dir)
fs.mkdirSync(dir);
} else {
console.log("Directory exists: " + dir)
}
//create component file in proper directory
outputName = dir + this.componentName + '.js'
fs.writeFile(outputName, result, 'utf8', function (err) {
if (err) return console.log(err)
console.log("New file created: ", outputName)
});
//create CSS file in proper directory
if (this.cssFile === 'y' || this.cssFile === 'Y') {
fs.writeFile(cssFilename, cssResult, 'utf8', function (err) {
if (err) return console.log(err)
console.log("New CSS file created: ", cssFilename)
});
}
}
})
/* Command for nesting child components
let's see how it works.
*/
program.parse(process.argv);
//Helpful posts
//https://edgethreesixty.co.uk/blog/read-files-line-line-node-js-using-new-es6-syntax/
//http://stackoverflow.com/questions/29410404/readline-output-to-file-node-js
//http://stackoverflow.com/questions/41562038/accessing-data-parsed-by-readline-fs-in-node-js-outside-of-the-callback-functi