@@ -19,23 +19,25 @@ function isValidPackageName(projectName: string) {
19
19
20
20
function askConfiguration ( ) {
21
21
return prompts ( [
22
- {
23
- type : 'text' ,
24
- name : 'packageName' ,
25
- message : 'Package name' ,
26
- validate : isValidPackageName ,
27
- } ,
28
22
{
29
23
type : 'text' ,
30
24
name : 'name' ,
31
25
message : 'Language name' ,
32
26
validate : required ,
33
27
} ,
28
+ {
29
+ type : 'text' ,
30
+ name : 'packageName' ,
31
+ message : 'Package name' ,
32
+ validate : isValidPackageName ,
33
+ initial : ( _ , answers ) => `my-dynamic-lang-${ answers . name } ` ,
34
+ } ,
34
35
{
35
36
type : 'text' ,
36
37
name : 'treeSitterPackage' ,
37
38
message : 'Tree-sitter package to use' ,
38
39
validate : isValidPackageName ,
40
+ initial : ( _ , answers ) => `tree-sitter-${ answers . name } ` ,
39
41
} ,
40
42
{
41
43
type : 'list' ,
@@ -58,19 +60,23 @@ function askConfiguration() {
58
60
59
61
type Answers = Awaited < ReturnType < typeof askConfiguration > >
60
62
61
- function copyTemplate ( targetDir : string ) {
63
+ function copyTemplate ( targetDir : string , skipDotFiles = false ) {
62
64
const templateDir = path . join ( __dirname , 'template' )
63
- return fs . cp ( templateDir , targetDir , { recursive : true } )
64
- }
65
-
66
- function removeDotFiles ( ) {
65
+ return fs . cp ( templateDir , targetDir , {
66
+ recursive : true , // Copy all files and folders
67
+ // includes hidden files if `skipDotFiles` is false
68
+ filter : ( src ) => {
69
+ const basename = path . basename ( src )
70
+ return ! skipDotFiles || ! basename . startsWith ( '.' )
71
+ }
72
+ } )
67
73
}
68
74
69
75
async function renameFiles ( dir : string , answer : Answers ) {
70
76
const name : Record < string , string > = {
71
77
$$PACKAGE_NAME$$ : answer . packageName ,
72
78
$$NAME$$ : answer . name ,
73
- $$TREE_SITTER_PACKAGE$$ : answer . name ,
79
+ $$TREE_SITTER_PACKAGE$$ : answer . treeSitterPackage ,
74
80
$$EXTENSIONS$$ : JSON . stringify ( answer . extensions ) ,
75
81
$$EXPANDO_CHAR$$ : JSON . stringify ( answer . expandoChar ) ,
76
82
}
@@ -89,17 +95,21 @@ async function renameFiles(dir: string, answer: Answers) {
89
95
}
90
96
}
91
97
function installTreeSitterPackage ( answer : Answers ) {
92
- execSync ( `pnpm install ${ answer . treeSitterPackage } --save-dev` )
98
+ console . log ( 'Installing tree-sitter package...' )
99
+ execSync ( `pnpm install ${ answer . treeSitterPackage } --save-dev --save-exact` )
100
+ console . log ( 'Copying source code...' )
101
+ execSync ( 'pnpm run source' )
102
+ console . log ( 'Compiling' )
103
+ execSync ( 'pnpm run build' )
93
104
}
94
105
95
106
async function main ( ) {
96
107
const cwd = process . cwd ( )
97
108
const config = await askConfiguration ( )
98
- await copyTemplate ( cwd )
99
- if ( process . argv . slice ( 2 ) . includes ( '--skip-dot-files' ) ) {
100
- removeDotFiles ( )
101
- }
109
+ const skipDotFiles = process . argv . slice ( 2 ) . includes ( '--skip-dot-files' )
110
+ await copyTemplate ( cwd , skipDotFiles )
102
111
await renameFiles ( cwd , config )
112
+ installTreeSitterPackage ( config )
103
113
}
104
114
105
115
main ( )
0 commit comments