-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
188 lines (163 loc) · 5.57 KB
/
create.js
File metadata and controls
188 lines (163 loc) · 5.57 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
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
const fse = require('fs-extra')
const path = require('path')
const color = {
green: '\x1b[42m%s\x1b[0m',
red: '\x1b[41m%s\x1b[0m'
}
const root = path.join(process.env.PWD)
const moduleName = process.argv[2]
const fileList = (name) => {
const PascalCaseName = toPascalCase(name)
const freeSpace = / /g
const templates = {
// ˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉ
// Компонент
// ˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍ
'component': [
{
name: `${name}.tsx`,
path: '',
content: `
import React, { FC } from 'react'
import styled from '@emotion/styled'
interface ${PascalCaseName}Props {}
const _${PascalCaseName}: FC<${PascalCaseName}Props> = () => {
return (
<${PascalCaseName}>
</${PascalCaseName}>
)
}
const ${PascalCaseName} = styled.div\`\`
export { _${PascalCaseName} as ${PascalCaseName} }
`.replace(freeSpace, '').replace(/\n/, '')
},
{
name: `index.ts`,
path: '',
content: `
import { ${PascalCaseName} } from './${name}'
export { ${PascalCaseName} }
export default ${PascalCaseName}
`.replace(freeSpace, '').replace(/\n/, '')
}
],
// ˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉˉ
// Store
// ˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍˍ
'store': [
{
name: 'slice.ts',
path: '',
content: `
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { AppThunk } from '../index'
import { notification } from 'antd'
type ${PascalCaseName}State = {
loading: boolean
}
const initialState: ${PascalCaseName}State = {
loading: false
}
export const ${name}Slice = createSlice({
name: '${name}',
initialState,
reducers: {
changeLoader: (state, action: PayloadAction<boolean>) => {
state.loading = action.payload
}
}
})
export const { changeLoader } = ${name}Slice.actions
export const asyncAction = (): AppThunk => async (dispatch, getState) => {
dispatch(changeLoader(true))
try {
// await api.auth.logout()
} catch (err) {
notification.error({
message: 'Ошибка',
description: err.message
})
}
dispatch(changeLoader(false))
}
`.replace(freeSpace, '').replace(/\n/, '')
},
{
name: `index.ts`,
path: '',
content: `
import { ${name}Slice } from './slice'
import { use${PascalCaseName} } from './hook'
export {
use${PascalCaseName},
${name}Slice
}
`.replace(freeSpace, '').replace(/\n/, '')
},
{
name: `hook.ts`,
path: '',
content: `
import { shallowEqual } from "react-redux"
import { RootState, useAppDispatch, useAppSelector } from "~/store"
import { asyncAction } from "./slice"
const ${name}State = (state: RootState) => ({ ...state.${name} })
export function use${PascalCaseName}() {
const ${name} = useAppSelector(${name}State, shallowEqual)
const dispatch = useAppDispatch()
return {
...${name},
asyncAction() {
dispatch(asyncAction())
}
}
}
`.replace(freeSpace, '').replace(/\n/, '')
}
]
}
return templates[moduleName]
}
const arg = process.argv.filter(item => item !== process.argv[0] && item !== process.argv[1] && item !== process.argv[2])
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
// Проверяем что-бы не перезаписать
// ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱
arg.forEach(async originalPath => {
const filleName = originalPath.split('/').reverse()[0]
const dataInfo = {
name: filleName,
path: path.join(root, originalPath),
originalPath
}
try {
const exists = await fse.pathExists(dataInfo.path)
if (exists) throw 'exists'
create(dataInfo)
} catch (error) {
if (error === 'exists') {
console.log(color.red, `${dataInfo.name} - уже существует`)
}
}
})
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
// Cоздания модуля
// ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱ ̱
const create = (component) => {
fse.mkdirp(component.path)
fileList(component.name).forEach( async item => {
if (item.isFolder) {
fse.mkdirp(path.join(component.path, item.path, item.name))
} else {
try {
const fille = path.join(component.path, item.path, item.name)
await fse.outputFile(fille, item.content)
} catch (error) {
console.log(color.red, `При создании файла: ${item.name} - произошла неизвесная ошибка ((.`)
}
}
})
console.log(color.green, `${moduleName}: ${component.name} - успешно создан.`)
}
function toPascalCase (str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}