@napi-rs/cli 3.0.0-alpha.5 → 3.0.0-alpha.50
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.
- package/dist/api/artifacts.js +32 -8
- package/dist/api/artifacts.js.map +1 -0
- package/dist/api/build.d.ts +1 -1
- package/dist/api/build.js +249 -25
- package/dist/api/build.js.map +1 -0
- package/dist/api/create-npm-dirs.js +46 -8
- package/dist/api/create-npm-dirs.js.map +1 -0
- package/dist/api/new.js +58 -20
- package/dist/api/new.js.map +1 -0
- package/dist/api/pre-publish.js +10 -7
- package/dist/api/pre-publish.js.map +1 -0
- package/dist/api/rename.js +10 -1
- package/dist/api/rename.js.map +1 -0
- package/dist/api/templates/.gitignore.d.ts +1 -1
- package/dist/api/templates/.gitignore.js +2 -0
- package/dist/api/templates/.gitignore.js.map +1 -0
- package/dist/api/templates/.npmignore.js +1 -0
- package/dist/api/templates/.npmignore.js.map +1 -0
- package/dist/api/templates/build.rs.js +1 -0
- package/dist/api/templates/build.rs.js.map +1 -0
- package/dist/api/templates/cargo.toml.js +1 -0
- package/dist/api/templates/cargo.toml.js.map +1 -0
- package/dist/api/templates/ci-template.d.ts +3 -1
- package/dist/api/templates/ci-template.js +167 -150
- package/dist/api/templates/ci-template.js.map +1 -0
- package/dist/api/templates/ci.yml.d.ts +3 -1
- package/dist/api/templates/ci.yml.js +13 -2
- package/dist/api/templates/ci.yml.js.map +1 -0
- package/dist/api/templates/index.js +1 -0
- package/dist/api/templates/index.js.map +1 -0
- package/dist/api/templates/js-binding.js +150 -225
- package/dist/api/templates/js-binding.js.map +1 -0
- package/dist/api/templates/lib.rs.js +2 -4
- package/dist/api/templates/lib.rs.js.map +1 -0
- package/dist/api/templates/load-wasi-template.d.ts +2 -0
- package/dist/api/templates/load-wasi-template.js +146 -0
- package/dist/api/templates/load-wasi-template.js.map +1 -0
- package/dist/api/templates/package.json.d.ts +9 -2
- package/dist/api/templates/package.json.js +27 -4
- package/dist/api/templates/package.json.js.map +1 -0
- package/dist/api/templates/wasi-worker-template.d.ts +2 -0
- package/dist/api/templates/wasi-worker-template.js +103 -0
- package/dist/api/templates/wasi-worker-template.js.map +1 -0
- package/dist/api/universalize.js +6 -5
- package/dist/api/universalize.js.map +1 -0
- package/dist/api/version.js +3 -2
- package/dist/api/version.js.map +1 -0
- package/dist/cli.js +1 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/artifacts.js +3 -2
- package/dist/commands/artifacts.js.map +1 -0
- package/dist/commands/build.js +2 -1
- package/dist/commands/build.js.map +1 -0
- package/dist/commands/create-npm-dirs.js +1 -0
- package/dist/commands/create-npm-dirs.js.map +1 -0
- package/dist/commands/help.js +1 -0
- package/dist/commands/help.js.map +1 -0
- package/dist/commands/new.js +17 -6
- package/dist/commands/new.js.map +1 -0
- package/dist/commands/pre-publish.js +1 -0
- package/dist/commands/pre-publish.js.map +1 -0
- package/dist/commands/rename.js +1 -0
- package/dist/commands/rename.js.map +1 -0
- package/dist/commands/universalize.js +1 -0
- package/dist/commands/universalize.js.map +1 -0
- package/dist/commands/version.js +1 -0
- package/dist/commands/version.js.map +1 -0
- package/dist/def/artifacts.d.ts +21 -1
- package/dist/def/artifacts.js +11 -2
- package/dist/def/artifacts.js.map +1 -0
- package/dist/def/build.d.ts +20 -2
- package/dist/def/build.js +15 -2
- package/dist/def/build.js.map +1 -0
- package/dist/def/create-npm-dirs.d.ts +10 -0
- package/dist/def/create-npm-dirs.js +5 -0
- package/dist/def/create-npm-dirs.js.map +1 -0
- package/dist/def/new.d.ts +24 -6
- package/dist/def/new.js +12 -1
- package/dist/def/new.js.map +1 -0
- package/dist/def/pre-publish.d.ts +10 -0
- package/dist/def/pre-publish.js +8 -3
- package/dist/def/pre-publish.js.map +1 -0
- package/dist/def/rename.d.ts +11 -1
- package/dist/def/rename.js +6 -1
- package/dist/def/rename.js.map +1 -0
- package/dist/def/universalize.d.ts +10 -0
- package/dist/def/universalize.js +5 -0
- package/dist/def/universalize.js.map +1 -0
- package/dist/def/version.d.ts +10 -0
- package/dist/def/version.js +5 -0
- package/dist/def/version.js.map +1 -0
- package/dist/index.cjs +3918 -715
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/__tests__/config.spec.d.ts +1 -0
- package/dist/utils/__tests__/config.spec.js +63 -0
- package/dist/utils/__tests__/config.spec.js.map +1 -0
- package/dist/utils/__tests__/target.spec.js +1 -0
- package/dist/utils/__tests__/target.spec.js.map +1 -0
- package/dist/utils/__tests__/typegen.spec.js +9 -4
- package/dist/utils/__tests__/typegen.spec.js.map +1 -0
- package/dist/utils/__tests__/version.spec.js +1 -0
- package/dist/utils/__tests__/version.spec.js.map +1 -0
- package/dist/utils/cargo.js +2 -1
- package/dist/utils/cargo.js.map +1 -0
- package/dist/utils/config.d.ts +40 -5
- package/dist/utils/config.js +39 -6
- package/dist/utils/config.js.map +1 -0
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/log.js +1 -0
- package/dist/utils/log.js.map +1 -0
- package/dist/utils/metadata.js +3 -2
- package/dist/utils/metadata.js.map +1 -0
- package/dist/utils/misc.d.ts +1 -1
- package/dist/utils/misc.js +4 -3
- package/dist/utils/misc.js.map +1 -0
- package/dist/utils/target.d.ts +9 -5
- package/dist/utils/target.js +25 -1
- package/dist/utils/target.js.map +1 -0
- package/dist/utils/typegen.d.ts +1 -1
- package/dist/utils/typegen.js +8 -6
- package/dist/utils/typegen.js.map +1 -0
- package/dist/utils/version.d.ts +2 -1
- package/dist/utils/version.js +11 -8
- package/dist/utils/version.js.map +1 -0
- package/package.json +46 -13
- package/src/api/artifacts.ts +83 -8
- package/src/api/build.ts +416 -27
- package/src/api/create-npm-dirs.ts +56 -8
- package/src/api/new.ts +82 -31
- package/src/api/pre-publish.ts +13 -7
- package/src/api/rename.ts +10 -1
- package/src/api/templates/.gitignore.ts +1 -0
- package/src/api/templates/ci-template.ts +176 -150
- package/src/api/templates/ci.yml.ts +16 -3
- package/src/api/templates/js-binding.ts +150 -225
- package/src/api/templates/lib.rs.ts +1 -4
- package/src/api/templates/load-wasi-template.ts +157 -0
- package/src/api/templates/package.json.ts +47 -5
- package/src/api/templates/wasi-worker-template.ts +103 -0
- package/src/api/universalize.ts +8 -5
- package/src/api/version.ts +5 -2
- package/src/commands/artifacts.ts +2 -2
- package/src/commands/build.ts +1 -1
- package/src/commands/new.ts +16 -6
- package/src/def/artifacts.ts +22 -3
- package/src/def/build.ts +32 -4
- package/src/def/create-npm-dirs.ts +9 -0
- package/src/def/new.ts +28 -3
- package/src/def/pre-publish.ts +12 -3
- package/src/def/rename.ts +11 -2
- package/src/def/universalize.ts +9 -0
- package/src/def/version.ts +9 -0
- package/src/index.ts +2 -0
- package/src/utils/__tests__/__snapshots__/config.spec.ts.md +97 -0
- package/src/utils/__tests__/__snapshots__/config.spec.ts.snap +0 -0
- package/src/utils/__tests__/__snapshots__/target.spec.ts.md +28 -0
- package/src/utils/__tests__/__snapshots__/target.spec.ts.snap +0 -0
- package/src/utils/__tests__/__snapshots__/typegen.spec.ts.md +555 -0
- package/src/utils/__tests__/__snapshots__/typegen.spec.ts.snap +0 -0
- package/src/utils/__tests__/__snapshots__/version.spec.ts.md +8 -7
- package/src/utils/__tests__/__snapshots__/version.spec.ts.snap +0 -0
- package/src/utils/__tests__/config.spec.ts +81 -0
- package/src/utils/__tests__/typegen.spec.ts +18 -3
- package/src/utils/cargo.ts +1 -1
- package/src/utils/config.ts +104 -12
- package/src/utils/index.ts +0 -1
- package/src/utils/metadata.ts +2 -2
- package/src/utils/misc.ts +11 -3
- package/src/utils/target.ts +34 -7
- package/src/utils/typegen.ts +11 -5
- package/src/utils/version.ts +10 -8
- package/dist/utils/ci.d.ts +0 -11
- package/dist/utils/ci.js +0 -95
- package/src/utils/ci.ts +0 -111
package/src/api/new.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { execSync } from 'node:child_process'
|
|
2
|
+
import path from 'node:path'
|
|
2
3
|
|
|
3
4
|
import {
|
|
4
5
|
applyDefaultNewOptions,
|
|
@@ -10,7 +11,11 @@ import {
|
|
|
10
11
|
debugFactory,
|
|
11
12
|
DEFAULT_TARGETS,
|
|
12
13
|
mkdirAsync,
|
|
14
|
+
readdirAsync,
|
|
15
|
+
statAsync,
|
|
16
|
+
type SupportedTestFramework,
|
|
13
17
|
writeFileAsync,
|
|
18
|
+
SupportedPackageManager,
|
|
14
19
|
} from '../utils/index.js'
|
|
15
20
|
import { napiEngineRequirement } from '../utils/version.js'
|
|
16
21
|
|
|
@@ -35,6 +40,9 @@ type NewOptions = Required<RawNewOptions>
|
|
|
35
40
|
|
|
36
41
|
function processOptions(options: RawNewOptions) {
|
|
37
42
|
debug('Processing options...')
|
|
43
|
+
if (!options.path) {
|
|
44
|
+
throw new Error('Please provide the path as the argument')
|
|
45
|
+
}
|
|
38
46
|
options.path = path.resolve(process.cwd(), options.path)
|
|
39
47
|
debug(`Resolved target path to: ${options.path}`)
|
|
40
48
|
|
|
@@ -54,6 +62,20 @@ function processOptions(options: RawNewOptions) {
|
|
|
54
62
|
throw new Error('At least one target must be enabled')
|
|
55
63
|
}
|
|
56
64
|
}
|
|
65
|
+
if (
|
|
66
|
+
options.targets.some((target) => target === 'wasm32-wasi-preview1-threads')
|
|
67
|
+
) {
|
|
68
|
+
const out = execSync(`rustup target list`, {
|
|
69
|
+
encoding: 'utf8',
|
|
70
|
+
})
|
|
71
|
+
if (out.includes('wasm32-wasip1-threads')) {
|
|
72
|
+
options.targets.map((target) =>
|
|
73
|
+
target === 'wasm32-wasi-preview1-threads'
|
|
74
|
+
? 'wasm32-wasip1-threads'
|
|
75
|
+
: target,
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
57
79
|
|
|
58
80
|
return applyDefaultNewOptions(options) as NewOptions
|
|
59
81
|
}
|
|
@@ -67,47 +89,75 @@ export async function newProject(userOptions: RawNewOptions) {
|
|
|
67
89
|
debug('Targets to be enabled:')
|
|
68
90
|
debug(options.targets)
|
|
69
91
|
|
|
70
|
-
const outputs = generateFiles(options)
|
|
92
|
+
const outputs = await generateFiles(options)
|
|
71
93
|
|
|
72
|
-
|
|
73
|
-
debug(`Try to create target directory: ${options.path}`)
|
|
74
|
-
if (!options.dryRun) {
|
|
75
|
-
await mkdirAsync(options.path, { recursive: true })
|
|
76
|
-
}
|
|
77
|
-
} catch (e) {
|
|
78
|
-
throw new Error(`Failed to create target directory: ${options.path}`, {
|
|
79
|
-
cause: e,
|
|
80
|
-
})
|
|
81
|
-
}
|
|
94
|
+
await ensurePath(options.path, options.dryRun)
|
|
82
95
|
|
|
83
96
|
await dumpOutputs(outputs, options.dryRun)
|
|
84
97
|
debug(`Project created at: ${options.path}`)
|
|
85
98
|
}
|
|
86
99
|
|
|
87
|
-
function
|
|
100
|
+
async function ensurePath(path: string, dryRun = false) {
|
|
101
|
+
const stat = await statAsync(path, {}).catch(() => undefined)
|
|
102
|
+
|
|
103
|
+
// file descriptor exists
|
|
104
|
+
if (stat) {
|
|
105
|
+
if (stat.isFile()) {
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Path ${path} for creating new napi-rs project already exists and it's not a directory.`,
|
|
108
|
+
)
|
|
109
|
+
} else if (stat.isDirectory()) {
|
|
110
|
+
const files = await readdirAsync(path)
|
|
111
|
+
if (files.length) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
`Path ${path} for creating new napi-rs project already exists and it's not empty.`,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!dryRun) {
|
|
120
|
+
try {
|
|
121
|
+
debug(`Try to create target directory: ${path}`)
|
|
122
|
+
if (!dryRun) {
|
|
123
|
+
await mkdirAsync(path, { recursive: true })
|
|
124
|
+
}
|
|
125
|
+
} catch (e) {
|
|
126
|
+
throw new Error(`Failed to create target directory: ${path}`, {
|
|
127
|
+
cause: e,
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function generateFiles(options: NewOptions): Promise<Output[]> {
|
|
134
|
+
const packageJson = await generatePackageJson(options)
|
|
88
135
|
return [
|
|
89
136
|
generateCargoToml,
|
|
90
137
|
generateLibRs,
|
|
91
138
|
generateBuildRs,
|
|
92
|
-
generatePackageJson,
|
|
93
139
|
generateGithubWorkflow,
|
|
94
140
|
generateIgnoreFiles,
|
|
95
|
-
]
|
|
96
|
-
|
|
141
|
+
]
|
|
142
|
+
.flatMap((generator) => {
|
|
143
|
+
const output = generator(options)
|
|
97
144
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
145
|
+
if (!output) {
|
|
146
|
+
return []
|
|
147
|
+
}
|
|
101
148
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
149
|
+
if (Array.isArray(output)) {
|
|
150
|
+
return output.map((o) => ({
|
|
151
|
+
...o,
|
|
152
|
+
target: path.join(options.path, o.target),
|
|
153
|
+
}))
|
|
154
|
+
} else {
|
|
155
|
+
return [{ ...output, target: path.join(options.path, output.target) }]
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
.concat([
|
|
159
|
+
{ ...packageJson, target: path.join(options.path, packageJson.target) },
|
|
160
|
+
])
|
|
111
161
|
}
|
|
112
162
|
|
|
113
163
|
function generateCargoToml(options: NewOptions): Output {
|
|
@@ -136,16 +186,17 @@ function generateBuildRs(_options: NewOptions): Output {
|
|
|
136
186
|
}
|
|
137
187
|
}
|
|
138
188
|
|
|
139
|
-
function generatePackageJson(options: NewOptions): Output {
|
|
189
|
+
async function generatePackageJson(options: NewOptions): Promise<Output> {
|
|
140
190
|
return {
|
|
141
191
|
target: './package.json',
|
|
142
|
-
content: createPackageJson({
|
|
192
|
+
content: await createPackageJson({
|
|
143
193
|
name: options.name,
|
|
144
194
|
binaryName: getBinaryName(options.name),
|
|
145
195
|
targets: options.targets,
|
|
146
196
|
license: options.license,
|
|
147
197
|
engineRequirement: napiEngineRequirement(options.minNodeApiVersion),
|
|
148
198
|
cliVersion: CLI_VERSION,
|
|
199
|
+
testFramework: options.testFramework as SupportedTestFramework,
|
|
149
200
|
}),
|
|
150
201
|
}
|
|
151
202
|
}
|
|
@@ -158,8 +209,8 @@ function generateGithubWorkflow(options: NewOptions): Output | null {
|
|
|
158
209
|
return {
|
|
159
210
|
target: './.github/workflows/ci.yml',
|
|
160
211
|
content: createGithubActionsCIYml(
|
|
161
|
-
getBinaryName(options.name),
|
|
162
212
|
options.targets,
|
|
213
|
+
options.packageManager as SupportedPackageManager,
|
|
163
214
|
),
|
|
164
215
|
}
|
|
165
216
|
}
|
package/src/api/pre-publish.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { execSync } from 'child_process'
|
|
2
|
-
import { existsSync, statSync } from 'fs'
|
|
3
|
-
import { join,
|
|
1
|
+
import { execSync } from 'node:child_process'
|
|
2
|
+
import { existsSync, statSync } from 'node:fs'
|
|
3
|
+
import { join, resolve } from 'node:path'
|
|
4
4
|
|
|
5
5
|
import { Octokit } from '@octokit/rest'
|
|
6
6
|
|
|
@@ -31,10 +31,13 @@ export async function prePublish(userOptions: PrePublishOptions) {
|
|
|
31
31
|
|
|
32
32
|
const options = applyDefaultPrePublishOptions(userOptions)
|
|
33
33
|
|
|
34
|
-
const packageJsonPath =
|
|
34
|
+
const packageJsonPath = resolve(options.cwd, options.packageJsonPath)
|
|
35
35
|
|
|
36
36
|
const { packageJson, targets, packageName, binaryName, npmClient } =
|
|
37
|
-
await readNapiConfig(
|
|
37
|
+
await readNapiConfig(
|
|
38
|
+
packageJsonPath,
|
|
39
|
+
options.configPath ? resolve(options.cwd, options.configPath) : undefined,
|
|
40
|
+
)
|
|
38
41
|
|
|
39
42
|
async function createGhRelease(packageName: string, version: string) {
|
|
40
43
|
if (!options.ghRelease) {
|
|
@@ -150,7 +153,9 @@ export async function prePublish(userOptions: PrePublishOptions) {
|
|
|
150
153
|
options.npmDir,
|
|
151
154
|
`${target.platformArchABI}`,
|
|
152
155
|
)
|
|
153
|
-
const
|
|
156
|
+
const ext =
|
|
157
|
+
target.platform === 'wasi' || target.platform === 'wasm' ? 'wasm' : 'node'
|
|
158
|
+
const filename = `${binaryName}.${target.platformArchABI}.${ext}`
|
|
154
159
|
const dstPath = join(pkgDir, filename)
|
|
155
160
|
|
|
156
161
|
if (!options.dryRun) {
|
|
@@ -187,7 +192,8 @@ export async function prePublish(userOptions: PrePublishOptions) {
|
|
|
187
192
|
'content-length': dstFileStats.size,
|
|
188
193
|
'content-type': 'application/octet-stream',
|
|
189
194
|
},
|
|
190
|
-
|
|
195
|
+
// @ts-expect-error octokit types are wrong
|
|
196
|
+
data: await readFileAsync(dstPath),
|
|
191
197
|
})
|
|
192
198
|
debug.info(`GitHub release created`)
|
|
193
199
|
debug.info(`Download URL: %s`, assetInfo.data.browser_download_url)
|
package/src/api/rename.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { resolve } from 'path'
|
|
1
|
+
import { resolve } from 'node:path'
|
|
2
2
|
|
|
3
3
|
import { isNil, merge, omitBy, pick } from 'lodash-es'
|
|
4
4
|
|
|
@@ -30,6 +30,15 @@ export async function renameProject(userOptions: RenameOptions) {
|
|
|
30
30
|
},
|
|
31
31
|
)
|
|
32
32
|
|
|
33
|
+
if (options.configPath) {
|
|
34
|
+
const configPath = resolve(options.cwd, options.configPath)
|
|
35
|
+
const configContent = await readFileAsync(configPath, 'utf8')
|
|
36
|
+
const configData = JSON.parse(configContent)
|
|
37
|
+
configData.binaryName = options.binaryName
|
|
38
|
+
configData.packageName = options.packageName
|
|
39
|
+
await writeFileAsync(configPath, JSON.stringify(configData, null, 2))
|
|
40
|
+
}
|
|
41
|
+
|
|
33
42
|
await writeFileAsync(
|
|
34
43
|
packageJsonPath,
|
|
35
44
|
JSON.stringify(packageJsonData, null, 2),
|