deveco-harness 0.1.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 (135) hide show
  1. package/README.en.md +71 -0
  2. package/README.md +71 -0
  3. package/deveco-harness.example.jsonc +32 -0
  4. package/dist/index.js +758 -0
  5. package/package.json +48 -0
  6. package/prompts/build.txt +164 -0
  7. package/prompts/plan.txt +118 -0
  8. package/skills/arkts-error-fixes/README.md +151 -0
  9. package/skills/arkts-error-fixes/SKILL.md +150 -0
  10. package/skills/arkts-error-fixes/assets/AnyTypeError.ets +269 -0
  11. package/skills/arkts-error-fixes/assets/AppStorageError.ets +22 -0
  12. package/skills/arkts-error-fixes/assets/ArrowFunctionConversionError.ets +42 -0
  13. package/skills/arkts-error-fixes/assets/AvoidAreaTypeError.ets +43 -0
  14. package/skills/arkts-error-fixes/assets/BreakpointTypeError.ets +71 -0
  15. package/skills/arkts-error-fixes/assets/CatchClauseTypeError.ets +54 -0
  16. package/skills/arkts-error-fixes/assets/ColorConsistencyError.ets +88 -0
  17. package/skills/arkts-error-fixes/assets/ColorPropertyError.ets +39 -0
  18. package/skills/arkts-error-fixes/assets/ContextTypeError.ets +105 -0
  19. package/skills/arkts-error-fixes/assets/DecoratorStateError.ets +64 -0
  20. package/skills/arkts-error-fixes/assets/DisplayListenerTypeError.ets +129 -0
  21. package/skills/arkts-error-fixes/assets/DuplicateEntryError.ets +35 -0
  22. package/skills/arkts-error-fixes/assets/ESObjectTypeError.ets +137 -0
  23. package/skills/arkts-error-fixes/assets/FontColorPropertyError.ets +42 -0
  24. package/skills/arkts-error-fixes/assets/FunctionReturnTypeError.ets +41 -0
  25. package/skills/arkts-error-fixes/assets/IDataSourceError.ets +79 -0
  26. package/skills/arkts-error-fixes/assets/ImplementationNotAllowedError.ets +38 -0
  27. package/skills/arkts-error-fixes/assets/InterfaceMethodSignatureError.ets +43 -0
  28. package/skills/arkts-error-fixes/assets/NotificationError.ets +34 -0
  29. package/skills/arkts-error-fixes/assets/ObjectLiteralInterfaceError.ets +49 -0
  30. package/skills/arkts-error-fixes/assets/ObjectLiteralTypeError.ets +54 -0
  31. package/skills/arkts-error-fixes/assets/ObjectSpreadError.ets +29 -0
  32. package/skills/arkts-error-fixes/assets/PossiblyNullError.ets +32 -0
  33. package/skills/arkts-error-fixes/assets/ResourceConversionError.ets +159 -0
  34. package/skills/arkts-error-fixes/assets/StandaloneFunctionContext.ets +116 -0
  35. package/skills/arkts-error-fixes/assets/StandaloneFunctionError.ets +85 -0
  36. package/skills/arkts-error-fixes/assets/StorageLinkDefaultError.ets +41 -0
  37. package/skills/arkts-error-fixes/assets/TitleButtonRectTypeError.ets +86 -0
  38. package/skills/arkts-error-fixes/assets/UnusedVariableWarning.ets +102 -0
  39. package/skills/arkts-error-fixes/assets/UtilityTypeError.ets +176 -0
  40. package/skills/arkts-error-fixes/assets/WindowRectSizeError.ets +65 -0
  41. package/skills/arkts-error-fixes/assets/WindowTypeError.ets +37 -0
  42. package/skills/arkts-error-fixes/reference/any_type_errors.md +433 -0
  43. package/skills/arkts-error-fixes/reference/appstorage_errors.md +95 -0
  44. package/skills/arkts-error-fixes/reference/arrow_function_conversion_errors.md +163 -0
  45. package/skills/arkts-error-fixes/reference/avoid_area_type_errors.md +59 -0
  46. package/skills/arkts-error-fixes/reference/breakpoint_type_errors.md +122 -0
  47. package/skills/arkts-error-fixes/reference/catch_clause_type_errors.md +89 -0
  48. package/skills/arkts-error-fixes/reference/color_consistency_errors.md +209 -0
  49. package/skills/arkts-error-fixes/reference/color_property_errors.md +136 -0
  50. package/skills/arkts-error-fixes/reference/context_type_errors.md +260 -0
  51. package/skills/arkts-error-fixes/reference/decorator_state_errors.md +109 -0
  52. package/skills/arkts-error-fixes/reference/display_listener_type_errors.md +149 -0
  53. package/skills/arkts-error-fixes/reference/duplicate_entry_errors.md +193 -0
  54. package/skills/arkts-error-fixes/reference/esobject_type_errors.md +110 -0
  55. package/skills/arkts-error-fixes/reference/fontcolor_property_errors.md +104 -0
  56. package/skills/arkts-error-fixes/reference/function_return_type_errors.md +194 -0
  57. package/skills/arkts-error-fixes/reference/idata_source_errors.md +77 -0
  58. package/skills/arkts-error-fixes/reference/implementation_not_allowed_errors.md +191 -0
  59. package/skills/arkts-error-fixes/reference/interface_method_signature_errors.md +143 -0
  60. package/skills/arkts-error-fixes/reference/notification_errors.md +110 -0
  61. package/skills/arkts-error-fixes/reference/object_literal_interface_errors.md +156 -0
  62. package/skills/arkts-error-fixes/reference/object_literal_type_errors.md +110 -0
  63. package/skills/arkts-error-fixes/reference/object_spread_errors.md +105 -0
  64. package/skills/arkts-error-fixes/reference/possibly_null_errors.md +156 -0
  65. package/skills/arkts-error-fixes/reference/resource_conversion_errors.md +154 -0
  66. package/skills/arkts-error-fixes/reference/standalone_function_errors.md +68 -0
  67. package/skills/arkts-error-fixes/reference/storage_link_default_errors.md +124 -0
  68. package/skills/arkts-error-fixes/reference/title_button_rect_type_errors.md +167 -0
  69. package/skills/arkts-error-fixes/reference/unused_variable_warnings.md +295 -0
  70. package/skills/arkts-error-fixes/reference/utility_type_errors.md +290 -0
  71. package/skills/arkts-error-fixes/reference/window_rect_size_errors.md +70 -0
  72. package/skills/arkts-error-fixes/reference/window_type_errors.md +95 -0
  73. package/skills/arkts-grammar-standards/SKILL.md +71 -0
  74. package/skills/arkts-grammar-standards/references/basic-syntax.md +103 -0
  75. package/skills/arkts-grammar-standards/references/restrictions.md +100 -0
  76. package/skills/arkts-grammar-standards/references/topic-aliases.json +93 -0
  77. package/skills/arkts-grammar-standards/references/ts-diff.md +108 -0
  78. package/skills/arkts-runtime-fix/SKILL.md +152 -0
  79. package/skills/arkts-runtime-fix/SKILL_CN.md +123 -0
  80. package/skills/arkts-runtime-fix/evals/evals.json +17 -0
  81. package/skills/arkts-runtime-fix/scripts/collect-hilog.mjs +116 -0
  82. package/skills/arkts-runtime-fix/scripts/collect-hilog.ts +115 -0
  83. package/skills/arkts-runtime-fix/scripts/fetch-faultlog.mjs +117 -0
  84. package/skills/arkts-runtime-fix/scripts/fetch-faultlog.ts +116 -0
  85. package/skills/arkts-runtime-fix/scripts/jscrash-report.mjs +149 -0
  86. package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.mjs +126 -0
  87. package/skills/arkts-runtime-fix/scripts/parse-jscrash-log.ts +126 -0
  88. package/skills/arkts-runtime-fix/scripts/probe-faultlogger.mjs +160 -0
  89. package/skills/arkts-runtime-fix/scripts/probe-faultlogger.ts +160 -0
  90. package/skills/arkts-runtime-fix/scripts/shared/hdc.mjs +107 -0
  91. package/skills/arkts-runtime-fix/scripts/shared/hdc.ts +58 -0
  92. package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.mjs +143 -0
  93. package/skills/arkts-runtime-fix/scripts/shared/jscrash-faultlogger.ts +148 -0
  94. package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.mjs +389 -0
  95. package/skills/arkts-runtime-fix/scripts/shared/jscrash-parse.ts +409 -0
  96. package/skills/arkts-runtime-fix/scripts/shared/utils.mjs +27 -0
  97. package/skills/arkts-runtime-fix/scripts/shared/utils.ts +27 -0
  98. package/skills/arkui-knowledge/SKILL.md +90 -0
  99. package/skills/arkui-knowledge/references/api-guardrails.md +60 -0
  100. package/skills/arkui-knowledge/references/common-mistakes.md +119 -0
  101. package/skills/arkui-knowledge/references/component-cookbook.md +135 -0
  102. package/skills/arkui-knowledge/references/ui-quality-checklist.md +36 -0
  103. package/skills/deveco-create-project/SKILL.md +144 -0
  104. package/skills/deveco-create-project/application/AppScope/app.json5 +10 -0
  105. package/skills/deveco-create-project/application/AppScope/resources/base/element/string.json +8 -0
  106. package/skills/deveco-create-project/application/AppScope/resources/base/media/background.png +0 -0
  107. package/skills/deveco-create-project/application/AppScope/resources/base/media/foreground.png +0 -0
  108. package/skills/deveco-create-project/application/AppScope/resources/base/media/layered_image.json +7 -0
  109. package/skills/deveco-create-project/application/build-profile.json5 +42 -0
  110. package/skills/deveco-create-project/application/code-linter.json5 +32 -0
  111. package/skills/deveco-create-project/application/entry/build-profile.json5 +33 -0
  112. package/skills/deveco-create-project/application/entry/hvigorfile.ts +7 -0
  113. package/skills/deveco-create-project/application/entry/obfuscation-rules.txt +23 -0
  114. package/skills/deveco-create-project/application/entry/oh-package.json5 +10 -0
  115. package/skills/deveco-create-project/application/entry/src/main/ets/entryability/EntryAbility.ets +63 -0
  116. package/skills/deveco-create-project/application/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets +31 -0
  117. package/skills/deveco-create-project/application/entry/src/main/ets/pages/Index.ets +38 -0
  118. package/skills/deveco-create-project/application/entry/src/main/module.json5 +50 -0
  119. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/color.json +8 -0
  120. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/float.json +8 -0
  121. package/skills/deveco-create-project/application/entry/src/main/resources/base/element/string.json +16 -0
  122. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/background.png +0 -0
  123. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/foreground.png +0 -0
  124. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/layered_image.json +7 -0
  125. package/skills/deveco-create-project/application/entry/src/main/resources/base/media/startIcon.png +0 -0
  126. package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/backup_config.json +3 -0
  127. package/skills/deveco-create-project/application/entry/src/main/resources/base/profile/main_pages.json +5 -0
  128. package/skills/deveco-create-project/application/entry/src/main/resources/dark/element/color.json +8 -0
  129. package/skills/deveco-create-project/application/hvigor/hvigor-config.json5 +23 -0
  130. package/skills/deveco-create-project/application/hvigorfile.ts +7 -0
  131. package/skills/deveco-create-project/application/oh-package.json5 +10 -0
  132. package/skills/deveco-create-project/scripts/copy-template.mjs +216 -0
  133. package/skills/deveco-create-project/scripts/copy-template.ts +214 -0
  134. package/skills/deveco-create-project/scripts/detect-sdk.mjs +82 -0
  135. package/skills/deveco-create-project/scripts/detect-sdk.ts +95 -0
@@ -0,0 +1,214 @@
1
+ /*
2
+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ import fs from 'node:fs';
17
+ import path from 'node:path';
18
+ import process from 'node:process';
19
+ import { fileURLToPath } from 'node:url';
20
+ import { detectApiLevel } from './detect-sdk';
21
+
22
+ type Args = {
23
+ projectPath: string;
24
+ appName: string;
25
+ bundleName: string;
26
+ apiLevel?: number;
27
+ templateDir: string;
28
+ };
29
+
30
+ type ApiConfig = {
31
+ sdkVersion: string;
32
+ modelVersion: string;
33
+ };
34
+
35
+ type Source = 'user_input' | 'sdk_pkg' | 'fallback';
36
+
37
+ type Resolved = {
38
+ apiLevel: number;
39
+ source: Source;
40
+ detectedFrom?: string;
41
+ devecoHome?: string;
42
+ };
43
+
44
+ const API_CONFIGS: Record<number, ApiConfig> = {
45
+ 17: { sdkVersion: '5.0.5(17)', modelVersion: '5.0.5' },
46
+ 18: { sdkVersion: '5.0.6(18)', modelVersion: '5.0.6' },
47
+ 19: { sdkVersion: '5.0.7(19)', modelVersion: '5.0.7' },
48
+ 20: { sdkVersion: '6.0.0(20)', modelVersion: '6.0.0' },
49
+ 21: { sdkVersion: '6.0.1(21)', modelVersion: '6.0.1' },
50
+ 22: { sdkVersion: '6.0.2(22)', modelVersion: '6.0.2' },
51
+ 23: { sdkVersion: '6.1.0(23)', modelVersion: '6.1.0' },
52
+ 24: { sdkVersion: '6.1.1(24)', modelVersion: '6.1.1' },
53
+ };
54
+
55
+ const REQUIRED_FILES = [
56
+ 'build-profile.json5',
57
+ 'AppScope/resources/base/media/layered_image.json',
58
+ 'AppScope/resources/base/media/background.png',
59
+ 'AppScope/resources/base/media/foreground.png',
60
+ 'entry/src/main/resources/base/media/layered_image.json',
61
+ 'entry/src/main/resources/base/media/background.png',
62
+ 'entry/src/main/resources/base/media/foreground.png',
63
+ ];
64
+
65
+ function parseArgs(argv: string[]): Args {
66
+ const values = new Map<string, string>();
67
+ for (let index = 0; index < argv.length; index += 1) {
68
+ const token = argv[index];
69
+ if (!token.startsWith('--')) {
70
+ continue;
71
+ }
72
+ const key = token.slice(2);
73
+ const value = argv[index + 1];
74
+ if (!value || value.startsWith('--')) {
75
+ throw new Error(`Missing value for --${key}`);
76
+ }
77
+ values.set(key, value);
78
+ index += 1;
79
+ }
80
+
81
+ const scriptDir = path.dirname(fileURLToPath(import.meta.url));
82
+ const templateDir = values.get('template-dir') ??
83
+ path.resolve(scriptDir, '../../deveco-create-project/application');
84
+ const projectPath = values.get('project-path');
85
+ const appName = values.get('app-name');
86
+ const bundleName = values.get('bundle-name') ?? (appName
87
+ ? `com.example.${appName.toLowerCase()}`
88
+ : undefined);
89
+ const apiLevelRaw = values.get('api-level');
90
+ const apiLevel = apiLevelRaw ? Number(apiLevelRaw) : undefined;
91
+
92
+ if (!projectPath) {
93
+ throw new Error('Missing required argument --project-path');
94
+ }
95
+ if (!appName) {
96
+ throw new Error('Missing required argument --app-name');
97
+ }
98
+ if (!bundleName) {
99
+ throw new Error('Missing required argument --bundle-name');
100
+ }
101
+ if (apiLevelRaw && (apiLevel === undefined || !Number.isInteger(apiLevel) || !API_CONFIGS[apiLevel])) {
102
+ throw new Error(`Unsupported apiLevel: ${apiLevelRaw}`);
103
+ }
104
+
105
+ return {
106
+ projectPath: path.resolve(projectPath),
107
+ appName,
108
+ bundleName,
109
+ apiLevel,
110
+ templateDir: path.resolve(templateDir),
111
+ };
112
+ }
113
+
114
+ async function resolve(args: Args): Promise<Resolved> {
115
+ if (args.apiLevel) {
116
+ return {
117
+ apiLevel: args.apiLevel,
118
+ source: 'user_input',
119
+ };
120
+ }
121
+ return detectApiLevel();
122
+ }
123
+
124
+ function copyDirectoryContents(sourceDir: string, targetDir: string): void {
125
+ fs.mkdirSync(targetDir, { recursive: true });
126
+ for (const entry of fs.readdirSync(sourceDir, { withFileTypes: true })) {
127
+ const sourcePath = path.join(sourceDir, entry.name);
128
+ const targetPath = path.join(targetDir, entry.name);
129
+ if (entry.isDirectory()) {
130
+ copyDirectoryContents(sourcePath, targetPath);
131
+ continue;
132
+ }
133
+ if (fs.existsSync(targetPath)) {
134
+ continue;
135
+ }
136
+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });
137
+ fs.copyFileSync(sourcePath, targetPath);
138
+ }
139
+ }
140
+
141
+ function replaceInFile(filePath: string, pairs: Array<[string, string]>): void {
142
+ const original = fs.readFileSync(filePath, 'utf-8');
143
+ let next = original;
144
+ for (const [from, to] of pairs) {
145
+ next = next.replaceAll(from, to);
146
+ }
147
+ if (next !== original) {
148
+ fs.writeFileSync(filePath, next, 'utf-8');
149
+ }
150
+ }
151
+
152
+ function updateApiLevel(targetRoot: string, apiLevel: number): void {
153
+ if (apiLevel === 22) {
154
+ return;
155
+ }
156
+ const config = API_CONFIGS[apiLevel];
157
+ replaceInFile(path.join(targetRoot, 'build-profile.json5'), [
158
+ ['6.0.2(22)', config.sdkVersion],
159
+ ]);
160
+ replaceInFile(path.join(targetRoot, 'hvigor/hvigor-config.json5'), [
161
+ ['6.0.2', config.modelVersion],
162
+ ]);
163
+ replaceInFile(path.join(targetRoot, 'oh-package.json5'), [
164
+ ['6.0.2', config.modelVersion],
165
+ ]);
166
+ }
167
+
168
+ function verifyFiles(targetRoot: string): string[] {
169
+ return REQUIRED_FILES.filter((relativePath) => !fs.existsSync(path.join(targetRoot, relativePath)));
170
+ }
171
+
172
+ async function main(): Promise<void> {
173
+ const args = parseArgs(process.argv.slice(2));
174
+ const resolved = await resolve(args);
175
+ if (!fs.existsSync(args.templateDir)) {
176
+ throw new Error(`Template directory not found: ${args.templateDir}`);
177
+ }
178
+
179
+ fs.mkdirSync(args.projectPath, { recursive: true });
180
+ const targetRoot = path.join(args.projectPath, args.appName);
181
+ copyDirectoryContents(args.templateDir, targetRoot);
182
+
183
+ replaceInFile(path.join(targetRoot, 'AppScope/resources/base/element/string.json'), [
184
+ ['MyApplication', args.appName],
185
+ ]);
186
+ replaceInFile(path.join(targetRoot, 'AppScope/app.json5'), [
187
+ ['com.example.myapplication', args.bundleName],
188
+ ]);
189
+ updateApiLevel(targetRoot, resolved.apiLevel);
190
+
191
+ const missingFiles = verifyFiles(targetRoot);
192
+ if (missingFiles.length > 0) {
193
+ throw new Error(`Template copy incomplete. Missing files: ${missingFiles.join(', ')}`);
194
+ }
195
+
196
+ console.log(JSON.stringify({
197
+ projectRoot: targetRoot,
198
+ appName: args.appName,
199
+ bundleName: args.bundleName,
200
+ apiLevel: resolved.apiLevel,
201
+ source: resolved.source,
202
+ detectedFrom: resolved.detectedFrom,
203
+ devecoHome: resolved.devecoHome,
204
+ verified: true,
205
+ }, null, 2));
206
+ }
207
+
208
+ try {
209
+ await main();
210
+ } catch (error) {
211
+ const message = error instanceof Error ? error.message : String(error);
212
+ console.error(message);
213
+ process.exit(1);
214
+ }
@@ -0,0 +1,82 @@
1
+ import path from 'node:path';
2
+ import fs from 'node:fs/promises';
3
+
4
+ function envPath() {
5
+ return String(process.env.DEVECO_HOME || '').trim();
6
+ }
7
+
8
+ async function isDir(file) {
9
+ if (!file) {
10
+ return false;
11
+ }
12
+ return fs.stat(file)
13
+ .then((info) => info.isDirectory())
14
+ .catch(() => false);
15
+ }
16
+
17
+ function nodePath(home) {
18
+ return process.platform === 'win32'
19
+ ? path.join(home, 'tools', 'node', 'node.exe')
20
+ : path.join(home, 'tools', 'node', 'bin', 'node');
21
+ }
22
+
23
+ async function findDevEcoHome() {
24
+ const env = envPath();
25
+ if (env && (await isDir(env)) && (await exists(nodePath(env)))) {
26
+ return env;
27
+ }
28
+ }
29
+
30
+ function ok(apiLevel) {
31
+ return Number.isInteger(apiLevel) && apiLevel >= 17 && apiLevel <= 24;
32
+ }
33
+
34
+ function parse(raw) {
35
+ if (typeof raw !== 'string') {
36
+ return;
37
+ }
38
+ const apiLevel = Number(raw);
39
+ if (!ok(apiLevel)) {
40
+ return;
41
+ }
42
+ return apiLevel;
43
+ }
44
+
45
+ async function json(file) {
46
+ if (!(await exists(file))) {
47
+ return;
48
+ }
49
+ return JSON.parse(await fs.readFile(file, 'utf8'));
50
+ }
51
+
52
+ async function exists(file) {
53
+ return fs.access(file).then(() => true).catch(() => false);
54
+ }
55
+
56
+ export async function detectApiLevel() {
57
+ const home = await findDevEcoHome().catch(() => undefined);
58
+ if (!home) {
59
+ return {
60
+ apiLevel: 22,
61
+ source: 'fallback',
62
+ };
63
+ }
64
+
65
+ const sdk = path.join(home, 'sdk', 'default', 'sdk-pkg.json');
66
+ const first = await json(sdk).catch(() => undefined);
67
+ const firstLevel = parse(first?.data?.apiVersion);
68
+ if (firstLevel) {
69
+ return {
70
+ apiLevel: firstLevel,
71
+ source: 'sdk_pkg',
72
+ detectedFrom: sdk,
73
+ devecoHome: home,
74
+ };
75
+ }
76
+
77
+ return {
78
+ apiLevel: 22,
79
+ source: 'fallback',
80
+ devecoHome: home,
81
+ };
82
+ }
@@ -0,0 +1,95 @@
1
+ import path from 'node:path';
2
+
3
+ type Source = 'sdk_pkg' | 'fallback';
4
+
5
+ type Result = {
6
+ apiLevel: number;
7
+ source: Source;
8
+ detectedFrom?: string;
9
+ devecoHome?: string;
10
+ };
11
+
12
+ type Pkg = {
13
+ data?: {
14
+ apiVersion?: unknown;
15
+ };
16
+ apiVersion?: unknown;
17
+ };
18
+
19
+ function envPath() {
20
+ return String(process.env.DEVECO_HOME || '').trim();
21
+ }
22
+
23
+ async function isDir(file: string) {
24
+ if (!file) {
25
+ return false;
26
+ }
27
+ return Bun.file(file)
28
+ .stat()
29
+ .then((info) => info.isDirectory())
30
+ .catch(() => false);
31
+ }
32
+
33
+ function nodePath(home: string) {
34
+ return process.platform === 'win32'
35
+ ? path.join(home, 'tools', 'node', 'node.exe')
36
+ : path.join(home, 'tools', 'node', 'bin', 'node');
37
+ }
38
+
39
+ async function findDevEcoHome() {
40
+ const env = envPath();
41
+ if (env && (await isDir(env)) && (await Bun.file(nodePath(env)).exists())) {
42
+ return env;
43
+ }
44
+ }
45
+
46
+ function ok(apiLevel: number) {
47
+ return Number.isInteger(apiLevel) && apiLevel >= 17 && apiLevel <= 24;
48
+ }
49
+
50
+ function parse(raw: unknown) {
51
+ if (typeof raw !== 'string') {
52
+ return;
53
+ }
54
+ const apiLevel = Number(raw);
55
+ if (!ok(apiLevel)) {
56
+ return;
57
+ }
58
+ return apiLevel;
59
+ }
60
+
61
+ async function json(file: string) {
62
+ const item = Bun.file(file);
63
+ if (!(await item.exists())) {
64
+ return;
65
+ }
66
+ return item.json() as Promise<Pkg>;
67
+ }
68
+
69
+ export async function detectApiLevel(): Promise<Result> {
70
+ const home = await findDevEcoHome().catch(() => undefined);
71
+ if (!home) {
72
+ return {
73
+ apiLevel: 22,
74
+ source: 'fallback',
75
+ };
76
+ }
77
+
78
+ const sdk = path.join(home, 'sdk', 'default', 'sdk-pkg.json');
79
+ const first = await json(sdk).catch(() => undefined);
80
+ const firstLevel = parse(first?.data?.apiVersion);
81
+ if (firstLevel) {
82
+ return {
83
+ apiLevel: firstLevel,
84
+ source: 'sdk_pkg',
85
+ detectedFrom: sdk,
86
+ devecoHome: home,
87
+ };
88
+ }
89
+
90
+ return {
91
+ apiLevel: 22,
92
+ source: 'fallback',
93
+ devecoHome: home,
94
+ };
95
+ }