@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,158 @@
1
+ import { sortBy } from 'lodash-es';
2
+ import { readFileAsync } from './misc.js';
3
+ const TOP_LEVEL_NAMESPACE = '__TOP_LEVEL_MODULE__';
4
+ export const DEFAULT_TYPE_DEF_HEADER = `/* auto-generated by NAPI-RS */
5
+ /* eslint-disable */
6
+ `;
7
+ var TypeDefKind;
8
+ (function (TypeDefKind) {
9
+ TypeDefKind["Const"] = "const";
10
+ TypeDefKind["Enum"] = "enum";
11
+ TypeDefKind["Interface"] = "interface";
12
+ TypeDefKind["Fn"] = "fn";
13
+ TypeDefKind["Struct"] = "struct";
14
+ TypeDefKind["Impl"] = "impl";
15
+ })(TypeDefKind || (TypeDefKind = {}));
16
+ function prettyPrint(line, ident) {
17
+ let s = line.js_doc ?? '';
18
+ switch (line.kind) {
19
+ case TypeDefKind.Interface:
20
+ s += `export interface ${line.name} {\n${line.def}\n}`;
21
+ break;
22
+ case TypeDefKind.Enum:
23
+ s += `export const enum ${line.name} {\n${line.def}\n}`;
24
+ break;
25
+ case TypeDefKind.Struct:
26
+ s += `export class ${line.name} {\n${line.def}\n}`;
27
+ if (line.original_name && line.original_name !== line.name) {
28
+ s += `\nexport type ${line.original_name} = ${line.name}`;
29
+ }
30
+ break;
31
+ default:
32
+ s += line.def;
33
+ }
34
+ return correctStringIdent(s, ident);
35
+ }
36
+ export async function processTypeDef(intermediateTypeFile, header) {
37
+ const defs = await readIntermediateTypeFile(intermediateTypeFile);
38
+ const groupedDefs = preprocessTypeDef(defs);
39
+ header = header ? header + '\n' : '';
40
+ let dts = '';
41
+ sortBy(Array.from(groupedDefs), ([namespace]) => namespace).forEach(([namespace, defs]) => {
42
+ if (namespace === TOP_LEVEL_NAMESPACE) {
43
+ for (const def of defs) {
44
+ dts += prettyPrint(def, 0) + '\n\n';
45
+ }
46
+ }
47
+ else {
48
+ dts += `export namespace ${namespace} {\n`;
49
+ for (const def of defs) {
50
+ dts += prettyPrint(def, 2) + '\n';
51
+ }
52
+ dts += '}\n\n';
53
+ }
54
+ });
55
+ if (dts.indexOf('ExternalObject<') > -1) {
56
+ header += `
57
+ export class ExternalObject<T> {
58
+ readonly '': {
59
+ readonly '': unique symbol
60
+ [K: symbol]: T
61
+ }
62
+ }
63
+ `;
64
+ }
65
+ return header + dts;
66
+ }
67
+ async function readIntermediateTypeFile(file) {
68
+ const content = await readFileAsync(file, 'utf8');
69
+ const defs = content
70
+ .split('\n')
71
+ .filter(Boolean)
72
+ .map((line) => {
73
+ line = line.trim();
74
+ if (!line.startsWith('{')) {
75
+ // crateName:{ "def": "", ... }
76
+ const start = line.indexOf(':') + 1;
77
+ line = line.slice(start);
78
+ }
79
+ return JSON.parse(line);
80
+ });
81
+ // move all `struct` def to the very top
82
+ // and order the rest alphabetically.
83
+ return defs.sort((a, b) => {
84
+ if (a.kind === TypeDefKind.Struct) {
85
+ if (b.kind === TypeDefKind.Struct) {
86
+ return a.name.localeCompare(b.name);
87
+ }
88
+ return -1;
89
+ }
90
+ else if (b.kind === TypeDefKind.Struct) {
91
+ return 1;
92
+ }
93
+ else {
94
+ return a.name.localeCompare(b.name);
95
+ }
96
+ });
97
+ }
98
+ function preprocessTypeDef(defs) {
99
+ const namespaceGrouped = new Map();
100
+ const classDefs = new Map();
101
+ for (const def of defs) {
102
+ const namespace = def.js_mod ?? TOP_LEVEL_NAMESPACE;
103
+ if (!namespaceGrouped.has(namespace)) {
104
+ namespaceGrouped.set(namespace, []);
105
+ }
106
+ const group = namespaceGrouped.get(namespace);
107
+ if (def.kind === TypeDefKind.Struct) {
108
+ group.push(def);
109
+ classDefs.set(def.name, def);
110
+ }
111
+ else if (def.kind === TypeDefKind.Impl) {
112
+ // merge `impl` into class definition
113
+ const classDef = classDefs.get(def.name);
114
+ if (classDef) {
115
+ if (classDef.def) {
116
+ classDef.def += '\n';
117
+ }
118
+ classDef.def += def.def;
119
+ }
120
+ }
121
+ else {
122
+ group.push(def);
123
+ }
124
+ }
125
+ return namespaceGrouped;
126
+ }
127
+ export function correctStringIdent(src, ident) {
128
+ let bracketDepth = 0;
129
+ const result = src
130
+ .split('\n')
131
+ .map((line) => {
132
+ line = line.trim();
133
+ if (line === '') {
134
+ return '';
135
+ }
136
+ const isInMultilineComment = line.startsWith('*');
137
+ const isClosingBracket = line.endsWith('}');
138
+ const isOpeningBracket = line.endsWith('{');
139
+ let rightIndent = ident;
140
+ if (isOpeningBracket && !isInMultilineComment) {
141
+ bracketDepth += 1;
142
+ rightIndent += (bracketDepth - 1) * 2;
143
+ }
144
+ else {
145
+ if (isClosingBracket && bracketDepth > 0 && !isInMultilineComment) {
146
+ bracketDepth -= 1;
147
+ }
148
+ rightIndent += bracketDepth * 2;
149
+ }
150
+ if (isInMultilineComment) {
151
+ rightIndent += 1;
152
+ }
153
+ const s = `${' '.repeat(rightIndent)}${line}`;
154
+ return s;
155
+ })
156
+ .join('\n');
157
+ return result;
158
+ }
@@ -0,0 +1,11 @@
1
+ export declare enum NapiVersion {
2
+ Napi1 = 1,
3
+ Napi2 = 2,
4
+ Napi3 = 3,
5
+ Napi4 = 4,
6
+ Napi5 = 5,
7
+ Napi6 = 6,
8
+ Napi7 = 7,
9
+ Napi8 = 8
10
+ }
11
+ export declare function napiEngineRequirement(napiVersion: NapiVersion): string;
@@ -0,0 +1,59 @@
1
+ export var NapiVersion;
2
+ (function (NapiVersion) {
3
+ NapiVersion[NapiVersion["Napi1"] = 1] = "Napi1";
4
+ NapiVersion[NapiVersion["Napi2"] = 2] = "Napi2";
5
+ NapiVersion[NapiVersion["Napi3"] = 3] = "Napi3";
6
+ NapiVersion[NapiVersion["Napi4"] = 4] = "Napi4";
7
+ NapiVersion[NapiVersion["Napi5"] = 5] = "Napi5";
8
+ NapiVersion[NapiVersion["Napi6"] = 6] = "Napi6";
9
+ NapiVersion[NapiVersion["Napi7"] = 7] = "Napi7";
10
+ NapiVersion[NapiVersion["Napi8"] = 8] = "Napi8";
11
+ })(NapiVersion = NapiVersion || (NapiVersion = {}));
12
+ /// because node support new napi version in some minor version updates, so we might meet such situation:
13
+ /// `node v10.20.0` supports `napi5` and `napi6`, but `node v12.0.0` only support `napi4`,
14
+ /// by which, we can not tell directly napi version supportness from node version directly.
15
+ const NAPI_VERSION_MATRIX = new Map([
16
+ [NapiVersion.Napi1, '8.6.0'],
17
+ [NapiVersion.Napi2, '8.10.0 | 9.3.0'],
18
+ [NapiVersion.Napi3, '6.14.2 | 8.11.2 | 9.11.0'],
19
+ [NapiVersion.Napi4, '10.16.0 | 11.8.0'],
20
+ [NapiVersion.Napi5, '10.17.0 | 12.11.0'],
21
+ [NapiVersion.Napi6, '10.20.0 | 12.17.0 | 14.0.0'],
22
+ [NapiVersion.Napi7, '10.23.0 | 12.19.0 | 14.12.0'],
23
+ [NapiVersion.Napi8, '12.22.0 | 14.17.0 | 15.12.0'],
24
+ ]);
25
+ function parseNodeVersion(v) {
26
+ const matches = v.match(/v?([0-9]+)\.([0-9]+)\.([0-9]+)/i);
27
+ if (!matches) {
28
+ throw new Error('Unknown node version number: ' + v);
29
+ }
30
+ const [, major, minor, patch] = matches;
31
+ return {
32
+ major: parseInt(major),
33
+ minor: parseInt(minor),
34
+ patch: parseInt(patch),
35
+ };
36
+ }
37
+ function requiredNodeVersions(napiVersion) {
38
+ const requirement = NAPI_VERSION_MATRIX.get(napiVersion);
39
+ if (!requirement) {
40
+ return [parseNodeVersion('10.0.0')];
41
+ }
42
+ return requirement.split('|').map(parseNodeVersion);
43
+ }
44
+ function toEngineRequirement(versions) {
45
+ const requirements = [];
46
+ versions.forEach((v, i) => {
47
+ let req = '';
48
+ if (i !== 0) {
49
+ const lastVersion = versions[i - 1];
50
+ req += `< ${lastVersion.major + 1}`;
51
+ }
52
+ req += `${i === 0 ? '' : ' || '}>= ${v.major}.${v.minor}.${v.patch}`;
53
+ requirements.push(req);
54
+ });
55
+ return requirements.join(' && ');
56
+ }
57
+ export function napiEngineRequirement(napiVersion) {
58
+ return toEngineRequirement(requiredNodeVersions(napiVersion));
59
+ }
package/package.json CHANGED
@@ -1,31 +1,58 @@
1
1
  {
2
2
  "name": "@napi-rs/cli",
3
- "version": "2.15.1",
3
+ "version": "3.0.0-alpha.0",
4
4
  "description": "Cli tools for napi-rs",
5
- "keywords": [
6
- "cli",
7
- "rust",
8
- "napi",
9
- "n-api",
10
- "neon"
11
- ],
12
5
  "author": "LongYinan <lynweklm@gmail.com>",
13
6
  "homepage": "https://github.com/napi-rs/napi-rs",
14
7
  "license": "MIT",
8
+ "type": "module",
9
+ "engines": {
10
+ "node": ">= 16"
11
+ },
15
12
  "bin": {
16
- "napi": "./scripts/index.js"
13
+ "napi": "./dist/cli.js",
14
+ "napi-raw": "./cli.mjs"
15
+ },
16
+ "main": "./dist/index.cjs",
17
+ "module": "./dist/index.js",
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "default": "./dist/index.js",
22
+ "types": "./dist/index.d.ts"
23
+ },
24
+ "require": {
25
+ "default": "./dist/index.cjs",
26
+ "types": "./dist/index.d.ts"
27
+ }
28
+ },
29
+ "./package.json": {
30
+ "import": "./package.json",
31
+ "require": "./package.json"
32
+ }
17
33
  },
18
34
  "files": [
19
- "scripts"
35
+ "dist",
36
+ "src"
37
+ ],
38
+ "keywords": [
39
+ "cli",
40
+ "rust",
41
+ "napi",
42
+ "n-api",
43
+ "node-api",
44
+ "node-addon",
45
+ "neon"
20
46
  ],
21
- "engines": {
22
- "node": ">= 10"
23
- },
24
47
  "maintainers": [
25
48
  {
26
49
  "name": "LongYinan",
27
50
  "email": "lynweklm@gmail.com",
28
51
  "homepage": "https://github.com/Brooooooklyn"
52
+ },
53
+ {
54
+ "name": "forehalo",
55
+ "homepage": "https://github.com/forehalo"
29
56
  }
30
57
  ],
31
58
  "repository": {
@@ -39,26 +66,35 @@
39
66
  "bugs": {
40
67
  "url": "https://github.com/napi-rs/napi-rs/issues"
41
68
  },
42
- "devDependencies": {
43
- "@octokit/rest": "^19.0.5",
44
- "@types/inquirer": "^9.0.3",
45
- "@types/js-yaml": "^4.0.5",
46
- "@types/lodash-es": "^4.17.6",
47
- "clipanion": "^3.1.0",
69
+ "dependencies": {
70
+ "@octokit/rest": "^19.0.7",
71
+ "clipanion": "^3.2.0",
48
72
  "colorette": "^2.0.19",
49
- "core-js": "^3.27.1",
50
73
  "debug": "^4.3.4",
51
- "env-paths": "^3.0.0",
52
- "fdir": "^5.3.0",
53
- "inquirer": "^9.1.4",
74
+ "inquirer": "^9.1.5",
54
75
  "js-yaml": "^4.1.0",
55
- "lodash-es": "4.17.21",
56
- "toml": "^3.0.0",
57
- "tslib": "^2.4.1",
76
+ "lodash-es": "^4.17.21",
58
77
  "typanion": "^3.12.1"
59
78
  },
79
+ "devDependencies": {
80
+ "@types/inquirer": "^9.0.3",
81
+ "@types/js-yaml": "^4.0.5",
82
+ "@types/lodash-es": "^4.17.6",
83
+ "ava": "^5.2.0",
84
+ "esbuild": "^0.17.14",
85
+ "prettier": "^2.8.7",
86
+ "ts-node": "^10.9.1",
87
+ "typescript": "^4.9.4"
88
+ },
60
89
  "funding": {
61
90
  "type": "github",
62
91
  "url": "https://github.com/sponsors/Brooooooklyn"
63
- }
92
+ },
93
+ "scripts": {
94
+ "codegen": "node --loader ts-node/esm/transpile-only ./codegen/index.ts",
95
+ "build": "tsc && yarn build:cjs",
96
+ "build:cjs": "node ./esbuild.mjs",
97
+ "test": "ava"
98
+ },
99
+ "gitHead": "7c9a4bae2da98752dbf683b1d8c9add81f30a61e"
64
100
  }
@@ -0,0 +1,93 @@
1
+ import { join, parse, resolve } from 'path'
2
+
3
+ import * as colors from 'colorette'
4
+
5
+ import {
6
+ applyDefaultArtifactsOptions,
7
+ ArtifactsOptions,
8
+ } from '../def/artifacts.js'
9
+ import {
10
+ readNapiConfig,
11
+ debugFactory,
12
+ readFileAsync,
13
+ writeFileAsync,
14
+ UniArchsByPlatform,
15
+ readdirAsync,
16
+ } from '../utils/index.js'
17
+
18
+ const debug = debugFactory('artifacts')
19
+
20
+ export async function collectArtifacts(userOptions: ArtifactsOptions) {
21
+ const options = applyDefaultArtifactsOptions(userOptions)
22
+
23
+ const packageJsonPath = resolve(options.cwd, options.packageJsonPath)
24
+ const { targets, binaryName } = await readNapiConfig(packageJsonPath)
25
+
26
+ const distDirs = targets.map((platform) =>
27
+ resolve(options.cwd, options.npmDir, platform.platformArchABI),
28
+ )
29
+
30
+ const universalSourceBins = new Set(
31
+ targets
32
+ .filter((platform) => platform.arch === 'universal')
33
+ .flatMap((p) =>
34
+ UniArchsByPlatform[p.platform]?.map((a) => `${p.platform}-${a}`),
35
+ )
36
+ .filter(Boolean) as string[],
37
+ )
38
+
39
+ await collectNodeBinaries(resolve(options.cwd, options.outputDir)).then(
40
+ (output) =>
41
+ Promise.all(
42
+ output.map(async (filePath) => {
43
+ debug.info(`Read [${colors.yellowBright(filePath)}]`)
44
+ const sourceContent = await readFileAsync(filePath)
45
+ const parsedName = parse(filePath)
46
+ const [_binaryName, platformArchABI] = parsedName.name.split('.')
47
+ if (_binaryName !== binaryName) {
48
+ debug.warn(
49
+ `[${_binaryName}] is not matched with [${binaryName}], skip`,
50
+ )
51
+ return
52
+ }
53
+ const dir = distDirs.find((dir) => dir.includes(platformArchABI))
54
+ if (!dir && universalSourceBins.has(platformArchABI)) {
55
+ debug.warn(
56
+ `[${platformArchABI}] has no dist dir but it is source bin for universal arch, skip`,
57
+ )
58
+ return
59
+ }
60
+ if (!dir) {
61
+ throw new Error(`No dist dir found for ${filePath}`)
62
+ }
63
+
64
+ const distFilePath = join(dir, parsedName.base)
65
+ debug.info(
66
+ `Write file content to [${colors.yellowBright(distFilePath)}]`,
67
+ )
68
+ await writeFileAsync(distFilePath, sourceContent)
69
+ const distFilePathLocal = join(
70
+ parse(packageJsonPath).dir,
71
+ parsedName.base,
72
+ )
73
+ debug.info(
74
+ `Write file content to [${colors.yellowBright(distFilePathLocal)}]`,
75
+ )
76
+ await writeFileAsync(distFilePathLocal, sourceContent)
77
+ }),
78
+ ),
79
+ )
80
+ }
81
+
82
+ async function collectNodeBinaries(root: string) {
83
+ const files = await readdirAsync(root, { withFileTypes: true })
84
+ const nodeBinaries = files
85
+ .filter((file) => file.isFile() && file.name.endsWith('.node'))
86
+ .map((file) => join(root, file.name))
87
+
88
+ const dirs = files.filter((file) => file.isDirectory())
89
+ for (const dir of dirs) {
90
+ nodeBinaries.push(...(await collectNodeBinaries(join(root, dir.name))))
91
+ }
92
+ return nodeBinaries
93
+ }