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