catchword 0.0.0-stage → 1.0.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 (157) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +314 -2
  3. package/bin/catchword.js +8 -0
  4. package/bin/falcon.js +5 -0
  5. package/dist/cli/adb.d.ts +6 -0
  6. package/dist/cli/adb.js +52 -0
  7. package/dist/cli/audit.d.ts +4 -0
  8. package/dist/cli/audit.js +91 -0
  9. package/dist/cli/build.d.ts +13 -0
  10. package/dist/cli/build.js +139 -0
  11. package/dist/cli/clean.d.ts +1 -0
  12. package/dist/cli/clean.js +15 -0
  13. package/dist/cli/create.d.ts +4 -0
  14. package/dist/cli/create.js +60 -0
  15. package/dist/cli/dev.d.ts +10 -0
  16. package/dist/cli/dev.js +23 -0
  17. package/dist/cli/index.d.ts +3 -0
  18. package/dist/cli/index.js +243 -0
  19. package/dist/cli/native.d.ts +18 -0
  20. package/dist/cli/native.js +232 -0
  21. package/dist/cli/simulator.d.ts +8 -0
  22. package/dist/cli/simulator.js +77 -0
  23. package/dist/cli/upload.d.ts +9 -0
  24. package/dist/cli/upload.js +135 -0
  25. package/dist/cli/util.d.ts +8 -0
  26. package/dist/cli/util.js +43 -0
  27. package/dist/config.d.ts +17 -0
  28. package/dist/config.js +90 -0
  29. package/dist/index.d.ts +16 -0
  30. package/dist/index.js +51 -0
  31. package/dist/native/api.d.ts +46 -0
  32. package/dist/native/api.js +185 -0
  33. package/dist/native/build.d.ts +47 -0
  34. package/dist/native/build.js +407 -0
  35. package/dist/native/elf.d.ts +15 -0
  36. package/dist/native/elf.js +200 -0
  37. package/dist/native/types.d.ts +9 -0
  38. package/dist/native/types.js +91 -0
  39. package/dist/native/wsl.d.ts +7 -0
  40. package/dist/native/wsl.js +29 -0
  41. package/dist/plugin/app-entry.d.ts +6 -0
  42. package/dist/plugin/app-entry.js +115 -0
  43. package/dist/plugin/assets.d.ts +4 -0
  44. package/dist/plugin/assets.js +84 -0
  45. package/dist/plugin/config.d.ts +14 -0
  46. package/dist/plugin/config.js +80 -0
  47. package/dist/plugin/context.d.ts +58 -0
  48. package/dist/plugin/context.js +255 -0
  49. package/dist/plugin/fallback.d.ts +4 -0
  50. package/dist/plugin/fallback.js +81 -0
  51. package/dist/plugin/image-service.d.ts +17 -0
  52. package/dist/plugin/image-service.js +138 -0
  53. package/dist/plugin/jsapi.d.ts +6 -0
  54. package/dist/plugin/jsapi.js +82 -0
  55. package/dist/plugin/output.d.ts +4 -0
  56. package/dist/plugin/output.js +75 -0
  57. package/dist/plugin/preview.d.ts +34 -0
  58. package/dist/plugin/preview.js +459 -0
  59. package/dist/plugin/sanitize.d.ts +5 -0
  60. package/dist/plugin/sanitize.js +48 -0
  61. package/dist/plugin/security.d.ts +4 -0
  62. package/dist/plugin/security.js +18 -0
  63. package/dist/plugin/sfc.d.ts +6 -0
  64. package/dist/plugin/sfc.js +181 -0
  65. package/dist/plugin/shim.d.ts +4 -0
  66. package/dist/plugin/shim.js +68 -0
  67. package/dist/plugin/state.d.ts +31 -0
  68. package/dist/plugin/state.js +61 -0
  69. package/dist/plugin/string-table.d.ts +7 -0
  70. package/dist/plugin/string-table.js +324 -0
  71. package/dist/plugin/style.d.ts +15 -0
  72. package/dist/plugin/style.js +114 -0
  73. package/dist/plugin/template.d.ts +7 -0
  74. package/dist/plugin/template.js +39 -0
  75. package/dist/steps/manifest.d.ts +9 -0
  76. package/dist/steps/manifest.js +60 -0
  77. package/dist/steps/pack.d.ts +3 -0
  78. package/dist/steps/pack.js +46 -0
  79. package/dist/steps/qjsc.d.ts +9 -0
  80. package/dist/steps/qjsc.js +186 -0
  81. package/dist/steps/report.d.ts +2 -0
  82. package/dist/steps/report.js +50 -0
  83. package/dist/steps/security.d.ts +17 -0
  84. package/dist/steps/security.js +310 -0
  85. package/dist/types.d.ts +158 -0
  86. package/dist/types.js +1 -0
  87. package/dist/util/class-tokenize.d.ts +2 -0
  88. package/dist/util/class-tokenize.js +56 -0
  89. package/dist/util/component-name.d.ts +6 -0
  90. package/dist/util/component-name.js +120 -0
  91. package/dist/util/fs.d.ts +14 -0
  92. package/dist/util/fs.js +70 -0
  93. package/dist/util/imports.d.ts +2 -0
  94. package/dist/util/imports.js +173 -0
  95. package/dist/util/log.d.ts +8 -0
  96. package/dist/util/log.js +24 -0
  97. package/package.json +61 -4
  98. package/template/README.md +38 -0
  99. package/template/catchword.config.ts +18 -0
  100. package/template/native/CMakeLists.txt +33 -0
  101. package/template/native/README.md +34 -0
  102. package/template/native/api.override.json +7 -0
  103. package/template/native/include/jsapi_demo.h +10 -0
  104. package/template/native/lib/README.md +10 -0
  105. package/template/native/sdk/README.md +6 -0
  106. package/template/native/src/bridge.cpp +9 -0
  107. package/template/native/src/jsapi_demo.cpp +11 -0
  108. package/template/package.json +38 -0
  109. package/template/src/app.js +45 -0
  110. package/template/src/app.json +11 -0
  111. package/template/src/base-page.js +150 -0
  112. package/template/src/components/HelloWorld.vue +20 -0
  113. package/template/src/mocks/$jsapi.test.js +5 -0
  114. package/template/src/mocks/test-module.js +5 -0
  115. package/template/src/pages/index/index.vue +41 -0
  116. package/template/src/pages/page/page.vue +63 -0
  117. package/template/src/shims.d.ts +21 -0
  118. package/template/src/styles/base.less +23 -0
  119. package/template/src/styles/mixin.less +32 -0
  120. package/template/src/styles/var.less +15 -0
  121. package/template/tsconfig.json +14 -0
  122. package/vendor/README.md +16 -0
  123. package/vendor/falcon-styler/.jscsrc +31 -0
  124. package/vendor/falcon-styler/LICENSE +21 -0
  125. package/vendor/falcon-styler/README.md +124 -0
  126. package/vendor/falcon-styler/index.js +273 -0
  127. package/vendor/falcon-styler/lib/shorthand-parser.js +78 -0
  128. package/vendor/falcon-styler/lib/util.js +30 -0
  129. package/vendor/falcon-styler/lib/validator.js +846 -0
  130. package/vendor/falcon-styler/package.json +32 -0
  131. package/vendor/licenses/falcon-styler.LICENSE +21 -0
  132. package/vendor/licenses/weex-template-compiler.README.md +3 -0
  133. package/vendor/package.json +5 -0
  134. package/vendor/qjsc/SHA256SUMS +11 -0
  135. package/vendor/qjsc/bin/darwin/arm64/qjsc20200705 +0 -0
  136. package/vendor/qjsc/bin/darwin/x64/qjsc20200412 +0 -0
  137. package/vendor/qjsc/bin/darwin/x64/qjsc20200705 +0 -0
  138. package/vendor/qjsc/bin/darwin/x64/qjsc20200906 +0 -0
  139. package/vendor/qjsc/bin/darwin/x64/qjscbn20200412 +0 -0
  140. package/vendor/qjsc/bin/darwin/x64/qjscbn20200705 +0 -0
  141. package/vendor/qjsc/bin/darwin/x64/qjscbn20200906 +0 -0
  142. package/vendor/qjsc/bin/linux/qjsc20200705 +0 -0
  143. package/vendor/qjsc/bin/win32/libgcc_s_dw2-1.dll +0 -0
  144. package/vendor/qjsc/bin/win32/pthreadGC-3.dll +0 -0
  145. package/vendor/qjsc/bin/win32/qjsc20200705.exe +0 -0
  146. package/vendor/web/falcon-js-web.js +863 -0
  147. package/vendor/web/falcon-jsapi-web.js +479 -0
  148. package/vendor/web/falcon-vue-render.js +11323 -0
  149. package/vendor/web/falcon-web-boot.js +4 -0
  150. package/vendor/web/falcon-web-hud.js +119 -0
  151. package/vendor/web/falcon-web-style.js +235 -0
  152. package/vendor/web/vue.runtime.common.dev.js +8467 -0
  153. package/vendor/web/vue.runtime.common.js +5 -0
  154. package/vendor/weex-template-compiler/README.md +3 -0
  155. package/vendor/weex-template-compiler/build.js +4824 -0
  156. package/vendor/weex-template-compiler/index.js +17 -0
  157. package/vendor/weex-template-compiler/package.json +25 -0
@@ -0,0 +1,15 @@
1
+ import fs from 'node:fs';
2
+ import { createContext } from '../plugin/context.js';
3
+ import { rmrf } from '../util/fs.js';
4
+ import { log } from '../util/log.js';
5
+ export async function runClean(dir) {
6
+ const ctx = createContext({ dir });
7
+ rmrf(ctx.buildDir);
8
+ rmrf(ctx.tmpDir);
9
+ for (const stale of [ctx.amrPath, ctx.amrPath + '.tmp', ctx.amrPath + '.sha256']) {
10
+ if (fs.existsSync(stale))
11
+ fs.unlinkSync(stale);
12
+ }
13
+ log.success(`已清理: ${ctx.buildDir}, ${ctx.tmpDir}, ${ctx.amrFileName}`);
14
+ return 0;
15
+ }
@@ -0,0 +1,4 @@
1
+ export interface CreateOptions {
2
+ preset?: string;
3
+ }
4
+ export declare function runCreate(name: string | undefined, options?: CreateOptions): Promise<number>;
@@ -0,0 +1,60 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { log } from '../util/log.js';
5
+ import { runNativeInit } from './native.js';
6
+ const here = path.dirname(fileURLToPath(import.meta.url));
7
+ const TEMPLATE_DIR = path.resolve(here, '../../template');
8
+ export async function runCreate(name, options = {}) {
9
+ if (!name) {
10
+ log.error('请输入应用目录名称:\nUsage: catchword create <name>');
11
+ return -1;
12
+ }
13
+ const preset = options.preset ?? 'base';
14
+ if (preset !== 'base' && preset !== 'native') {
15
+ log.error(`未知模板预设: ${preset}(可选 base | native)`);
16
+ return -1;
17
+ }
18
+ const destDir = path.resolve(process.cwd(), name);
19
+ if (fs.existsSync(destDir)) {
20
+ log.error(`创建失败,目录已存在: ${destDir}`);
21
+ return -1;
22
+ }
23
+ if (!fs.existsSync(TEMPLATE_DIR)) {
24
+ log.error(`模板目录缺失: ${TEMPLATE_DIR}`);
25
+ return -1;
26
+ }
27
+ fs.cpSync(TEMPLATE_DIR, destDir, {
28
+ recursive: true,
29
+ filter(source) {
30
+ const rel = path.relative(TEMPLATE_DIR, source);
31
+ return rel !== 'native' && !rel.startsWith('native' + path.sep);
32
+ }
33
+ });
34
+ const ignoreFile = path.join(destDir, '.gitignore');
35
+ if (!fs.existsSync(ignoreFile)) {
36
+ fs.writeFileSync(ignoreFile, 'node_modules\n.falcon_\n.falcon_tmp\ndist\n*.amr\n');
37
+ }
38
+ const pkgFile = path.join(destDir, 'package.json');
39
+ const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf8'));
40
+ pkg.name = name;
41
+ pkg.appid = '800' + Date.now();
42
+ fs.writeFileSync(pkgFile, JSON.stringify(pkg, null, 2));
43
+ if (preset === 'native') {
44
+ const code = await runNativeInit({
45
+ dir: destDir,
46
+ config: { native: { dir: 'native' } }
47
+ });
48
+ if (code !== 0)
49
+ return code;
50
+ }
51
+ log.success(`创建成功: ${destDir}`);
52
+ log.info(`
53
+ cd ${name}
54
+ bun install # 或 npm install / pnpm install
55
+ bun run dev # 浏览器预览
56
+ bun run build # 构建 .falcon_
57
+ bun run build:prod${preset === 'native' ? '\nbun run native # 抽取契约、生成类型、编译 .so' : ''}
58
+ `);
59
+ return 0;
60
+ }
@@ -0,0 +1,10 @@
1
+ import type { FalconUserConfig } from '../types.js';
2
+ export interface DevOptions {
3
+ dir: string;
4
+ host?: string;
5
+ port?: number;
6
+ open?: boolean;
7
+ silent?: boolean;
8
+ config?: FalconUserConfig;
9
+ }
10
+ export declare function runDev(opts: DevOptions): Promise<number>;
@@ -0,0 +1,23 @@
1
+ import { createServer } from 'vite';
2
+ import { createCatchwordPlugins } from '../index.js';
3
+ import { loadConfig } from '../config.js';
4
+ import { log } from '../util/log.js';
5
+ export async function runDev(opts) {
6
+ const config = opts.config ?? (await loadConfig(opts.dir, { mode: 'development' })).config;
7
+ const { ctx, plugins } = createCatchwordPlugins({ dir: opts.dir, config });
8
+ const server = await createServer({
9
+ configFile: false,
10
+ root: ctx.root,
11
+ plugins,
12
+ logLevel: opts.silent ? 'silent' : 'info',
13
+ server: {
14
+ host: opts.host ?? '127.0.0.1',
15
+ port: opts.port ?? 8081,
16
+ open: opts.open === true
17
+ }
18
+ });
19
+ await server.listen();
20
+ server.printUrls();
21
+ log.info(`打开预览: http://localhost:${server.config.server.port}/_preview.app.html`);
22
+ return 0;
23
+ }
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ declare let result: number | undefined;
3
+ export { result };
@@ -0,0 +1,243 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire } from 'node:module';
3
+ import { Command } from 'commander';
4
+ import { runBuild } from './build.js';
5
+ import { runDev } from './dev.js';
6
+ import { runAdb } from './adb.js';
7
+ import { runUpload } from './upload.js';
8
+ import { runCreate } from './create.js';
9
+ import { runSimulator } from './simulator.js';
10
+ import { runClean } from './clean.js';
11
+ import { runNativeApi, runNativeTypes, runNativeBuildCommand, runNativeVerifyCommand, runNativeInit } from './native.js';
12
+ import { runAudit } from './audit.js';
13
+ import { loadConfig } from '../config.js';
14
+ import { log } from '../util/log.js';
15
+ const require = createRequire(import.meta.url);
16
+ const pkg = require('../../package.json');
17
+ const program = new Command();
18
+ program
19
+ .name('catchword')
20
+ .description('Falcon 小程序构建工具(Vite / QuickJS / .amr)')
21
+ .version(pkg.version)
22
+ .option('-d, --dir <dir>', '小程序工程所在目录', './')
23
+ .option('-s, --silent', '静默无提示', false);
24
+ let result;
25
+ function finish(code) {
26
+ result = code;
27
+ if (typeof code === 'number' && code !== 0)
28
+ process.exitCode = code;
29
+ }
30
+ function pick(cli, config, fallback) {
31
+ if (cli !== undefined)
32
+ return cli;
33
+ if (config !== undefined)
34
+ return config;
35
+ return fallback;
36
+ }
37
+ program
38
+ .command('build', { isDefault: true })
39
+ .alias('b')
40
+ .description('构建应用')
41
+ .option('-c, --compress', '压缩脚本')
42
+ .option('-q, --qjsc', '使用 qjsc 预编译')
43
+ .option('-p, --pack', '打包 .amr')
44
+ .option('-m, --mock', '模拟 JSAPI')
45
+ .option('-n, --native [target]', '先编译原生 .so(默认 x7)')
46
+ .option('-e, --env <vars...>', '构建期常量,如 -e FOO=bar DEBUG')
47
+ .action(async (...args) => {
48
+ const cmd = args[args.length - 1];
49
+ const opts = cmd.optsWithGlobals();
50
+ const loaded = await loadConfig(opts.dir, { mode: 'production' });
51
+ const cfg = loaded.config.build ?? {};
52
+ if (loaded.path && !opts.silent)
53
+ log.info(`配置文件: ${loaded.path}`);
54
+ finish(await runBuild({
55
+ dir: opts.dir,
56
+ compress: pick(opts.compress, cfg.compress, false),
57
+ qjsc: pick(opts.qjsc, cfg.qjsc, false),
58
+ pack: pick(opts.pack, cfg.pack, false),
59
+ mock: pick(opts.mock, cfg.mock, false),
60
+ nativeTarget: opts.native,
61
+ env: opts.env ?? cfg.env,
62
+ silent: opts.silent,
63
+ config: loaded.config
64
+ }));
65
+ });
66
+ for (const name of ['dev', 'preview']) {
67
+ const command = program
68
+ .command(name)
69
+ .description('浏览器预览应用(Vite dev server)')
70
+ .option('--host <host>', '监听地址')
71
+ .option('--port <port>', '端口')
72
+ .option('--open', '自动打开浏览器');
73
+ if (name === 'dev')
74
+ command.alias('p');
75
+ command.action(async (...args) => {
76
+ const cmd = args[args.length - 1];
77
+ const opts = cmd.optsWithGlobals();
78
+ const loaded = await loadConfig(opts.dir, { mode: 'development' });
79
+ const pv = loaded.config.preview ?? {};
80
+ if (loaded.path && !opts.silent)
81
+ log.info(`配置文件: ${loaded.path}`);
82
+ finish(await runDev({
83
+ dir: opts.dir,
84
+ host: opts.host ?? pv.host,
85
+ port: opts.port !== undefined ? Number(opts.port) : pv.port,
86
+ open: pick(opts.open, pv.open, false),
87
+ silent: opts.silent,
88
+ config: loaded.config
89
+ }));
90
+ });
91
+ }
92
+ program
93
+ .command('create')
94
+ .alias('c')
95
+ .argument('[name]', '小程序名称')
96
+ .option('--preset <name>', '模板预设: base | native', 'base')
97
+ .description('创建小程序')
98
+ .action(async (...args) => {
99
+ const cmd = args[args.length - 1];
100
+ const opts = cmd.optsWithGlobals();
101
+ finish(await runCreate(args[0], { preset: opts.preset }));
102
+ });
103
+ program
104
+ .command('adb')
105
+ .alias('a')
106
+ .argument('[path]', 'amr 包路径(默认取当前工程产物)')
107
+ .option('-p, --page <page>', '启动页面', '')
108
+ .description('通过 adb 推包 → 安装 → 启动')
109
+ .action(async (...args) => {
110
+ const cmd = args[args.length - 1];
111
+ const opts = cmd.optsWithGlobals();
112
+ finish(await runAdb({
113
+ dir: opts.dir,
114
+ appPath: args[0],
115
+ page: opts.page
116
+ }));
117
+ });
118
+ program
119
+ .command('upload')
120
+ .alias('u')
121
+ .argument('[path]', 'amr 包路径')
122
+ .option('-h, --host <ip>', '设备 IP', '127.0.0.1')
123
+ .option('--port <port>', '端口', '5556')
124
+ .option('-p, --page <page>', '启动页面', 'index')
125
+ .option('-n, --noPath', '忽略传入路径,使用当前工程产物', false)
126
+ .description('通过 HTTP 上传并安装到设备')
127
+ .action(async (...args) => {
128
+ const cmd = args[args.length - 1];
129
+ const opts = cmd.optsWithGlobals();
130
+ finish(await runUpload({
131
+ dir: opts.dir,
132
+ appPath: opts.noPath ? undefined : args[0],
133
+ host: opts.host,
134
+ port: opts.port,
135
+ page: opts.page,
136
+ noPath: opts.noPath
137
+ }));
138
+ });
139
+ program
140
+ .command('simulator')
141
+ .alias('s')
142
+ .description('模拟器预览(appx)')
143
+ .option('--simpath <path>', '模拟器路径')
144
+ .option('-p, --page <page>', '预览页面')
145
+ .action(async (...args) => {
146
+ const cmd = args[args.length - 1];
147
+ const opts = cmd.optsWithGlobals();
148
+ const loaded = await loadConfig(opts.dir, { mode: 'production' });
149
+ finish(await runSimulator({
150
+ dir: opts.dir,
151
+ simpath: opts.simpath,
152
+ page: opts.page,
153
+ config: loaded.config
154
+ }));
155
+ });
156
+ const native = program.command('native').description('原生 JSAPI(jsapi/)相关命令');
157
+ native
158
+ .command('init')
159
+ .description('向当前工程加入 native 骨架')
160
+ .action(async (...args) => {
161
+ const cmd = args[args.length - 1];
162
+ const opts = cmd.optsWithGlobals();
163
+ finish(await runNativeInit({ dir: opts.dir, silent: opts.silent }));
164
+ });
165
+ native
166
+ .command('api')
167
+ .description('从 C++ 源码抽取原生 API 契约到 api.json')
168
+ .option('--check', '只比对,不写文件(CI 用)')
169
+ .action(async (...args) => {
170
+ const cmd = args[args.length - 1];
171
+ const opts = cmd.optsWithGlobals();
172
+ finish(await runNativeApi({ dir: opts.dir, check: opts.check, silent: opts.silent }));
173
+ });
174
+ native
175
+ .command('build')
176
+ .description('编译原生 libjsapi_bridge.so(CMake + 交叉工具链 + strip + 校验)')
177
+ .option('-t, --target <name>', '目标平台', 'x7')
178
+ .option('--skip-verify', '跳过 ELF 校验')
179
+ .option('--strict', '把校验提示视为失败')
180
+ .action(async (...args) => {
181
+ const cmd = args[args.length - 1];
182
+ const opts = cmd.optsWithGlobals();
183
+ finish(await runNativeBuildCommand({
184
+ dir: opts.dir,
185
+ target: opts.target,
186
+ skipVerify: opts.skipVerify,
187
+ strict: opts.strict,
188
+ silent: opts.silent
189
+ }));
190
+ });
191
+ native
192
+ .command('verify')
193
+ .description('检查 .so 的架构 / GLIBC 版本上限')
194
+ .argument('[file]', '产物路径,默认取 native.targets 的 out')
195
+ .option('-t, --target <name>', '目标平台', 'x7')
196
+ .option('--strict', '把校验提示视为失败')
197
+ .action(async (...args) => {
198
+ const cmd = args[args.length - 1];
199
+ const opts = cmd.optsWithGlobals();
200
+ finish(await runNativeVerifyCommand({
201
+ dir: opts.dir,
202
+ target: opts.target,
203
+ file: args[0],
204
+ strict: opts.strict,
205
+ silent: opts.silent
206
+ }));
207
+ });
208
+ native
209
+ .command('types')
210
+ .description('从 api.json 生成 TypeScript 声明')
211
+ .option('--check', '只比对,不写文件(CI 用)')
212
+ .action(async (...args) => {
213
+ const cmd = args[args.length - 1];
214
+ const opts = cmd.optsWithGlobals();
215
+ finish(await runNativeTypes({ dir: opts.dir, check: opts.check, silent: opts.silent }));
216
+ });
217
+ program
218
+ .command('audit')
219
+ .description('审计构建产物中可被逆向提取的字符串/标识符/URL/源路径')
220
+ .option('--strict', '发现源路径或明文 URL 时返回非零(CI 用)')
221
+ .action(async (...args) => {
222
+ const cmd = args[args.length - 1];
223
+ const opts = cmd.optsWithGlobals();
224
+ finish(await runAudit(opts.dir, { strict: opts.strict === true }));
225
+ });
226
+ program
227
+ .command('clean')
228
+ .description('清理构建产物与 .amr')
229
+ .action(async (...args) => {
230
+ const cmd = args[args.length - 1];
231
+ const opts = cmd.optsWithGlobals();
232
+ finish(await runClean(opts.dir));
233
+ });
234
+ program.showHelpAfterError();
235
+ process.on('uncaughtException', (err) => {
236
+ console.error(err);
237
+ process.exit(1);
238
+ });
239
+ program.parseAsync(process.argv).catch((err) => {
240
+ console.error(err);
241
+ process.exit(1);
242
+ });
243
+ export { result };
@@ -0,0 +1,18 @@
1
+ import type { FalconUserConfig } from '../types.js';
2
+ export interface NativeOptions {
3
+ dir: string;
4
+ config?: FalconUserConfig;
5
+ check?: boolean;
6
+ silent?: boolean;
7
+ }
8
+ export declare function runNativeInit(opts: NativeOptions): Promise<number>;
9
+ export declare function runNativeApi(opts: NativeOptions): Promise<number>;
10
+ export declare function runNativeTypes(opts: NativeOptions): Promise<number>;
11
+ export interface NativeBuildCliOptions extends NativeOptions {
12
+ target?: string;
13
+ skipVerify?: boolean;
14
+ strict?: boolean;
15
+ file?: string;
16
+ }
17
+ export declare function runNativeBuildCommand(opts: NativeBuildCliOptions): Promise<number>;
18
+ export declare function runNativeVerifyCommand(opts: NativeBuildCliOptions): Promise<number>;
@@ -0,0 +1,232 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { loadConfig } from '../config.js';
5
+ import { diffApi, extractApi, readApiFile, resolveNativePaths, writeJsonFile } from '../native/api.js';
6
+ import { generateTypes, readOverrides, writeTypesFile } from '../native/types.js';
7
+ import { log } from '../util/log.js';
8
+ import { runNativeBuild, runNativeVerify } from '../native/build.js';
9
+ const here = path.dirname(fileURLToPath(import.meta.url));
10
+ const TEMPLATE_NATIVE = path.resolve(here, '../../template/native');
11
+ function copyMissing(source, dest) {
12
+ fs.mkdirSync(dest, { recursive: true });
13
+ for (const entry of fs.readdirSync(source, { withFileTypes: true })) {
14
+ const from = path.join(source, entry.name);
15
+ const to = path.join(dest, entry.name);
16
+ if (entry.isDirectory()) {
17
+ copyMissing(from, to);
18
+ }
19
+ else if (!fs.existsSync(to)) {
20
+ fs.copyFileSync(from, to);
21
+ }
22
+ }
23
+ }
24
+ function ensureGitignore(root, lines) {
25
+ const file = path.join(root, '.gitignore');
26
+ const current = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '';
27
+ const existing = new Set(current.split(/\r?\n/).map((line) => line.trim()));
28
+ const missing = lines.filter((line) => !existing.has(line));
29
+ if (missing.length === 0)
30
+ return;
31
+ const prefix = current && !current.endsWith('\n') ? '\n' : '';
32
+ fs.writeFileSync(file, current + prefix + missing.join('\n') + '\n');
33
+ }
34
+ function ensureScripts(root, scripts) {
35
+ const file = path.join(root, 'package.json');
36
+ const pkg = JSON.parse(fs.readFileSync(file, 'utf8'));
37
+ const current = pkg.scripts ?? {};
38
+ let changed = false;
39
+ for (const [name, command] of Object.entries(scripts)) {
40
+ if (current[name] === undefined) {
41
+ current[name] = command;
42
+ changed = true;
43
+ }
44
+ }
45
+ if (!changed)
46
+ return;
47
+ pkg.scripts = current;
48
+ fs.writeFileSync(file, JSON.stringify(pkg, null, 2) + '\n');
49
+ }
50
+ function ensureTsconfigInclude(root, entry) {
51
+ const file = path.join(root, 'tsconfig.json');
52
+ if (!fs.existsSync(file))
53
+ return;
54
+ const current = fs.readFileSync(file, 'utf8');
55
+ if (current.includes(entry))
56
+ return;
57
+ const match = /"include"\s*:\s*\[[\s\S]*?\]/.exec(current);
58
+ if (!match)
59
+ return;
60
+ const close = match.index + match[0].lastIndexOf(']');
61
+ const head = current.slice(0, close).replace(/[ \t]*$/, '');
62
+ const needsComma = !/\[$/.test(head) && !/,$/.test(head);
63
+ const next = head +
64
+ (needsComma ? ',' : '') +
65
+ '\n ' +
66
+ JSON.stringify(entry) +
67
+ '\n ' +
68
+ current.slice(close);
69
+ fs.writeFileSync(file, next);
70
+ }
71
+ export async function runNativeInit(opts) {
72
+ if (!fs.existsSync(TEMPLATE_NATIVE)) {
73
+ log.error(`native 模板缺失: ${TEMPLATE_NATIVE}`);
74
+ return 2;
75
+ }
76
+ const config = opts.config ?? (await resolveConfig(opts));
77
+ const root = path.resolve(opts.dir);
78
+ const paths = resolveNativePaths(root, config);
79
+ const nativeDir = path.relative(root, paths.dir).split(path.sep).join('/') || '.';
80
+ const hadApi = fs.existsSync(paths.apiFile);
81
+ copyMissing(TEMPLATE_NATIVE, paths.dir);
82
+ ensureGitignore(root, [`${nativeDir}/build`, `${nativeDir}/produce`, 'scripts/build.env']);
83
+ ensureTsconfigInclude(root, `${nativeDir}/api.d.ts`);
84
+ ensureScripts(root, {
85
+ native: 'catchword native api && catchword native types && catchword native build -t x7',
86
+ 'native:api': 'catchword native api',
87
+ 'native:types': 'catchword native types',
88
+ 'native:check': 'catchword native api --check && catchword native types --check'
89
+ });
90
+ if (!hadApi) {
91
+ const apiCode = await runNativeApi({ dir: root, config, silent: opts.silent });
92
+ if (apiCode !== 0)
93
+ return apiCode;
94
+ const typesCode = await runNativeTypes({ dir: root, config, silent: opts.silent });
95
+ if (typesCode !== 0)
96
+ return typesCode;
97
+ }
98
+ log.success(`已加入 native 骨架: ${paths.dir}`);
99
+ return 0;
100
+ }
101
+ async function resolveConfig(opts) {
102
+ if (opts.config)
103
+ return opts.config;
104
+ return (await loadConfig(opts.dir, { mode: 'production' })).config;
105
+ }
106
+ function report(api) {
107
+ const total = api.modules.reduce((n, m) => n + m.methods.length, 0);
108
+ log.info(`原生模块 ${api.modules.length} 个,方法 ${total} 个`);
109
+ for (const module of api.modules) {
110
+ const async = module.methods.filter((m) => m.async).length;
111
+ const raw = module.methods.filter((m) => m.style === 'raw').length;
112
+ const parts = [`同步 ${module.methods.length - async}`, `async ${async}`];
113
+ if (raw)
114
+ parts.push(`裸CFunction ${raw}`);
115
+ log.info(` ${module.name.padEnd(11)} ${String(module.methods.length).padStart(3)} 方法 ${parts.join(' / ')} ${module.file}`);
116
+ }
117
+ const extracted = new Set(api.modules.map((m) => m.name));
118
+ const registered = new Set(api.registry.map((r) => r.name));
119
+ for (const name of registered) {
120
+ if (!extracted.has(name))
121
+ log.warn(`注册表中有 ${name},但没抽到它的 CreateClass`);
122
+ }
123
+ for (const name of extracted) {
124
+ if (registered.size > 0 && !registered.has(name)) {
125
+ log.warn(`${name} 抽到了,但没在 kModules 注册(运行时取不到)`);
126
+ }
127
+ }
128
+ }
129
+ export async function runNativeApi(opts) {
130
+ const config = await resolveConfig(opts);
131
+ const root = path.resolve(opts.dir);
132
+ const paths = resolveNativePaths(root, config);
133
+ if (!fs.existsSync(paths.sourceDir)) {
134
+ log.error(`找不到原生源码目录: ${paths.sourceDir}`);
135
+ log.error('请在 catchword.config.ts 里配置 native.dir(默认 jsapi)');
136
+ return 2;
137
+ }
138
+ const next = extractApi({
139
+ sourceDir: paths.sourceDir,
140
+ registryFile: paths.registryFile,
141
+ root
142
+ });
143
+ if (opts.check) {
144
+ if (!fs.existsSync(paths.apiFile)) {
145
+ log.error(`缺少 ${paths.apiFile},请先运行 catchword native api`);
146
+ return 3;
147
+ }
148
+ const diffs = diffApi(readApiFile(paths.apiFile), next);
149
+ if (diffs.length === 0) {
150
+ log.success('原生契约与 api.json 一致');
151
+ return 0;
152
+ }
153
+ log.error(`原生契约与 api.json 不一致(${diffs.length} 处):`);
154
+ for (const line of diffs)
155
+ log.error(` ${line}`);
156
+ log.error('确认无误后重新运行 catchword native api 更新');
157
+ return 4;
158
+ }
159
+ writeJsonFile(paths.apiFile, next);
160
+ if (!opts.silent) {
161
+ report(next);
162
+ log.success(`已写入 ${paths.apiFile}`);
163
+ }
164
+ return 0;
165
+ }
166
+ export async function runNativeTypes(opts) {
167
+ const config = await resolveConfig(opts);
168
+ const root = path.resolve(opts.dir);
169
+ const paths = resolveNativePaths(root, config);
170
+ let api;
171
+ if (fs.existsSync(paths.apiFile)) {
172
+ api = readApiFile(paths.apiFile);
173
+ }
174
+ else if (fs.existsSync(paths.sourceDir)) {
175
+ log.warn('没有 api.json,直接从源码抽取(建议先运行 catchword native api)');
176
+ api = extractApi({
177
+ sourceDir: paths.sourceDir,
178
+ registryFile: paths.registryFile,
179
+ root
180
+ });
181
+ }
182
+ else {
183
+ log.error(`找不到原生源码目录: ${paths.sourceDir}`);
184
+ return 2;
185
+ }
186
+ const overrides = readOverrides(paths.overridesFile);
187
+ const content = generateTypes(api, overrides);
188
+ if (opts.check) {
189
+ if (!fs.existsSync(paths.typesFile)) {
190
+ log.error(`缺少 ${paths.typesFile},请先运行 catchword native types`);
191
+ return 3;
192
+ }
193
+ const current = fs.readFileSync(paths.typesFile, 'utf8');
194
+ if (current === content) {
195
+ log.success('原生类型声明与 api.json 一致');
196
+ return 0;
197
+ }
198
+ log.error(`类型声明与 api.json 不一致: ${paths.typesFile}`);
199
+ log.error('确认无误后重新运行 catchword native types 更新');
200
+ return 4;
201
+ }
202
+ writeTypesFile(paths.typesFile, content);
203
+ const total = api.modules.reduce((n, m) => n + m.methods.length, 0);
204
+ const overridden = Object.values(overrides.methods ?? {}).reduce((n, group) => n + Object.keys(group).length, 0);
205
+ log.success(`已写入 ${paths.typesFile}(${api.modules.length} 个模块 / ${total} 个方法` +
206
+ (overridden ? ` / ${overridden} 个手写签名` : '') +
207
+ ')');
208
+ return 0;
209
+ }
210
+ export async function runNativeBuildCommand(opts) {
211
+ const config = await resolveConfig(opts);
212
+ const root = path.resolve(opts.dir);
213
+ const target = opts.target ?? 'x7';
214
+ return runNativeBuild({
215
+ root,
216
+ config,
217
+ target,
218
+ skipVerify: opts.skipVerify,
219
+ strict: opts.strict
220
+ });
221
+ }
222
+ export async function runNativeVerifyCommand(opts) {
223
+ const config = await resolveConfig(opts);
224
+ const root = path.resolve(opts.dir);
225
+ return runNativeVerify({
226
+ root,
227
+ config,
228
+ target: opts.target ?? 'x7',
229
+ file: opts.file,
230
+ strict: opts.strict
231
+ });
232
+ }
@@ -0,0 +1,8 @@
1
+ import type { FalconUserConfig } from '../types.js';
2
+ export interface SimulatorOptions {
3
+ dir: string;
4
+ simpath?: string;
5
+ page?: string;
6
+ config?: FalconUserConfig;
7
+ }
8
+ export declare function runSimulator(opts: SimulatorOptions): Promise<number>;