-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
186 lines (160 loc) · 6.18 KB
/
index.ts
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
#! /usr/bin/env node
console.log("Loading scaffoldit...")
import fs from "fs"
import entry from "./templates/entry.js"
import component from "./templates/component.js"
import types from "./templates/types.js"
import { parseArgsForOptions } from "./utils/index.js"
import story from "./templates/story.js"
import tests from "./templates/tests.js"
import DEFAULT_CONFIG from "./scaffoldit.config.js"
import { conditionallyWriteFile } from "./utils/interactions.js"
import { init } from "./utils/template-fns.js"
import path from "path"
import { ScaffolditConfig } from "./scaffoldit.js"
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const defaultConfigFilePath = path.resolve(
__dirname,
"scaffoldit.config.js"
)
export const appConfigFilePath = path.resolve(
process.cwd(),
"scaffoldit.config.js"
)
export const scaffolditConfigFilePath = fs.existsSync(appConfigFilePath) ? appConfigFilePath : defaultConfigFilePath
console.log('scaffolditConfigFilePath: ', scaffolditConfigFilePath)
const scaffolditConfigFilePathURL = new URL(
`file://${scaffolditConfigFilePath}`
).href
const scaffolditConfig: ScaffolditConfig = fs.existsSync(
scaffolditConfigFilePath
)
? (await import(scaffolditConfigFilePathURL)).default
: DEFAULT_CONFIG
// Parse command-line arguments and scaffoldit configuration to determine options and component information
const nodeArgs = process.argv.slice(2)
const [destination, componentName] = nodeArgs
const { customTemplates: customTemplatesConfig, ...configObj } =
scaffolditConfig
const options = { ...configObj, ...parseArgsForOptions(nodeArgs) }
if (!destination) {
console.log(
'Please specify a destination for the component. e.g. "yarn run scaffoldit <destination> <componentName>"'
)
process.exit(9)
}
// Format the component destination and name, and determine whether TypeScript is being used
const formattedDestination = destination.replace(/\\/g, "/")
const formattedComponentName = componentName
? componentName[0].toUpperCase() + componentName.slice(1)
: ""
// Define the root path for the component and the file extensions to use for the component, based on whether TypeScript is being used
const rootPath = `${formattedDestination}/${formattedComponentName}`
const main = async () => {
// If the `init` option is set, remove it from the options object before callling the `init` function
if (options.init) {
delete options.init
await init(options, customTemplatesConfig)
}
// If the rootPath directory doesn't exist, create it
if (!fs.existsSync(rootPath)) {
fs.mkdirSync(rootPath, { recursive: true })
}
if (customTemplatesConfig.defaultTemplatesEnabled) {
// Write the main component file to the rootPath directory
const componentFilePath = `${rootPath}/${formattedComponentName}.${extensions.jsx}`
await conditionallyWriteFile(
componentFilePath,
component(formattedComponentName),
options.forceOverwrite
)
// If the `noEntry` option is not set, write the component entry file to the rootPath directory
if (!options.noEntry) {
const entryFilePath = `${rootPath}/index.${extensions.js}`
await conditionallyWriteFile(
entryFilePath,
entry(formattedComponentName),
options.forceOverwrite
)
}
// If the `noTypescript` option is not set, write the TypeScript types file to the rootPath directory
if (!options.noTypescript) {
const typesFilePath = `${rootPath}/${formattedComponentName}.types.ts`
await conditionallyWriteFile(
typesFilePath,
types(formattedComponentName),
options.forceOverwrite
)
}
// If the `noStories` option is not set, write the component story file to the rootPath directory
if (!options.noStories) {
const storyFilePath = `${rootPath}/${formattedComponentName}.stories.${extensions.jsx}`
await conditionallyWriteFile(
storyFilePath,
story(formattedComponentName, formattedDestination),
options.forceOverwrite
)
}
// If the `noTests` option is not set, write the component test file to the rootPath directory
if (!options.noTests) {
const testsFilePath = `${rootPath}/${formattedComponentName}.test.${extensions.jsx}`
await conditionallyWriteFile(
testsFilePath,
tests(formattedComponentName),
options.forceOverwrite
)
}
}
if (customTemplatesConfig.enabled) {
if (!fs.existsSync(customTemplatesConfig.path)) {
console.log(
"Custom templates directory does not exist. Please run `scaffoldit init` to reconfigure the directory path."
)
process.exit()
}
console.log(
"customTemplatesConfig.templates: ",
customTemplatesConfig.templates
)
const templateDirectoryPath = `${process.cwd()}/${
customTemplatesConfig.path
}`
// If the `customTemplates` option is enabled, import the custom templates
const importedCustomTemplatesResponse = await Promise.all(
customTemplatesConfig.templates.map((template) => {
const templateFileName = `${template[0]}.mjs`
const templateFilePath = path.resolve(
templateDirectoryPath,
templateFileName
)
const templateFilePathURL = new URL(`file://${templateFilePath}`).href
return import(templateFilePathURL)
})
)
// Write the custom templates to the rootPath directory
const responseFromWritingCustomTemplates = await Promise.all(
importedCustomTemplatesResponse.map((customTemplate, idx) => {
const templateFileExtension = customTemplatesConfig.templates[idx][1]
const templateFilePath = `${rootPath}/${formattedComponentName}.${templateFileExtension}`
return conditionallyWriteFile(
templateFilePath,
customTemplate.default(formattedComponentName),
options.forceOverwrite
)
})
)
console.log(
"responseFromWritingCustomTemplates: ",
responseFromWritingCustomTemplates
)
}
process.exit(0)
}
export const useTypescript = !options.noTypescript
export const extensions = {
jsx: useTypescript ? "tsx" : "jsx",
js: useTypescript ? "ts" : "js",
}
main()