@napi-rs/cli 3.7.2 → 3.7.3

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 (40) hide show
  1. package/dist/cli.js +201 -52
  2. package/dist/index.cjs +200 -51
  3. package/dist/index.d.cts +4 -4
  4. package/dist/index.d.ts +4 -4
  5. package/dist/index.js +201 -52
  6. package/package.json +3 -3
  7. package/src/api/__tests__/__snapshots__/templates.spec.ts.md +353 -0
  8. package/src/api/__tests__/__snapshots__/templates.spec.ts.snap +0 -0
  9. package/src/api/__tests__/android-build.spec.ts +113 -0
  10. package/src/api/__tests__/artifacts.spec.ts +95 -0
  11. package/src/api/__tests__/build-regressions.spec.ts +128 -0
  12. package/src/api/__tests__/build.spec.ts +1020 -0
  13. package/src/api/__tests__/create-npm-dirs.spec.ts +561 -0
  14. package/src/api/__tests__/new-node-api-version.spec.ts +87 -0
  15. package/src/api/__tests__/new.spec.ts +527 -0
  16. package/src/api/__tests__/rename.spec.ts +181 -0
  17. package/src/api/__tests__/templates.spec.ts +164 -0
  18. package/src/api/__tests__/universalize.spec.ts +128 -0
  19. package/src/api/artifacts.ts +7 -16
  20. package/src/api/build.ts +346 -105
  21. package/src/api/new.ts +65 -1
  22. package/src/commands/__tests__/new.spec.ts +13 -0
  23. package/src/def/build.ts +6 -6
  24. package/src/utils/__tests__/__fixtures__/napi_type_def +202 -0
  25. package/src/utils/__tests__/__fixtures__/runtime_string_enum_flag +2 -0
  26. package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
  27. package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
  28. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +180 -0
  29. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  30. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +1197 -0
  31. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  32. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +22 -0
  33. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  34. package/src/utils/__tests__/config.spec.ts +81 -0
  35. package/src/utils/__tests__/metadata.spec.ts +28 -0
  36. package/src/utils/__tests__/misc.spec.ts +56 -0
  37. package/src/utils/__tests__/target.spec.ts +19 -0
  38. package/src/utils/__tests__/typegen.spec.ts +85 -0
  39. package/src/utils/__tests__/version.spec.ts +7 -0
  40. package/src/utils/version.ts +2 -0
@@ -0,0 +1,128 @@
1
+ import { existsSync } from 'node:fs'
2
+ import { mkdir, rm, writeFile } from 'node:fs/promises'
3
+ import { tmpdir } from 'node:os'
4
+ import { join } from 'node:path'
5
+
6
+ import ava, { type TestFn } from 'ava'
7
+
8
+ import { buildProject, generateTypeDef } from '../build.js'
9
+
10
+ const test = ava as TestFn<{
11
+ tmpDir: string
12
+ projectDir: string
13
+ typeDefDir: string
14
+ }>
15
+
16
+ test.beforeEach(async (t) => {
17
+ const tmpDir = join(
18
+ tmpdir(),
19
+ 'napi-rs-test',
20
+ `build-regressions-${Date.now()}-${Math.random().toString(36).slice(2)}`,
21
+ )
22
+ const projectDir = join(tmpDir, 'project')
23
+ const typeDefDir = join(projectDir, 'type-def')
24
+
25
+ await mkdir(typeDefDir, { recursive: true })
26
+ t.context = { tmpDir, projectDir, typeDefDir }
27
+ })
28
+
29
+ test.afterEach.always(async (t) => {
30
+ if (existsSync(t.context.tmpDir)) {
31
+ await rm(t.context.tmpDir, { recursive: true, force: true })
32
+ }
33
+ })
34
+
35
+ async function writeMixedTargetProject(projectDir: string) {
36
+ await mkdir(join(projectDir, 'src'), { recursive: true })
37
+ await writeFile(
38
+ join(projectDir, 'Cargo.toml'),
39
+ `[package]
40
+ name = "build_regression"
41
+ version = "0.1.0"
42
+ edition = "2021"
43
+
44
+ [lib]
45
+ crate-type = ["cdylib"]
46
+
47
+ [[bin]]
48
+ name = "selected-bin"
49
+ path = "src/main.rs"
50
+ `,
51
+ )
52
+ await writeFile(join(projectDir, 'src', 'lib.rs'), 'pub fn library() {}\n')
53
+ await writeFile(join(projectDir, 'src', 'main.rs'), 'fn main() {}\n')
54
+ await writeFile(
55
+ join(projectDir, 'package.json'),
56
+ JSON.stringify({
57
+ name: 'build-regression',
58
+ version: '0.1.0',
59
+ napi: { binaryName: 'build-regression' },
60
+ }),
61
+ )
62
+ }
63
+
64
+ test.serial(
65
+ '`--bin` copies the selected executable from a mixed cdylib/bin crate',
66
+ async (t) => {
67
+ const { projectDir } = t.context
68
+ await writeMixedTargetProject(projectDir)
69
+
70
+ const outputs = await (async () => {
71
+ const originalCargo = process.env.CARGO
72
+ delete process.env.CARGO
73
+ try {
74
+ return await (
75
+ await buildProject({
76
+ cwd: projectDir,
77
+ bin: 'selected-bin',
78
+ outputDir: 'dist',
79
+ })
80
+ ).task
81
+ } finally {
82
+ if (originalCargo === undefined) {
83
+ delete process.env.CARGO
84
+ } else {
85
+ process.env.CARGO = originalCargo
86
+ }
87
+ }
88
+ })()
89
+
90
+ const executableName =
91
+ process.platform === 'win32' ? 'selected-bin.exe' : 'selected-bin'
92
+ const executablePath = join(projectDir, 'dist', executableName)
93
+
94
+ t.deepEqual(outputs, [{ kind: 'exe', path: executablePath }])
95
+ t.true(existsSync(executablePath))
96
+ t.false(existsSync(join(projectDir, 'dist', 'build-regression.node')))
97
+ },
98
+ )
99
+
100
+ test.serial(
101
+ 'an explicit dtsHeaderFile takes precedence over config and inline headers',
102
+ async (t) => {
103
+ const { projectDir, typeDefDir } = t.context
104
+ await Promise.all([
105
+ writeFile(join(projectDir, 'explicit-header.d.ts'), '// explicit\n'),
106
+ writeFile(join(projectDir, 'config-header.d.ts'), '// config file\n'),
107
+ writeFile(
108
+ join(typeDefDir, 'value.type'),
109
+ '{"kind":"fn","name":"value","def":"function value(): void"}\n',
110
+ ),
111
+ ])
112
+
113
+ const { dts } = await generateTypeDef({
114
+ cwd: projectDir,
115
+ typeDefDir,
116
+ dtsHeaderFile: 'explicit-header.d.ts',
117
+ configDtsHeaderFile: 'config-header.d.ts',
118
+ dtsHeader: '// explicit inline\n',
119
+ configDtsHeader: '// config inline\n',
120
+ })
121
+
122
+ t.true(dts.startsWith('// explicit\n'))
123
+ t.false(dts.includes('// config file'))
124
+ t.false(dts.includes('// explicit inline'))
125
+ t.false(dts.includes('// config inline'))
126
+ t.true(dts.includes('function value(): void'))
127
+ },
128
+ )