Skip to content

Commit d554166

Browse files
feat: move copy-src command to postinstall.js
fix #12
1 parent 9346e04 commit d554166

File tree

8 files changed

+34
-19
lines changed

8 files changed

+34
-19
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
"version": "0.0.1",
99
"description": "Monorepo for `@ast-grep/lang-*` packages",
1010
"scripts": {
11-
"postinstall": "pnpm -r compile && pnpm -r copy-src",
12-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"postinstall": "pnpm -r compile"
1312
},
1413
"keywords": [],
1514
"author": "Herrington Darkholme",

packages/toml/nursery.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const assert = require('node:assert')
44

55
setup({
66
name: 'toml',
7-
packageName: '@tree-sitter-grammars/tree-sitter-toml',
87
languageRegistration: toml,
98
testRunner: (parse) => {
109
const sg = parse('name = "Tom"')

packages/toml/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"copy-src": "node nursery.js copy",
87
"build": "tree-sitter build -o parser.so",
98
"postinstall": "node postinstall.js",
109
"test": "node nursery.js test"

packages/toml/postinstall.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
const { postinstall } = require('@ast-grep/setup-lang')
2-
postinstall(__dirname)
2+
postinstall({
3+
directory: __dirname,
4+
treeSitterPackage: '@tree-sitter-grammars/tree-sitter-toml',
5+
})

scripts/nursery/index.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import { cp } from 'fs/promises'
21
import { parse, registerDynamicLanguage, SgRoot, DynamicLangRegistrations } from '@ast-grep/napi'
3-
import path from 'path'
4-
5-
async function copySrc(packageName: string) {
6-
const src = path.join(process.cwd(), 'node_modules', packageName, 'src')
7-
await cp(src, 'src', { recursive: true })
8-
}
92

103
/** Setup ast-grep/lang package's pre-release */
114
interface SetupConfig {
125
/** Name of the language. e.g. toml */
136
name: string
147
/** Language registration object, usually the export of index.js */
158
languageRegistration: DynamicLangRegistrations[string]
16-
/** Package name of tree-sitter package. e.g. tree-sitter-toml */
179
packageName: string
1810
/** Test cases running in CI */
1911
testRunner: (parse: (c: string) => SgRoot) => void
@@ -28,9 +20,7 @@ function test(setupConfig: SetupConfig) {
2820
/** Setup ast-grep/lang package's pre-release build and test */
2921
export function setup(setupConfig: SetupConfig) {
3022
const arg = process.argv[2]
31-
if (arg === 'copy') {
32-
copySrc(setupConfig.packageName)
33-
} else if (arg === 'test') {
23+
if (arg === 'test') {
3424
test(setupConfig)
3525
}
3626
}

scripts/nursery/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ast-grep/nursery",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"private": false,
55
"description": "",
66
"main": "index.js",

scripts/setup/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ function log(...args: unknown[]) {
99
console.debug(`@ast-grep/lang:`, ...args)
1010
}
1111

12+
interface SetupConfig {
13+
/** Directory of the lang package. e.g. __dirname */
14+
directory: string
15+
/** Package name of tree-sitter package. e.g. tree-sitter-css */
16+
treeSitterPackage: string
17+
}
18+
1219
/**
1320
* Move prebuild or build parser
1421
*/
15-
async function postinstall(dir: string) {
22+
function postinstall(config: SetupConfig) {
23+
const dir = config.directory
1624
const parser = path.join(dir, 'parser.so')
1725
if (fs.existsSync(parser)) {
1826
log('parser already exists, skipping build')
@@ -24,15 +32,32 @@ async function postinstall(dir: string) {
2432
fs.copyFileSync(prebuild, parser)
2533
return
2634
}
35+
buildDynamicLib(config)
36+
}
37+
38+
function buildDynamicLib(config: SetupConfig) {
2739
log('building parser')
2840
try {
41+
copySrcIfNeeded(config)
2942
execSync('npm run build')
3043
} catch (e: unknown) {
3144
log('build failed, please ensure tree-sitter-cli is installed as peer dependency')
3245
log(e)
3346
}
3447
}
3548

49+
function copySrcIfNeeded(config: SetupConfig) {
50+
const { directory, treeSitterPackage } = config
51+
const existing = path.join(directory, 'src')
52+
const src = path.join(directory, 'node_modules', treeSitterPackage, 'src')
53+
if (fs.existsSync(existing)) {
54+
log('src exists, skipping copy')
55+
return
56+
}
57+
log('copying tree-sitter src')
58+
fs.cpSync(src, 'src', { recursive: true })
59+
}
60+
3661
const PLATFORM_MAP: Record<string, string> = {
3762
darwin: 'macOS',
3863
linux: 'Linux',

scripts/setup/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ast-grep/setup-lang",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"private": false,
55
"description": "",
66
"main": "index.js",

0 commit comments

Comments
 (0)