-
Notifications
You must be signed in to change notification settings - Fork 3
/
generator.js
131 lines (115 loc) · 4.06 KB
/
generator.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
#! /usr/bin/env node
const program = require('commander')
const fs = require('fs')
const es = require('event-stream')
const {
hasWhiteSpace,
findIndentCount,
} = require('./utils/string')
const { getFunctionalComponentCode } = require('./utils/react/component')
const { getComponentStyleCode } = require('./utils/react/styles')
const { createDirectory, createFile } = require('./utils/files')
const { initStore } = require('./utils/path')
const { error } = require('./utils/message')
const packageJson = require("./package.json");
program
.name('@chef/component')
.description('CLI to generate your component skeleton in few seconds')
.version(packageJson.version)
.option('-f, --definitionFile <value>', 'definition file')
.option('-s, --style <value>', 'component style type are css or sass or styled')
.option('-l, --location <value>', 'location separated by slashes')
.option('-e, --extension <value>', 'component types are jsx or js or tsx')
.parse()
const firstParam = program.args[0]
const { definitionFile, style, location = 'components', extension = 'js' } = program.opts()
const generateLocation = location.split('/')
const { basePath, setBasePath, getBasePath, removeLastPathItem, resetAllPaths } = initStore(generateLocation)
let lineNumber = 0;
const TYPE = 'react'
const SPACER = 'space'
const INDENT_SIZE = 2
const FILE_EXTENSION = extension
const DEFINITION_FILE = firstParam || definitionFile
if (!DEFINITION_FILE) {
error('Please provide a indented definition file.')
}
let currentIndentation = 0
const createComponentSet = (line) => {
if (line.indexOf('/') === -1) {
const componentName = line.trim()
const componentCode = getFunctionalComponentCode({
extension,
name: componentName,
style
})
const indexFileExtensionMap = {
'tsx': 'ts',
'jsx': 'js',
}
const componentIndexFileExtension = indexFileExtensionMap[FILE_EXTENSION] || FILE_EXTENSION
createFile({
pathToFile: `${getBasePath()}/${componentName}.${FILE_EXTENSION}`,
data: componentCode
})
createFile({
pathToFile: `${getBasePath()}/index.${componentIndexFileExtension}`,
data: `export { default } from './${componentName}'`
})
if (['css', 'sass', 'styled'].includes(style)) {
const styleExtensionMap = {
'styled': `${style}.js`,
'sass': 'styles.scss',
'css': `styles.css`
}
const STYLE_FILE_EXTENSION = styleExtensionMap[style]
const componentStyleCode = getComponentStyleCode({
style
})
createFile({
pathToFile: `${getBasePath()}/${componentName}.${STYLE_FILE_EXTENSION}`,
data: componentStyleCode
})
}
}
}
const fileStream = fs.createReadStream(DEFINITION_FILE)
.pipe(es.split())
.pipe(es.mapSync(function (line) {
if (!hasWhiteSpace(line)) {
resetAllPaths()
basePath.push(...generateLocation);
setBasePath(line)
createDirectory(getBasePath())
currentIndentation = 0;
} else if (hasWhiteSpace(line)) {
if (currentIndentation === findIndentCount(line)) {
basePath.splice(-1, 1, line.trim())
} else if (findIndentCount(line) > currentIndentation) {
setBasePath(line)
} else if (findIndentCount(line) < currentIndentation) {
const repeatTimes = currentIndentation / findIndentCount(line)
const repeatTimesArray = new Array(Math.ceil(repeatTimes)).fill(7)
repeatTimesArray.forEach(() => removeLastPathItem())
setBasePath(line)
}
currentIndentation = findIndentCount(line)
createDirectory(getBasePath())
createComponentSet(line)
}
// pause the readstream
fileStream.pause();
lineNumber += 1;
// process line here and call fileStream.resume() when rdy
// function below was for logging memory usage
// logMemoryUsage(lineNumber);
// resume the readstream, possibly from a callback
fileStream.resume();
})
.on('error', function (err) {
console.log('Error while reading file.', err);
})
.on('end', function () {
console.log('Happy Coding!')
})
);