Skip to content

Commit b7e7261

Browse files
feat: add pnpm create template
fix #9
1 parent edc25b3 commit b7e7261

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

packages/toml/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In a pnpm project, run:
88
pnpm install @ast-grep/lang-toml
99
pnpm install @ast-grep/napi
1010
# install the tree-sitter-cli if no prebuild is available
11-
pnpm install @tree-sitter/cli
11+
pnpm install @tree-sitter/cli --save-dev
1212
```
1313

1414
## Usage

scripts/create-lang/index.ts

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import prompts from 'prompts'
2+
import path from 'node:path'
3+
import fs from 'node:fs/promises'
4+
import { execSync } from 'node:child_process'
25

36
function required(s: string): string | true {
47
if (s.length === 0) {
@@ -7,8 +10,21 @@ function required(s: string): string | true {
710
return true
811
}
912

13+
function isValidPackageName(projectName: string) {
14+
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(
15+
projectName,
16+
) || 'Invalid package name'
17+
}
18+
19+
1020
function askConfiguration() {
1121
return prompts([
22+
{
23+
type: 'text',
24+
name: 'packageName',
25+
message: 'Package name',
26+
validate: isValidPackageName,
27+
},
1228
{
1329
type: 'text',
1430
name: 'name',
@@ -19,11 +35,11 @@ function askConfiguration() {
1935
type: 'text',
2036
name: 'treeSitterPackage',
2137
message: 'Tree-sitter package to use',
22-
validate: required,
38+
validate: isValidPackageName,
2339
},
2440
{
2541
type: 'list',
26-
name: 'extension',
42+
name: 'extensions',
2743
message: 'File extensions used by the language, comma separated',
2844
separator: ',',
2945
validate: required,
@@ -40,22 +56,50 @@ function askConfiguration() {
4056
])
4157
}
4258

43-
async function copyTemplate() {
59+
type Answers = Awaited<ReturnType<typeof askConfiguration>>
60+
61+
function copyTemplate(targetDir: string) {
62+
const templateDir = path.join(__dirname, 'template')
63+
return fs.cp(templateDir, targetDir, { recursive: true })
4464
}
4565

4666
function removeDotFiles() {
4767
}
48-
function renameFiles() {
49-
// Rename files
68+
69+
async function renameFiles(dir: string, answer: Answers) {
70+
const name: Record<string, string> = {
71+
$$PACKAGE_NAME$$: answer.packageName,
72+
$$NAME$$: answer.name,
73+
$$TREE_SITTER_PACKAGE$$: answer.name,
74+
$$EXTENSIONS$$: JSON.stringify(answer.extensions),
75+
$$EXPANDO_CHAR$$: JSON.stringify(answer.expandoChar),
76+
}
77+
for (const file of await fs.readdir(dir)) {
78+
const filePath = path.join(dir, file)
79+
const stats = await fs.stat(filePath)
80+
if (stats.isDirectory()) {
81+
renameFiles(filePath, answer)
82+
} else {
83+
const content = await fs.readFile(filePath, 'utf-8')
84+
const newContent = content.replace(/(\$\$[A-Z_]+\$\$)/g, (match) => {
85+
return name[match] || match
86+
})
87+
await fs.writeFile(filePath, newContent)
88+
}
89+
}
90+
}
91+
function installTreeSitterPackage(answer: Answers) {
92+
execSync(`pnpm install ${answer.treeSitterPackage} --save-dev`)
5093
}
5194

5295
async function main() {
96+
const cwd = process.cwd()
5397
const config = await askConfiguration()
54-
await copyTemplate()
98+
await copyTemplate(cwd)
5599
if (process.argv.slice(2).includes('--skip-dot-files')) {
56100
removeDotFiles()
57101
}
58-
renameFiles()
102+
await renameFiles(cwd, config)
59103
}
60104

61105
main()
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
# ast-grep napi language for $NAME$
1+
# ast-grep napi language for $$NAME$$
22

33
## Installation
44

55
In a pnpm project, run:
66

77
```bash
8-
pnpm install $PACKAGE_NAME$
8+
pnpm install $$PACKAGE_NAME$$
99
pnpm install @ast-grep/napi
1010
# install the tree-sitter-cli if no prebuild is available
11-
pnpm install @tree-sitter/cli
11+
pnpm install @tree-sitter/cli --save-dev
1212
```
1313

1414
## Usage
1515

1616
```js
17-
import $NAME$ from '$PACKAGE_NAME$'
17+
import $$NAME$$ from '$$PACKAGE_NAME$$'
1818
import { registerDynamicLanguage, parse } from '@ast-grep/napi'
1919

20-
registerDynamicLanguage({ $NAME$ })
20+
registerDynamicLanguage({ $$NAME$$ })
2121

22-
const sg = parse('$NAME$', `your code`)
22+
const sg = parse('$$NAME$$', `your code`)
2323
sg.root().kind()
2424
```

scripts/create-lang/template/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ const libPath = path.join(__dirname, 'parser.so')
44
module.exports = {
55
libraryPath: libPath,
66
extensions: $$EXTENSIONS$$,
7-
languageSymbol: $$LANGUAGE_SYMBOL$$,
8-
expandoChar: '_',
7+
languageSymbol: 'tree_sitter_$$NAME$$',
8+
expandoChar: '$$EXPANDO_CHAR$$',
99
}

scripts/create-lang/template/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@ast-grep/lang-$$NAME$$",
2+
"name": "$$PACKAGE_NAME$$",
33
"version": "0.0.1",
44
"description": "",
55
"main": "index.js",

0 commit comments

Comments
 (0)