Skip to content

Commit 017e69e

Browse files
feat: improve cli workflow
1 parent b7e7261 commit 017e69e

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

scripts/create-lang/index.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@ function isValidPackageName(projectName: string) {
1919

2020
function askConfiguration() {
2121
return prompts([
22-
{
23-
type: 'text',
24-
name: 'packageName',
25-
message: 'Package name',
26-
validate: isValidPackageName,
27-
},
2822
{
2923
type: 'text',
3024
name: 'name',
3125
message: 'Language name',
3226
validate: required,
3327
},
28+
{
29+
type: 'text',
30+
name: 'packageName',
31+
message: 'Package name',
32+
validate: isValidPackageName,
33+
initial: (_, answers) => `my-dynamic-lang-${answers.name}`,
34+
},
3435
{
3536
type: 'text',
3637
name: 'treeSitterPackage',
3738
message: 'Tree-sitter package to use',
3839
validate: isValidPackageName,
40+
initial: (_, answers) => `tree-sitter-${answers.name}`,
3941
},
4042
{
4143
type: 'list',
@@ -58,19 +60,23 @@ function askConfiguration() {
5860

5961
type Answers = Awaited<ReturnType<typeof askConfiguration>>
6062

61-
function copyTemplate(targetDir: string) {
63+
function copyTemplate(targetDir: string, skipDotFiles = false) {
6264
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+
})
6773
}
6874

6975
async function renameFiles(dir: string, answer: Answers) {
7076
const name: Record<string, string> = {
7177
$$PACKAGE_NAME$$: answer.packageName,
7278
$$NAME$$: answer.name,
73-
$$TREE_SITTER_PACKAGE$$: answer.name,
79+
$$TREE_SITTER_PACKAGE$$: answer.treeSitterPackage,
7480
$$EXTENSIONS$$: JSON.stringify(answer.extensions),
7581
$$EXPANDO_CHAR$$: JSON.stringify(answer.expandoChar),
7682
}
@@ -89,17 +95,21 @@ async function renameFiles(dir: string, answer: Answers) {
8995
}
9096
}
9197
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')
93104
}
94105

95106
async function main() {
96107
const cwd = process.cwd()
97108
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)
102111
await renameFiles(cwd, config)
112+
installTreeSitterPackage(config)
103113
}
104114

105115
main()

0 commit comments

Comments
 (0)