@ast-grep/setup-lang 0.0.1 → 0.0.3

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