Skip to content

Commit 2ce5e01

Browse files
committed
create project from prompts input and template folder
1 parent d5103fa commit 2ce5e01

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
index.ts

index.ts

+49-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import chalk from 'chalk'
2+
import fs from 'fs'
13
import minimist from 'minimist'
24
import prompts from 'prompts'
3-
import chalk from 'chalk'
45
;(async function () {
56
const isValidPackageName = (projectName: string) =>
67
/^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(
@@ -16,8 +17,8 @@ import chalk from 'chalk'
1617
.replace(/[^a-z0-9-~]+/g, '-')
1718

1819
let result: {
19-
projectName?: string
20-
packageName?: string
20+
displayName?: string
21+
namespace?: string
2122
needsVitest?: boolean
2223
needsEslint?: boolean
2324
needsPrettier?: boolean
@@ -32,7 +33,9 @@ import chalk from 'chalk'
3233
// - Add Vitest for testing?
3334
// - Add ESLint for code quality?
3435
// - Add Prettier for code formatting?
35-
// - TODO, select required permissions
36+
// - TODO, select required permissions?
37+
// - TODO, set manifest description
38+
// - TODO, set manifest categories
3639
result = await prompts(
3740
[
3841
{
@@ -87,8 +90,48 @@ import chalk from 'chalk'
8790
process.exit(1)
8891
}
8992

90-
console.log(chalk.green(JSON.stringify(result, null, 2)))
91-
// TODO - copy template files into target directory, replacing names as needed
93+
console.log(
94+
'creating new Managed Component project with parameters:',
95+
chalk.green(JSON.stringify(result, null, 2))
96+
)
97+
98+
const templateDir = './template'
99+
const files = fs.readdirSync(templateDir)
100+
if (!fs.existsSync(result.namespace as string)) {
101+
fs.mkdirSync(result.namespace as string)
102+
}
103+
for (const file of files) {
104+
const src = `${templateDir}/${file}`
105+
const dest = `${result.namespace}/${file}`
106+
if (fs.lstatSync(src).isDirectory()) {
107+
if (file === 'src') {
108+
fs.mkdirSync(dest)
109+
const srcFiles = fs.readdirSync(src)
110+
for (const file of srcFiles) {
111+
const srcFile = `${src}/${file}`
112+
const destFile = `${dest}/${file}`
113+
fs.copyFileSync(srcFile, destFile)
114+
}
115+
}
116+
continue
117+
} else {
118+
const data = fs.readFileSync(src, 'utf8')
119+
const replaced = data.replace(
120+
/{{ displayName }}/g,
121+
result.displayName as string
122+
)
123+
const replaced2 = replaced.replace(
124+
/{{ namespace }}/g,
125+
result.namespace as string
126+
)
127+
const replaced3 = replaced2.replace(
128+
/\[ \] find & replace /g,
129+
'[x] find & replace '
130+
)
131+
await fs.writeFileSync(dest, replaced3)
132+
}
133+
}
134+
console.log(chalk.green('✔') + ' Project created')
92135
})()
93136

94137
export {}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"create-mc": "index.js"
88
},
99
"scripts": {
10-
"test": "tsc --noEmit"
10+
"test": "tsc --noEmit",
11+
"build": "tsc"
1112
},
1213
"engines": {
1314
"node": "^18.0.0"

0 commit comments

Comments
 (0)