@mylasertools/kerfscript 0.3.0 → 0.5.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 (62) hide show
  1. package/CHANGELOG.md +114 -0
  2. package/LANGUAGE.md +43 -23
  3. package/README.md +65 -2
  4. package/dist/assembly.d.cts +20 -0
  5. package/dist/assembly.d.ts +20 -0
  6. package/dist/ast.d.cts +13 -1
  7. package/dist/ast.d.ts +13 -1
  8. package/dist/cli.js +5037 -1359
  9. package/dist/diagnostics.d.cts +6 -0
  10. package/dist/diagnostics.d.ts +6 -0
  11. package/dist/evaluator.d.cts +15 -0
  12. package/dist/evaluator.d.ts +15 -0
  13. package/dist/format.d.cts +4 -0
  14. package/dist/format.d.ts +4 -0
  15. package/dist/geometry.d.cts +2 -0
  16. package/dist/geometry.d.ts +2 -0
  17. package/dist/highlight.cjs +151 -0
  18. package/dist/highlight.d.cts +13 -0
  19. package/dist/highlight.d.ts +13 -0
  20. package/dist/highlight.js +131 -0
  21. package/dist/index.cjs +369 -20
  22. package/dist/index.d.cts +6 -3
  23. package/dist/index.d.ts +6 -3
  24. package/dist/index.js +369 -20
  25. package/dist/language.cjs +417 -0
  26. package/dist/language.d.cts +143 -0
  27. package/dist/language.d.ts +143 -0
  28. package/dist/language.js +417 -0
  29. package/dist/lexer.d.cts +17 -0
  30. package/dist/lexer.d.ts +17 -0
  31. package/dist/libraries.cjs +380 -0
  32. package/dist/libraries.d.cts +112 -0
  33. package/dist/libraries.d.ts +112 -0
  34. package/dist/libraries.js +380 -0
  35. package/dist/lsp.d.cts +12 -0
  36. package/dist/lsp.d.ts +12 -0
  37. package/dist/render.d.cts +2 -0
  38. package/dist/render.d.ts +2 -0
  39. package/dist/signatures.cjs +675 -0
  40. package/dist/signatures.d.cts +20 -0
  41. package/dist/signatures.d.ts +20 -0
  42. package/dist/signatures.js +655 -0
  43. package/docs/cli.md +45 -2
  44. package/docs/desktop.md +5 -0
  45. package/docs/editors.md +121 -0
  46. package/docs/javascript.md +157 -20
  47. package/docs/kerf.md +10 -0
  48. package/docs/library/box.svg +11 -0
  49. package/docs/library/calibration.svg +11 -0
  50. package/docs/library/dividers.svg +11 -0
  51. package/docs/library/enclosure.svg +11 -0
  52. package/docs/library/hanging.svg +11 -0
  53. package/docs/library/hinge.svg +11 -0
  54. package/docs/library/tabs.svg +11 -0
  55. package/docs/library.md +339 -0
  56. package/docs/openscad.md +1 -1
  57. package/docs/parts-and-sheets.md +32 -0
  58. package/docs/shared-libraries.md +229 -0
  59. package/docs/specification.md +529 -0
  60. package/docs/versions.md +2 -2
  61. package/docs/webassembly.md +3 -1
  62. package/package.json +51 -4
@@ -0,0 +1,229 @@
1
+ # Shared libraries
2
+
3
+ A shared library is a folder of `.kerf` files with a `kerf.json` that names
4
+ it. You publish one by tagging a GitHub repository, or by putting a `.tar.gz`
5
+ anywhere that serves it over https. A project lists the libraries its designs
6
+ use in its own `kerf.json`. `kerf install` copies them into `kerf_libraries/`
7
+ and pins each one in `kerf.lock`, by its exact commit and a hash of its
8
+ files, so a design renders the same way on every machine, now and later.
9
+
10
+ The [standard library](library.md) is built into every renderer and needs
11
+ none of this: `use <kerf/box>` always works.
12
+
13
+ ## Use a library
14
+
15
+ In the folder that holds your designs:
16
+
17
+ ```sh
18
+ kerf add github:maker/kerf-feet@v1.0.0
19
+ ```
20
+
21
+ This creates a `kerf.json` the first time:
22
+
23
+ ```json
24
+ {
25
+ "libraries": {
26
+ "feet": "github:maker/kerf-feet@v1.0.0"
27
+ }
28
+ }
29
+ ```
30
+
31
+ The library's own `kerf.json` gives its name, here `feet`. Its files go into
32
+ `kerf_libraries/feet/`, and `kerf.lock` records exactly what was installed.
33
+ A design then loads them with the library's name first:
34
+
35
+ ```kerf
36
+ use <feet/feet>
37
+
38
+ part("base") difference() {
39
+ rect([100, 60]);
40
+ feet([100, 60]);
41
+ }
42
+ ```
43
+
44
+ `kerf render`, `kerf check`, the desktop app and the language server in your
45
+ editor all find it. The playground has the standard library only.
46
+
47
+ A source is one of:
48
+
49
+ | Source | Means |
50
+ | ----------------------------- | ----------------------------------------------------------------------------------- |
51
+ | `github:owner/repo@v1.0.0` | A GitHub repository at a tag, a branch or a commit; without `@`, its default branch |
52
+ | `https://…/feet-1.0.0.tar.gz` | A `.tar.gz` such as `kerf pack` writes, from any https link |
53
+ | `./libraries/feet` | A folder on this computer, for a library you are writing |
54
+
55
+ To change version, add it again with the new tag; `kerf add` replaces the
56
+ source. `kerf remove feet` takes a library out.
57
+
58
+ ## What to commit
59
+
60
+ Commit `kerf.json` and `kerf.lock`. Then either:
61
+
62
+ - commit `kerf_libraries/` too, so the project works without a download,
63
+ in the desktop app as well, or
64
+ - leave it out (add it to `.gitignore`) and run `kerf install` after each
65
+ clone. It downloads the exact files `kerf.lock` pins.
66
+
67
+ ## How a design finds a library
68
+
69
+ For a path that does not start with `kerf/`, a design looks in its project,
70
+ the nearest folder at or above the design's own with a `kerf.json`:
71
+
72
+ 1. `use <name/...>`, where `name` is the project's own name in its
73
+ `kerf.json`: a file of the project, from its top folder. A library uses
74
+ this for its own files, and so do the designs and examples inside it.
75
+ 2. `use <name/...>`, where `kerf_libraries/name/` exists: that installed
76
+ library's file.
77
+ 3. Anything else: a file in the design's folder or below it, as before.
78
+
79
+ Each path is tried as written and with `.kerf` added. A design in
80
+ `projects/shelf/designs/` therefore finds `use <feet/feet>` in
81
+ `projects/shelf/kerf_libraries/feet/feet.kerf` when `projects/shelf/` has
82
+ the `kerf.json`.
83
+
84
+ ## Reproducing a design
85
+
86
+ `kerf.lock` holds one entry for each library, including the ones other
87
+ libraries use:
88
+
89
+ ```json
90
+ {
91
+ "lockVersion": 1,
92
+ "libraries": {
93
+ "feet": {
94
+ "source": "github:maker/kerf-feet@v1.0.0",
95
+ "version": "1.0.0",
96
+ "resolved": "https://github.com/maker/kerf-feet/archive/3f2c…e91a.tar.gz",
97
+ "integrity": "sha256-4hC4hj++68l+quohmEdLL/KYF9lwtTD7Hon6G+5V9kY="
98
+ }
99
+ }
100
+ }
101
+ ```
102
+
103
+ - `resolved` is the exact commit. A tag can be moved to other code later; the
104
+ commit cannot, so `kerf install` downloads from here, not from the tag.
105
+ - `integrity` is a sha256 hash of the library's files. `kerf install` refuses
106
+ files that do not match it. The hash ignores line endings, so git's
107
+ conversions on Windows do not change it.
108
+
109
+ In CI, install exactly what is pinned, and fail if `kerf.lock` would need to
110
+ change:
111
+
112
+ ```sh
113
+ kerf install --frozen
114
+ kerf render designs/shelf.kerf -o shelf.svg
115
+ ```
116
+
117
+ `kerf install --check` downloads nothing. It checks that `kerf.json`,
118
+ `kerf.lock` and `kerf_libraries/` agree, and that no one has edited an
119
+ installed file. It exits with 1 and lists the differences when they do not.
120
+
121
+ The libraries are one part of a design's output; the renderer is the other.
122
+ Pin the `kerf` or npm package version too, as in [Versions](versions.md).
123
+
124
+ ## Libraries that use libraries
125
+
126
+ A library's `kerf.json` can name libraries of its own, and `kerf install`
127
+ installs them too. There is one copy of each name in `kerf_libraries/`. When
128
+ two libraries want the same library from different sources, the install
129
+ stops and asks you to choose. Add that library to your own `kerf.json`; the
130
+ project's choice wins:
131
+
132
+ ```json
133
+ {
134
+ "libraries": {
135
+ "stand": "github:maker/kerf-stand@v2.0.0",
136
+ "lamp": "github:other/kerf-lamp@v1.4.0",
137
+ "feet": "github:maker/kerf-feet@v1.1.0"
138
+ }
139
+ }
140
+ ```
141
+
142
+ ## Publish a library
143
+
144
+ A library's top folder holds its `kerf.json`:
145
+
146
+ ```json
147
+ {
148
+ "name": "feet",
149
+ "version": "1.0.0",
150
+ "description": "Holes for rubber feet",
151
+ "license": "MIT",
152
+ "language": "0.2",
153
+ "libraries": {}
154
+ }
155
+ ```
156
+
157
+ | Field | Use |
158
+ | ------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
159
+ | `name` | Required. Lowercase letters, digits, `-` and `_`. Designs write `use <name/...>`. `kerf` is the standard library's |
160
+ | `version` | Required to pack. `1.2.0`: raise the first number when a module's parameters change, or when existing designs would cut differently |
161
+ | `description` | One line on what it makes |
162
+ | `license` | The licence others may use it under |
163
+ | `language` | The oldest KerfScript language version it works with; older `kerf` versions refuse to install it |
164
+ | `libraries` | Libraries it uses, from `github:` or `https:` sources |
165
+
166
+ A library is its `.kerf` files in any folder, its `kerf.json`, and the
167
+ `README`, `LICENSE` and `CHANGELOG` in its top folder. Pictures, tests and
168
+ dot folders such as `.github` are left out when it is installed.
169
+
170
+ Inside the library, load its own files by its name, `use <feet/size>`, not
171
+ `use <size>`. That works while you write it, since the project is named
172
+ `feet`, and once it is installed next to someone else's designs. Put a
173
+ comment above each module: editors show it on hover, as for the
174
+ [standard library](library.md).
175
+
176
+ Before each release, run:
177
+
178
+ ```sh
179
+ kerf pack
180
+ ```
181
+
182
+ It parses every `.kerf` file and checks every `use <...>`. Each must name the
183
+ standard library, the library's own files as `<name/...>`, or a library in
184
+ its `kerf.json`. Then it writes `feet-1.0.0.tar.gz`. To publish:
185
+
186
+ 1. On GitHub, commit, then tag the version and push the tag:
187
+ `git tag v1.0.0 && git push origin v1.0.0`. Others run
188
+ `kerf add github:you/kerf-feet@v1.0.0`.
189
+ 2. Anywhere else, upload the `.tar.gz` (a GitHub release asset works too), and
190
+ others run `kerf add https://…/feet-1.0.0.tar.gz`.
191
+
192
+ Do not move a tag or replace a file once it is published. The projects that
193
+ pinned it would no longer install; publish a new version instead.
194
+
195
+ ## Safety
196
+
197
+ A KerfScript program cannot read files or reach the network, so a library can
198
+ only make shapes and run within the renderer's step limits. `kerf install`
199
+ writes only inside `kerf_libraries/`. It refuses an archive with a file that
200
+ would land outside its folder, a plain `http://` link (except on this
201
+ computer), and a download over 20 MB. The desktop app reads only `.kerf`
202
+ files, and only from the design's folder and the project's.
203
+
204
+ ## From JavaScript
205
+
206
+ The `@mylasertools/kerfscript/libraries` module, for Node.js, finds and
207
+ installs libraries the same way the command line does:
208
+
209
+ ```js
210
+ import { render } from '@mylasertools/kerfscript';
211
+ import { resolver, install, check } from '@mylasertools/kerfscript/libraries';
212
+ import { readFileSync } from 'node:fs';
213
+
214
+ await install('projects/shelf', { frozen: true });
215
+ const file = 'projects/shelf/designs/shelf.kerf';
216
+ const result = render(readFileSync(file, 'utf8'), { resolve: resolver(file) });
217
+ ```
218
+
219
+ | Export | Does |
220
+ | ----------------------------------------- | ---------------------------------------------------------------- |
221
+ | `resolver(designFile)` | A `resolve` option for `render()` that finds libraries as above |
222
+ | `libraryFile(folder, path)` | The file `use <path>` names for a design in `folder` |
223
+ | `findProject(folder)` | The nearest folder at or above `folder` with a `kerf.json` |
224
+ | `install(root, { frozen, fetch, log })` | `kerf install`; resolves to the new `kerf.lock` |
225
+ | `add(root, source)`, `remove(root, name)` | `kerf add`, `kerf remove` |
226
+ | `check(root)` | `kerf install --check`: a list of problems, empty when all agree |
227
+ | `pack(root, out?)` | `kerf pack` |
228
+
229
+ Problems are thrown as `LibraryError`, with a message to show as it is.