@skylence-ai/hashline 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.
- package/LICENSE +195 -0
- package/README.md +199 -0
- package/binary-install.js +212 -0
- package/binary.js +128 -0
- package/install.js +4 -0
- package/npm-shrinkwrap.json +546 -0
- package/package.json +92 -0
- package/run-hashline.js +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and its Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any
|
|
81
|
+
Contribution embodied within the Work constitutes direct or contributory
|
|
82
|
+
patent infringement, then any patent licenses granted to You under this
|
|
83
|
+
License for that Work shall terminate as of the date such litigation is
|
|
84
|
+
filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
92
|
+
a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the attribution
|
|
105
|
+
notices contained within such NOTICE file, in at least one
|
|
106
|
+
of the following places: within a NOTICE text provided as part of
|
|
107
|
+
the Derivative Works; within the Source form or documentation, if
|
|
108
|
+
provided along with the Derivative Works; or, within a display
|
|
109
|
+
generated by the Derivative Works, if and wherever such third-party
|
|
110
|
+
notices normally appear. The contents of the NOTICE file are for
|
|
111
|
+
informational purposes only and do not modify the License. You may
|
|
112
|
+
add Your own attribution notices within Derivative Works that You
|
|
113
|
+
distribute, alongside or in addition to the NOTICE text from the
|
|
114
|
+
Work, provided that such additional attribution notices cannot be
|
|
115
|
+
construed as modifying the License.
|
|
116
|
+
|
|
117
|
+
You may add Your own license statement for Your modifications and
|
|
118
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
119
|
+
publish, distribute, sublicense, and/or sell copies of those
|
|
120
|
+
modifications, or for such Derivative Works as a whole, under the
|
|
121
|
+
terms of this License, if You meet the following conditions:
|
|
122
|
+
|
|
123
|
+
(e) You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
125
|
+
publish, distribute, sublicense, and/or sell copies of those
|
|
126
|
+
modifications, or for such Derivative Works as a whole, under the
|
|
127
|
+
terms of this License.
|
|
128
|
+
|
|
129
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
130
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
131
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
132
|
+
this License, without any additional terms or conditions.
|
|
133
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
134
|
+
the terms of any separate license agreement you may have executed
|
|
135
|
+
with Licensor regarding such Contributions.
|
|
136
|
+
|
|
137
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
138
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
139
|
+
except as required for reasonable and customary use in describing the
|
|
140
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
141
|
+
|
|
142
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
143
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
144
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
145
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
146
|
+
implied, including, without limitation, any warranties or conditions
|
|
147
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
148
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
149
|
+
appropriateness of using or reproducing the Work and assume any
|
|
150
|
+
risks associated with Your exercise of permissions under this License.
|
|
151
|
+
|
|
152
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
153
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
154
|
+
unless required by applicable law (such as deliberate and grossly
|
|
155
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
156
|
+
liable to You for damages, including any direct, indirect, special,
|
|
157
|
+
incidental, or exemplary damages of any character arising as a
|
|
158
|
+
result of this License or out of the use or inability to use the
|
|
159
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
160
|
+
work stoppage, computer failure or malfunction, or all other
|
|
161
|
+
commercial damages or losses), even if such Contributor has been
|
|
162
|
+
advised of the possibility of such damages.
|
|
163
|
+
|
|
164
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
165
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
166
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
167
|
+
or other liability obligations and/or rights consistent with this
|
|
168
|
+
License. However, in accepting such obligations, You may include
|
|
169
|
+
additional terms and conditions to such obligations, and charges
|
|
170
|
+
for such obligations.
|
|
171
|
+
|
|
172
|
+
END OF TERMS AND CONDITIONS
|
|
173
|
+
|
|
174
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
175
|
+
|
|
176
|
+
To apply the Apache License to your work, attach the following
|
|
177
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
178
|
+
replaced with your own identifying information. (Don't include
|
|
179
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
180
|
+
comment syntax for the file format. Please also include a
|
|
181
|
+
"NOTICE" file with a copy of the Apache License in it.
|
|
182
|
+
|
|
183
|
+
Copyright 2025 skylence-be
|
|
184
|
+
|
|
185
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
186
|
+
you may not use this file except in compliance with the License.
|
|
187
|
+
You may obtain a copy of the License at
|
|
188
|
+
|
|
189
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
190
|
+
|
|
191
|
+
Unless required by applicable law or agreed to in writing, software
|
|
192
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
193
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
194
|
+
See the License for the specific language governing permissions and
|
|
195
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# hashline
|
|
2
|
+
|
|
3
|
+
A line-oriented, content-hash-guarded file editor. Instead of `old_string`/`new_string`
|
|
4
|
+
matching, you address lines by number and bind every edit to a 4-hex content tag of the
|
|
5
|
+
file. A stale tag (file changed since it was read) is caught before any write, and when
|
|
6
|
+
possible recovered via a 3-way merge instead of failing.
|
|
7
|
+
|
|
8
|
+
The engine is written in Rust and ships two interchangeable surfaces over one core:
|
|
9
|
+
|
|
10
|
+
| Surface | Entry point | Transport |
|
|
11
|
+
|---------|-------------|-----------|
|
|
12
|
+
| CLI | `hashline read` / `hashline edit` | in-process |
|
|
13
|
+
| MCP | `hashline_read` / `hashline_edit` | stdio MCP server |
|
|
14
|
+
|
|
15
|
+
## Install / use
|
|
16
|
+
|
|
17
|
+
### MCP (zero-install, recommended)
|
|
18
|
+
|
|
19
|
+
Add to your MCP client config (Claude Code, Claude Desktop, etc.):
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
"hashline": {
|
|
23
|
+
"command": "npx",
|
|
24
|
+
"args": ["-y", "@skylence-ai/hashline", "mcp"]
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`npx` downloads the prebuilt binary for your platform on first run. No compiler needed.
|
|
29
|
+
|
|
30
|
+
### CLI via npx
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npx -y @skylence-ai/hashline read src/main.rs
|
|
34
|
+
echo '...' | npx -y @skylence-ai/hashline edit
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### cargo binstall (no compile)
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
cargo binstall hashline
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### curl / Homebrew
|
|
44
|
+
|
|
45
|
+
Download a prebuilt binary directly from [GitHub Releases](https://github.com/skylence-be/hashline/releases) for your platform, or wait for a Homebrew formula.
|
|
46
|
+
|
|
47
|
+
### Docker note
|
|
48
|
+
|
|
49
|
+
Using hashline under Docker requires mounting your project directory as a volume so the editor can access files on the host. `npx` (above) is simpler for local dev.
|
|
50
|
+
|
|
51
|
+
### Build from source
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
cargo install --git https://github.com/skylence-be/hashline
|
|
55
|
+
# or
|
|
56
|
+
git clone https://github.com/skylence-be/hashline
|
|
57
|
+
cd hashline
|
|
58
|
+
cargo build --release
|
|
59
|
+
# binary at target/release/hashline
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## CLI
|
|
63
|
+
|
|
64
|
+
### `hashline read <path>`
|
|
65
|
+
|
|
66
|
+
Prints a hashline header followed by numbered lines:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
¶src/main.rs#A1B2
|
|
70
|
+
1:fn main() {
|
|
71
|
+
2: println!("hello");
|
|
72
|
+
3:}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Record the `¶path#TAG` header. It anchors subsequent edits to this exact file state.
|
|
76
|
+
If the file changes between read and edit the tag becomes stale and the edit is rejected.
|
|
77
|
+
|
|
78
|
+
### `hashline edit [--json]`
|
|
79
|
+
|
|
80
|
+
Reads a hashline patch from stdin and applies each section to disk. Exits non-zero
|
|
81
|
+
on a stale tag, printing re-read guidance to stderr. Prints the new `¶path#TAG`
|
|
82
|
+
header for each section on success.
|
|
83
|
+
|
|
84
|
+
The `--json` flag emits a JSON array with one object per section:
|
|
85
|
+
```json
|
|
86
|
+
[{"path":"src/main.rs","newHeader":"¶src/main.rs#C3D4","firstChangedLine":2,"warnings":[]}]
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### `hashline mcp`
|
|
90
|
+
|
|
91
|
+
Starts a stdio MCP server exposing `hashline_read` and `hashline_edit` tools.
|
|
92
|
+
Compatible with any MCP client (Claude Code, Claude Desktop, etc.).
|
|
93
|
+
|
|
94
|
+
## Patch format
|
|
95
|
+
|
|
96
|
+
A patch is one or more sections. Each section begins with a `¶path#TAG` header
|
|
97
|
+
(obtained from `hashline read`) followed by edit operations.
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
¶src/main.rs#A1B2
|
|
101
|
+
replace 2..2:
|
|
102
|
+
+ println!("world");
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Operations
|
|
106
|
+
|
|
107
|
+
| Operation | Syntax | Notes |
|
|
108
|
+
|-----------|--------|-------|
|
|
109
|
+
| Replace lines | `replace N..M:` + `+`-body | Replaces lines N–M with payload |
|
|
110
|
+
| Replace single line | `replace N:` + `+`-body | Shorthand for `replace N..N:` |
|
|
111
|
+
| Delete lines | `delete N` or `delete N..M` | No body rows |
|
|
112
|
+
| Insert before line | `insert before N:` + `+`-body | |
|
|
113
|
+
| Insert after line | `insert after N:` + `+`-body | |
|
|
114
|
+
| Insert at top | `insert head:` + `+`-body | |
|
|
115
|
+
| Insert at bottom | `insert tail:` + `+`-body | |
|
|
116
|
+
| Replace block | `replace block N:` + `+`-body | Tree-sitter block at line N |
|
|
117
|
+
| Delete block | `delete block N` | Tree-sitter block at line N |
|
|
118
|
+
| Delete whole file | `delete file` | Hash-guarded; no body |
|
|
119
|
+
|
|
120
|
+
Body rows are prefixed with `+`. Use `++` for a literal `+` line, `+-` for a literal `-` line.
|
|
121
|
+
|
|
122
|
+
### Create a new file
|
|
123
|
+
|
|
124
|
+
Use a tag-less `¶path` header (no `#TAG`) with `insert head:` or `insert tail:` ops.
|
|
125
|
+
The file is created if it does not exist; replace/delete ops on an absent path are an error.
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
¶src/new.rs
|
|
129
|
+
insert head:
|
|
130
|
+
+fn hello() {}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Delete a whole file
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
¶src/old.rs#ABCD
|
|
137
|
+
delete file
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Content hash
|
|
141
|
+
|
|
142
|
+
The tag is a 4-uppercase-hex string derived from the file's normalized content:
|
|
143
|
+
|
|
144
|
+
1. Strip trailing `[ \t\r]` from every line (CRLF-safe)
|
|
145
|
+
2. Compute CRC32 (IEEE polynomial) of the UTF-8 bytes
|
|
146
|
+
3. Take the low 16 bits → format as 4 upper-hex digits
|
|
147
|
+
|
|
148
|
+
Internal consistency (same algorithm on read and post-write recompute) is the hard
|
|
149
|
+
requirement. The algorithm is documented here so snapshot files remain interpretable
|
|
150
|
+
across implementations.
|
|
151
|
+
|
|
152
|
+
## Block operations
|
|
153
|
+
|
|
154
|
+
Block ops (`replace block N:` / `delete block N`) resolve the syntactic block beginning
|
|
155
|
+
on line N using tree-sitter. Supported languages (by file extension):
|
|
156
|
+
|
|
157
|
+
| Extension | Language |
|
|
158
|
+
|-----------|----------|
|
|
159
|
+
| `.rs` | Rust |
|
|
160
|
+
| `.ts` | TypeScript |
|
|
161
|
+
| `.tsx` | TypeScript JSX |
|
|
162
|
+
| `.js`, `.mjs`, `.cjs` | JavaScript |
|
|
163
|
+
| `.jsx` | JavaScript JSX |
|
|
164
|
+
| `.py` | Python |
|
|
165
|
+
| `.go` | Go |
|
|
166
|
+
|
|
167
|
+
The resolver finds the outermost named AST node that begins on line N (excluding the
|
|
168
|
+
whole-file root). If no node begins there, or the resolved subtree contains a syntax
|
|
169
|
+
error, the op fails with a clear message. Fall back to an explicit line range.
|
|
170
|
+
|
|
171
|
+
## Stale-tag recovery
|
|
172
|
+
|
|
173
|
+
When an edit's tag no longer matches the live file, hashline attempts recovery before
|
|
174
|
+
hard-failing:
|
|
175
|
+
|
|
176
|
+
1. **3-way merge**: apply the edit to the snapshot that minted the stale tag, then
|
|
177
|
+
zero-fuzz merge the resulting diff onto the current file. Emits a warning on success.
|
|
178
|
+
2. **Session-chain replay**: if the snapshot is not the latest, replay the edit directly
|
|
179
|
+
onto the current file after verifying line counts and anchor content match.
|
|
180
|
+
|
|
181
|
+
Snapshots are stored under `$HASHLINE_DATA_DIR/snapshots/v1/` (or the OS cache dir
|
|
182
|
+
`hashline/snapshots/v1/` when `HASHLINE_DATA_DIR` is unset).
|
|
183
|
+
|
|
184
|
+
## MCP tool descriptions
|
|
185
|
+
|
|
186
|
+
`hashline_read`:
|
|
187
|
+
> Read a file and return its content tagged with a hashline header (¶path#TAG) followed
|
|
188
|
+
> by numbered lines (N:LINE). Pass the returned header verbatim in hashline_edit patches
|
|
189
|
+
> so the editor can verify the file has not changed since the read.
|
|
190
|
+
|
|
191
|
+
`hashline_edit`:
|
|
192
|
+
> Apply a hashline patch to one or more files. Each section starts with a ¶path#TAG
|
|
193
|
+
> header (obtained from hashline_read) followed by edit operations on the file's ORIGINAL
|
|
194
|
+
> line numbers. Returns the new ¶path#TAG header per section so the next edit is grounded.
|
|
195
|
+
> Returns IsError when the content tag is stale. Re-read the file first.
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
Apache-2.0. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
const { createWriteStream, existsSync, mkdirSync, mkdtemp } = require("fs");
|
|
2
|
+
const { join, sep } = require("path");
|
|
3
|
+
const { spawnSync } = require("child_process");
|
|
4
|
+
const { tmpdir } = require("os");
|
|
5
|
+
|
|
6
|
+
const axios = require("axios");
|
|
7
|
+
const rimraf = require("rimraf");
|
|
8
|
+
const tmpDir = tmpdir();
|
|
9
|
+
|
|
10
|
+
const error = (msg) => {
|
|
11
|
+
console.error(msg);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
class Package {
|
|
16
|
+
constructor(platform, name, url, filename, zipExt, binaries) {
|
|
17
|
+
let errors = [];
|
|
18
|
+
if (typeof url !== "string") {
|
|
19
|
+
errors.push("url must be a string");
|
|
20
|
+
} else {
|
|
21
|
+
try {
|
|
22
|
+
new URL(url);
|
|
23
|
+
} catch (e) {
|
|
24
|
+
errors.push(e);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (name && typeof name !== "string") {
|
|
28
|
+
errors.push("package name must be a string");
|
|
29
|
+
}
|
|
30
|
+
if (!name) {
|
|
31
|
+
errors.push("You must specify the name of your package");
|
|
32
|
+
}
|
|
33
|
+
if (binaries && typeof binaries !== "object") {
|
|
34
|
+
errors.push("binaries must be a string => string map");
|
|
35
|
+
}
|
|
36
|
+
if (!binaries) {
|
|
37
|
+
errors.push("You must specify the binaries in the package");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (errors.length > 0) {
|
|
41
|
+
let errorMsg =
|
|
42
|
+
"One or more of the parameters you passed to the Binary constructor are invalid:\n";
|
|
43
|
+
errors.forEach((error) => {
|
|
44
|
+
errorMsg += error;
|
|
45
|
+
});
|
|
46
|
+
errorMsg +=
|
|
47
|
+
'\n\nCorrect usage: new Package("my-binary", "https://example.com/binary/download.tar.gz", {"my-binary": "my-binary"})';
|
|
48
|
+
error(errorMsg);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.platform = platform;
|
|
52
|
+
this.url = url;
|
|
53
|
+
this.name = name;
|
|
54
|
+
this.filename = filename;
|
|
55
|
+
this.zipExt = zipExt;
|
|
56
|
+
this.installDirectory = join(__dirname, "node_modules", ".bin_real");
|
|
57
|
+
this.binaries = binaries;
|
|
58
|
+
|
|
59
|
+
if (!existsSync(this.installDirectory)) {
|
|
60
|
+
mkdirSync(this.installDirectory, { recursive: true });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
exists() {
|
|
65
|
+
for (const binaryName in this.binaries) {
|
|
66
|
+
const binRelPath = this.binaries[binaryName];
|
|
67
|
+
const binPath = join(this.installDirectory, binRelPath);
|
|
68
|
+
if (!existsSync(binPath)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
install(fetchOptions, suppressLogs = false) {
|
|
76
|
+
if (this.exists()) {
|
|
77
|
+
if (!suppressLogs) {
|
|
78
|
+
console.error(
|
|
79
|
+
`${this.name} is already installed, skipping installation.`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
return Promise.resolve();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (existsSync(this.installDirectory)) {
|
|
86
|
+
rimraf.sync(this.installDirectory);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
mkdirSync(this.installDirectory, { recursive: true });
|
|
90
|
+
|
|
91
|
+
if (!suppressLogs) {
|
|
92
|
+
console.error(`Downloading release from ${this.url}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return axios({ ...fetchOptions, url: this.url, responseType: "stream" })
|
|
96
|
+
.then((res) => {
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
|
|
99
|
+
let tempFile = join(directory, this.filename);
|
|
100
|
+
const sink = res.data.pipe(createWriteStream(tempFile));
|
|
101
|
+
sink.on("error", (err) => reject(err));
|
|
102
|
+
sink.on("close", () => {
|
|
103
|
+
if (/\.tar\.*/.test(this.zipExt)) {
|
|
104
|
+
const result = spawnSync("tar", [
|
|
105
|
+
"xf",
|
|
106
|
+
tempFile,
|
|
107
|
+
// The tarballs are stored with a leading directory
|
|
108
|
+
// component; we strip one component in the
|
|
109
|
+
// shell installers too.
|
|
110
|
+
"--strip-components",
|
|
111
|
+
"1",
|
|
112
|
+
"-C",
|
|
113
|
+
this.installDirectory,
|
|
114
|
+
]);
|
|
115
|
+
if (result.status == 0) {
|
|
116
|
+
resolve();
|
|
117
|
+
} else if (result.error) {
|
|
118
|
+
reject(result.error);
|
|
119
|
+
} else {
|
|
120
|
+
reject(
|
|
121
|
+
new Error(
|
|
122
|
+
`An error occurred untarring the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
} else if (this.zipExt == ".zip") {
|
|
127
|
+
let result;
|
|
128
|
+
if (this.platform.artifactName.includes("windows")) {
|
|
129
|
+
// Windows does not have "unzip" by default on many installations, instead
|
|
130
|
+
// we use Expand-Archive from powershell
|
|
131
|
+
result = spawnSync("powershell.exe", [
|
|
132
|
+
"-NoProfile",
|
|
133
|
+
"-NonInteractive",
|
|
134
|
+
"-Command",
|
|
135
|
+
`& {
|
|
136
|
+
param([string]$LiteralPath, [string]$DestinationPath)
|
|
137
|
+
Expand-Archive -LiteralPath $LiteralPath -DestinationPath $DestinationPath -Force
|
|
138
|
+
}`,
|
|
139
|
+
tempFile,
|
|
140
|
+
this.installDirectory,
|
|
141
|
+
]);
|
|
142
|
+
} else {
|
|
143
|
+
result = spawnSync("unzip", [
|
|
144
|
+
"-q",
|
|
145
|
+
tempFile,
|
|
146
|
+
"-d",
|
|
147
|
+
this.installDirectory,
|
|
148
|
+
]);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (result.status == 0) {
|
|
152
|
+
resolve();
|
|
153
|
+
} else if (result.error) {
|
|
154
|
+
reject(result.error);
|
|
155
|
+
} else {
|
|
156
|
+
reject(
|
|
157
|
+
new Error(
|
|
158
|
+
`An error occurred unzipping the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
|
|
159
|
+
),
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
reject(
|
|
164
|
+
new Error(`Unrecognized file extension: ${this.zipExt}`),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
})
|
|
171
|
+
.then(() => {
|
|
172
|
+
if (!suppressLogs) {
|
|
173
|
+
console.error(`${this.name} has been installed!`);
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
.catch((e) => {
|
|
177
|
+
error(`Error fetching release: ${e.message}`);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
run(binaryName, fetchOptions) {
|
|
182
|
+
const promise = !this.exists()
|
|
183
|
+
? this.install(fetchOptions, true)
|
|
184
|
+
: Promise.resolve();
|
|
185
|
+
|
|
186
|
+
promise
|
|
187
|
+
.then(() => {
|
|
188
|
+
const [, , ...args] = process.argv;
|
|
189
|
+
|
|
190
|
+
const options = { cwd: process.cwd(), stdio: "inherit" };
|
|
191
|
+
|
|
192
|
+
const binRelPath = this.binaries[binaryName];
|
|
193
|
+
if (!binRelPath) {
|
|
194
|
+
error(`${binaryName} is not a known binary in ${this.name}`);
|
|
195
|
+
}
|
|
196
|
+
const binPath = join(this.installDirectory, binRelPath);
|
|
197
|
+
const result = spawnSync(binPath, args, options);
|
|
198
|
+
|
|
199
|
+
if (result.error) {
|
|
200
|
+
error(result.error);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
process.exit(result.status);
|
|
204
|
+
})
|
|
205
|
+
.catch((e) => {
|
|
206
|
+
error(e.message);
|
|
207
|
+
process.exit(1);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
module.exports.Package = Package;
|