@ast-grep/setup-lang 0.0.1 → 0.0.2
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 +11 -0
- package/index.js +56 -49
- package/index.ts +80 -0
- package/package.json +9 -2
- 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,11 @@
|
|
|
1
|
+
interface SetupConfig {
|
|
2
|
+
/** Directory of the lang package. e.g. __dirname */
|
|
3
|
+
dirname: string;
|
|
4
|
+
/** Package name of tree-sitter package. e.g. tree-sitter-css */
|
|
5
|
+
treeSitterPackage: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Move prebuild or build parser
|
|
9
|
+
*/
|
|
10
|
+
declare function postinstall(config: SetupConfig): void;
|
|
11
|
+
export { postinstall };
|
package/index.js
CHANGED
|
@@ -1,65 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
exports.postinstall = postinstall;
|
|
7
|
+
const node_child_process_1 = require("node:child_process");
|
|
8
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
5
10
|
/**
|
|
6
11
|
* Log to console
|
|
7
|
-
* @param {...string} args
|
|
8
12
|
*/
|
|
9
13
|
function log(...args) {
|
|
10
|
-
|
|
14
|
+
console.debug(`@ast-grep/lang:`, ...args);
|
|
11
15
|
}
|
|
12
|
-
|
|
13
16
|
/**
|
|
14
17
|
* Move prebuild or build parser
|
|
15
|
-
* @param {string} dir
|
|
16
18
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
19
|
+
function postinstall(config) {
|
|
20
|
+
const dir = config.dirname;
|
|
21
|
+
const parser = node_path_1.default.join(dir, 'parser.so');
|
|
22
|
+
if (node_fs_1.default.existsSync(parser)) {
|
|
23
|
+
log('parser already exists, skipping build');
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const prebuild = resolvePrebuild(dir);
|
|
27
|
+
if (prebuild) {
|
|
28
|
+
log('copying prebuild parser');
|
|
29
|
+
node_fs_1.default.copyFileSync(prebuild, parser);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
buildSrc(config);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
log('build failed, please ensure tree-sitter-cli is installed as peer dependency');
|
|
37
|
+
log(e);
|
|
38
|
+
}
|
|
36
39
|
}
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
function buildSrc(config) {
|
|
41
|
+
const { dirname } = config;
|
|
42
|
+
const existing = node_path_1.default.join(dirname, 'src');
|
|
43
|
+
if (!node_fs_1.default.existsSync(existing)) {
|
|
44
|
+
log('tree-sitter src not found. If you are making a lang package, please run `pnpm source`.');
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
log('building parser from source');
|
|
48
|
+
(0, node_child_process_1.execSync)('npm run build');
|
|
42
49
|
}
|
|
43
|
-
|
|
50
|
+
const PLATFORM_MAP = {
|
|
51
|
+
darwin: 'macOS',
|
|
52
|
+
linux: 'Linux',
|
|
53
|
+
win32: 'Windows',
|
|
54
|
+
};
|
|
44
55
|
const ARCH_MAP = {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
56
|
+
x64: 'x64',
|
|
57
|
+
arm64: 'ARM64',
|
|
58
|
+
};
|
|
49
59
|
/**
|
|
50
60
|
* Resolve prebuild path
|
|
51
|
-
* @param {string} dir
|
|
52
61
|
*/
|
|
53
62
|
function resolvePrebuild(dir) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
const os = PLATFORM_MAP[process.platform];
|
|
64
|
+
const arch = ARCH_MAP[process.arch];
|
|
65
|
+
const prebuild = node_path_1.default.join(dir, 'prebuilds', `prebuild-${os}-${arch}`, 'parser.so');
|
|
66
|
+
if (!os || !arch || !node_fs_1.default.existsSync(prebuild)) {
|
|
67
|
+
log(`no prebuild for ${os} ${arch}`);
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
log(`found prebuild for ${os} ${arch}`);
|
|
71
|
+
return prebuild;
|
|
63
72
|
}
|
|
64
|
-
|
|
65
|
-
exports.postinstall = postinstall
|
package/index.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { execSync } from 'node:child_process'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Log to console
|
|
7
|
+
*/
|
|
8
|
+
function log(...args: unknown[]) {
|
|
9
|
+
console.debug(`@ast-grep/lang:`, ...args)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface SetupConfig {
|
|
13
|
+
/** Directory of the lang package. e.g. __dirname */
|
|
14
|
+
dirname: string
|
|
15
|
+
/** Package name of tree-sitter package. e.g. tree-sitter-css */
|
|
16
|
+
treeSitterPackage: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Move prebuild or build parser
|
|
21
|
+
*/
|
|
22
|
+
function postinstall(config: SetupConfig) {
|
|
23
|
+
const dir = config.dirname
|
|
24
|
+
const parser = path.join(dir, 'parser.so')
|
|
25
|
+
if (fs.existsSync(parser)) {
|
|
26
|
+
log('parser already exists, skipping build')
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
const prebuild = resolvePrebuild(dir)
|
|
30
|
+
if (prebuild) {
|
|
31
|
+
log('copying prebuild parser')
|
|
32
|
+
fs.copyFileSync(prebuild, parser)
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
buildSrc(config)
|
|
37
|
+
} catch (e: unknown) {
|
|
38
|
+
log('build failed, please ensure tree-sitter-cli is installed as peer dependency')
|
|
39
|
+
log(e)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function buildSrc(config: SetupConfig) {
|
|
44
|
+
const { dirname } = config
|
|
45
|
+
const existing = path.join(dirname, 'src')
|
|
46
|
+
if (!fs.existsSync(existing)) {
|
|
47
|
+
log('tree-sitter src not found. If you are making a lang package, please run `pnpm source`.')
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
log('building parser from source')
|
|
51
|
+
execSync('npm run build')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const PLATFORM_MAP: Record<string, string> = {
|
|
55
|
+
darwin: 'macOS',
|
|
56
|
+
linux: 'Linux',
|
|
57
|
+
win32: 'Windows',
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const ARCH_MAP: Record<string, string> = {
|
|
61
|
+
x64: 'x64',
|
|
62
|
+
arm64: 'ARM64',
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Resolve prebuild path
|
|
67
|
+
*/
|
|
68
|
+
function resolvePrebuild(dir: string) {
|
|
69
|
+
const os = PLATFORM_MAP[process.platform]
|
|
70
|
+
const arch = ARCH_MAP[process.arch]
|
|
71
|
+
const prebuild = path.join(dir, 'prebuilds', `prebuild-${os}-${arch}`, 'parser.so')
|
|
72
|
+
if (!os || !arch || !fs.existsSync(prebuild)) {
|
|
73
|
+
log(`no prebuild for ${os} ${arch}`)
|
|
74
|
+
return undefined
|
|
75
|
+
}
|
|
76
|
+
log(`found prebuild for ${os} ${arch}`)
|
|
77
|
+
return prebuild
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export { postinstall }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ast-grep/setup-lang",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
@@ -10,5 +10,12 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public",
|
|
12
12
|
"registry": "https://registry.npmjs.org/"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"typescript": "^5.7.3",
|
|
16
|
+
"@types/node": "22.10.5"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"compile-ts": "tsc"
|
|
13
20
|
}
|
|
14
|
-
}
|
|
21
|
+
}
|
package/tsconfig.json
ADDED