@ast-grep/create-lang 0.0.0-prerelease
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/index.d.ts +1 -0
- package/index.js +119 -0
- package/index.ts +126 -0
- package/package.json +28 -0
- package/template/.github/workflows/release.yml +22 -0
- package/template/README.md +24 -0
- package/template/index.d.ts +10 -0
- package/template/index.js +9 -0
- package/template/nursery.js +12 -0
- package/template/package.json +42 -0
- package/template/postinstall.js +4 -0
- package/tsconfig.json +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ast-grep
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const prompts_1 = __importDefault(require("prompts"));
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
9
|
+
const node_child_process_1 = require("node:child_process");
|
|
10
|
+
function required(s) {
|
|
11
|
+
if (s.length === 0) {
|
|
12
|
+
return 'This value is required';
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
// https://github.com/vitejs/vite/blob/76082e3d3033b09b02b6db64de6e36942593c753/packages/create-vite/src/index.ts#L557
|
|
17
|
+
function isValidPackageName(projectName) {
|
|
18
|
+
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(projectName) || 'Invalid package name';
|
|
19
|
+
}
|
|
20
|
+
function askConfiguration() {
|
|
21
|
+
return (0, prompts_1.default)([
|
|
22
|
+
{
|
|
23
|
+
type: 'text',
|
|
24
|
+
name: 'name',
|
|
25
|
+
message: 'Language name',
|
|
26
|
+
validate: required,
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
type: 'text',
|
|
30
|
+
name: 'packageName',
|
|
31
|
+
message: 'Package name',
|
|
32
|
+
validate: isValidPackageName,
|
|
33
|
+
initial: (_, answers) => `my-dynamic-lang-${answers.name}`,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
type: 'text',
|
|
37
|
+
name: 'treeSitterPackage',
|
|
38
|
+
message: 'Tree-sitter package to use',
|
|
39
|
+
validate: isValidPackageName,
|
|
40
|
+
initial: (_, answers) => `tree-sitter-${answers.name}`,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
type: 'list',
|
|
44
|
+
name: 'extensions',
|
|
45
|
+
message: 'File extensions used by the language, comma separated',
|
|
46
|
+
separator: ',',
|
|
47
|
+
validate: required,
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
type: 'text',
|
|
51
|
+
name: 'expandoChar',
|
|
52
|
+
message: 'Expando char used in pattern',
|
|
53
|
+
initial: '$',
|
|
54
|
+
validate: (value) => {
|
|
55
|
+
return value.length === 1 ? true : 'Expando char must be a single character';
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: 'confirm',
|
|
60
|
+
name: 'includeDotFiles',
|
|
61
|
+
message: 'Include gitignore and npm publish files?',
|
|
62
|
+
initial: true,
|
|
63
|
+
}
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
66
|
+
function copyTemplate(targetDir, includeDotFiles) {
|
|
67
|
+
const templateDir = node_path_1.default.join(__dirname, 'template');
|
|
68
|
+
return promises_1.default.cp(templateDir, targetDir, {
|
|
69
|
+
recursive: true, // Copy all files and folders
|
|
70
|
+
// includes hidden files if `includeDotFiles` is true
|
|
71
|
+
filter: (src) => {
|
|
72
|
+
const basename = node_path_1.default.basename(src);
|
|
73
|
+
return includeDotFiles || !basename.startsWith('.');
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async function renameFiles(dir, answer) {
|
|
78
|
+
const name = {
|
|
79
|
+
$$PACKAGE_NAME$$: answer.packageName,
|
|
80
|
+
$$NAME$$: answer.name,
|
|
81
|
+
$$TREE_SITTER_PACKAGE$$: answer.treeSitterPackage,
|
|
82
|
+
$$EXTENSIONS$$: JSON.stringify(answer.extensions),
|
|
83
|
+
$$EXPANDO_CHAR$$: answer.expandoChar,
|
|
84
|
+
};
|
|
85
|
+
for (const file of await promises_1.default.readdir(dir)) {
|
|
86
|
+
const filePath = node_path_1.default.join(dir, file);
|
|
87
|
+
const stats = await promises_1.default.stat(filePath);
|
|
88
|
+
if (stats.isDirectory()) {
|
|
89
|
+
renameFiles(filePath, answer);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
const content = await promises_1.default.readFile(filePath, 'utf-8');
|
|
93
|
+
const newContent = content.replace(/(\$\$[A-Z_]+\$\$)/g, (match) => {
|
|
94
|
+
return name[match] || match;
|
|
95
|
+
});
|
|
96
|
+
await promises_1.default.writeFile(filePath, newContent);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function installTreeSitterPackage(answer) {
|
|
101
|
+
console.log('Installing tree-sitter package...');
|
|
102
|
+
(0, node_child_process_1.execSync)(`pnpm install ${answer.treeSitterPackage} --save-dev --save-exact`);
|
|
103
|
+
console.log('Copying source code...');
|
|
104
|
+
(0, node_child_process_1.execSync)('pnpm run source');
|
|
105
|
+
console.log('Compiling');
|
|
106
|
+
(0, node_child_process_1.execSync)('pnpm run build');
|
|
107
|
+
}
|
|
108
|
+
async function main() {
|
|
109
|
+
let cwd = process.cwd();
|
|
110
|
+
if (process.argv.length > 2) {
|
|
111
|
+
const targetDir = process.argv[2];
|
|
112
|
+
cwd = node_path_1.default.join(cwd, targetDir);
|
|
113
|
+
}
|
|
114
|
+
const config = await askConfiguration();
|
|
115
|
+
await copyTemplate(cwd, config.includeDotFiles);
|
|
116
|
+
await renameFiles(cwd, config);
|
|
117
|
+
installTreeSitterPackage(config);
|
|
118
|
+
}
|
|
119
|
+
main();
|
package/index.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import prompts from 'prompts'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import fs from 'node:fs/promises'
|
|
4
|
+
import { execSync } from 'node:child_process'
|
|
5
|
+
|
|
6
|
+
function required(s: string): string | true {
|
|
7
|
+
if (s.length === 0) {
|
|
8
|
+
return 'This value is required'
|
|
9
|
+
}
|
|
10
|
+
return true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// https://github.com/vitejs/vite/blob/76082e3d3033b09b02b6db64de6e36942593c753/packages/create-vite/src/index.ts#L557
|
|
14
|
+
function isValidPackageName(projectName: string) {
|
|
15
|
+
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(
|
|
16
|
+
projectName,
|
|
17
|
+
) || 'Invalid package name'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
function askConfiguration() {
|
|
22
|
+
return prompts([
|
|
23
|
+
{
|
|
24
|
+
type: 'text',
|
|
25
|
+
name: 'name',
|
|
26
|
+
message: 'Language name',
|
|
27
|
+
validate: required,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: 'text',
|
|
31
|
+
name: 'packageName',
|
|
32
|
+
message: 'Package name',
|
|
33
|
+
validate: isValidPackageName,
|
|
34
|
+
initial: (_, answers) => `my-dynamic-lang-${answers.name}`,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
type: 'text',
|
|
38
|
+
name: 'treeSitterPackage',
|
|
39
|
+
message: 'Tree-sitter package to use',
|
|
40
|
+
validate: isValidPackageName,
|
|
41
|
+
initial: (_, answers) => `tree-sitter-${answers.name}`,
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
type: 'list',
|
|
45
|
+
name: 'extensions',
|
|
46
|
+
message: 'File extensions used by the language, comma separated',
|
|
47
|
+
separator: ',',
|
|
48
|
+
validate: required,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: 'text',
|
|
52
|
+
name: 'expandoChar',
|
|
53
|
+
message: 'Expando char used in pattern',
|
|
54
|
+
initial: '$',
|
|
55
|
+
validate: (value) => {
|
|
56
|
+
return value.length === 1 ? true : 'Expando char must be a single character'
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
type: 'confirm',
|
|
61
|
+
name: 'includeDotFiles',
|
|
62
|
+
message: 'Include gitignore and npm publish files?',
|
|
63
|
+
initial: true,
|
|
64
|
+
}
|
|
65
|
+
])
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Answers = Awaited<ReturnType<typeof askConfiguration>>
|
|
69
|
+
|
|
70
|
+
function copyTemplate(targetDir: string, includeDotFiles: boolean) {
|
|
71
|
+
const templateDir = path.join(__dirname, 'template')
|
|
72
|
+
return fs.cp(templateDir, targetDir, {
|
|
73
|
+
recursive: true, // Copy all files and folders
|
|
74
|
+
// includes hidden files if `includeDotFiles` is true
|
|
75
|
+
filter: (src) => {
|
|
76
|
+
const basename = path.basename(src)
|
|
77
|
+
return includeDotFiles || !basename.startsWith('.')
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function renameFiles(dir: string, answer: Answers) {
|
|
83
|
+
const name: Record<string, string> = {
|
|
84
|
+
$$PACKAGE_NAME$$: answer.packageName,
|
|
85
|
+
$$NAME$$: answer.name,
|
|
86
|
+
$$TREE_SITTER_PACKAGE$$: answer.treeSitterPackage,
|
|
87
|
+
$$EXTENSIONS$$: JSON.stringify(answer.extensions),
|
|
88
|
+
$$EXPANDO_CHAR$$: answer.expandoChar,
|
|
89
|
+
}
|
|
90
|
+
for (const file of await fs.readdir(dir)) {
|
|
91
|
+
const filePath = path.join(dir, file)
|
|
92
|
+
const stats = await fs.stat(filePath)
|
|
93
|
+
if (stats.isDirectory()) {
|
|
94
|
+
renameFiles(filePath, answer)
|
|
95
|
+
} else {
|
|
96
|
+
const content = await fs.readFile(filePath, 'utf-8')
|
|
97
|
+
const newContent = content.replace(/(\$\$[A-Z_]+\$\$)/g, (match) => {
|
|
98
|
+
return name[match] || match
|
|
99
|
+
})
|
|
100
|
+
await fs.writeFile(filePath, newContent)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function installTreeSitterPackage(answer: Answers) {
|
|
105
|
+
console.log('Installing tree-sitter package...')
|
|
106
|
+
execSync(`pnpm install ${answer.treeSitterPackage} --save-dev --save-exact`)
|
|
107
|
+
console.log('Copying source code...')
|
|
108
|
+
execSync('pnpm run source')
|
|
109
|
+
console.log('Compiling')
|
|
110
|
+
execSync('pnpm run build')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function main() {
|
|
114
|
+
let cwd = process.cwd()
|
|
115
|
+
if (process.argv.length > 2) {
|
|
116
|
+
const targetDir = process.argv[2]
|
|
117
|
+
cwd = path.join(cwd, targetDir)
|
|
118
|
+
}
|
|
119
|
+
const config = await askConfiguration()
|
|
120
|
+
await copyTemplate(cwd, config.includeDotFiles)
|
|
121
|
+
await renameFiles(cwd, config)
|
|
122
|
+
installTreeSitterPackage(config)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
main()
|
|
126
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ast-grep/create-lang",
|
|
3
|
+
"version": "0.0.0-prerelease",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"keywords": [],
|
|
8
|
+
"author": "",
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"bin": {
|
|
11
|
+
"create-lang": "index.js"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"prompts": "2.4.2"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public",
|
|
18
|
+
"registry": "https://registry.npmjs.org/"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "22.10.5",
|
|
22
|
+
"@types/prompts": "^2.4.9",
|
|
23
|
+
"typescript": "^5.7.3"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"compile-ts": "tsc"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
name: Publish package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
inputs:
|
|
6
|
+
directory:
|
|
7
|
+
description: The working directory of the package, useful for monorepos
|
|
8
|
+
default: .
|
|
9
|
+
type: string
|
|
10
|
+
dry-run:
|
|
11
|
+
description: Dry run (no publish)
|
|
12
|
+
type: boolean
|
|
13
|
+
default: true
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
npm-publish:
|
|
17
|
+
uses: ast-grep/langs/.github/workflows/release.yml@main
|
|
18
|
+
with:
|
|
19
|
+
directory: ${{ inputs.directory }}
|
|
20
|
+
dry-run: ${{ inputs.dry-run }}
|
|
21
|
+
secrets:
|
|
22
|
+
NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# ast-grep napi language for $$NAME$$
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
In a pnpm project, run:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install $$PACKAGE_NAME$$
|
|
9
|
+
pnpm install @ast-grep/napi
|
|
10
|
+
# install the tree-sitter-cli if no prebuild is available
|
|
11
|
+
pnpm install @tree-sitter/cli --save-dev
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import $$NAME$$ from '$$PACKAGE_NAME$$'
|
|
18
|
+
import { registerDynamicLanguage, parse } from '@ast-grep/napi'
|
|
19
|
+
|
|
20
|
+
registerDynamicLanguage({ $$NAME$$ })
|
|
21
|
+
|
|
22
|
+
const sg = parse('$$NAME$$', `your code`)
|
|
23
|
+
sg.root().kind()
|
|
24
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { setup } = require('@ast-grep/nursery')
|
|
2
|
+
const languageRegistration = require('./index')
|
|
3
|
+
|
|
4
|
+
setup({
|
|
5
|
+
dirname: __dirname,
|
|
6
|
+
name: '$$NAME$$',
|
|
7
|
+
treeSitterPackage: '$$TREE_SITTER_PACKAGE$$',
|
|
8
|
+
languageRegistration,
|
|
9
|
+
testRunner: (parse) => {
|
|
10
|
+
// add test here
|
|
11
|
+
}
|
|
12
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "$$PACKAGE_NAME$$",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tree-sitter build -o parser.so",
|
|
8
|
+
"source": "node nursery.js source",
|
|
9
|
+
"prepublishOnly": "node nursery.js source",
|
|
10
|
+
"postinstall": "node postinstall.js",
|
|
11
|
+
"test": "node nursery.js test"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"index.d.ts",
|
|
16
|
+
"type.d.ts",
|
|
17
|
+
"postinstall.js",
|
|
18
|
+
"src",
|
|
19
|
+
"prebuilds"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [],
|
|
22
|
+
"author": "",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@ast-grep/setup-lang": "0.0.2"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"tree-sitter-cli": "0.24.6"
|
|
29
|
+
},
|
|
30
|
+
"peerDependenciesMeta": {
|
|
31
|
+
"tree-sitter-cli": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@ast-grep/nursery": "0.0.2"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public",
|
|
40
|
+
"registry": "https://registry.npmjs.org/"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/tsconfig.json
ADDED