@napi-rs/cli 3.7.4 → 3.8.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 (43) hide show
  1. package/README.md +33 -0
  2. package/cli.mjs +9 -12
  3. package/dist/cli.js +9248 -1311
  4. package/dist/index.cjs +9268 -1331
  5. package/dist/index.d.cts +175 -157
  6. package/dist/index.d.ts +175 -157
  7. package/dist/index.js +9248 -1311
  8. package/docs/artifacts.md +33 -0
  9. package/docs/build.md +58 -0
  10. package/docs/create-npm-dirs.md +32 -0
  11. package/docs/new.md +39 -0
  12. package/docs/pre-publish.md +37 -0
  13. package/docs/rename.md +37 -0
  14. package/docs/universalize.md +31 -0
  15. package/docs/version.md +31 -0
  16. package/docs/wasi.md +161 -0
  17. package/package.json +11 -10
  18. package/src/api/__tests__/__snapshots__/templates.spec.ts.md +1019 -114
  19. package/src/api/__tests__/__snapshots__/templates.spec.ts.snap +0 -0
  20. package/src/api/__tests__/artifacts.spec.ts +17 -0
  21. package/src/api/__tests__/create-npm-dirs.spec.ts +14 -15
  22. package/src/api/__tests__/new.spec.ts +41 -5
  23. package/src/api/artifacts.ts +934 -108
  24. package/src/api/build.ts +1317 -188
  25. package/src/api/create-npm-dirs.ts +472 -101
  26. package/src/api/new.ts +354 -35
  27. package/src/api/pre-publish.ts +2845 -99
  28. package/src/api/rename.ts +1638 -124
  29. package/src/api/templates/js-binding.ts +216 -38
  30. package/src/api/templates/load-wasi-template.ts +1641 -111
  31. package/src/api/templates/wasi-worker-template.ts +32 -3
  32. package/src/commands/rename.ts +8 -3
  33. package/src/def/artifacts.ts +2 -2
  34. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +2 -2
  35. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  36. package/src/utils/__tests__/misc.spec.ts +124 -3
  37. package/src/utils/__tests__/version.spec.ts +47 -1
  38. package/src/utils/config.ts +15 -2
  39. package/src/utils/metadata.ts +155 -47
  40. package/src/utils/misc.ts +6079 -0
  41. package/src/utils/target.ts +66 -8
  42. package/src/utils/typegen.ts +1223 -63
  43. package/src/utils/version.ts +96 -0
@@ -1,3 +1,5 @@
1
+ import { Comparator, Range, minVersion, subset } from 'semver'
2
+
1
3
  export enum NapiVersion {
2
4
  Napi1 = 1,
3
5
  Napi2,
@@ -31,6 +33,10 @@ export const SUPPORTED_NAPI_VERSIONS = Object.values(NapiVersion).filter(
31
33
  (v): v is NapiVersion => typeof v === 'number',
32
34
  )
33
35
 
36
+ // emnapi v2 is ESM-only. These are the Node.js lines where require(esm) is
37
+ // enabled by default without an experimental warning.
38
+ export const MINIMUM_WASI_NODE_VERSION = '^20.19.0 || ^22.13.0 || >=23.5.0'
39
+
34
40
  interface NodeVersion {
35
41
  major: number
36
42
  minor: number
@@ -82,3 +88,93 @@ function toEngineRequirement(versions: NodeVersion[]): string {
82
88
  export function napiEngineRequirement(napiVersion: NapiVersion): string {
83
89
  return toEngineRequirement(requiredNodeVersions(napiVersion))
84
90
  }
91
+
92
+ export function restrictWasiNodeEngine(nodeRange: string) {
93
+ try {
94
+ if (subset(nodeRange, MINIMUM_WASI_NODE_VERSION)) {
95
+ return nodeRange
96
+ }
97
+
98
+ if (subset(MINIMUM_WASI_NODE_VERSION, nodeRange)) {
99
+ return MINIMUM_WASI_NODE_VERSION
100
+ }
101
+
102
+ const supportedRangeSets = new Range(MINIMUM_WASI_NODE_VERSION).set
103
+ const restrictedRangeSets = new Range(nodeRange).set
104
+ .flatMap((comparators) =>
105
+ supportedRangeSets.map((supportedComparators) =>
106
+ normalizeComparatorSet([...comparators, ...supportedComparators]),
107
+ ),
108
+ )
109
+ .filter(
110
+ (candidate): candidate is string =>
111
+ candidate !== undefined && minVersion(candidate) !== null,
112
+ )
113
+
114
+ if (restrictedRangeSets.length > 0) {
115
+ return restrictedRangeSets.join(' || ')
116
+ }
117
+ } catch {
118
+ // Fall back to the supported WASI floor for malformed ranges.
119
+ return MINIMUM_WASI_NODE_VERSION
120
+ }
121
+
122
+ // The declared range is valid but disjoint from the WASI floor. Broadening
123
+ // it here would publish metadata claiming support for Node.js versions the
124
+ // package explicitly excluded, so fail loudly instead.
125
+ throw new Error(
126
+ `Cannot restrict engines.node "${nodeRange}" to the Node.js versions supported by WASI packages: it does not intersect "${MINIMUM_WASI_NODE_VERSION}". Broaden engines.node to include a supported Node.js version or remove the WASI targets.`,
127
+ )
128
+ }
129
+
130
+ function normalizeComparatorSet(comparators: Comparator[]) {
131
+ const exactMatch = comparators.find(({ operator }) => operator === '')
132
+ if (exactMatch) {
133
+ return comparators.every((comparator) => comparator.test(exactMatch.semver))
134
+ ? exactMatch.value
135
+ : undefined
136
+ }
137
+
138
+ let lowerBound: Comparator | undefined
139
+ let upperBound: Comparator | undefined
140
+
141
+ for (const rawComparator of comparators) {
142
+ const comparator = stabilizePrereleaseComparator(rawComparator)
143
+ if (comparator.operator === '>' || comparator.operator === '>=') {
144
+ if (
145
+ !lowerBound ||
146
+ comparator.semver.compare(lowerBound.semver) > 0 ||
147
+ (comparator.semver.compare(lowerBound.semver) === 0 &&
148
+ comparator.operator === '>')
149
+ ) {
150
+ lowerBound = comparator
151
+ }
152
+ } else if (comparator.operator === '<' || comparator.operator === '<=') {
153
+ if (
154
+ !upperBound ||
155
+ comparator.semver.compare(upperBound.semver) < 0 ||
156
+ (comparator.semver.compare(upperBound.semver) === 0 &&
157
+ comparator.operator === '<')
158
+ ) {
159
+ upperBound = comparator
160
+ }
161
+ }
162
+ }
163
+
164
+ return [lowerBound?.value, upperBound?.value].filter(Boolean).join(' ')
165
+ }
166
+
167
+ function stabilizePrereleaseComparator(comparator: Comparator) {
168
+ if (comparator.semver.prerelease.length === 0) {
169
+ return comparator
170
+ }
171
+
172
+ const stableVersion = `${comparator.semver.major}.${comparator.semver.minor}.${comparator.semver.patch}`
173
+ if (comparator.operator === '>' || comparator.operator === '>=') {
174
+ return new Comparator(`>=${stableVersion}`)
175
+ }
176
+ if (comparator.operator === '<' || comparator.operator === '<=') {
177
+ return new Comparator(`<${stableVersion}-0`)
178
+ }
179
+ return comparator
180
+ }