generated from johnfmorton/vite-module-builder-w-ghpages-npm-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject-setup.js
225 lines (199 loc) · 7.01 KB
/
project-setup.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
221
222
223
224
225
const readline = require('readline')
const fs = require('fs')
const url = require('url')
const path = require('path')
const colours = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
underscore: '\x1b[4m',
blink: '\x1b[5m',
reverse: '\x1b[7m',
hidden: '\x1b[8m',
fg: {
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
gray: '\x1b[90m',
crimson: '\x1b[38m', // Scarlet
},
bg: {
black: '\x1b[40m',
red: '\x1b[41m',
green: '\x1b[42m',
yellow: '\x1b[43m',
blue: '\x1b[44m',
magenta: '\x1b[45m',
cyan: '\x1b[46m',
white: '\x1b[47m',
gray: '\x1b[100m',
crimson: '\x1b[48m',
},
}
const REPLACE_ME = 'vite-module-builder-w-ghpages-npm-template'
const GIT_URL =
'https://github.com/johnfmorton/vite-module-builder-w-ghpages-npm-template'
// Array of file paths to search and replace
const files = [
'index.html',
'package.json',
'README.md',
'vite.config.js',
'vite.demo.config.js',
'demo-page-assets/demo.ts',
// 'lib/vite-module-builder-w-ghpages-npm-template.ts',
]
// Create a readline interface to prompt the user for input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
// Define a function to validate the replacement text
function validateReplacementText(replacementText) {
// Check if the replacement text is lowercase
if (replacementText !== replacementText.toLowerCase()) {
console.log(
colours.bg.red +
colours.fg.white +
'ERROR: Replacement text must be lowercase.'
)
return false
}
// Check if the replacement text contains any spaces
if (replacementText.includes(' ')) {
console.log(
colours.bg.red +
colours.fg.white +
'ERROR: Replacement text cannot contain spaces.'
)
return false
}
// Check if the replacement text includes at least one hyphen
if (!replacementText.includes('-')) {
console.log(
colours.bg.red +
colours.fg.white +
'ERROR: Replacement text must include at least one hyphen.'
)
return false
}
// Check if the replacement text starts with a letter
if (!/^[a-z]/.test(replacementText)) {
console.log(
colours.bg.red +
colours.fg.white +
'ERROR: Replacement text must start with a letter.'
)
return false
}
// If all validation checks pass, return true
return true
}
// Define a function to validate the Git repo URL
function validateGitRepoUrl(gitRepoUrl) {
// Parse the URL using Node.js' built-in `url` module
const parsedUrl = url.parse(gitRepoUrl)
// Check if the protocol is `http` or `https`
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
console.log(
colours.bg.red +
colours.fg.white +
'ERROR: Invalid Git repo URL: must use HTTP or HTTPS protocol.'
)
return false
}
// Check if the hostname is present
if (!parsedUrl.hostname) {
console.log(
colours.bg.red +
colours.fg.white +
'ERROR: Invalid Git repo URL: missing hostname.'
)
return false
}
// If all validation checks pass, return true
return true
}
// Prompt the user for the replacement text
rl.question(
'Enter your projet name: (lowercase, no spaces, at least one hyphen, may include numbers, must start with a letter): ',
(replacementText) => {
// Validate the replacement text
const valid1 = validateReplacementText(replacementText)
if (!valid1) {
// If the validation fails, close the readline interface and exit
rl.close()
process.exitCode = 1
return
}
// Prompt the user for the Git repo URL
rl.question('Enter your Git repo URL: ', (gitRepoUrl) => {
// Validate the Git repo URL
const valid2 = validateGitRepoUrl(gitRepoUrl)
if (!valid2) {
// If the validation fails, close the readline interface and exit
rl.close()
process.exitCode = 1
return
}
// Loop through each file in the files array
files.forEach((file) => {
// Read the contents of the file
const content = fs.readFileSync(file, 'utf8')
// create a regex to match the replacement text
const fileNameRegex = new RegExp(REPLACE_ME, 'g')
const gitRepoRegex = new RegExp(GIT_URL, 'g')
// Replace all instances of 'REPLACE_ME' with the user-supplied replacement text
const updatedContent = content.replace(
fileNameRegex,
replacementText
)
// Replace all instances of 'GIT_URL' with the user-supplied Git repo URL
const finalContent = updatedContent.replace(
gitRepoRegex,
gitRepoUrl
)
// Write the final content back to the file
fs.writeFileSync(file, finalContent, 'utf8')
// Log a message indicating the file was updated
console.log(
colours.bg.green,
colours.fg.white,
`Updated ${file} `
)
})
// Close the readline interface
rl.close()
const fileDirecotry = 'lib/'
const fileToRename = `${REPLACE_ME}.ts` // the file to rename
const extname = path.extname(fileToRename)
const oldFilePath = path.join(fileDirecotry, fileToRename) // create the old file path
const newFilePath = path.join(
fileDirecotry,
replacementText + extname
) // create the new file path with the new filename and same extension
fs.rename(oldFilePath, newFilePath, function (err) {
if (err) throw err
// console.log('File renamed successfully!')
rl.close()
})
const successMessage = `
****************************************************
* Setup complete. Happy coding! *
* To get started, run the following command: *
* *
* npm run dev *
* *
****************************************************
`
console.log('')
// Log a message indicating the setup is complete
console.log(colours.bg.blue + colours.fg.white + successMessage)
})
}
)