@ast-grep/napi 0.1.0

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 (5) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +105 -0
  3. package/index.d.ts +43 -0
  4. package/index.js +242 -0
  5. package/package.json +74 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 N-API for Rust
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/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # `@napi-rs/package-template`
2
+
3
+ ![https://github.com/napi-rs/package-template/actions](https://github.com/napi-rs/package-template/workflows/CI/badge.svg)
4
+
5
+ > Template project for writing node packages with napi-rs.
6
+
7
+ # Usage
8
+
9
+ 1. Click **Use this template**.
10
+ 2. **Clone** your project.
11
+ 3. Run `yarn install` to install dependencies.
12
+ 4. Run `npx napi rename -n [name]` command under the project folder to rename your package.
13
+
14
+ ## Install this test package
15
+
16
+ ```
17
+ yarn add @napi-rs/package-template
18
+ ```
19
+
20
+ ## Support matrix
21
+
22
+ ### Operating Systems
23
+
24
+ | | node14 | node16 | node18 |
25
+ | ---------------- | ------ | ------ | ------ |
26
+ | Windows x64 | ✓ | ✓ | ✓ |
27
+ | macOS x64 | ✓ | ✓ | ✓ |
28
+ | macOS arm64 | ✓ | ✓ | ✓ |
29
+ | Linux x64 gnu | ✓ | ✓ | ✓ |
30
+ <!-- | Windows x32 | ✓ | ✓ | ✓ | -->
31
+ <!-- | Windows arm64 | ✓ | ✓ | ✓ | -->
32
+ <!-- | Linux x64 musl | ✓ | ✓ | ✓ | -->
33
+ <!-- | Linux arm gnu | ✓ | ✓ | ✓ | -->
34
+ <!-- | Linux arm64 gnu | ✓ | ✓ | ✓ | -->
35
+ <!-- | Linux arm64 musl | ✓ | ✓ | ✓ | -->
36
+ <!-- | Android arm64 | ✓ | ✓ | ✓ | -->
37
+ <!-- | Android armv7 | ✓ | ✓ | ✓ | -->
38
+ <!-- | FreeBSD x64 | ✓ | ✓ | ✓ | -->
39
+
40
+ ## Ability
41
+
42
+ ### Build
43
+
44
+ After `yarn build/npm run build` command, you can see `package-template.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs).
45
+
46
+ ### Test
47
+
48
+ With [ava](https://github.com/avajs/ava), run `yarn test/npm run test` to testing native addon. You can also switch to another testing framework if you want.
49
+
50
+ ### CI
51
+
52
+ With GitHub Actions, each commit and pull request will be built and tested automatically in [`node@14`, `node@16`, `@node18`] x [`macOS`, `Linux`, `Windows`] matrix. You will never be afraid of the native addon broken in these platforms.
53
+
54
+ ### Release
55
+
56
+ Release native package is very difficult in old days. Native packages may ask developers who use it to install `build toolchain` like `gcc/llvm`, `node-gyp` or something more.
57
+
58
+ With `GitHub actions`, we can easily prebuild a `binary` for major platforms. And with `N-API`, we should never be afraid of **ABI Compatible**.
59
+
60
+ The other problem is how to deliver prebuild `binary` to users. Downloading it in `postinstall` script is a common way that most packages do it right now. The problem with this solution is it introduced many other packages to download binary that has not been used by `runtime codes`. The other problem is some users may not easily download the binary from `GitHub/CDN` if they are behind a private network (But in most cases, they have a private NPM mirror).
61
+
62
+ In this package, we choose a better way to solve this problem. We release different `npm packages` for different platforms. And add it to `optionalDependencies` before releasing the `Major` package to npm.
63
+
64
+ `NPM` will choose which native package should download from `registry` automatically. You can see [npm](./npm) dir for details. And you can also run `yarn add @napi-rs/package-template` to see how it works.
65
+
66
+ ## Develop requirements
67
+
68
+ - Install the latest `Rust`
69
+ - Install `Node.js@10+` which fully supported `Node-API`
70
+ - Install `yarn@1.x`
71
+
72
+ ## Test in local
73
+
74
+ - yarn
75
+ - yarn build
76
+ - yarn test
77
+
78
+ And you will see:
79
+
80
+ ```bash
81
+ $ ava --verbose
82
+
83
+ ✔ sync function from native code
84
+ ✔ sleep function from native code (201ms)
85
+
86
+
87
+ 2 tests passed
88
+ ✨ Done in 1.12s.
89
+ ```
90
+
91
+ ## Release package
92
+
93
+ Ensure you have set your **NPM_TOKEN** in the `GitHub` project setting.
94
+
95
+ In `Settings -> Secrets`, add **NPM_TOKEN** into it.
96
+
97
+ When you want to release the package:
98
+
99
+ ```
100
+ npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
101
+
102
+ git push
103
+ ```
104
+
105
+ GitHub actions will do the rest job for you.
package/index.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ export interface Pos {
7
+ row: number
8
+ col: number
9
+ }
10
+ export interface Range {
11
+ start: Pos
12
+ end: Pos
13
+ }
14
+ export class SgNode {
15
+ range(): Range
16
+ isLeaf(): boolean
17
+ kind(): string
18
+ text(): string
19
+ matches(m: string): boolean
20
+ inside(m: string): boolean
21
+ has(m: string): boolean
22
+ precedes(m: string): boolean
23
+ follows(m: string): boolean
24
+ children(): Array<SgNode>
25
+ findByString(pattern: string): SgNode | null
26
+ findAll(pattern: string): Array<SgNode>
27
+ field(name: string): SgNode | null
28
+ parent(): SgNode | null
29
+ child(nth: number): SgNode | null
30
+ ancestors(): Array<SgNode>
31
+ next(): SgNode | null
32
+ nextAll(): Array<SgNode>
33
+ prev(): SgNode | null
34
+ prevAll(): Array<SgNode>
35
+ }
36
+ export type SgRoot = AstGrep
37
+ export class AstGrep {
38
+ static html(src: string): SgRoot
39
+ static js(src: string): SgRoot
40
+ static ts(src: string): SgRoot
41
+ static tsx(src: string): SgRoot
42
+ root(): SgNode
43
+ }
package/index.js ADDED
@@ -0,0 +1,242 @@
1
+ const { existsSync, readFileSync } = require('fs')
2
+ const { join } = require('path')
3
+
4
+ const { platform, arch } = process
5
+
6
+ let nativeBinding = null
7
+ let localFileExisted = false
8
+ let loadError = null
9
+
10
+ function isMusl() {
11
+ // For Node 10
12
+ if (!process.report || typeof process.report.getReport !== 'function') {
13
+ try {
14
+ return readFileSync('/usr/bin/ldd', 'utf8').includes('musl')
15
+ } catch (e) {
16
+ return true
17
+ }
18
+ } else {
19
+ const { glibcVersionRuntime } = process.report.getReport().header
20
+ return !glibcVersionRuntime
21
+ }
22
+ }
23
+
24
+ switch (platform) {
25
+ case 'android':
26
+ switch (arch) {
27
+ case 'arm64':
28
+ localFileExisted = existsSync(join(__dirname, 'ast-grep-napi.android-arm64.node'))
29
+ try {
30
+ if (localFileExisted) {
31
+ nativeBinding = require('./ast-grep-napi.android-arm64.node')
32
+ } else {
33
+ nativeBinding = require('ast-grep-napi-android-arm64')
34
+ }
35
+ } catch (e) {
36
+ loadError = e
37
+ }
38
+ break
39
+ case 'arm':
40
+ localFileExisted = existsSync(join(__dirname, 'ast-grep-napi.android-arm-eabi.node'))
41
+ try {
42
+ if (localFileExisted) {
43
+ nativeBinding = require('./ast-grep-napi.android-arm-eabi.node')
44
+ } else {
45
+ nativeBinding = require('ast-grep-napi-android-arm-eabi')
46
+ }
47
+ } catch (e) {
48
+ loadError = e
49
+ }
50
+ break
51
+ default:
52
+ throw new Error(`Unsupported architecture on Android ${arch}`)
53
+ }
54
+ break
55
+ case 'win32':
56
+ switch (arch) {
57
+ case 'x64':
58
+ localFileExisted = existsSync(
59
+ join(__dirname, 'ast-grep-napi.win32-x64-msvc.node')
60
+ )
61
+ try {
62
+ if (localFileExisted) {
63
+ nativeBinding = require('./ast-grep-napi.win32-x64-msvc.node')
64
+ } else {
65
+ nativeBinding = require('ast-grep-napi-win32-x64-msvc')
66
+ }
67
+ } catch (e) {
68
+ loadError = e
69
+ }
70
+ break
71
+ case 'ia32':
72
+ localFileExisted = existsSync(
73
+ join(__dirname, 'ast-grep-napi.win32-ia32-msvc.node')
74
+ )
75
+ try {
76
+ if (localFileExisted) {
77
+ nativeBinding = require('./ast-grep-napi.win32-ia32-msvc.node')
78
+ } else {
79
+ nativeBinding = require('ast-grep-napi-win32-ia32-msvc')
80
+ }
81
+ } catch (e) {
82
+ loadError = e
83
+ }
84
+ break
85
+ case 'arm64':
86
+ localFileExisted = existsSync(
87
+ join(__dirname, 'ast-grep-napi.win32-arm64-msvc.node')
88
+ )
89
+ try {
90
+ if (localFileExisted) {
91
+ nativeBinding = require('./ast-grep-napi.win32-arm64-msvc.node')
92
+ } else {
93
+ nativeBinding = require('ast-grep-napi-win32-arm64-msvc')
94
+ }
95
+ } catch (e) {
96
+ loadError = e
97
+ }
98
+ break
99
+ default:
100
+ throw new Error(`Unsupported architecture on Windows: ${arch}`)
101
+ }
102
+ break
103
+ case 'darwin':
104
+ switch (arch) {
105
+ case 'x64':
106
+ localFileExisted = existsSync(join(__dirname, 'ast-grep-napi.darwin-x64.node'))
107
+ try {
108
+ if (localFileExisted) {
109
+ nativeBinding = require('./ast-grep-napi.darwin-x64.node')
110
+ } else {
111
+ nativeBinding = require('ast-grep-napi-darwin-x64')
112
+ }
113
+ } catch (e) {
114
+ loadError = e
115
+ }
116
+ break
117
+ case 'arm64':
118
+ localFileExisted = existsSync(
119
+ join(__dirname, 'ast-grep-napi.darwin-arm64.node')
120
+ )
121
+ try {
122
+ if (localFileExisted) {
123
+ nativeBinding = require('./ast-grep-napi.darwin-arm64.node')
124
+ } else {
125
+ nativeBinding = require('ast-grep-napi-darwin-arm64')
126
+ }
127
+ } catch (e) {
128
+ loadError = e
129
+ }
130
+ break
131
+ default:
132
+ throw new Error(`Unsupported architecture on macOS: ${arch}`)
133
+ }
134
+ break
135
+ case 'freebsd':
136
+ if (arch !== 'x64') {
137
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
138
+ }
139
+ localFileExisted = existsSync(join(__dirname, 'ast-grep-napi.freebsd-x64.node'))
140
+ try {
141
+ if (localFileExisted) {
142
+ nativeBinding = require('./ast-grep-napi.freebsd-x64.node')
143
+ } else {
144
+ nativeBinding = require('ast-grep-napi-freebsd-x64')
145
+ }
146
+ } catch (e) {
147
+ loadError = e
148
+ }
149
+ break
150
+ case 'linux':
151
+ switch (arch) {
152
+ case 'x64':
153
+ if (isMusl()) {
154
+ localFileExisted = existsSync(
155
+ join(__dirname, 'ast-grep-napi.linux-x64-musl.node')
156
+ )
157
+ try {
158
+ if (localFileExisted) {
159
+ nativeBinding = require('./ast-grep-napi.linux-x64-musl.node')
160
+ } else {
161
+ nativeBinding = require('ast-grep-napi-linux-x64-musl')
162
+ }
163
+ } catch (e) {
164
+ loadError = e
165
+ }
166
+ } else {
167
+ localFileExisted = existsSync(
168
+ join(__dirname, 'ast-grep-napi.linux-x64-gnu.node')
169
+ )
170
+ try {
171
+ if (localFileExisted) {
172
+ nativeBinding = require('./ast-grep-napi.linux-x64-gnu.node')
173
+ } else {
174
+ nativeBinding = require('ast-grep-napi-linux-x64-gnu')
175
+ }
176
+ } catch (e) {
177
+ loadError = e
178
+ }
179
+ }
180
+ break
181
+ case 'arm64':
182
+ if (isMusl()) {
183
+ localFileExisted = existsSync(
184
+ join(__dirname, 'ast-grep-napi.linux-arm64-musl.node')
185
+ )
186
+ try {
187
+ if (localFileExisted) {
188
+ nativeBinding = require('./ast-grep-napi.linux-arm64-musl.node')
189
+ } else {
190
+ nativeBinding = require('ast-grep-napi-linux-arm64-musl')
191
+ }
192
+ } catch (e) {
193
+ loadError = e
194
+ }
195
+ } else {
196
+ localFileExisted = existsSync(
197
+ join(__dirname, 'ast-grep-napi.linux-arm64-gnu.node')
198
+ )
199
+ try {
200
+ if (localFileExisted) {
201
+ nativeBinding = require('./ast-grep-napi.linux-arm64-gnu.node')
202
+ } else {
203
+ nativeBinding = require('ast-grep-napi-linux-arm64-gnu')
204
+ }
205
+ } catch (e) {
206
+ loadError = e
207
+ }
208
+ }
209
+ break
210
+ case 'arm':
211
+ localFileExisted = existsSync(
212
+ join(__dirname, 'ast-grep-napi.linux-arm-gnueabihf.node')
213
+ )
214
+ try {
215
+ if (localFileExisted) {
216
+ nativeBinding = require('./ast-grep-napi.linux-arm-gnueabihf.node')
217
+ } else {
218
+ nativeBinding = require('ast-grep-napi-linux-arm-gnueabihf')
219
+ }
220
+ } catch (e) {
221
+ loadError = e
222
+ }
223
+ break
224
+ default:
225
+ throw new Error(`Unsupported architecture on Linux: ${arch}`)
226
+ }
227
+ break
228
+ default:
229
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
230
+ }
231
+
232
+ if (!nativeBinding) {
233
+ if (loadError) {
234
+ throw loadError
235
+ }
236
+ throw new Error(`Failed to load native binding`)
237
+ }
238
+
239
+ const { SgNode, AstGrep } = nativeBinding
240
+
241
+ module.exports.SgNode = SgNode
242
+ module.exports.AstGrep = AstGrep
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@ast-grep/napi",
3
+ "version": "0.1.0",
4
+ "description": "Search and Rewrite code at large scale using precise AST pattern",
5
+ "main": "index.js",
6
+ "repository": "https://github.com/HerringtonDarkholme/ast-grep",
7
+ "license": "MIT",
8
+ "keywords": [
9
+ "ast",
10
+ "pattern",
11
+ "codemod",
12
+ "search",
13
+ "rewrite"
14
+ ],
15
+ "files": [
16
+ "index.d.ts",
17
+ "index.js"
18
+ ],
19
+ "napi": {
20
+ "name": "ast-grep-napi",
21
+ "triples": {
22
+ "defaults": true,
23
+ "additional": [
24
+ "i686-pc-windows-msvc",
25
+ "aarch64-apple-darwin",
26
+ "aarch64-pc-windows-msvc"
27
+ ]
28
+ }
29
+ },
30
+ "engines": {
31
+ "node": ">= 10"
32
+ },
33
+ "publishConfig": {
34
+ "registry": "https://registry.npmjs.org/",
35
+ "access": "public"
36
+ },
37
+ "scripts": {
38
+ "artifacts": "napi artifacts",
39
+ "build": "napi build --platform --release",
40
+ "build:debug": "napi build --platform",
41
+ "prepublishOnly": "napi prepublish -t npm",
42
+ "test": "ava",
43
+ "version": "napi version"
44
+ },
45
+ "devDependencies": {
46
+ "@napi-rs/cli": "2.11.4",
47
+ "@swc-node/register": "1.5.1",
48
+ "ava": "4.3.3",
49
+ "chalk": "5.0.1",
50
+ "typescript": "4.8.2"
51
+ },
52
+ "ava": {
53
+ "require": [
54
+ "@swc-node/register"
55
+ ],
56
+ "extensions": [
57
+ "ts"
58
+ ],
59
+ "timeout": "2m",
60
+ "workerThreads": false,
61
+ "environmentVariables": {
62
+ "TS_NODE_PROJECT": "./tsconfig.json"
63
+ }
64
+ },
65
+ "packageManager": "yarn@3.2.3",
66
+ "optionalDependencies": {
67
+ "@ast-grep/napi-win32-x64-msvc": "0.1.0",
68
+ "@ast-grep/napi-darwin-x64": "0.1.0",
69
+ "@ast-grep/napi-linux-x64-gnu": "0.1.0",
70
+ "@ast-grep/napi-win32-ia32-msvc": "0.1.0",
71
+ "@ast-grep/napi-darwin-arm64": "0.1.0",
72
+ "@ast-grep/napi-win32-arm64-msvc": "0.1.0"
73
+ }
74
+ }