1
+ import chalk from 'chalk'
2
+ import fs from 'fs'
1
3
import minimist from 'minimist'
2
4
import prompts from 'prompts'
3
- import chalk from 'chalk'
4
5
; ( async function ( ) {
5
6
const isValidPackageName = ( projectName : string ) =>
6
7
/ ^ (?: @ [ a - z 0 - 9 - * ~ ] [ a -z 0 -9 -* ._ ~ ] * \/ ) ? [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * $ / . test (
@@ -16,8 +17,8 @@ import chalk from 'chalk'
16
17
. replace ( / [ ^ a - z 0 - 9 - ~ ] + / g, '-' )
17
18
18
19
let result : {
19
- projectName ?: string
20
- packageName ?: string
20
+ displayName ?: string
21
+ namespace ?: string
21
22
needsVitest ?: boolean
22
23
needsEslint ?: boolean
23
24
needsPrettier ?: boolean
@@ -32,7 +33,9 @@ import chalk from 'chalk'
32
33
// - Add Vitest for testing?
33
34
// - Add ESLint for code quality?
34
35
// - 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
36
39
result = await prompts (
37
40
[
38
41
{
@@ -87,8 +90,48 @@ import chalk from 'chalk'
87
90
process . exit ( 1 )
88
91
}
89
92
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
+ / { { d i s p l a y N a m e } } / g,
121
+ result . displayName as string
122
+ )
123
+ const replaced2 = replaced . replace (
124
+ / { { n a m e s p a c e } } / g,
125
+ result . namespace as string
126
+ )
127
+ const replaced3 = replaced2 . replace (
128
+ / \[ \] f i n d & r e p l a c e / g,
129
+ '[x] find & replace '
130
+ )
131
+ await fs . writeFileSync ( dest , replaced3 )
132
+ }
133
+ }
134
+ console . log ( chalk . green ( '✔' ) + ' Project created' )
92
135
} ) ( )
93
136
94
137
export { }
0 commit comments