Skip to content

Commit e03121d

Browse files
fix: update file
1 parent 04615f4 commit e03121d

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"version": "0.0.1",
99
"description": "Monorepo for `@ast-grep/lang-*` packages",
1010
"scripts": {
11-
"postinstall": "pnpm -r compile"
11+
"postinstall": "pnpm -r compile-ts"
1212
},
1313
"keywords": [],
1414
"author": "Herrington Darkholme",

scripts/nursery/index.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
2-
import { readFileSync, writeFileSync } from 'node:fs'
1+
import fs from 'node:fs'
32
import path from 'node:path'
43
import { parse, registerDynamicLanguage, SgRoot, DynamicLangRegistrations } from '@ast-grep/napi'
54

5+
/**
6+
* Log to console
7+
*/
8+
function log(...args: unknown[]) {
9+
console.debug(`@ast-grep/lang:`, ...args)
10+
}
11+
612
/** Setup ast-grep/lang package's pre-release */
713
interface SetupConfig {
814
/** the root directory of the package */
@@ -29,10 +35,23 @@ export function setup(setupConfig: SetupConfig) {
2935
if (arg === 'test') {
3036
test(setupConfig)
3137
} else if (arg === 'source') {
38+
copySrcIfNeeded(setupConfig)
3239
generateLangNodeTypes(setupConfig)
3340
}
3441
}
3542

43+
function copySrcIfNeeded(config: SetupConfig) {
44+
const { dirname, treeSitterPackage } = config
45+
const existing = path.join(dirname, 'src')
46+
const src = path.join(dirname, 'node_modules', treeSitterPackage, 'src')
47+
if (fs.existsSync(existing)) {
48+
log('src exists, skipping copy')
49+
return
50+
}
51+
log('copying tree-sitter src')
52+
fs.cpSync(src, 'src', { recursive: true })
53+
}
54+
3655
interface NodeBasicInfo {
3756
type: string
3857
named: boolean
@@ -83,7 +102,7 @@ function processNodeTypes(nodeTypes: NodeType[]): Record<string, NodeType> {
83102

84103
function readLangNodeTypes(dirname: string): NodeType[] {
85104
const staticNodePath = path.join(dirname, 'src', 'node-types.json')
86-
const content = readFileSync(staticNodePath, 'utf-8')
105+
const content = fs.readFileSync(staticNodePath, 'utf-8')
87106
return JSON.parse(content)
88107
}
89108

@@ -98,7 +117,7 @@ function generateLangNodeTypes(setupConfig: SetupConfig) {
98117
`type ${lang}Types = ${JSON.stringify(nodeTypeMap, null, 2)};` +
99118
'\n' +
100119
`export default ${lang}Types;`
101-
writeFileSync(path.join(dirname, `type.d.ts`), fileContent)
120+
fs.writeFileSync(path.join(dirname, `type.d.ts`), fileContent)
102121
} catch (e) {
103122
console.error(`Error while generating node types for ${lang}`)
104123
throw e

scripts/nursery/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"author": "",
99
"license": "ISC",
1010
"scripts": {
11-
"compile": "tsc",
11+
"compile-ts": "tsc",
1212
"prepublishOnly": "tsc"
1313
},
1414
"dependencies": {

scripts/setup/index.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { execSync } from 'child_process'
2-
import fs from 'fs'
3-
import path from 'path'
1+
import { execSync } from 'node:child_process'
2+
import fs from 'node:fs'
3+
import path from 'node:path'
44

55
/**
66
* Log to console
@@ -32,30 +32,23 @@ function postinstall(config: SetupConfig) {
3232
fs.copyFileSync(prebuild, parser)
3333
return
3434
}
35-
buildDynamicLib(config)
36-
}
37-
38-
function buildDynamicLib(config: SetupConfig) {
39-
log('building parser')
4035
try {
41-
copySrcIfNeeded(config)
42-
execSync('npm run build')
36+
buildSrc(config)
4337
} catch (e: unknown) {
4438
log('build failed, please ensure tree-sitter-cli is installed as peer dependency')
4539
log(e)
4640
}
4741
}
4842

49-
function copySrcIfNeeded(config: SetupConfig) {
43+
function buildSrc(config: SetupConfig) {
5044
const { directory, treeSitterPackage } = config
5145
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')
46+
if (!fs.existsSync(existing)) {
47+
log('tree-sitter src not found. If you are making a lang package, please run `pnpm source`.')
5548
return
5649
}
57-
log('copying tree-sitter src')
58-
fs.cpSync(src, 'src', { recursive: true })
50+
log('building parser from source')
51+
execSync('npm run build')
5952
}
6053

6154
const PLATFORM_MAP: Record<string, string> = {

scripts/setup/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"author": "",
99
"license": "ISC",
1010
"scripts": {
11-
"compile": "tsc",
11+
"compile-ts": "tsc",
1212
"prepublishOnly": "tsc"
1313
},
1414
"publishConfig": {

0 commit comments

Comments
 (0)