@deveco-test/hmos-deveco-cli 0.3.0-TD.2-alpha2 → 0.3.0-TD.2

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/LICENSE +21 -21
  2. package/README.md +785 -785
  3. package/SKILL.md +172 -172
  4. package/dist/cli.js +3 -3
  5. package/index.zip +0 -0
  6. package/package.json +102 -102
  7. package/scripts/install-better-sqlite3.mjs +51 -51
  8. package/scripts/install-jieba-wasm.mjs +47 -47
  9. package/scripts/lib/better-sqlite3-vendor.mjs +128 -128
  10. package/scripts/lib/cli-data-dir.mjs +44 -44
  11. package/scripts/lib/doc-init-log-path.mjs +15 -15
  12. package/scripts/lib/jieba-wasm-vendor.mjs +102 -102
  13. package/scripts/postinstall.mjs +54 -54
  14. package/src/resources/aclPermission/aclPermissionsInfo.json +310 -310
  15. package/templates/application/AppScope/app.json5 +10 -10
  16. package/templates/application/AppScope/resources/base/element/string.json +8 -8
  17. package/templates/application/AppScope/resources/base/media/layered_image.json +6 -6
  18. package/templates/application/build-profile.json5 +41 -41
  19. package/templates/application/code-linter.json5 +31 -31
  20. package/templates/application/entry/build-profile.json5 +32 -32
  21. package/templates/application/entry/gitignore.txt +5 -5
  22. package/templates/application/entry/hvigorfile.ts +7 -7
  23. package/templates/application/entry/obfuscation-rules.txt +19 -19
  24. package/templates/application/entry/oh-package.json5 +10 -10
  25. package/templates/application/entry/src/main/ets/entryability/EntryAbility.ets +62 -62
  26. package/templates/application/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets +30 -30
  27. package/templates/application/entry/src/main/ets/pages/Index.ets +37 -37
  28. package/templates/application/entry/src/main/module.json5 +49 -49
  29. package/templates/application/entry/src/main/resources/base/element/color.json +7 -7
  30. package/templates/application/entry/src/main/resources/base/element/float.json +8 -8
  31. package/templates/application/entry/src/main/resources/base/element/string.json +15 -15
  32. package/templates/application/entry/src/main/resources/base/media/layered_image.json +6 -6
  33. package/templates/application/entry/src/main/resources/base/profile/backup_config.json +2 -2
  34. package/templates/application/entry/src/main/resources/base/profile/main_pages.json +5 -5
  35. package/templates/application/entry/src/main/resources/dark/element/color.json +7 -7
  36. package/templates/application/gitignore.txt +11 -11
  37. package/templates/application/hvigor/hvigor-config.json5 +23 -23
  38. package/templates/application/hvigorfile.ts +7 -7
  39. package/templates/application/oh-package.json5 +10 -10
@@ -1,47 +1,47 @@
1
- /*
2
- * Copyright (c) 2026 Huawei Device Co., Ltd.
3
- * SPDX-License-Identifier: MIT
4
- */
5
-
6
- import { appendFile, mkdir } from 'fs/promises';
7
- import { dirname } from 'path';
8
- import { getDocInitLogPath } from './lib/doc-init-log-path.mjs';
9
- import { installJiebaWasmViaNpm } from './lib/jieba-wasm-vendor.mjs';
10
-
11
- async function appendInstallLog(message) {
12
- try {
13
- const logPath = getDocInitLogPath();
14
- await mkdir(dirname(logPath), { recursive: true });
15
- await appendFile(
16
- logPath,
17
- `${new Date().toISOString()} [jieba-wasm] ${message}\n`
18
- );
19
- } catch {
20
- // ignore logging failures
21
- }
22
- }
23
-
24
- export async function ensureJiebaWasmForDocs() {
25
- await appendInstallLog(
26
- `ensure @node-rs/jieba-wasm32-wasi@2.0.1 for Node ${process.version}`
27
- );
28
- const result = await installJiebaWasmViaNpm();
29
- if (result.skipped) {
30
- if (result.reason === 'already-installed') {
31
- await appendInstallLog('skip: jieba wasm32-wasi already loadable');
32
- } else if (result.reason === 'native-available') {
33
- await appendInstallLog('skip: jieba native already loadable');
34
- } else {
35
- await appendInstallLog(`skip: ${result.reason}`);
36
- }
37
- } else if (result.ok) {
38
- await appendInstallLog(
39
- 'installed @node-rs/jieba-wasm32-wasi@2.0.1 via npm'
40
- );
41
- } else {
42
- await appendInstallLog(
43
- `npm install failed (${result.reason}): ${result.error ?? 'unknown error'}`
44
- );
45
- }
46
- return result;
47
- }
1
+ /*
2
+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { appendFile, mkdir } from 'fs/promises';
7
+ import { dirname } from 'path';
8
+ import { getDocInitLogPath } from './lib/doc-init-log-path.mjs';
9
+ import { installJiebaWasmViaNpm } from './lib/jieba-wasm-vendor.mjs';
10
+
11
+ async function appendInstallLog(message) {
12
+ try {
13
+ const logPath = getDocInitLogPath();
14
+ await mkdir(dirname(logPath), { recursive: true });
15
+ await appendFile(
16
+ logPath,
17
+ `${new Date().toISOString()} [jieba-wasm] ${message}\n`
18
+ );
19
+ } catch {
20
+ // ignore logging failures
21
+ }
22
+ }
23
+
24
+ export async function ensureJiebaWasmForDocs() {
25
+ await appendInstallLog(
26
+ `ensure @node-rs/jieba-wasm32-wasi@2.0.1 for Node ${process.version}`
27
+ );
28
+ const result = await installJiebaWasmViaNpm();
29
+ if (result.skipped) {
30
+ if (result.reason === 'already-installed') {
31
+ await appendInstallLog('skip: jieba wasm32-wasi already loadable');
32
+ } else if (result.reason === 'native-available') {
33
+ await appendInstallLog('skip: jieba native already loadable');
34
+ } else {
35
+ await appendInstallLog(`skip: ${result.reason}`);
36
+ }
37
+ } else if (result.ok) {
38
+ await appendInstallLog(
39
+ 'installed @node-rs/jieba-wasm32-wasi@2.0.1 via npm'
40
+ );
41
+ } else {
42
+ await appendInstallLog(
43
+ `npm install failed (${result.reason}): ${result.error ?? 'unknown error'}`
44
+ );
45
+ }
46
+ return result;
47
+ }
@@ -1,128 +1,128 @@
1
- /*
2
- * Copyright (c) 2026 Huawei Device Co., Ltd.
3
- * SPDX-License-Identifier: MIT
4
- */
5
-
6
- import { dirname, join } from 'path';
7
- import { fileURLToPath } from 'url';
8
- import { existsSync } from 'fs';
9
- import { spawnSync } from 'child_process';
10
-
11
- export const BETTER_SQLITE3_VERSION_LEGACY = '11.10.0';
12
- export const BETTER_SQLITE3_VERSION_NODE20 = '12.9.0';
13
- export const BETTER_SQLITE3_VERSION_NODE21 = '9.6.0';
14
- export const BETTER_SQLITE3_VERSION_LATEST = '12.11.1';
15
-
16
- const scriptDir = dirname(fileURLToPath(import.meta.url));
17
- export const packageRoot = join(scriptDir, '..', '..');
18
-
19
- export function resolveTargetVersion(nodeVersion = process.version) {
20
- const major = Number(nodeVersion.slice(1).split('.')[0]);
21
- if (major === 18) {
22
- return BETTER_SQLITE3_VERSION_LEGACY;
23
- }
24
- if (major === 20 || major === 23) {
25
- return BETTER_SQLITE3_VERSION_NODE20;
26
- }
27
- if (major === 21) {
28
- return BETTER_SQLITE3_VERSION_NODE21;
29
- }
30
- return BETTER_SQLITE3_VERSION_LATEST;
31
- }
32
-
33
- export function isUnsupportedNodeVersion(nodeVersion = process.version) {
34
- return Number(nodeVersion.slice(1).split('.')[0]) === 19;
35
- }
36
-
37
- function resolveNpmCliPath() {
38
- const nodeDir = dirname(process.execPath);
39
- const candidates = [
40
- join(nodeDir, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
41
- join(nodeDir, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js'),
42
- ];
43
- return candidates.find((candidate) => existsSync(candidate)) ?? null;
44
- }
45
-
46
- function quoteWindowsCmdArg(arg) {
47
- return /\s/.test(arg) ? `"${arg.replace(/"/g, '\\"')}"` : arg;
48
- }
49
-
50
- function spawnDetail(result) {
51
- return [result.stderr, result.stdout, result.error?.message]
52
- .filter(Boolean)
53
- .join('\n')
54
- .trim();
55
- }
56
-
57
- export function runNpmInstallBetterSqlite3(targetVersion) {
58
- const args = ['install', `better-sqlite3@${targetVersion}`, '--no-save'];
59
- const npmCli = resolveNpmCliPath();
60
- if (npmCli) {
61
- return spawnSync(process.execPath, [npmCli, ...args], {
62
- cwd: packageRoot,
63
- stdio: 'pipe',
64
- encoding: 'utf-8',
65
- });
66
- }
67
-
68
- if (process.platform === 'win32') {
69
- const command = ['npm', ...args].map(quoteWindowsCmdArg).join(' ');
70
- return spawnSync(command, {
71
- cwd: packageRoot,
72
- stdio: 'pipe',
73
- encoding: 'utf-8',
74
- shell: true,
75
- });
76
- }
77
-
78
- return spawnSync('npm', args, {
79
- cwd: packageRoot,
80
- stdio: 'pipe',
81
- encoding: 'utf-8',
82
- });
83
- }
84
-
85
- export async function isBetterSqlite3Loadable() {
86
- try {
87
- await import('better-sqlite3');
88
- return true;
89
- } catch {
90
- return false;
91
- }
92
- }
93
-
94
- export async function installBetterSqlite3ViaNpm(nodeVersion = process.version) {
95
- if (isUnsupportedNodeVersion(nodeVersion)) {
96
- return {
97
- ok: false,
98
- reason: 'unsupported-node',
99
- error: 'Node.js 19 is not supported for better-sqlite3',
100
- };
101
- }
102
-
103
- if (await isBetterSqlite3Loadable()) {
104
- return { ok: true, skipped: true, reason: 'already-installed' };
105
- }
106
-
107
- const targetVersion = resolveTargetVersion(nodeVersion);
108
- const result = runNpmInstallBetterSqlite3(targetVersion);
109
- if (result.status !== 0) {
110
- return {
111
- ok: false,
112
- reason: 'npm-install-failed',
113
- version: targetVersion,
114
- error: spawnDetail(result) || `exit ${result.status ?? 'null'}`,
115
- };
116
- }
117
-
118
- if (!(await isBetterSqlite3Loadable())) {
119
- return {
120
- ok: false,
121
- reason: 'load-failed',
122
- version: targetVersion,
123
- error: 'npm install finished but better-sqlite3 still cannot be imported',
124
- };
125
- }
126
-
127
- return { ok: true, version: targetVersion, method: 'npm' };
128
- }
1
+ /*
2
+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { dirname, join } from 'path';
7
+ import { fileURLToPath } from 'url';
8
+ import { existsSync } from 'fs';
9
+ import { spawnSync } from 'child_process';
10
+
11
+ export const BETTER_SQLITE3_VERSION_LEGACY = '11.10.0';
12
+ export const BETTER_SQLITE3_VERSION_NODE20 = '12.9.0';
13
+ export const BETTER_SQLITE3_VERSION_NODE21 = '9.6.0';
14
+ export const BETTER_SQLITE3_VERSION_LATEST = '12.11.1';
15
+
16
+ const scriptDir = dirname(fileURLToPath(import.meta.url));
17
+ export const packageRoot = join(scriptDir, '..', '..');
18
+
19
+ export function resolveTargetVersion(nodeVersion = process.version) {
20
+ const major = Number(nodeVersion.slice(1).split('.')[0]);
21
+ if (major === 18) {
22
+ return BETTER_SQLITE3_VERSION_LEGACY;
23
+ }
24
+ if (major === 20 || major === 23) {
25
+ return BETTER_SQLITE3_VERSION_NODE20;
26
+ }
27
+ if (major === 21) {
28
+ return BETTER_SQLITE3_VERSION_NODE21;
29
+ }
30
+ return BETTER_SQLITE3_VERSION_LATEST;
31
+ }
32
+
33
+ export function isUnsupportedNodeVersion(nodeVersion = process.version) {
34
+ return Number(nodeVersion.slice(1).split('.')[0]) === 19;
35
+ }
36
+
37
+ function resolveNpmCliPath() {
38
+ const nodeDir = dirname(process.execPath);
39
+ const candidates = [
40
+ join(nodeDir, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
41
+ join(nodeDir, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js'),
42
+ ];
43
+ return candidates.find((candidate) => existsSync(candidate)) ?? null;
44
+ }
45
+
46
+ function quoteWindowsCmdArg(arg) {
47
+ return /\s/.test(arg) ? `"${arg.replace(/"/g, '\\"')}"` : arg;
48
+ }
49
+
50
+ function spawnDetail(result) {
51
+ return [result.stderr, result.stdout, result.error?.message]
52
+ .filter(Boolean)
53
+ .join('\n')
54
+ .trim();
55
+ }
56
+
57
+ export function runNpmInstallBetterSqlite3(targetVersion) {
58
+ const args = ['install', `better-sqlite3@${targetVersion}`, '--no-save'];
59
+ const npmCli = resolveNpmCliPath();
60
+ if (npmCli) {
61
+ return spawnSync(process.execPath, [npmCli, ...args], {
62
+ cwd: packageRoot,
63
+ stdio: 'pipe',
64
+ encoding: 'utf-8',
65
+ });
66
+ }
67
+
68
+ if (process.platform === 'win32') {
69
+ const command = ['npm', ...args].map(quoteWindowsCmdArg).join(' ');
70
+ return spawnSync(command, {
71
+ cwd: packageRoot,
72
+ stdio: 'pipe',
73
+ encoding: 'utf-8',
74
+ shell: true,
75
+ });
76
+ }
77
+
78
+ return spawnSync('npm', args, {
79
+ cwd: packageRoot,
80
+ stdio: 'pipe',
81
+ encoding: 'utf-8',
82
+ });
83
+ }
84
+
85
+ export async function isBetterSqlite3Loadable() {
86
+ try {
87
+ await import('better-sqlite3');
88
+ return true;
89
+ } catch {
90
+ return false;
91
+ }
92
+ }
93
+
94
+ export async function installBetterSqlite3ViaNpm(nodeVersion = process.version) {
95
+ if (isUnsupportedNodeVersion(nodeVersion)) {
96
+ return {
97
+ ok: false,
98
+ reason: 'unsupported-node',
99
+ error: 'Node.js 19 is not supported for better-sqlite3',
100
+ };
101
+ }
102
+
103
+ if (await isBetterSqlite3Loadable()) {
104
+ return { ok: true, skipped: true, reason: 'already-installed' };
105
+ }
106
+
107
+ const targetVersion = resolveTargetVersion(nodeVersion);
108
+ const result = runNpmInstallBetterSqlite3(targetVersion);
109
+ if (result.status !== 0) {
110
+ return {
111
+ ok: false,
112
+ reason: 'npm-install-failed',
113
+ version: targetVersion,
114
+ error: spawnDetail(result) || `exit ${result.status ?? 'null'}`,
115
+ };
116
+ }
117
+
118
+ if (!(await isBetterSqlite3Loadable())) {
119
+ return {
120
+ ok: false,
121
+ reason: 'load-failed',
122
+ version: targetVersion,
123
+ error: 'npm install finished but better-sqlite3 still cannot be imported',
124
+ };
125
+ }
126
+
127
+ return { ok: true, version: targetVersion, method: 'npm' };
128
+ }
@@ -1,44 +1,44 @@
1
- /*
2
- * Copyright (c) 2026 Huawei Device Co., Ltd.
3
- * SPDX-License-Identifier: MIT
4
- *
5
- * Keep in sync with src/utils/cli-data-dir.ts
6
- */
7
-
8
- import { homedir } from 'os';
9
- import { join, resolve } from 'path';
10
-
11
- const APP_NAME = 'deveco-cli';
12
-
13
- /** Strip quotes from cmd.exe `set VAR="path"` (quotes become part of the value). */
14
- function normalizeEnvDataDir(value) {
15
- let normalized = value.trim();
16
- if (
17
- (normalized.startsWith('"') && normalized.endsWith('"')) ||
18
- (normalized.startsWith("'") && normalized.endsWith("'"))
19
- ) {
20
- normalized = normalized.slice(1, -1).trim();
21
- }
22
- return normalized;
23
- }
24
-
25
- export function isCliDataDirConfigured() {
26
- const raw = process.env.DEVECO_CLI_DATA_DIR;
27
- return raw !== undefined && normalizeEnvDataDir(raw) !== '';
28
- }
29
-
30
- function getDefaultCliDataDir() {
31
- return join(homedir(), '.local', 'share', APP_NAME);
32
- }
33
-
34
- export function getCliDataDir() {
35
- const raw = process.env.DEVECO_CLI_DATA_DIR;
36
- if (raw === undefined || raw === '') {
37
- return getDefaultCliDataDir();
38
- }
39
- const envDir = normalizeEnvDataDir(raw);
40
- if (!envDir) {
41
- return getDefaultCliDataDir();
42
- }
43
- return resolve(envDir);
44
- }
1
+ /*
2
+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3
+ * SPDX-License-Identifier: MIT
4
+ *
5
+ * Keep in sync with src/utils/cli-data-dir.ts
6
+ */
7
+
8
+ import { homedir } from 'os';
9
+ import { join, resolve } from 'path';
10
+
11
+ const APP_NAME = 'deveco-cli';
12
+
13
+ /** Strip quotes from cmd.exe `set VAR="path"` (quotes become part of the value). */
14
+ function normalizeEnvDataDir(value) {
15
+ let normalized = value.trim();
16
+ if (
17
+ (normalized.startsWith('"') && normalized.endsWith('"')) ||
18
+ (normalized.startsWith("'") && normalized.endsWith("'"))
19
+ ) {
20
+ normalized = normalized.slice(1, -1).trim();
21
+ }
22
+ return normalized;
23
+ }
24
+
25
+ export function isCliDataDirConfigured() {
26
+ const raw = process.env.DEVECO_CLI_DATA_DIR;
27
+ return raw !== undefined && normalizeEnvDataDir(raw) !== '';
28
+ }
29
+
30
+ function getDefaultCliDataDir() {
31
+ return join(homedir(), '.local', 'share', APP_NAME);
32
+ }
33
+
34
+ export function getCliDataDir() {
35
+ const raw = process.env.DEVECO_CLI_DATA_DIR;
36
+ if (raw === undefined || raw === '') {
37
+ return getDefaultCliDataDir();
38
+ }
39
+ const envDir = normalizeEnvDataDir(raw);
40
+ if (!envDir) {
41
+ return getDefaultCliDataDir();
42
+ }
43
+ return resolve(envDir);
44
+ }
@@ -1,15 +1,15 @@
1
- /*
2
- * Copyright (c) 2026 Huawei Device Co., Ltd.
3
- * SPDX-License-Identifier: MIT
4
- */
5
-
6
- import { join } from 'path';
7
- import { getCliDataDir } from './cli-data-dir.mjs';
8
-
9
- export function getDocInitLogDir() {
10
- return join(getCliDataDir(), 'logs');
11
- }
12
-
13
- export function getDocInitLogPath() {
14
- return join(getDocInitLogDir(), 'doc-init.log');
15
- }
1
+ /*
2
+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { join } from 'path';
7
+ import { getCliDataDir } from './cli-data-dir.mjs';
8
+
9
+ export function getDocInitLogDir() {
10
+ return join(getCliDataDir(), 'logs');
11
+ }
12
+
13
+ export function getDocInitLogPath() {
14
+ return join(getDocInitLogDir(), 'doc-init.log');
15
+ }
@@ -1,102 +1,102 @@
1
- /*
2
- * Copyright (c) 2026 Huawei Device Co., Ltd.
3
- * SPDX-License-Identifier: MIT
4
- */
5
-
6
- import { dirname, join } from 'path';
7
- import { existsSync } from 'fs';
8
- import { spawnSync } from 'child_process';
9
- import { fileURLToPath } from 'url';
10
- import { packageRoot } from './better-sqlite3-vendor.mjs';
11
-
12
- const JIEBA_WASM_VERSION = '2.0.1';
13
-
14
- function resolveNpmCliPath() {
15
- const nodeDir = dirname(process.execPath);
16
- const candidates = [
17
- join(nodeDir, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
18
- join(nodeDir, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js'),
19
- ];
20
- return candidates.find((candidate) => existsSync(candidate)) ?? null;
21
- }
22
-
23
- function quoteWindowsCmdArg(arg) {
24
- return /\s/.test(arg) ? `"${arg.replace(/"/g, '\\"')}"` : arg;
25
- }
26
-
27
- function spawnDetail(result) {
28
- return [result.stderr, result.stdout, result.error?.message]
29
- .filter(Boolean)
30
- .join('\n')
31
- .trim();
32
- }
33
-
34
- function runNpmInstallJiebaWasm(targetVersion) {
35
- const args = [
36
- 'install',
37
- `@node-rs/jieba-wasm32-wasi@${targetVersion}`,
38
- '--no-save',
39
- '--cpu=wasm32',
40
- ];
41
- const npmCli = resolveNpmCliPath();
42
- if (npmCli) {
43
- return spawnSync(process.execPath, [npmCli, ...args], {
44
- cwd: packageRoot,
45
- stdio: 'pipe',
46
- encoding: 'utf-8',
47
- });
48
- }
49
- if (process.platform === 'win32') {
50
- const command = ['npm', ...args].map(quoteWindowsCmdArg).join(' ');
51
- return spawnSync(command, {
52
- cwd: packageRoot,
53
- stdio: 'pipe',
54
- encoding: 'utf-8',
55
- shell: true,
56
- });
57
- }
58
- return spawnSync('npm', args, {
59
- cwd: packageRoot,
60
- stdio: 'pipe',
61
- encoding: 'utf-8',
62
- });
63
- }
64
-
65
- export async function installJiebaWasmViaNpm(targetVersion = JIEBA_WASM_VERSION) {
66
- try {
67
- await import('@node-rs/jieba-wasm32-wasi');
68
- return { ok: true, skipped: true, reason: 'already-installed' };
69
- } catch {
70
- // continue — try native or install wasm
71
- }
72
-
73
- try {
74
- await import('@node-rs/jieba');
75
- return { ok: true, skipped: true, reason: 'native-available' };
76
- } catch {
77
- // continue — install wasm fallback
78
- }
79
-
80
- const result = runNpmInstallJiebaWasm(targetVersion);
81
- if (result.status !== 0) {
82
- return {
83
- ok: false,
84
- reason: 'npm-install-failed',
85
- version: targetVersion,
86
- error: spawnDetail(result) || `exit ${result.status ?? 'null'}`,
87
- };
88
- }
89
-
90
- try {
91
- await import('@node-rs/jieba-wasm32-wasi');
92
- } catch {
93
- return {
94
- ok: false,
95
- reason: 'load-failed',
96
- version: targetVersion,
97
- error:
98
- 'npm install finished but @node-rs/jieba-wasm32-wasi still cannot be imported',
99
- };
100
- }
101
- return { ok: true, version: targetVersion, method: 'npm' };
102
- }
1
+ /*
2
+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { dirname, join } from 'path';
7
+ import { existsSync } from 'fs';
8
+ import { spawnSync } from 'child_process';
9
+ import { fileURLToPath } from 'url';
10
+ import { packageRoot } from './better-sqlite3-vendor.mjs';
11
+
12
+ const JIEBA_WASM_VERSION = '2.0.1';
13
+
14
+ function resolveNpmCliPath() {
15
+ const nodeDir = dirname(process.execPath);
16
+ const candidates = [
17
+ join(nodeDir, 'node_modules', 'npm', 'bin', 'npm-cli.js'),
18
+ join(nodeDir, '..', 'lib', 'node_modules', 'npm', 'bin', 'npm-cli.js'),
19
+ ];
20
+ return candidates.find((candidate) => existsSync(candidate)) ?? null;
21
+ }
22
+
23
+ function quoteWindowsCmdArg(arg) {
24
+ return /\s/.test(arg) ? `"${arg.replace(/"/g, '\\"')}"` : arg;
25
+ }
26
+
27
+ function spawnDetail(result) {
28
+ return [result.stderr, result.stdout, result.error?.message]
29
+ .filter(Boolean)
30
+ .join('\n')
31
+ .trim();
32
+ }
33
+
34
+ function runNpmInstallJiebaWasm(targetVersion) {
35
+ const args = [
36
+ 'install',
37
+ `@node-rs/jieba-wasm32-wasi@${targetVersion}`,
38
+ '--no-save',
39
+ '--cpu=wasm32',
40
+ ];
41
+ const npmCli = resolveNpmCliPath();
42
+ if (npmCli) {
43
+ return spawnSync(process.execPath, [npmCli, ...args], {
44
+ cwd: packageRoot,
45
+ stdio: 'pipe',
46
+ encoding: 'utf-8',
47
+ });
48
+ }
49
+ if (process.platform === 'win32') {
50
+ const command = ['npm', ...args].map(quoteWindowsCmdArg).join(' ');
51
+ return spawnSync(command, {
52
+ cwd: packageRoot,
53
+ stdio: 'pipe',
54
+ encoding: 'utf-8',
55
+ shell: true,
56
+ });
57
+ }
58
+ return spawnSync('npm', args, {
59
+ cwd: packageRoot,
60
+ stdio: 'pipe',
61
+ encoding: 'utf-8',
62
+ });
63
+ }
64
+
65
+ export async function installJiebaWasmViaNpm(targetVersion = JIEBA_WASM_VERSION) {
66
+ try {
67
+ await import('@node-rs/jieba-wasm32-wasi');
68
+ return { ok: true, skipped: true, reason: 'already-installed' };
69
+ } catch {
70
+ // continue — try native or install wasm
71
+ }
72
+
73
+ try {
74
+ await import('@node-rs/jieba');
75
+ return { ok: true, skipped: true, reason: 'native-available' };
76
+ } catch {
77
+ // continue — install wasm fallback
78
+ }
79
+
80
+ const result = runNpmInstallJiebaWasm(targetVersion);
81
+ if (result.status !== 0) {
82
+ return {
83
+ ok: false,
84
+ reason: 'npm-install-failed',
85
+ version: targetVersion,
86
+ error: spawnDetail(result) || `exit ${result.status ?? 'null'}`,
87
+ };
88
+ }
89
+
90
+ try {
91
+ await import('@node-rs/jieba-wasm32-wasi');
92
+ } catch {
93
+ return {
94
+ ok: false,
95
+ reason: 'load-failed',
96
+ version: targetVersion,
97
+ error:
98
+ 'npm install finished but @node-rs/jieba-wasm32-wasi still cannot be imported',
99
+ };
100
+ }
101
+ return { ok: true, version: targetVersion, method: 'npm' };
102
+ }