@napi-rs/cli 3.0.0-alpha.4 → 3.0.0-alpha.5

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 (39) hide show
  1. package/cli.mjs +0 -0
  2. package/dist/api/build.js +26 -12
  3. package/dist/api/templates/ci-template.js +1 -3
  4. package/dist/api/templates/ci.yml.js +8 -2
  5. package/dist/api/templates/js-binding.d.ts +1 -1
  6. package/dist/api/templates/js-binding.js +4 -3
  7. package/dist/api/templates/package.json.js +29 -27
  8. package/dist/commands/artifacts.js +1 -2
  9. package/dist/commands/help.js +1 -2
  10. package/dist/commands/new.js +1 -4
  11. package/dist/def/artifacts.js +1 -2
  12. package/dist/def/build.js +1 -2
  13. package/dist/def/create-npm-dirs.js +1 -2
  14. package/dist/def/new.js +1 -2
  15. package/dist/def/pre-publish.js +1 -2
  16. package/dist/def/rename.js +1 -2
  17. package/dist/def/universalize.js +1 -2
  18. package/dist/def/version.js +1 -2
  19. package/dist/index.cjs +2400 -7660
  20. package/dist/utils/__tests__/typegen.spec.js +1 -1
  21. package/dist/utils/config.d.ts +9 -1
  22. package/dist/utils/typegen.d.ts +4 -1
  23. package/dist/utils/typegen.js +20 -1
  24. package/dist/utils/version.js +2 -2
  25. package/package.json +13 -13
  26. package/src/api/artifacts.ts +2 -2
  27. package/src/api/build.ts +34 -11
  28. package/src/api/pre-publish.ts +8 -5
  29. package/src/api/templates/ci-template.ts +1 -3
  30. package/src/api/templates/ci.yml.ts +8 -2
  31. package/src/api/templates/js-binding.ts +8 -3
  32. package/src/api/templates/package.json.ts +31 -26
  33. package/src/commands/new.ts +1 -5
  34. package/src/utils/__tests__/__snapshots__/version.spec.ts.md +7 -7
  35. package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
  36. package/src/utils/__tests__/typegen.spec.ts +1 -1
  37. package/src/utils/config.ts +12 -1
  38. package/src/utils/typegen.ts +20 -1
  39. package/src/utils/version.ts +1 -1
package/cli.mjs CHANGED
File without changes
package/dist/api/build.js CHANGED
@@ -4,7 +4,7 @@ import { tmpdir } from 'os';
4
4
  import { parse, join, resolve } from 'path';
5
5
  import * as colors from 'colorette';
6
6
  import { CLI_VERSION, copyFileAsync, debugFactory, DEFAULT_TYPE_DEF_HEADER, fileExists, getSystemDefaultTarget, getTargetLinker, mkdirAsync, parseMetadata, parseTriple, processTypeDef, readNapiConfig, targetToEnvVar, tryInstallCargoBinary, unlinkAsync, writeFileAsync, } from '../utils/index.js';
7
- import { createJsBinding } from './templates/index.js';
7
+ import { createCjsBinding } from './templates/index.js';
8
8
  const debug = debugFactory('build');
9
9
  export async function buildProject(options) {
10
10
  debug('napi build command receive options: %O', options);
@@ -89,6 +89,7 @@ class Builder {
89
89
  debug(`Start building crate: ${this.crate.name}`);
90
90
  debug(' %i', `cargo ${this.args.join(' ')}`);
91
91
  const controller = new AbortController();
92
+ const watch = this.options.watch;
92
93
  const buildTask = new Promise((resolve, reject) => {
93
94
  const command = process.env.CARGO ?? (this.options.useCross ? 'cross' : 'cargo');
94
95
  const buildProcess = spawn(command, this.args, {
@@ -96,7 +97,7 @@ class Builder {
96
97
  ...process.env,
97
98
  ...this.envs,
98
99
  },
99
- stdio: 'inherit',
100
+ stdio: watch ? ['inherit', 'inherit', 'pipe'] : 'inherit',
100
101
  cwd: this.cwd,
101
102
  signal: controller.signal,
102
103
  });
@@ -114,6 +115,14 @@ class Builder {
114
115
  cause: e,
115
116
  }));
116
117
  });
118
+ // watch mode only, they are piped through stderr
119
+ buildProcess.stderr?.on('data', (data) => {
120
+ const output = data.toString();
121
+ console.error(output);
122
+ if (/Finished\s(dev|release)/.test(output)) {
123
+ this.postBuild().catch(() => { });
124
+ }
125
+ });
117
126
  });
118
127
  return {
119
128
  task: buildTask.then(() => this.postBuild()),
@@ -133,7 +142,7 @@ class Builder {
133
142
  // ===>
134
143
  // cargo watch [...] -- build --target x86_64-unknown-linux-gnu
135
144
  // cargo watch [...] -- zigbuild --target x86_64-unknown-linux-gnu
136
- this.args.push('watch', '--why', '-i', '*.{js,ts,node}', '-w', this.crateDir, '--', 'cargo');
145
+ this.args.push('watch', '--why', '-i', '*.{js,ts,node}', '-w', this.crateDir, '--', 'cargo', 'build');
137
146
  set = true;
138
147
  }
139
148
  }
@@ -325,8 +334,8 @@ class Builder {
325
334
  await this.copyArtifact();
326
335
  // only for cdylib
327
336
  if (this.cdyLibName) {
328
- await this.generateTypeDef();
329
- await this.writeJsBinding();
337
+ const idents = await this.generateTypeDef();
338
+ await this.writeJsBinding(idents);
330
339
  }
331
340
  return this.outputs;
332
341
  }
@@ -384,10 +393,10 @@ class Builder {
384
393
  }
385
394
  async generateTypeDef() {
386
395
  if (!(await fileExists(this.envs.TYPE_DEF_TMP_PATH))) {
387
- return;
396
+ return [];
388
397
  }
389
398
  const dest = join(this.outputDir, this.options.dts ?? 'index.d.ts');
390
- const dts = await processTypeDef(this.envs.TYPE_DEF_TMP_PATH, !this.options.noDtsHeader
399
+ const { dts, exports } = await processTypeDef(this.envs.TYPE_DEF_TMP_PATH, !this.options.noDtsHeader
391
400
  ? this.options.dtsHeader ?? DEFAULT_TYPE_DEF_HEADER
392
401
  : '');
393
402
  try {
@@ -403,17 +412,22 @@ class Builder {
403
412
  debug.error('Failed to write type def file');
404
413
  debug.error(e);
405
414
  }
415
+ return exports;
406
416
  }
407
- async writeJsBinding() {
408
- if (!this.options.platform || this.options.noJsBinding) {
417
+ async writeJsBinding(idents) {
418
+ if (!this.options.platform ||
419
+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
420
+ this.options.noJsBinding ||
421
+ idents.length === 0) {
409
422
  return;
410
423
  }
411
- const dest = join(this.outputDir, this.options.jsBinding ?? 'index.js');
412
- const js = createJsBinding(this.config.binaryName, this.config.packageName);
424
+ const name = parse(this.options.jsBinding ?? 'index.js').name;
425
+ const cjs = createCjsBinding(this.config.binaryName, this.config.packageName, idents);
413
426
  try {
427
+ const dest = join(this.outputDir, `${name}.js`);
414
428
  debug('Writing js binding to:');
415
429
  debug(' %i', dest);
416
- await writeFileAsync(dest, js, 'utf-8');
430
+ await writeFileAsync(dest, cjs, 'utf-8');
417
431
  this.outputs.push({
418
432
  kind: 'js',
419
433
  path: dest,
@@ -198,9 +198,7 @@ jobs:
198
198
  usesh: true
199
199
  mem: 3000
200
200
  prepare: |
201
- pkg install -y -f curl node libnghttp2
202
- curl -qL https://www.npmjs.com/install.sh | sh
203
- npm install --location=global --ignore-scripts yarn
201
+ pkg install -y -f curl node libnghttp2 npm yarn
204
202
  curl https://sh.rustup.rs -sSf --output rustup.sh
205
203
  sh rustup.sh -y --profile minimal --default-toolchain beta
206
204
  export PATH="/usr/local/cargo/bin:$PATH"
@@ -44,9 +44,15 @@ export const createGithubActionsCIYml = (binaryName, targets) => {
44
44
  delete fullTemplate.jobs[TEST_MACOS_WINDOWS];
45
45
  }
46
46
  else {
47
- const filterTarget = enableWindowsX86 ? 'macos-latest' : 'windows-latest';
47
+ const filterTargets = new Set();
48
+ if (enableWindowsX86) {
49
+ filterTargets.add('windows-latest');
50
+ }
51
+ if (enableMacOSUni || enableMacOSX86) {
52
+ filterTargets.add('macos-latest');
53
+ }
48
54
  fullTemplate.jobs[TEST_MACOS_WINDOWS].strategy.matrix.settings =
49
- fullTemplate.jobs[TEST_MACOS_WINDOWS].strategy.matrix.settings.filter(({ host }) => host !== filterTarget);
55
+ fullTemplate.jobs[TEST_MACOS_WINDOWS].strategy.matrix.settings.filter(({ host }) => filterTargets.has(host));
50
56
  requiredSteps.push(TEST_MACOS_WINDOWS);
51
57
  }
52
58
  if (!enableLinuxX86Gnu) {
@@ -1 +1 @@
1
- export declare function createJsBinding(localName: string, pkgName: string): string;
1
+ export declare function createCjsBinding(localName: string, pkgName: string, idents: string[]): string;
@@ -1,8 +1,6 @@
1
- export function createJsBinding(localName, pkgName) {
1
+ export function createCjsBinding(localName, pkgName, idents) {
2
2
  return `// prettier-ignore
3
- /* tslint:disable */
4
3
  /* eslint-disable */
5
-
6
4
  /* auto-generated by NAPI-RS */
7
5
 
8
6
  const { existsSync, readFileSync } = require('fs')
@@ -253,5 +251,8 @@ if (!nativeBinding) {
253
251
  throw new Error(\`Failed to load native binding\`)
254
252
  }
255
253
 
254
+ ${idents
255
+ .map((ident) => `module.exports.${ident} = binding.${ident}`)
256
+ .join('\n')}
256
257
  `;
257
258
  }
@@ -1,29 +1,31 @@
1
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
- "binaryName": "${binaryName}",
13
- "targets": [
14
- ${targets.map((t) => `"${t}"`).join(',\n ')}
15
- ]
16
- },
17
- "scripts": {
18
- "test": "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
- "version": "napi version"
24
- },
25
- "devDependencies": {
26
- "@napi-rs/cli": "^${cliVersion}"
27
- }
28
- }`;
2
+ const content = {
3
+ name,
4
+ version: '1.0.0',
5
+ license,
6
+ engines: {
7
+ node: engineRequirement,
8
+ },
9
+ type: 'commonjs',
10
+ main: 'index.js',
11
+ types: 'index.d.ts',
12
+ module: undefined,
13
+ exports: undefined,
14
+ napi: {
15
+ binaryName,
16
+ targets,
17
+ },
18
+ scripts: {
19
+ test: 'node -e "assert(require(\'.\').sum(1, 2) === 3)"',
20
+ build: 'napi build --release --platform --strip',
21
+ 'build:debug': 'napi build',
22
+ prepublishOnly: 'napi prepublish -t npm',
23
+ artifacts: 'napi artifacts',
24
+ version: 'napi version',
25
+ },
26
+ devDependencies: {
27
+ '@napi-rs/cli': `^${cliVersion}`,
28
+ },
29
+ };
30
+ return JSON.stringify(content, null, 2);
29
31
  };
@@ -1,7 +1,7 @@
1
1
  import { Command } from 'clipanion';
2
2
  import { collectArtifacts } from '../api/artifacts.js';
3
3
  import { BaseArtifactsCommand } from '../def/artifacts.js';
4
- class ArtifactsCommand extends BaseArtifactsCommand {
4
+ export class ArtifactsCommand extends BaseArtifactsCommand {
5
5
  static usage = Command.Usage({
6
6
  description: 'Copy artifacts from Github Actions into specified dir',
7
7
  examples: [
@@ -17,4 +17,3 @@ e.g: index.linux-x64-gnu.node --> ./npm/linux-x64-gnu/index.node`,
17
17
  await collectArtifacts(this.getOptions());
18
18
  }
19
19
  }
20
- export { ArtifactsCommand };
@@ -4,10 +4,9 @@ import { Command } from 'clipanion';
4
4
  *
5
5
  * Paths: `-h`, `--help`
6
6
  */
7
- class HelpCommand extends Command {
7
+ export class HelpCommand extends Command {
8
8
  static paths = [[`-h`], [`--help`]];
9
9
  async execute() {
10
10
  await this.context.stdout.write(this.cli.usage());
11
11
  }
12
12
  }
13
- export { HelpCommand };
@@ -75,9 +75,6 @@ export class NewCommand extends BaseNewCommand {
75
75
  .then(({ minNodeApiVersion }) => minNodeApiVersion);
76
76
  }
77
77
  async fetchTargets() {
78
- if (this.enableDefaultTargets) {
79
- return DEFAULT_TARGETS.concat();
80
- }
81
78
  if (this.enableAllTargets) {
82
79
  return AVAILABLE_TARGETS.concat();
83
80
  }
@@ -86,7 +83,7 @@ export class NewCommand extends BaseNewCommand {
86
83
  type: 'checkbox',
87
84
  loop: false,
88
85
  message: 'Choose target(s) your crate will be compiled to',
89
- default: DEFAULT_TARGETS,
86
+ default: this.enableDefaultTargets ? DEFAULT_TARGETS : [],
90
87
  choices: AVAILABLE_TARGETS,
91
88
  });
92
89
  return targets;
@@ -1,7 +1,7 @@
1
1
  // This file is generated by codegen/index.ts
2
2
  // Do not edit this file manually
3
3
  import { Command, Option } from 'clipanion';
4
- class BaseArtifactsCommand extends Command {
4
+ export class BaseArtifactsCommand extends Command {
5
5
  static paths = [['artifacts']];
6
6
  static usage = Command.Usage({
7
7
  description: 'Copy artifacts from Github Actions into npm packages and ready to publish',
@@ -27,7 +27,6 @@ class BaseArtifactsCommand extends Command {
27
27
  };
28
28
  }
29
29
  }
30
- export { BaseArtifactsCommand };
31
30
  export function applyDefaultArtifactsOptions(options) {
32
31
  return {
33
32
  cwd: process.cwd(),
package/dist/def/build.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // This file is generated by codegen/index.ts
2
2
  // Do not edit this file manually
3
3
  import { Command, Option } from 'clipanion';
4
- class BaseBuildCommand extends Command {
4
+ export class BaseBuildCommand extends Command {
5
5
  static paths = [['build']];
6
6
  static usage = Command.Usage({
7
7
  description: 'Build the napi-rs project',
@@ -111,4 +111,3 @@ class BaseBuildCommand extends Command {
111
111
  };
112
112
  }
113
113
  }
114
- export { BaseBuildCommand };
@@ -1,7 +1,7 @@
1
1
  // This file is generated by codegen/index.ts
2
2
  // Do not edit this file manually
3
3
  import { Command, Option } from 'clipanion';
4
- class BaseCreateNpmDirsCommand extends Command {
4
+ export class BaseCreateNpmDirsCommand extends Command {
5
5
  static paths = [['create-npm-dirs']];
6
6
  static usage = Command.Usage({
7
7
  description: 'Create npm package dirs for different platforms',
@@ -27,7 +27,6 @@ class BaseCreateNpmDirsCommand extends Command {
27
27
  };
28
28
  }
29
29
  }
30
- export { BaseCreateNpmDirsCommand };
31
30
  export function applyDefaultCreateNpmDirsOptions(options) {
32
31
  return {
33
32
  cwd: process.cwd(),
package/dist/def/new.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // Do not edit this file manually
3
3
  import { Command, Option } from 'clipanion';
4
4
  import * as typanion from 'typanion';
5
- class BaseNewCommand extends Command {
5
+ export class BaseNewCommand extends Command {
6
6
  static paths = [['new']];
7
7
  static usage = Command.Usage({
8
8
  description: 'Create a new project with pre-configured boilerplate',
@@ -51,7 +51,6 @@ class BaseNewCommand extends Command {
51
51
  };
52
52
  }
53
53
  }
54
- export { BaseNewCommand };
55
54
  export function applyDefaultNewOptions(options) {
56
55
  return {
57
56
  minNodeApiVersion: 4,
@@ -1,7 +1,7 @@
1
1
  // This file is generated by codegen/index.ts
2
2
  // Do not edit this file manually
3
3
  import { Command, Option } from 'clipanion';
4
- class BasePrePublishCommand extends Command {
4
+ export class BasePrePublishCommand extends Command {
5
5
  static paths = [['pre-publish']];
6
6
  static usage = Command.Usage({
7
7
  description: 'Update package.json and copy addons into per platform packages',
@@ -43,7 +43,6 @@ class BasePrePublishCommand extends Command {
43
43
  };
44
44
  }
45
45
  }
46
- export { BasePrePublishCommand };
47
46
  export function applyDefaultPrePublishOptions(options) {
48
47
  return {
49
48
  cwd: process.cwd(),
@@ -1,7 +1,7 @@
1
1
  // This file is generated by codegen/index.ts
2
2
  // Do not edit this file manually
3
3
  import { Command, Option } from 'clipanion';
4
- class BaseRenameCommand extends Command {
4
+ export class BaseRenameCommand extends Command {
5
5
  static paths = [['rename']];
6
6
  static usage = Command.Usage({
7
7
  description: 'Rename the napi-rs project',
@@ -47,7 +47,6 @@ class BaseRenameCommand extends Command {
47
47
  };
48
48
  }
49
49
  }
50
- export { BaseRenameCommand };
51
50
  export function applyDefaultRenameOptions(options) {
52
51
  return {
53
52
  cwd: process.cwd(),
@@ -1,7 +1,7 @@
1
1
  // This file is generated by codegen/index.ts
2
2
  // Do not edit this file manually
3
3
  import { Command, Option } from 'clipanion';
4
- class BaseUniversalizeCommand extends Command {
4
+ export class BaseUniversalizeCommand extends Command {
5
5
  static paths = [['universalize']];
6
6
  static usage = Command.Usage({
7
7
  description: 'Combile built binaries into one universal binary',
@@ -23,7 +23,6 @@ class BaseUniversalizeCommand extends Command {
23
23
  };
24
24
  }
25
25
  }
26
- export { BaseUniversalizeCommand };
27
26
  export function applyDefaultUniversalizeOptions(options) {
28
27
  return {
29
28
  cwd: process.cwd(),
@@ -1,7 +1,7 @@
1
1
  // This file is generated by codegen/index.ts
2
2
  // Do not edit this file manually
3
3
  import { Command, Option } from 'clipanion';
4
- class BaseVersionCommand extends Command {
4
+ export class BaseVersionCommand extends Command {
5
5
  static paths = [['version']];
6
6
  static usage = Command.Usage({
7
7
  description: 'Update version in created npm packages',
@@ -23,7 +23,6 @@ class BaseVersionCommand extends Command {
23
23
  };
24
24
  }
25
25
  }
26
- export { BaseVersionCommand };
27
26
  export function applyDefaultVersionOptions(options) {
28
27
  return {
29
28
  cwd: process.cwd(),