@ast-grep/setup-lang 0.0.1

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.
Files changed (2) hide show
  1. package/index.js +65 -0
  2. package/package.json +14 -0
package/index.js ADDED
@@ -0,0 +1,65 @@
1
+ const { execSync } = require('child_process')
2
+ const fs = require('fs')
3
+ const path = require('path')
4
+
5
+ /**
6
+ * Log to console
7
+ * @param {...string} args
8
+ */
9
+ function log(...args) {
10
+ console.debug(`@ast-grep/lang:`, ...args)
11
+ }
12
+
13
+ /**
14
+ * Move prebuild or build parser
15
+ * @param {string} dir
16
+ */
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
+ }
36
+ }
37
+
38
+ const PLATFORM_MAP = {
39
+ darwin: 'macOS',
40
+ linux: 'Linux',
41
+ win32: 'Windows',
42
+ }
43
+
44
+ const ARCH_MAP = {
45
+ x64: 'x64',
46
+ arm64: 'ARM64',
47
+ }
48
+
49
+ /**
50
+ * Resolve prebuild path
51
+ * @param {string} dir
52
+ */
53
+ 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
+ }
64
+
65
+ exports.postinstall = postinstall
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@ast-grep/setup-lang",
3
+ "version": "0.0.1",
4
+ "private": false,
5
+ "description": "",
6
+ "main": "index.js",
7
+ "keywords": [],
8
+ "author": "",
9
+ "license": "ISC",
10
+ "publishConfig": {
11
+ "access": "public",
12
+ "registry": "https://registry.npmjs.org/"
13
+ }
14
+ }