@napi-rs/cli 2.15.2 → 3.0.0-alpha.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 (194) hide show
  1. package/README.md +17 -78
  2. package/cli.mjs +12 -0
  3. package/dist/api/artifacts.d.ts +2 -0
  4. package/dist/api/artifacts.js +50 -0
  5. package/dist/api/build.d.ts +14 -0
  6. package/dist/api/build.js +404 -0
  7. package/dist/api/create-npm-dirs.d.ts +2 -0
  8. package/dist/api/create-npm-dirs.js +63 -0
  9. package/dist/api/new.d.ts +4 -0
  10. package/dist/api/new.js +154 -0
  11. package/dist/api/pre-publish.d.ts +2 -0
  12. package/dist/api/pre-publish.js +157 -0
  13. package/dist/api/rename.d.ts +2 -0
  14. package/dist/api/rename.js +28 -0
  15. package/dist/api/templates/.gitignore.d.ts +1 -0
  16. package/dist/api/templates/.gitignore.js +198 -0
  17. package/dist/api/templates/.npmignore.d.ts +1 -0
  18. package/dist/api/templates/.npmignore.js +14 -0
  19. package/dist/api/templates/build.rs.d.ts +1 -0
  20. package/dist/api/templates/build.rs.js +4 -0
  21. package/dist/api/templates/cargo.toml.d.ts +6 -0
  22. package/dist/api/templates/cargo.toml.js +25 -0
  23. package/dist/api/templates/ci-template.d.ts +1 -0
  24. package/dist/api/templates/ci-template.js +566 -0
  25. package/dist/api/templates/ci.yml.d.ts +1 -0
  26. package/dist/api/templates/ci.yml.js +92 -0
  27. package/dist/api/templates/index.d.ts +8 -0
  28. package/dist/api/templates/index.js +8 -0
  29. package/dist/api/templates/js-binding.d.ts +1 -0
  30. package/dist/api/templates/js-binding.js +124 -0
  31. package/dist/api/templates/lib.rs.d.ts +1 -0
  32. package/dist/api/templates/lib.rs.js +10 -0
  33. package/dist/api/templates/package.json.d.ts +8 -0
  34. package/dist/api/templates/package.json.js +30 -0
  35. package/dist/api/universalize.d.ts +2 -0
  36. package/dist/api/universalize.js +38 -0
  37. package/dist/api/version.d.ts +2 -0
  38. package/dist/api/version.js +16 -0
  39. package/dist/cli.d.ts +1 -0
  40. package/dist/cli.js +25 -0
  41. package/dist/commands/artifacts.d.ts +6 -0
  42. package/dist/commands/artifacts.js +19 -0
  43. package/dist/commands/build.d.ts +6 -0
  44. package/dist/commands/build.js +34 -0
  45. package/dist/commands/create-npm-dirs.d.ts +4 -0
  46. package/dist/commands/create-npm-dirs.js +7 -0
  47. package/dist/commands/help.d.ts +10 -0
  48. package/dist/commands/help.js +12 -0
  49. package/dist/commands/new.d.ts +12 -0
  50. package/dist/commands/new.js +112 -0
  51. package/dist/commands/pre-publish.d.ts +4 -0
  52. package/dist/commands/pre-publish.js +8 -0
  53. package/dist/commands/rename.d.ts +4 -0
  54. package/dist/commands/rename.js +7 -0
  55. package/dist/commands/universalize.d.ts +4 -0
  56. package/dist/commands/universalize.js +7 -0
  57. package/dist/commands/version.d.ts +4 -0
  58. package/dist/commands/version.js +7 -0
  59. package/dist/def/artifacts.d.ts +50 -0
  60. package/dist/def/artifacts.js +38 -0
  61. package/dist/def/build.d.ts +150 -0
  62. package/dist/def/build.js +105 -0
  63. package/dist/def/commands/artifacts.d.ts +15 -0
  64. package/dist/def/commands/artifacts.js +29 -0
  65. package/dist/def/commands/build.d.ts +53 -0
  66. package/dist/def/commands/build.js +105 -0
  67. package/dist/def/commands/create-npm-dirs.d.ts +15 -0
  68. package/dist/def/commands/create-npm-dirs.js +29 -0
  69. package/dist/def/commands/new.d.ts +27 -0
  70. package/dist/def/commands/new.js +53 -0
  71. package/dist/def/commands/pre-publish.d.ts +23 -0
  72. package/dist/def/commands/pre-publish.js +45 -0
  73. package/dist/def/commands/rename.d.ts +25 -0
  74. package/dist/def/commands/rename.js +49 -0
  75. package/dist/def/commands/universalize.d.ts +13 -0
  76. package/dist/def/commands/universalize.js +25 -0
  77. package/dist/def/commands/version.d.ts +13 -0
  78. package/dist/def/commands/version.js +25 -0
  79. package/dist/def/create-npm-dirs.d.ts +50 -0
  80. package/dist/def/create-npm-dirs.js +38 -0
  81. package/dist/def/new.d.ts +106 -0
  82. package/dist/def/new.js +66 -0
  83. package/dist/def/options/artifacts.d.ts +35 -0
  84. package/dist/def/options/artifacts.js +11 -0
  85. package/dist/def/options/build.d.ts +97 -0
  86. package/dist/def/options/build.js +3 -0
  87. package/dist/def/options/create-npm-dirs.d.ts +35 -0
  88. package/dist/def/options/create-npm-dirs.js +11 -0
  89. package/dist/def/options/new.d.ts +79 -0
  90. package/dist/def/options/new.js +15 -0
  91. package/dist/def/options/pre-publish.d.ts +65 -0
  92. package/dist/def/options/pre-publish.js +13 -0
  93. package/dist/def/options/rename.d.ts +75 -0
  94. package/dist/def/options/rename.js +11 -0
  95. package/dist/def/options/universalize.d.ts +28 -0
  96. package/dist/def/options/universalize.js +10 -0
  97. package/dist/def/options/version.d.ts +28 -0
  98. package/dist/def/options/version.js +10 -0
  99. package/dist/def/pre-publish.d.ts +88 -0
  100. package/dist/def/pre-publish.js +56 -0
  101. package/dist/def/rename.d.ts +100 -0
  102. package/dist/def/rename.js +58 -0
  103. package/dist/def/universalize.d.ts +41 -0
  104. package/dist/def/universalize.js +33 -0
  105. package/dist/def/version.d.ts +41 -0
  106. package/dist/def/version.js +33 -0
  107. package/dist/index.cjs +20641 -0
  108. package/dist/index.d.ts +30 -0
  109. package/dist/index.js +30 -0
  110. package/dist/utils/__tests__/target.spec.d.ts +1 -0
  111. package/dist/utils/__tests__/target.spec.js +10 -0
  112. package/dist/utils/__tests__/typegen.spec.d.ts +1 -0
  113. package/dist/utils/__tests__/typegen.spec.js +37 -0
  114. package/dist/utils/__tests__/version.spec.d.ts +1 -0
  115. package/dist/utils/__tests__/version.spec.js +5 -0
  116. package/dist/utils/cargo.d.ts +1 -0
  117. package/dist/utils/cargo.js +33 -0
  118. package/dist/utils/ci.d.ts +11 -0
  119. package/dist/utils/ci.js +86 -0
  120. package/dist/utils/config.d.ts +65 -0
  121. package/dist/utils/config.js +42 -0
  122. package/dist/utils/index.d.ts +9 -0
  123. package/dist/utils/index.js +9 -0
  124. package/dist/utils/log.d.ts +10 -0
  125. package/dist/utils/log.js +14 -0
  126. package/dist/utils/metadata.d.ts +24 -0
  127. package/dist/utils/metadata.js +19 -0
  128. package/dist/utils/misc.d.ts +13 -0
  129. package/dist/utils/misc.js +35 -0
  130. package/dist/utils/target.d.ts +30 -0
  131. package/dist/utils/target.js +109 -0
  132. package/dist/utils/typegen.d.ts +3 -0
  133. package/dist/utils/typegen.js +158 -0
  134. package/dist/utils/version.d.ts +11 -0
  135. package/dist/utils/version.js +59 -0
  136. package/package.json +63 -27
  137. package/src/api/artifacts.ts +93 -0
  138. package/src/api/build.ts +549 -0
  139. package/src/api/create-npm-dirs.ts +105 -0
  140. package/src/api/new.ts +206 -0
  141. package/src/api/pre-publish.ts +216 -0
  142. package/src/api/rename.ts +51 -0
  143. package/src/api/templates/.gitignore.ts +198 -0
  144. package/src/api/templates/.npmignore.ts +14 -0
  145. package/src/api/templates/build.rs.ts +4 -0
  146. package/src/api/templates/cargo.toml.ts +35 -0
  147. package/src/api/templates/ci-template.ts +566 -0
  148. package/src/api/templates/ci.yml.ts +116 -0
  149. package/src/api/templates/index.ts +8 -0
  150. package/src/api/templates/js-binding.ts +130 -0
  151. package/src/api/templates/lib.rs.ts +10 -0
  152. package/src/api/templates/package.json.ts +44 -0
  153. package/src/api/universalize.ts +76 -0
  154. package/src/api/version.ts +26 -0
  155. package/src/cli.ts +29 -0
  156. package/src/commands/artifacts.ts +23 -0
  157. package/src/commands/build.ts +42 -0
  158. package/src/commands/create-npm-dirs.ts +8 -0
  159. package/src/commands/help.ts +13 -0
  160. package/src/commands/new.ts +138 -0
  161. package/src/commands/pre-publish.ts +9 -0
  162. package/src/commands/rename.ts +8 -0
  163. package/src/commands/universalize.ts +8 -0
  164. package/src/commands/version.ts +8 -0
  165. package/src/def/artifacts.ts +79 -0
  166. package/src/def/build.ts +242 -0
  167. package/src/def/create-npm-dirs.ts +79 -0
  168. package/src/def/new.ts +144 -0
  169. package/src/def/pre-publish.ts +120 -0
  170. package/src/def/rename.ts +122 -0
  171. package/src/def/universalize.ts +66 -0
  172. package/src/def/version.ts +65 -0
  173. package/src/index.ts +31 -0
  174. package/src/utils/__tests__/__fixtures__/napi_type_def +201 -0
  175. package/src/utils/__tests__/__snapshots__/target.spec.ts.md +110 -0
  176. package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
  177. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +618 -0
  178. package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
  179. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +20 -0
  180. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  181. package/src/utils/__tests__/target.spec.ts +19 -0
  182. package/src/utils/__tests__/typegen.spec.ts +49 -0
  183. package/src/utils/__tests__/version.spec.ts +13 -0
  184. package/src/utils/cargo.ts +35 -0
  185. package/src/utils/ci.ts +102 -0
  186. package/src/utils/config.ts +123 -0
  187. package/src/utils/index.ts +9 -0
  188. package/src/utils/log.ts +34 -0
  189. package/src/utils/metadata.ts +59 -0
  190. package/src/utils/misc.ts +45 -0
  191. package/src/utils/target.ts +145 -0
  192. package/src/utils/typegen.ts +193 -0
  193. package/src/utils/version.ts +76 -0
  194. package/scripts/index.js +0 -153779
@@ -0,0 +1,30 @@
1
+ import { collectArtifacts } from './api/artifacts.js';
2
+ import { buildProject } from './api/build.js';
3
+ import { createNpmDirs } from './api/create-npm-dirs.js';
4
+ import { newProject } from './api/new.js';
5
+ import { prePublish } from './api/pre-publish.js';
6
+ import { renameProject } from './api/rename.js';
7
+ import { universalizeBinaries } from './api/universalize.js';
8
+ import { version } from './api/version.js';
9
+ /**
10
+ *
11
+ * @usage
12
+ *
13
+ * ```ts
14
+ * const cli = new NapiCli()
15
+ *
16
+ * cli.build({
17
+ * cwd: '/path/to/your/project',
18
+ * })
19
+ * ```
20
+ */
21
+ export declare class NapiCli {
22
+ artifacts: typeof collectArtifacts;
23
+ new: typeof newProject;
24
+ build: typeof buildProject;
25
+ createNpmDirs: typeof createNpmDirs;
26
+ prePublish: typeof prePublish;
27
+ rename: typeof renameProject;
28
+ universalize: typeof universalizeBinaries;
29
+ version: typeof version;
30
+ }
package/dist/index.js ADDED
@@ -0,0 +1,30 @@
1
+ import { collectArtifacts } from './api/artifacts.js';
2
+ import { buildProject } from './api/build.js';
3
+ import { createNpmDirs } from './api/create-npm-dirs.js';
4
+ import { newProject } from './api/new.js';
5
+ import { prePublish } from './api/pre-publish.js';
6
+ import { renameProject } from './api/rename.js';
7
+ import { universalizeBinaries } from './api/universalize.js';
8
+ import { version } from './api/version.js';
9
+ /**
10
+ *
11
+ * @usage
12
+ *
13
+ * ```ts
14
+ * const cli = new NapiCli()
15
+ *
16
+ * cli.build({
17
+ * cwd: '/path/to/your/project',
18
+ * })
19
+ * ```
20
+ */
21
+ export class NapiCli {
22
+ artifacts = collectArtifacts;
23
+ new = newProject;
24
+ build = buildProject;
25
+ createNpmDirs = createNpmDirs;
26
+ prePublish = prePublish;
27
+ rename = renameProject;
28
+ universalize = universalizeBinaries;
29
+ version = version;
30
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import os from 'os';
2
+ import test from 'ava';
3
+ import { parseTriple, getSystemDefaultTarget, AVAILABLE_TARGETS, } from '../target.js';
4
+ test('should parse triple correctly', (t) => {
5
+ t.snapshot(AVAILABLE_TARGETS.map(parseTriple));
6
+ });
7
+ test('should get system default target correctly', (t) => {
8
+ const target = getSystemDefaultTarget();
9
+ t.is(target.platform, os.platform());
10
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,37 @@
1
+ import { join } from 'path';
2
+ import { fileURLToPath } from 'url';
3
+ import test from 'ava';
4
+ import { correctStringIdent, processTypeDef } from '../typegen.js';
5
+ test('should ident string correctly', (t) => {
6
+ const input = `
7
+ /**
8
+ * should keep
9
+ * class A {
10
+ * foo = () => {}
11
+ * bar = () => {}
12
+ * }
13
+ */
14
+ class A {
15
+ foo() {
16
+ a = b
17
+ }
18
+
19
+ bar = () => {
20
+
21
+ }
22
+ boz = 1
23
+ }
24
+
25
+ namespace B {
26
+ namespace C {
27
+ type D = A
28
+ }
29
+ }
30
+ `;
31
+ t.snapshot(correctStringIdent(input, 0), 'original ident is 0');
32
+ t.snapshot(correctStringIdent(input, 2), 'original ident is 2');
33
+ });
34
+ test('should process type def correctly', async (t) => {
35
+ const dts = await processTypeDef(join(fileURLToPath(import.meta.url), '../', '__fixtures__', 'napi_type_def'));
36
+ t.snapshot(dts);
37
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import test from 'ava';
2
+ import { napiEngineRequirement, NapiVersion } from '../version.js';
3
+ test('should generate correct napi engine requirement', (t) => {
4
+ t.snapshot(Object.values(NapiVersion).filter((v) => typeof v === 'number').map(napiEngineRequirement));
5
+ });
@@ -0,0 +1 @@
1
+ export declare function tryInstallCargoBinary(name: string, bin: string): void;
@@ -0,0 +1,33 @@
1
+ import { execSync } from 'child_process';
2
+ import { debug } from './log.js';
3
+ export function tryInstallCargoBinary(name, bin) {
4
+ if (detectCargoBinary(bin)) {
5
+ debug('Cargo binary already installed: %s', name);
6
+ return;
7
+ }
8
+ try {
9
+ debug('Installing cargo binary: %s', name);
10
+ execSync(`cargo install ${name}`, {
11
+ stdio: 'inherit',
12
+ });
13
+ }
14
+ catch (e) {
15
+ throw new Error(`Failed to install cargo binary: ${name}`, {
16
+ cause: e,
17
+ });
18
+ }
19
+ }
20
+ function detectCargoBinary(bin) {
21
+ debug('Detecting cargo binary: %s', bin);
22
+ try {
23
+ execSync(`cargo help ${bin}`, {
24
+ stdio: 'ignore',
25
+ });
26
+ debug('Cargo binary detected: %s', bin);
27
+ return true;
28
+ }
29
+ catch (e) {
30
+ debug('Cargo binary not detected: %s', bin);
31
+ return false;
32
+ }
33
+ }
@@ -0,0 +1,11 @@
1
+ import { TargetTriple } from './target.js';
2
+ export declare const CIConfig: Partial<Record<TargetTriple, {
3
+ host: string;
4
+ build_image?: string;
5
+ build_setup?: string[];
6
+ test?: boolean;
7
+ test_image?: string;
8
+ test_setup?: string[];
9
+ yarn_cpu?: string;
10
+ yarn_libc?: string;
11
+ }>>;
@@ -0,0 +1,86 @@
1
+ export const CIConfig = {
2
+ 'x86_64-apple-darwin': {
3
+ host: 'macos-latest',
4
+ },
5
+ 'x86_64-pc-windows-msvc': {
6
+ host: 'windows-latest',
7
+ },
8
+ 'i686-pc-windows-msvc': {
9
+ host: 'windows-latest',
10
+ test: false,
11
+ },
12
+ 'x86_64-unknown-linux-gnu': {
13
+ host: 'ubuntu-latest',
14
+ build_image: 'ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian',
15
+ },
16
+ 'x86_64-unknown-linux-musl': {
17
+ host: 'ubuntu-latest',
18
+ build_image: 'ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine',
19
+ test_image: 'node:${{ matrix.node }}-alpine',
20
+ yarn_libc: 'musl',
21
+ },
22
+ 'aarch64-apple-darwin': {
23
+ host: 'macos-latest',
24
+ build_setup: [
25
+ 'sudo rm -Rf /Library/Developer/CommandLineTools/SDKs/*',
26
+ 'export CC=$(xcrun -f clang)',
27
+ 'export CXX=$(xcrun -f clang++)',
28
+ 'export SDK_ROOT=$(xcrun --sdk macosx --show-sdk-path)',
29
+ 'export CFLAGS="-isysroot $SDK_ROOT -isystem $SDK_ROOT"',
30
+ ],
31
+ test: false,
32
+ },
33
+ 'aarch64-unknown-linux-gnu': {
34
+ host: 'ubuntu-latest',
35
+ build_image: 'ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian-aarch64',
36
+ build_setup: [
37
+ 'sudo apt-get update',
38
+ 'sudo apt-get install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu -y',
39
+ 'export CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu',
40
+ ],
41
+ test_image: 'ghcr.io/napi-rs/napi-rs/nodejs:aarch64-${{ matrix.node }}',
42
+ yarn_cpu: 'arm64',
43
+ },
44
+ 'aarch64-unknown-linux-musl': {
45
+ host: 'ubuntu-latest',
46
+ build_image: 'ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-alpine',
47
+ build_setup: ['rustup target add aarch64-unknown-linux-musl'],
48
+ test_image: 'multiarch/alpine:aarch64-latest-stable',
49
+ test_setup: ['apk add nodejs npm yarn'],
50
+ yarn_cpu: 'arm64',
51
+ yarn_libc: 'musl',
52
+ },
53
+ 'aarch64-pc-windows-msvc': {
54
+ host: 'windows-latest',
55
+ test: false,
56
+ },
57
+ 'armv7-unknown-linux-gnueabihf': {
58
+ host: 'ubuntu-latest',
59
+ build_setup: [
60
+ 'sudo apt-get update',
61
+ 'sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf -y',
62
+ ],
63
+ yarn_cpu: 'arm',
64
+ test_image: 'ghcr.io/napi-rs/napi-rs/nodejs:armhf-${{ matrix.node }}',
65
+ },
66
+ 'aarch64-linux-android': {
67
+ host: 'ubuntu-latest',
68
+ build_setup: [
69
+ 'export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang"',
70
+ 'export CC="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang"',
71
+ 'export CXX="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android24-clang++"',
72
+ 'export PATH="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin:${PATH}"',
73
+ ],
74
+ test: false,
75
+ },
76
+ 'armv7-linux-androideabi': {
77
+ host: 'ubuntu-latest',
78
+ build_setup: [
79
+ 'export CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi24-clang"',
80
+ 'export CC="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi24-clang"',
81
+ 'export CXX="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi24-clang++"',
82
+ 'export PATH="${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin:${PATH}"',
83
+ ],
84
+ test: false,
85
+ },
86
+ };
@@ -0,0 +1,65 @@
1
+ import { Target } from './target.js';
2
+ interface UserNapiConfig {
3
+ /**
4
+ * Name of the binary to be generated, default to `index`
5
+ */
6
+ binaryName?: string;
7
+ /**
8
+ * Name of the npm package, default to the name of root package.json name
9
+ *
10
+ * Always given `@scope/pkg` and arch suffix will be appended like `@scope/pkg-linux-gnu-x64`
11
+ */
12
+ packageName?: string;
13
+ /**
14
+ * All targets the crate will be compiled for
15
+ */
16
+ targets?: string[];
17
+ /**
18
+ * The npm client project uses.
19
+ */
20
+ npmClient?: string;
21
+ /**
22
+ * @deprecated binaryName instead
23
+ */
24
+ name?: string;
25
+ /**
26
+ * @deprecated use packageName instead
27
+ */
28
+ package?: {
29
+ name?: string;
30
+ };
31
+ /**
32
+ * @deprecated use targets instead
33
+ */
34
+ triples?: {
35
+ /**
36
+ * Whether enable default targets
37
+ */
38
+ default: boolean;
39
+ /**
40
+ * Additional targets to be compiled for
41
+ */
42
+ additional?: string[];
43
+ };
44
+ }
45
+ interface CommonPackageJsonFields {
46
+ name: string;
47
+ version: string;
48
+ description?: string;
49
+ keywords?: string[];
50
+ author?: string;
51
+ authors?: string[];
52
+ license?: string;
53
+ repository?: any;
54
+ homepage?: any;
55
+ engines?: Record<string, string>;
56
+ publishConfig?: any;
57
+ bugs?: any;
58
+ napi?: UserNapiConfig;
59
+ }
60
+ export type NapiConfig = Required<Pick<UserNapiConfig, 'binaryName' | 'packageName' | 'npmClient'>> & {
61
+ targets: Target[];
62
+ packageJson: CommonPackageJsonFields;
63
+ };
64
+ export declare function readNapiConfig(path: string): Promise<NapiConfig>;
65
+ export {};
@@ -0,0 +1,42 @@
1
+ import { merge, omit } from 'lodash-es';
2
+ import { fileExists, readFileAsync } from './misc.js';
3
+ import { DEFAULT_TARGETS, parseTriple } from './target.js';
4
+ export async function readNapiConfig(path) {
5
+ if (!(await fileExists(path))) {
6
+ throw new Error(`napi-rs config not found at ${path}`);
7
+ }
8
+ // May support multiple config sources later on.
9
+ const content = await readFileAsync(path, 'utf8');
10
+ let pkgJson;
11
+ try {
12
+ pkgJson = JSON.parse(content);
13
+ }
14
+ catch (e) {
15
+ throw new Error('Failed to parse napi-rs config', {
16
+ cause: e,
17
+ });
18
+ }
19
+ const userNapiConfig = pkgJson.napi ?? {};
20
+ const napiConfig = merge({
21
+ binaryName: 'index',
22
+ packageName: pkgJson.name,
23
+ targets: [],
24
+ packageJson: pkgJson,
25
+ npmClient: 'npm',
26
+ }, omit(userNapiConfig, 'targets'));
27
+ let targets = userNapiConfig.targets ?? [];
28
+ // compatible with old config
29
+ if (userNapiConfig.package?.name) {
30
+ napiConfig.packageName = userNapiConfig.package.name;
31
+ }
32
+ if (!targets.length) {
33
+ if (userNapiConfig.triples?.default) {
34
+ targets = targets.concat(DEFAULT_TARGETS);
35
+ }
36
+ if (userNapiConfig.triples?.additional?.length) {
37
+ targets = targets.concat(userNapiConfig.triples.additional);
38
+ }
39
+ }
40
+ napiConfig.targets = targets.map(parseTriple);
41
+ return napiConfig;
42
+ }
@@ -0,0 +1,9 @@
1
+ export * from './ci.js';
2
+ export * from './log.js';
3
+ export * from './misc.js';
4
+ export * from './target.js';
5
+ export * from './version.js';
6
+ export * from './metadata.js';
7
+ export * from './config.js';
8
+ export * from './cargo.js';
9
+ export * from './typegen.js';
@@ -0,0 +1,9 @@
1
+ export * from './ci.js';
2
+ export * from './log.js';
3
+ export * from './misc.js';
4
+ export * from './target.js';
5
+ export * from './version.js';
6
+ export * from './metadata.js';
7
+ export * from './config.js';
8
+ export * from './cargo.js';
9
+ export * from './typegen.js';
@@ -0,0 +1,10 @@
1
+ import rawDebug from 'debug';
2
+ declare module 'debug' {
3
+ interface Debugger {
4
+ info: typeof console.error;
5
+ warn: typeof console.error;
6
+ error: typeof console.error;
7
+ }
8
+ }
9
+ export declare const debugFactory: (namespace: string) => rawDebug.Debugger;
10
+ export declare const debug: rawDebug.Debugger;
@@ -0,0 +1,14 @@
1
+ import * as colors from 'colorette';
2
+ import rawDebug from 'debug';
3
+ // debug('%i', 'This is an info')
4
+ rawDebug.formatters.i = (v) => {
5
+ return colors.green(v);
6
+ };
7
+ export const debugFactory = (namespace) => {
8
+ const debug = rawDebug(`napi:${namespace}`);
9
+ debug.info = (...args) => console.error(colors.black(colors.bgGreen(' INFO ')), ...args);
10
+ debug.warn = (...args) => console.error(colors.black(colors.bgYellow(' WARNING ')), ...args);
11
+ debug.error = (...args) => console.error(colors.white(colors.bgRed(' ERROR ')), ...args.map((arg) => arg instanceof Error ? arg.stack ?? arg.message : arg));
12
+ return debug;
13
+ };
14
+ export const debug = debugFactory('utils');
@@ -0,0 +1,24 @@
1
+ export type CrateTargetKind = 'bin' | 'example' | 'test' | 'bench' | 'lib' | 'rlib' | 'cdylib' | 'custom-build';
2
+ export interface CrateTarget {
3
+ name: string;
4
+ kind: CrateTargetKind[];
5
+ crate_types: CrateTargetKind[];
6
+ }
7
+ export interface Crate {
8
+ id: string;
9
+ name: string;
10
+ src_path: string;
11
+ version: string;
12
+ edition: string;
13
+ targets: CrateTarget[];
14
+ features: Record<string, string[]>;
15
+ manifest_path: string;
16
+ }
17
+ export interface CargoWorkspaceMetadata {
18
+ version: number;
19
+ packages: Crate[];
20
+ workspace_members: string[];
21
+ target_directory: string;
22
+ workspace_root: string;
23
+ }
24
+ export declare function parseMetadata(manifestPath: string): CargoWorkspaceMetadata;
@@ -0,0 +1,19 @@
1
+ import { execSync } from 'child_process';
2
+ import fs from 'fs';
3
+ export function parseMetadata(manifestPath) {
4
+ if (!fs.existsSync(manifestPath)) {
5
+ throw new Error(`No crate found in manifest: ${manifestPath}`);
6
+ }
7
+ const cmd = `cargo metadata --manifest-path ${manifestPath} --format-version 1 --no-deps`;
8
+ try {
9
+ const output = execSync(cmd, {
10
+ encoding: 'utf-8',
11
+ });
12
+ return JSON.parse(output);
13
+ }
14
+ catch (e) {
15
+ throw new Error(`Failed to parse cargo metadata output by command: ${cmd}`, {
16
+ cause: e,
17
+ });
18
+ }
19
+ }
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { readFile, writeFile, copyFile, mkdir, unlink, stat, readdir } from 'fs';
3
+ export declare const readFileAsync: typeof readFile.__promisify__;
4
+ export declare const writeFileAsync: typeof writeFile.__promisify__;
5
+ export declare const unlinkAsync: typeof unlink.__promisify__;
6
+ export declare const copyFileAsync: typeof copyFile.__promisify__;
7
+ export declare const mkdirAsync: typeof mkdir.__promisify__;
8
+ export declare const statAsync: typeof stat.__promisify__;
9
+ export declare const readdirAsync: typeof readdir.__promisify__;
10
+ export declare function fileExists(path: string): Promise<boolean>;
11
+ export declare function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K>;
12
+ export declare function updatePackageJson(path: string, partial: Record<string, any>): Promise<void>;
13
+ export declare const CLI_VERSION: any;
@@ -0,0 +1,35 @@
1
+ import { readFile, writeFile, copyFile, mkdir, unlink, stat, readdir } from 'fs';
2
+ import { createRequire } from 'module';
3
+ import { promisify } from 'util';
4
+ import { debug } from './log.js';
5
+ const require = createRequire(import.meta.url);
6
+ const pkgJson = require('../../package.json');
7
+ export const readFileAsync = promisify(readFile);
8
+ export const writeFileAsync = promisify(writeFile);
9
+ export const unlinkAsync = promisify(unlink);
10
+ export const copyFileAsync = promisify(copyFile);
11
+ export const mkdirAsync = promisify(mkdir);
12
+ export const statAsync = promisify(stat);
13
+ export const readdirAsync = promisify(readdir);
14
+ export async function fileExists(path) {
15
+ const exists = await statAsync(path)
16
+ .then(() => true)
17
+ .catch(() => false);
18
+ return exists;
19
+ }
20
+ export function pick(o, ...keys) {
21
+ return keys.reduce((acc, key) => {
22
+ acc[key] = o[key];
23
+ return acc;
24
+ }, {});
25
+ }
26
+ export async function updatePackageJson(path, partial) {
27
+ const exists = await fileExists(path);
28
+ if (!exists) {
29
+ debug(`File not exists ${path}`);
30
+ return;
31
+ }
32
+ const old = require(path);
33
+ await writeFileAsync(path, JSON.stringify({ ...old, ...partial }, null, 2));
34
+ }
35
+ export const CLI_VERSION = pkgJson.version;
@@ -0,0 +1,30 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ export declare const AVAILABLE_TARGETS: readonly ["aarch64-apple-darwin", "aarch64-linux-android", "aarch64-unknown-linux-gnu", "aarch64-unknown-linux-musl", "aarch64-pc-windows-msvc", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "i686-pc-windows-msvc", "armv7-unknown-linux-gnueabihf", "armv7-linux-androideabi", "universal-apple-darwin"];
3
+ export type TargetTriple = (typeof AVAILABLE_TARGETS)[number];
4
+ export declare const DEFAULT_TARGETS: readonly ["x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu"];
5
+ export declare const TARGET_LINKER: Record<string, string>;
6
+ type NodeJSArch = 'arm' | 'arm64' | 'ia32' | 'mips' | 'mipsel' | 'ppc' | 'ppc64' | 's390' | 's390x' | 'x32' | 'x64' | 'universal';
7
+ export declare const NodeArchToCpu: Record<string, string>;
8
+ export declare const UniArchsByPlatform: Partial<Record<NodeJS.Platform, NodeJSArch[]>>;
9
+ export interface Target {
10
+ triple: string;
11
+ platformArchABI: string;
12
+ platform: NodeJS.Platform;
13
+ arch: NodeJSArch;
14
+ abi: string | null;
15
+ }
16
+ /**
17
+ * A triple is a specific format for specifying a target architecture.
18
+ * Triples may be referred to as a target triple which is the architecture for the artifact produced, and the host triple which is the architecture that the compiler is running on.
19
+ * The general format of the triple is `<arch><sub>-<vendor>-<sys>-<abi>` where:
20
+ * - `arch` = The base CPU architecture, for example `x86_64`, `i686`, `arm`, `thumb`, `mips`, etc.
21
+ * - `sub` = The CPU sub-architecture, for example `arm` has `v7`, `v7s`, `v5te`, etc.
22
+ * - `vendor` = The vendor, for example `unknown`, `apple`, `pc`, `nvidia`, etc.
23
+ * - `sys` = The system name, for example `linux`, `windows`, `darwin`, etc. none is typically used for bare-metal without an OS.
24
+ * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
25
+ */
26
+ export declare function parseTriple(rawTriple: string): Target;
27
+ export declare function getSystemDefaultTarget(): Target;
28
+ export declare function getTargetLinker(target: string): string | undefined;
29
+ export declare function targetToEnvVar(target: string): string;
30
+ export {};
@@ -0,0 +1,109 @@
1
+ import { execSync } from 'child_process';
2
+ export const AVAILABLE_TARGETS = [
3
+ 'aarch64-apple-darwin',
4
+ 'aarch64-linux-android',
5
+ 'aarch64-unknown-linux-gnu',
6
+ 'aarch64-unknown-linux-musl',
7
+ 'aarch64-pc-windows-msvc',
8
+ 'x86_64-apple-darwin',
9
+ 'x86_64-pc-windows-msvc',
10
+ 'x86_64-unknown-linux-gnu',
11
+ 'x86_64-unknown-linux-musl',
12
+ 'x86_64-unknown-freebsd',
13
+ 'i686-pc-windows-msvc',
14
+ 'armv7-unknown-linux-gnueabihf',
15
+ 'armv7-linux-androideabi',
16
+ 'universal-apple-darwin',
17
+ ];
18
+ export const DEFAULT_TARGETS = [
19
+ 'x86_64-apple-darwin',
20
+ 'x86_64-pc-windows-msvc',
21
+ 'x86_64-unknown-linux-gnu',
22
+ ];
23
+ export const TARGET_LINKER = {
24
+ 'aarch64-unknown-linux-gnu': 'aarch64-linux-gnu-gcc',
25
+ 'armv7-unknown-linux-gnueabihf': 'arm-linux-gnueabihf-gcc',
26
+ 'aarch64-unknown-linux-musl': 'aarch64-linux-musl-gcc',
27
+ };
28
+ const CpuToNodeArch = {
29
+ x86_64: 'x64',
30
+ aarch64: 'arm64',
31
+ i686: 'ia32',
32
+ armv7: 'arm',
33
+ };
34
+ export const NodeArchToCpu = {
35
+ x64: 'x86_64',
36
+ arm64: 'aarch64',
37
+ ia32: 'i686',
38
+ arm: 'armv7',
39
+ };
40
+ const SysToNodePlatform = {
41
+ linux: 'linux',
42
+ freebsd: 'freebsd',
43
+ darwin: 'darwin',
44
+ windows: 'win32',
45
+ };
46
+ export const UniArchsByPlatform = {
47
+ darwin: ['x64', 'arm64'],
48
+ };
49
+ /**
50
+ * A triple is a specific format for specifying a target architecture.
51
+ * Triples may be referred to as a target triple which is the architecture for the artifact produced, and the host triple which is the architecture that the compiler is running on.
52
+ * The general format of the triple is `<arch><sub>-<vendor>-<sys>-<abi>` where:
53
+ * - `arch` = The base CPU architecture, for example `x86_64`, `i686`, `arm`, `thumb`, `mips`, etc.
54
+ * - `sub` = The CPU sub-architecture, for example `arm` has `v7`, `v7s`, `v5te`, etc.
55
+ * - `vendor` = The vendor, for example `unknown`, `apple`, `pc`, `nvidia`, etc.
56
+ * - `sys` = The system name, for example `linux`, `windows`, `darwin`, etc. none is typically used for bare-metal without an OS.
57
+ * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc.
58
+ */
59
+ export function parseTriple(rawTriple) {
60
+ const triple = rawTriple.endsWith('eabi')
61
+ ? `${rawTriple.slice(0, -4)}-eabi`
62
+ : rawTriple;
63
+ const triples = triple.split('-');
64
+ let cpu;
65
+ let sys;
66
+ let abi = null;
67
+ if (triples.length === 2) {
68
+ // aarch64-fuchsia
69
+ // ^ cpu ^ sys
70
+ ;
71
+ [cpu, sys] = triples;
72
+ }
73
+ else {
74
+ // aarch64-unknown-linux-musl
75
+ // ^ cpu ^ sys ^ abi
76
+ // aarch64-apple-darwin
77
+ // ^ cpu ^ sys (abi is None)
78
+ ;
79
+ [cpu, , sys, abi = null] = triples;
80
+ }
81
+ const platform = SysToNodePlatform[sys] ?? sys;
82
+ const arch = CpuToNodeArch[cpu] ?? cpu;
83
+ return {
84
+ triple: rawTriple,
85
+ platformArchABI: abi ? `${platform}-${arch}-${abi}` : `${platform}-${arch}`,
86
+ platform,
87
+ arch,
88
+ abi,
89
+ };
90
+ }
91
+ export function getSystemDefaultTarget() {
92
+ const host = execSync(`rustc -vV`, {
93
+ env: process.env,
94
+ })
95
+ .toString('utf8')
96
+ .split('\n')
97
+ .find((line) => line.startsWith('host: '));
98
+ const triple = host?.slice('host: '.length);
99
+ if (!triple) {
100
+ throw new TypeError(`Can not parse target triple from host`);
101
+ }
102
+ return parseTriple(triple);
103
+ }
104
+ export function getTargetLinker(target) {
105
+ return TARGET_LINKER[target];
106
+ }
107
+ export function targetToEnvVar(target) {
108
+ return target.replace(/-/g, '_').toUpperCase();
109
+ }
@@ -0,0 +1,3 @@
1
+ export declare const DEFAULT_TYPE_DEF_HEADER = "/* auto-generated by NAPI-RS */\n/* eslint-disable */\n";
2
+ export declare function processTypeDef(intermediateTypeFile: string, header?: string): Promise<string>;
3
+ export declare function correctStringIdent(src: string, ident: number): string;