@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,124 @@
1
+ /* eslint-disable @typescript-eslint/switch-exhaustiveness-check */
2
+ function loadNapiModule(binaryName, packageName) {
3
+ const { existsSync, readFileSync } = require('fs');
4
+ const { join } = require('path');
5
+ const { platform, arch } = process;
6
+ const candidates = [];
7
+ function isMusl() {
8
+ // For Node 10
9
+ if (!process.report || typeof process.report.getReport !== 'function') {
10
+ try {
11
+ const lddPath = require('child_process')
12
+ .execSync('which ldd')
13
+ .toString()
14
+ .trim();
15
+ return readFileSync(lddPath, 'utf8').includes('musl');
16
+ }
17
+ catch (e) {
18
+ return true;
19
+ }
20
+ }
21
+ else {
22
+ // @ts-expect-error
23
+ const { glibcVersionRuntime } = process.report.getReport().header;
24
+ return !glibcVersionRuntime;
25
+ }
26
+ }
27
+ switch (platform) {
28
+ case 'android':
29
+ switch (arch) {
30
+ case 'arm64':
31
+ candidates.push('android-arm64');
32
+ break;
33
+ case 'arm':
34
+ candidates.push('android-arm-eabi');
35
+ break;
36
+ }
37
+ break;
38
+ case 'win32':
39
+ switch (arch) {
40
+ case 'x64':
41
+ candidates.push('win32-x64-msvc');
42
+ break;
43
+ case 'ia32':
44
+ candidates.push('win32-ia32-msvc');
45
+ break;
46
+ case 'arm64':
47
+ candidates.push('win32-arm64-msvc');
48
+ break;
49
+ }
50
+ break;
51
+ case 'darwin':
52
+ candidates.push('darwin-universal');
53
+ switch (arch) {
54
+ case 'x64':
55
+ candidates.push('darwin-x64');
56
+ break;
57
+ case 'arm64':
58
+ candidates.push('darwin-arm64');
59
+ break;
60
+ }
61
+ break;
62
+ case 'freebsd':
63
+ if (arch === 'x64') {
64
+ candidates.push('freebsd-x64');
65
+ }
66
+ break;
67
+ case 'linux':
68
+ switch (arch) {
69
+ case 'x64':
70
+ if (isMusl()) {
71
+ candidates.push('linux-x64-musl');
72
+ }
73
+ else {
74
+ candidates.push('linux-x64-gnu');
75
+ }
76
+ break;
77
+ case 'arm64':
78
+ if (isMusl()) {
79
+ candidates.push('linux-arm64-musl');
80
+ }
81
+ else {
82
+ candidates.push('linux-arm64-gnu');
83
+ }
84
+ break;
85
+ case 'arm':
86
+ candidates.push('linux-arm-gnueabihf');
87
+ break;
88
+ }
89
+ break;
90
+ }
91
+ let nativeBinding;
92
+ let loadError;
93
+ for (const suffix of candidates) {
94
+ const localPath = join(__dirname, `${binaryName}.${suffix}.node`);
95
+ const pkgPath = `${packageName}-${suffix}`;
96
+ try {
97
+ if (existsSync(localPath)) {
98
+ nativeBinding = require(localPath);
99
+ }
100
+ else {
101
+ nativeBinding = require(pkgPath);
102
+ }
103
+ }
104
+ catch (e) {
105
+ loadError = e;
106
+ continue;
107
+ }
108
+ loadError = null;
109
+ break;
110
+ }
111
+ if (!nativeBinding) {
112
+ if (loadError) {
113
+ throw loadError;
114
+ }
115
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`);
116
+ }
117
+ return nativeBinding;
118
+ }
119
+ export function createJsBinding(localName, pkgName) {
120
+ return `${loadNapiModule.toString()}
121
+
122
+ module.exports = loadNapiModule('${localName}', '${pkgName}')
123
+ `;
124
+ }
@@ -0,0 +1 @@
1
+ export declare const createLibRs: () => string;
@@ -0,0 +1,10 @@
1
+ export const createLibRs = () => `#![deny(clippy::all)]
2
+
3
+ #[macro_use]
4
+ extern crate napi_derive;
5
+
6
+ #[napi]
7
+ pub fn sum(a: i32, b: i32) -> i32 {
8
+ a + b
9
+ }
10
+ `;
@@ -0,0 +1,8 @@
1
+ export declare const createPackageJson: ({ name, binaryName, targets, license, engineRequirement, cliVersion, }: {
2
+ name: string;
3
+ binaryName: string;
4
+ targets: string[];
5
+ license: string;
6
+ engineRequirement: string;
7
+ cliVersion: string;
8
+ }) => string;
@@ -0,0 +1,30 @@
1
+ export const createPackageJson = ({ name, binaryName, targets, license, engineRequirement, cliVersion, }) => {
2
+ return `{
3
+ "name": "${name}",
4
+ "version": "1.0.0",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "license": "${license}",
8
+ "engines": {
9
+ "node": "${engineRequirement}"
10
+ },
11
+ "napi": {
12
+ "name": "${binaryName}",
13
+ "targets": [
14
+ ${targets.map((t) => `"${t}"`).join(',\n ')}
15
+ ]
16
+ },
17
+ "scripts": {
18
+ "test": "yarn build:debug --platform && node -e \\"assert(require('.').sum(1, 2) === 3)\\"",
19
+ "build": "napi build --release --platform --strip",
20
+ "build:debug": "napi build",
21
+ "prepublishOnly": "napi prepublish -t npm",
22
+ "artifacts": "napi artifacts",
23
+ "universal": "napi universal",
24
+ "version": "napi version"
25
+ },
26
+ "devDependencies": {
27
+ "@napi-rs/cli": "^${cliVersion}"
28
+ }
29
+ }`;
30
+ };
@@ -0,0 +1,2 @@
1
+ import { UniversalizeOptions } from '../def/universalize.js';
2
+ export declare function universalizeBinaries(userOptions: UniversalizeOptions): Promise<void>;
@@ -0,0 +1,38 @@
1
+ import { spawnSync } from 'child_process';
2
+ import { join, resolve } from 'path';
3
+ import { applyDefaultUniversalizeOptions, } from '../def/universalize.js';
4
+ import { readNapiConfig } from '../utils/config.js';
5
+ import { debugFactory } from '../utils/log.js';
6
+ import { fileExists } from '../utils/misc.js';
7
+ import { UniArchsByPlatform } from '../utils/target.js';
8
+ const debug = debugFactory('universalize');
9
+ const universalizers = {
10
+ darwin: (inputs, output) => {
11
+ spawnSync('lipo', ['-create', '-output', output, ...inputs], {
12
+ stdio: 'inherit',
13
+ });
14
+ },
15
+ };
16
+ export async function universalizeBinaries(userOptions) {
17
+ const options = applyDefaultUniversalizeOptions(userOptions);
18
+ const packageJsonPath = join(options.cwd, options.packageJsonPath);
19
+ const config = await readNapiConfig(packageJsonPath);
20
+ const target = config.targets.find((t) => t.platform === process.platform && t.arch === 'universal');
21
+ if (!target) {
22
+ throw new Error(`'universal' arch for platform '${process.platform}' not found in config!`);
23
+ }
24
+ const srcFiles = UniArchsByPlatform[process.platform]?.map((arch) => `${config.binaryName}.${process.platform}-${arch}.node`);
25
+ if (!srcFiles || !universalizers[process.platform]) {
26
+ throw new Error(`'universal' arch for platform '${process.platform}' not supported.`);
27
+ }
28
+ debug(`Looking up source binaries to combine: `);
29
+ debug(' %O', srcFiles);
30
+ const srcFileLookup = await Promise.all(srcFiles.map((f) => fileExists(resolve(options.cwd, options.outputDir, f))));
31
+ const notFoundFiles = srcFiles.filter((_, i) => !srcFileLookup[i]);
32
+ if (notFoundFiles.length) {
33
+ throw new Error(`Some binary files were not found: ${JSON.stringify(notFoundFiles)}`);
34
+ }
35
+ const output = resolve(options.cwd, options.outputDir, `${config.binaryName}.${process.platform}-universal.node`);
36
+ universalizers[process.platform]?.(srcFiles, output);
37
+ debug(`Produced universal binary: ${output}`);
38
+ }
@@ -0,0 +1,2 @@
1
+ import { VersionOptions } from '../def/version.js';
2
+ export declare function version(userOptions: VersionOptions): Promise<void>;
@@ -0,0 +1,16 @@
1
+ import { join, resolve } from 'path';
2
+ import { applyDefaultVersionOptions } from '../def/version.js';
3
+ import { readNapiConfig, debugFactory, updatePackageJson, } from '../utils/index.js';
4
+ const debug = debugFactory('version');
5
+ export async function version(userOptions) {
6
+ const options = applyDefaultVersionOptions(userOptions);
7
+ const packageJsonPath = resolve(options.cwd, options.packageJsonPath);
8
+ const config = await readNapiConfig(packageJsonPath);
9
+ for (const target of config.targets) {
10
+ const pkgDir = resolve(options.cwd, options.npmDir, target.platformArchABI);
11
+ debug(`Update version to %i in [%i]`, config.packageJson.version, pkgDir);
12
+ await updatePackageJson(join(pkgDir, 'package.json'), {
13
+ version: config.packageJson.version,
14
+ });
15
+ }
16
+ }
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,25 @@
1
+ import { Cli } from 'clipanion';
2
+ import { ArtifactsCommand } from './commands/artifacts.js';
3
+ import { BuildCommand } from './commands/build.js';
4
+ import { CreateNpmDirsCommand } from './commands/create-npm-dirs.js';
5
+ import { HelpCommand } from './commands/help.js';
6
+ import { NewCommand } from './commands/new.js';
7
+ import { PrePublishCommand } from './commands/pre-publish.js';
8
+ import { RenameCommand } from './commands/rename.js';
9
+ import { UniversalizeCommand } from './commands/universalize.js';
10
+ import { VersionCommand } from './commands/version.js';
11
+ import { CLI_VERSION } from './utils/misc.js';
12
+ const cli = new Cli({
13
+ binaryName: 'napi',
14
+ binaryVersion: CLI_VERSION,
15
+ });
16
+ cli.register(NewCommand);
17
+ cli.register(BuildCommand);
18
+ cli.register(CreateNpmDirsCommand);
19
+ cli.register(ArtifactsCommand);
20
+ cli.register(UniversalizeCommand);
21
+ cli.register(RenameCommand);
22
+ cli.register(PrePublishCommand);
23
+ cli.register(VersionCommand);
24
+ cli.register(HelpCommand);
25
+ void cli.runExit(process.argv.slice(2));
@@ -0,0 +1,6 @@
1
+ import { BaseArtifactsCommand } from '../def/artifacts.js';
2
+ export declare class ArtifactsCommand extends BaseArtifactsCommand {
3
+ static usage: import("clipanion").Usage;
4
+ static paths: string[][];
5
+ execute(): Promise<void>;
6
+ }
@@ -0,0 +1,19 @@
1
+ import { Command } from 'clipanion';
2
+ import { collectArtifacts } from '../api/artifacts.js';
3
+ import { BaseArtifactsCommand } from '../def/artifacts.js';
4
+ export class ArtifactsCommand extends BaseArtifactsCommand {
5
+ static usage = Command.Usage({
6
+ description: 'Copy artifacts from Github Actions into specified dir',
7
+ examples: [
8
+ [
9
+ '$0 artifacts --dir . --dist ./npm',
10
+ `Copy [binaryName].[platform].node under current dir(.) into packages under npm dir.
11
+ e.g: index.linux-x64-gnu.node --> ./npm/linux-x64-gnu/index.node`,
12
+ ],
13
+ ],
14
+ });
15
+ static paths = [['artifacts']];
16
+ async execute() {
17
+ await collectArtifacts(this.getOptions());
18
+ }
19
+ }
@@ -0,0 +1,6 @@
1
+ import { BaseBuildCommand } from '../def/build.js';
2
+ export declare class BuildCommand extends BaseBuildCommand {
3
+ pipe: string | undefined;
4
+ cargoOptions: string[];
5
+ execute(): Promise<void>;
6
+ }
@@ -0,0 +1,34 @@
1
+ import { execSync } from 'child_process';
2
+ import { Option } from 'clipanion';
3
+ import { buildProject } from '../api/build.js';
4
+ import { BaseBuildCommand } from '../def/build.js';
5
+ import { debugFactory } from '../utils/index.js';
6
+ const debug = debugFactory('build');
7
+ export class BuildCommand extends BaseBuildCommand {
8
+ pipe = Option.String('--pipe', {
9
+ description: 'Pipe all outputs file to given command. e.g. `napi build --pipe "npx prettier --write"`',
10
+ });
11
+ cargoOptions = Option.Rest();
12
+ async execute() {
13
+ const { task } = await buildProject({
14
+ ...this.getOptions(),
15
+ cargoOptions: this.cargoOptions,
16
+ });
17
+ const outputs = await task;
18
+ if (this.pipe) {
19
+ for (const output of outputs) {
20
+ debug('Piping output file to command: %s', this.pipe);
21
+ try {
22
+ execSync(`${this.pipe} ${output.path}`, {
23
+ stdio: 'inherit',
24
+ cwd: this.cwd,
25
+ });
26
+ }
27
+ catch (e) {
28
+ debug.error(`Failed to pipe output file ${output.path} to command`);
29
+ debug.error(e);
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
@@ -0,0 +1,4 @@
1
+ import { BaseCreateNpmDirsCommand } from '../def/create-npm-dirs.js';
2
+ export declare class CreateNpmDirsCommand extends BaseCreateNpmDirsCommand {
3
+ execute(): Promise<void>;
4
+ }
@@ -0,0 +1,7 @@
1
+ import { createNpmDirs } from '../api/create-npm-dirs.js';
2
+ import { BaseCreateNpmDirsCommand } from '../def/create-npm-dirs.js';
3
+ export class CreateNpmDirsCommand extends BaseCreateNpmDirsCommand {
4
+ async execute() {
5
+ await createNpmDirs(this.getOptions());
6
+ }
7
+ }
@@ -0,0 +1,10 @@
1
+ import { Command } from 'clipanion';
2
+ /**
3
+ * A command that prints the usage of all commands.
4
+ *
5
+ * Paths: `-h`, `--help`
6
+ */
7
+ export declare class HelpCommand extends Command<any> {
8
+ static paths: string[][];
9
+ execute(): Promise<void>;
10
+ }
@@ -0,0 +1,12 @@
1
+ import { Command } from 'clipanion';
2
+ /**
3
+ * A command that prints the usage of all commands.
4
+ *
5
+ * Paths: `-h`, `--help`
6
+ */
7
+ export class HelpCommand extends Command {
8
+ static paths = [[`-h`], [`--help`]];
9
+ async execute() {
10
+ await this.context.stdout.write(this.cli.usage());
11
+ }
12
+ }
@@ -0,0 +1,12 @@
1
+ import { BaseNewCommand } from '../def/new.js';
2
+ export declare class NewCommand extends BaseNewCommand {
3
+ interactive: boolean;
4
+ execute(): Promise<0 | 1>;
5
+ private fetchOptions;
6
+ private fetchName;
7
+ private fetchLicense;
8
+ private fetchNapiVersion;
9
+ private fetchTargets;
10
+ private fetchTypeDef;
11
+ private fetchGithubActions;
12
+ }
@@ -0,0 +1,112 @@
1
+ import path from 'path';
2
+ import { Option } from 'clipanion';
3
+ import inquirer from 'inquirer';
4
+ import { newProject } from '../api/new.js';
5
+ import { BaseNewCommand } from '../def/new.js';
6
+ import { AVAILABLE_TARGETS, debugFactory, DEFAULT_TARGETS, } from '../utils/index.js';
7
+ import { napiEngineRequirement } from '../utils/version.js';
8
+ const debug = debugFactory('new');
9
+ export class NewCommand extends BaseNewCommand {
10
+ interactive = Option.Boolean('--interactive,-i', false, {
11
+ description: 'Ask project basic information interactively without just using the default.',
12
+ });
13
+ async execute() {
14
+ try {
15
+ const options = await this.fetchOptions();
16
+ await newProject(options);
17
+ return 0;
18
+ }
19
+ catch (e) {
20
+ debug('Failed to create new project');
21
+ debug.error(e);
22
+ return 1;
23
+ }
24
+ }
25
+ async fetchOptions() {
26
+ const cmdOptions = super.getOptions();
27
+ if (this.interactive) {
28
+ return {
29
+ ...cmdOptions,
30
+ name: await this.fetchName(path.parse(cmdOptions.path).base),
31
+ minNodeApiVersion: await this.fetchNapiVersion(),
32
+ targets: await this.fetchTargets(),
33
+ license: await this.fetchLicense(),
34
+ enableTypeDef: await this.fetchTypeDef(),
35
+ enableGithubActions: await this.fetchGithubActions(),
36
+ };
37
+ }
38
+ return cmdOptions;
39
+ }
40
+ async fetchName(defaultName) {
41
+ return (this.$$name ??
42
+ (await inquirer
43
+ .prompt({
44
+ type: 'input',
45
+ name: 'name',
46
+ message: 'Package name (the name field in your package.json file)',
47
+ default: defaultName,
48
+ })
49
+ .then(({ name }) => name)));
50
+ }
51
+ async fetchLicense() {
52
+ return inquirer
53
+ .prompt({
54
+ type: 'input',
55
+ name: 'license',
56
+ message: 'License for open-sourced project',
57
+ default: this.license,
58
+ })
59
+ .then(({ license }) => license);
60
+ }
61
+ async fetchNapiVersion() {
62
+ return inquirer
63
+ .prompt({
64
+ type: 'list',
65
+ name: 'minNodeApiVersion',
66
+ message: 'Minimum node-api version (with node version requirement)',
67
+ loop: false,
68
+ choices: new Array(8).fill(0).map((_, i) => ({
69
+ name: `napi${i + 1} (${napiEngineRequirement(i + 1)})`,
70
+ value: i + 1,
71
+ })),
72
+ // choice index
73
+ default: this.minNodeApiVersion - 1,
74
+ })
75
+ .then(({ minNodeApiVersion }) => minNodeApiVersion);
76
+ }
77
+ async fetchTargets() {
78
+ if (this.enableDefaultTargets) {
79
+ return DEFAULT_TARGETS.concat();
80
+ }
81
+ if (this.enableAllTargets) {
82
+ return AVAILABLE_TARGETS.concat();
83
+ }
84
+ const { targets } = await inquirer.prompt({
85
+ name: 'targets',
86
+ type: 'checkbox',
87
+ loop: false,
88
+ message: 'Choose target(s) your crate will be compiled to',
89
+ default: DEFAULT_TARGETS,
90
+ choices: AVAILABLE_TARGETS,
91
+ });
92
+ return targets;
93
+ }
94
+ async fetchTypeDef() {
95
+ const { enableTypeDef } = await inquirer.prompt({
96
+ name: 'enableTypeDef',
97
+ type: 'confirm',
98
+ message: 'Enable type definition auto-generation',
99
+ default: this.enableTypeDef,
100
+ });
101
+ return enableTypeDef;
102
+ }
103
+ async fetchGithubActions() {
104
+ const { enableGithubActions } = await inquirer.prompt({
105
+ name: 'enableGithubActions',
106
+ type: 'confirm',
107
+ message: 'Enable Github Actions CI',
108
+ default: this.enableGithubActions,
109
+ });
110
+ return enableGithubActions;
111
+ }
112
+ }
@@ -0,0 +1,4 @@
1
+ import { BasePrePublishCommand } from '../def/pre-publish.js';
2
+ export declare class PrePublishCommand extends BasePrePublishCommand {
3
+ execute(): Promise<void>;
4
+ }
@@ -0,0 +1,8 @@
1
+ import { prePublish } from '../api/pre-publish.js';
2
+ import { BasePrePublishCommand } from '../def/pre-publish.js';
3
+ export class PrePublishCommand extends BasePrePublishCommand {
4
+ async execute() {
5
+ // @ts-expect-error const 'npm' | 'lerna' to string
6
+ await prePublish(this.getOptions());
7
+ }
8
+ }
@@ -0,0 +1,4 @@
1
+ import { BaseRenameCommand } from '../def/rename.js';
2
+ export declare class RenameCommand extends BaseRenameCommand {
3
+ execute(): Promise<void>;
4
+ }
@@ -0,0 +1,7 @@
1
+ import { renameProject } from '../api/rename.js';
2
+ import { BaseRenameCommand } from '../def/rename.js';
3
+ export class RenameCommand extends BaseRenameCommand {
4
+ async execute() {
5
+ await renameProject(this.getOptions());
6
+ }
7
+ }
@@ -0,0 +1,4 @@
1
+ import { BaseUniversalizeCommand } from '../def/universalize.js';
2
+ export declare class UniversalizeCommand extends BaseUniversalizeCommand {
3
+ execute(): Promise<void>;
4
+ }
@@ -0,0 +1,7 @@
1
+ import { universalizeBinaries } from '../api/universalize.js';
2
+ import { BaseUniversalizeCommand } from '../def/universalize.js';
3
+ export class UniversalizeCommand extends BaseUniversalizeCommand {
4
+ async execute() {
5
+ await universalizeBinaries(this.getOptions());
6
+ }
7
+ }
@@ -0,0 +1,4 @@
1
+ import { BaseVersionCommand } from '../def/version.js';
2
+ export declare class VersionCommand extends BaseVersionCommand {
3
+ execute(): Promise<void>;
4
+ }
@@ -0,0 +1,7 @@
1
+ import { version } from '../api/version.js';
2
+ import { BaseVersionCommand } from '../def/version.js';
3
+ export class VersionCommand extends BaseVersionCommand {
4
+ async execute() {
5
+ await version(this.getOptions());
6
+ }
7
+ }
@@ -0,0 +1,50 @@
1
+ import { Command } from 'clipanion';
2
+ export declare abstract class BaseArtifactsCommand extends Command {
3
+ static paths: string[][];
4
+ static usage: import("clipanion").Usage;
5
+ cwd: string;
6
+ packageJsonPath: string;
7
+ outputDir: string;
8
+ npmDir: string;
9
+ getOptions(): {
10
+ cwd: string;
11
+ packageJsonPath: string;
12
+ outputDir: string;
13
+ npmDir: string;
14
+ };
15
+ }
16
+ /**
17
+ * Copy artifacts from Github Actions into npm packages and ready to publish
18
+ */
19
+ export interface ArtifactsOptions {
20
+ /**
21
+ * The working directory of where napi command will be executed in, all other paths options are relative to this path
22
+ *
23
+ * @default process.cwd()
24
+ */
25
+ cwd?: string;
26
+ /**
27
+ * Path to `package.json`
28
+ *
29
+ * @default 'package.json'
30
+ */
31
+ packageJsonPath?: string;
32
+ /**
33
+ * Path to the folder where all built `.node` files put, same as `--output-dir` of build command
34
+ *
35
+ * @default './'
36
+ */
37
+ outputDir?: string;
38
+ /**
39
+ * Path to the folder where the npm packages put
40
+ *
41
+ * @default 'npm'
42
+ */
43
+ npmDir?: string;
44
+ }
45
+ export declare function applyDefaultArtifactsOptions(options: ArtifactsOptions): {
46
+ cwd: string;
47
+ packageJsonPath: string;
48
+ outputDir: string;
49
+ npmDir: string;
50
+ };
@@ -0,0 +1,38 @@
1
+ // This file is generated by codegen/index.ts
2
+ // Do not edit this file manually
3
+ import { Command, Option } from 'clipanion';
4
+ export class BaseArtifactsCommand extends Command {
5
+ static paths = [['artifacts']];
6
+ static usage = Command.Usage({
7
+ description: 'Copy artifacts from Github Actions into npm packages and ready to publish',
8
+ });
9
+ cwd = Option.String('--cwd', process.cwd(), {
10
+ description: 'The working directory of where napi command will be executed in, all other paths options are relative to this path',
11
+ });
12
+ packageJsonPath = Option.String('--package-json-path', 'package.json', {
13
+ description: 'Path to `package.json`',
14
+ });
15
+ outputDir = Option.String('--output-dir,-o', './', {
16
+ description: 'Path to the folder where all built `.node` files put, same as `--output-dir` of build command',
17
+ });
18
+ npmDir = Option.String('--npm-dir', 'npm', {
19
+ description: 'Path to the folder where the npm packages put',
20
+ });
21
+ getOptions() {
22
+ return {
23
+ cwd: this.cwd,
24
+ packageJsonPath: this.packageJsonPath,
25
+ outputDir: this.outputDir,
26
+ npmDir: this.npmDir,
27
+ };
28
+ }
29
+ }
30
+ export function applyDefaultArtifactsOptions(options) {
31
+ return {
32
+ cwd: process.cwd(),
33
+ packageJsonPath: 'package.json',
34
+ outputDir: './',
35
+ npmDir: 'npm',
36
+ ...options,
37
+ };
38
+ }