@napi-rs/cli 3.0.0-alpha.17 → 3.0.0-alpha.19

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 (67) hide show
  1. package/dist/api/build.js +1 -1
  2. package/dist/api/build.js.map +1 -1
  3. package/dist/api/create-npm-dirs.js +2 -2
  4. package/dist/api/create-npm-dirs.js.map +1 -1
  5. package/dist/api/pre-publish.js +3 -3
  6. package/dist/api/pre-publish.js.map +1 -1
  7. package/dist/api/rename.js +8 -0
  8. package/dist/api/rename.js.map +1 -1
  9. package/dist/api/universalize.js +1 -1
  10. package/dist/api/universalize.js.map +1 -1
  11. package/dist/api/version.js +1 -1
  12. package/dist/api/version.js.map +1 -1
  13. package/dist/def/artifacts.d.ts +11 -1
  14. package/dist/def/artifacts.js +5 -1
  15. package/dist/def/artifacts.js.map +1 -1
  16. package/dist/def/build.d.ts +9 -3
  17. package/dist/def/build.js +7 -3
  18. package/dist/def/build.js.map +1 -1
  19. package/dist/def/create-npm-dirs.d.ts +10 -0
  20. package/dist/def/create-npm-dirs.js +4 -0
  21. package/dist/def/create-npm-dirs.js.map +1 -1
  22. package/dist/def/new.d.ts +2 -2
  23. package/dist/def/pre-publish.d.ts +10 -0
  24. package/dist/def/pre-publish.js +5 -1
  25. package/dist/def/pre-publish.js.map +1 -1
  26. package/dist/def/rename.d.ts +11 -1
  27. package/dist/def/rename.js +5 -1
  28. package/dist/def/rename.js.map +1 -1
  29. package/dist/def/universalize.d.ts +10 -0
  30. package/dist/def/universalize.js +4 -0
  31. package/dist/def/universalize.js.map +1 -1
  32. package/dist/def/version.d.ts +10 -0
  33. package/dist/def/version.js +4 -0
  34. package/dist/def/version.js.map +1 -1
  35. package/dist/index.cjs +82 -20
  36. package/dist/utils/__tests__/config.spec.d.ts +1 -0
  37. package/dist/utils/__tests__/config.spec.js +63 -0
  38. package/dist/utils/__tests__/config.spec.js.map +1 -0
  39. package/dist/utils/config.d.ts +2 -3
  40. package/dist/utils/config.js +33 -3
  41. package/dist/utils/config.js.map +1 -1
  42. package/dist/utils/version.d.ts +2 -1
  43. package/dist/utils/version.js +10 -8
  44. package/dist/utils/version.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/api/build.ts +4 -1
  47. package/src/api/create-npm-dirs.ts +5 -2
  48. package/src/api/pre-publish.ts +6 -3
  49. package/src/api/rename.ts +9 -0
  50. package/src/api/universalize.ts +4 -1
  51. package/src/api/version.ts +4 -1
  52. package/src/def/artifacts.ts +11 -2
  53. package/src/def/build.ts +15 -6
  54. package/src/def/create-npm-dirs.ts +9 -0
  55. package/src/def/new.ts +1 -1
  56. package/src/def/pre-publish.ts +10 -1
  57. package/src/def/rename.ts +11 -2
  58. package/src/def/universalize.ts +9 -0
  59. package/src/def/version.ts +9 -0
  60. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  61. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  62. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  63. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +8 -7
  64. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  65. package/src/utils/__tests__/config.spec.ts +81 -0
  66. package/src/utils/config.ts +43 -4
  67. package/src/utils/version.ts +10 -8
@@ -0,0 +1,81 @@
1
+ import { unlink, writeFile } from 'node:fs/promises'
2
+ import { tmpdir } from 'node:os'
3
+ import { join } from 'node:path'
4
+
5
+ import ava, { TestFn } from 'ava'
6
+
7
+ import {
8
+ CommonPackageJsonFields,
9
+ UserNapiConfig,
10
+ readNapiConfig,
11
+ } from '../config.js'
12
+
13
+ const NON_EXISTENT_FILE = 'non-existent-file'
14
+
15
+ const test = ava as TestFn<{
16
+ configPath: string
17
+ packageJson: string
18
+ pkgJson: CommonPackageJsonFields
19
+ config: UserNapiConfig
20
+ }>
21
+
22
+ test.before(async (t) => {
23
+ const tmp = tmpdir()
24
+ const configPath = join(tmp, 'napi.json')
25
+ const packageJson = join(tmp, 'package.json')
26
+ const pkgJson = {
27
+ name: '@napi-rs/testing',
28
+ version: '0.0.0',
29
+ napi: {
30
+ binaryName: 'testing',
31
+ packageName: '@napi-rs/testing',
32
+ targets: [
33
+ 'x86_64-unknown-linux-gnu',
34
+ 'x86_64-pc-windows-msvc',
35
+ 'x86_64-apple-darwin',
36
+ ],
37
+ },
38
+ }
39
+ await writeFile(packageJson, JSON.stringify(pkgJson, null, 2))
40
+ const config = {
41
+ binaryName: 'testing',
42
+ packageName: '@node-rs/testing',
43
+ targets: [
44
+ 'x86_64-unknown-linux-gnu',
45
+ 'x86_64-apple-darwin',
46
+ 'aarch64-apple-darwin',
47
+ ],
48
+ }
49
+ await writeFile(configPath, JSON.stringify(config, null, 2))
50
+ t.context = { configPath, config, packageJson, pkgJson }
51
+ })
52
+
53
+ test.after(async (t) => {
54
+ await unlink(t.context.configPath)
55
+ await unlink(t.context.packageJson)
56
+ })
57
+
58
+ test('should throw if package.json not found', async (t) => {
59
+ await t.throwsAsync(() => readNapiConfig(NON_EXISTENT_FILE), {
60
+ message: `package.json not found at ${NON_EXISTENT_FILE}`,
61
+ })
62
+ })
63
+
64
+ test('should throw if napi.json not found', async (t) => {
65
+ const { packageJson } = t.context
66
+ await t.throwsAsync(() => readNapiConfig(packageJson, NON_EXISTENT_FILE), {
67
+ message: `NAPI-RS config not found at ${NON_EXISTENT_FILE}`,
68
+ })
69
+ })
70
+
71
+ test('should be able to read config from package.json', async (t) => {
72
+ const { packageJson } = t.context
73
+ const config = await readNapiConfig(packageJson)
74
+ t.snapshot(config)
75
+ })
76
+
77
+ test('should be able to read config from napi.json', async (t) => {
78
+ const { packageJson, configPath } = t.context
79
+ const config = await readNapiConfig(packageJson, configPath)
80
+ t.snapshot(config)
81
+ })
@@ -1,9 +1,10 @@
1
+ import { underline, yellow } from 'colorette'
1
2
  import { merge, omit } from 'lodash-es'
2
3
 
3
4
  import { fileExists, readFileAsync } from './misc.js'
4
5
  import { DEFAULT_TARGETS, parseTriple, Target } from './target.js'
5
6
 
6
- interface UserNapiConfig {
7
+ export interface UserNapiConfig {
7
8
  /**
8
9
  * Name of the binary to be generated, default to `index`
9
10
  */
@@ -89,9 +90,15 @@ export type NapiConfig = Required<
89
90
  packageJson: CommonPackageJsonFields
90
91
  }
91
92
 
92
- export async function readNapiConfig(path: string): Promise<NapiConfig> {
93
+ export async function readNapiConfig(
94
+ path: string,
95
+ configPath?: string,
96
+ ): Promise<NapiConfig> {
97
+ if (configPath && !(await fileExists(configPath))) {
98
+ throw new Error(`NAPI-RS config not found at ${configPath}`)
99
+ }
93
100
  if (!(await fileExists(path))) {
94
- throw new Error(`napi-rs config not found at ${path}`)
101
+ throw new Error(`package.json not found at ${path}`)
95
102
  }
96
103
  // May support multiple config sources later on.
97
104
  const content = await readFileAsync(path, 'utf8')
@@ -99,12 +106,34 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
99
106
  try {
100
107
  pkgJson = JSON.parse(content) as CommonPackageJsonFields
101
108
  } catch (e) {
102
- throw new Error('Failed to parse napi-rs config', {
109
+ throw new Error(`Failed to parse package.json at ${path}`, {
103
110
  cause: e,
104
111
  })
105
112
  }
106
113
 
114
+ let separatedConfig: UserNapiConfig | undefined
115
+ if (configPath) {
116
+ const configContent = await readFileAsync(configPath, 'utf8')
117
+ try {
118
+ separatedConfig = JSON.parse(configContent) as UserNapiConfig
119
+ } catch (e) {
120
+ throw new Error(`Failed to parse NAPI-RS config at ${configPath}`, {
121
+ cause: e,
122
+ })
123
+ }
124
+ }
125
+
107
126
  const userNapiConfig = pkgJson.napi ?? {}
127
+ if (pkgJson.napi && separatedConfig) {
128
+ const pkgJsonPath = underline(path)
129
+ const configPathUnderline = underline(configPath!)
130
+ console.warn(
131
+ yellow(
132
+ `Both napi field in ${pkgJsonPath} and [NAPI-RS config](${configPathUnderline}) file are found, the NAPI-RS config file will be used.`,
133
+ ),
134
+ )
135
+ Object.assign(userNapiConfig, separatedConfig)
136
+ }
108
137
  const napiConfig: NapiConfig = merge(
109
138
  {
110
139
  binaryName: 'index',
@@ -120,16 +149,26 @@ export async function readNapiConfig(path: string): Promise<NapiConfig> {
120
149
 
121
150
  // compatible with old config
122
151
  if (userNapiConfig?.name) {
152
+ console.warn(
153
+ `[DEPRECATED] napi.name is deprecated, use napi.binaryName instead.`,
154
+ )
123
155
  napiConfig.binaryName = userNapiConfig.name
124
156
  }
125
157
 
126
158
  if (!targets.length) {
159
+ let deprecatedWarned = false
160
+ const warning = `[DEPRECATED] napi.triples is deprecated, use napi.targets instead.`
127
161
  if (userNapiConfig.triples?.defaults) {
162
+ deprecatedWarned = true
163
+ console.warn(warning)
128
164
  targets = targets.concat(DEFAULT_TARGETS)
129
165
  }
130
166
 
131
167
  if (userNapiConfig.triples?.additional?.length) {
132
168
  targets = targets.concat(userNapiConfig.triples.additional)
169
+ if (!deprecatedWarned) {
170
+ console.warn(warning)
171
+ }
133
172
  }
134
173
  }
135
174
 
@@ -7,20 +7,22 @@ export enum NapiVersion {
7
7
  Napi6,
8
8
  Napi7,
9
9
  Napi8,
10
+ Napi9,
10
11
  }
11
12
 
12
13
  /// because node support new napi version in some minor version updates, so we might meet such situation:
13
14
  /// `node v10.20.0` supports `napi5` and `napi6`, but `node v12.0.0` only support `napi4`,
14
- /// by which, we can not tell directly napi version supportness from node version directly.
15
+ /// by which, we can not tell directly napi version supportless from node version directly.
15
16
  const NAPI_VERSION_MATRIX = new Map<NapiVersion, string>([
16
- [NapiVersion.Napi1, '8.6.0'],
17
- [NapiVersion.Napi2, '8.10.0 | 9.3.0'],
18
- [NapiVersion.Napi3, '6.14.2 | 8.11.2 | 9.11.0'],
19
- [NapiVersion.Napi4, '10.16.0 | 11.8.0'],
20
- [NapiVersion.Napi5, '10.17.0 | 12.11.0'],
17
+ [NapiVersion.Napi1, '8.6.0 | 9.0.0 | 10.0.0'],
18
+ [NapiVersion.Napi2, '8.10.0 | 9.3.0 | 10.0.0'],
19
+ [NapiVersion.Napi3, '6.14.2 | 8.11.2 | 9.11.0 | 10.0.0'],
20
+ [NapiVersion.Napi4, '10.16.0 | 11.8.0 | 12.0.0'],
21
+ [NapiVersion.Napi5, '10.17.0 | 12.11.0 | 13.0.0'],
21
22
  [NapiVersion.Napi6, '10.20.0 | 12.17.0 | 14.0.0'],
22
- [NapiVersion.Napi7, '10.23.0 | 12.19.0 | 14.12.0'],
23
- [NapiVersion.Napi8, '12.22.0 | 14.17.0 | 15.12.0'],
23
+ [NapiVersion.Napi7, '10.23.0 | 12.19.0 | 14.12.0 | 15.0.0'],
24
+ [NapiVersion.Napi8, '12.22.0 | 14.17.0 | 15.12.0 | 16.0.0'],
25
+ [NapiVersion.Napi9, '18.17.0 | 20.3.0 | 21.1.0'],
24
26
  ])
25
27
 
26
28
  interface NodeVersion {