@mica-js/sdk 0.5.0 → 0.6.1
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/README.md +24 -9
- package/dist/build/cli.d.ts +2 -1
- package/dist/build/cli.js +49 -10
- package/dist/build/pack.d.ts +34 -0
- package/dist/build/pack.js +126 -0
- package/dist/build/project.d.ts +3 -0
- package/dist/build/project.js +19 -4
- package/dist/build/unified-build.d.ts +24 -1
- package/dist/build/unified-build.js +119 -10
- package/dist/build/unified-package.d.ts +2 -0
- package/dist/build/unified-package.js +4 -0
- package/dist/build/unified-prepare.d.ts +5 -0
- package/dist/build/unified-prepare.js +65 -25
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -120,28 +120,35 @@ dist/
|
|
|
120
120
|
| 命令 | 用途 |
|
|
121
121
|
| -------------------------------- | --------------------------------------------------- |
|
|
122
122
|
| `pnpm exec mica build` | 构建组件产物,或构建配置了 `device` 的统一器件包 |
|
|
123
|
+
| `pnpm exec mica build --watch` | 持续构建统一器件包;整体替换 `outDir`,失败继续监听 |
|
|
124
|
+
| `pnpm exec mica pack` | 把已有统一器件包打成 ZIP,不重新构建 |
|
|
123
125
|
| `pnpm exec mica prepare` | 只生成类型环境,不构建产物 |
|
|
124
126
|
| `pnpm exec mica prepare --watch` | 监听类型输入,适合与编辑器或 `tsc --watch` 同时使用 |
|
|
125
127
|
| `pnpm exec mica --help` | 查看命令和参数;仅输入 `mica` 也会显示帮助 |
|
|
126
128
|
|
|
127
129
|
常用参数:
|
|
128
130
|
|
|
129
|
-
| 参数 | 含义
|
|
130
|
-
| -------------------- |
|
|
131
|
-
| `--root <dir>` | 从指定目录开始查找工程,默认当前目录
|
|
132
|
-
| `--config <file>` | 显式指定 `mica.config.ts`;相对路径基于查找起点
|
|
133
|
-
| `--source-dir <dir>` | 覆盖配置中的源码目录
|
|
134
|
-
| `--
|
|
131
|
+
| 参数 | 含义 |
|
|
132
|
+
| -------------------- | ----------------------------------------------------------------------------- |
|
|
133
|
+
| `--root <dir>` | 从指定目录开始查找工程,默认当前目录 |
|
|
134
|
+
| `--config <file>` | 显式指定 `mica.config.ts`;相对路径基于查找起点 |
|
|
135
|
+
| `--source-dir <dir>` | 覆盖配置中的源码目录 |
|
|
136
|
+
| `--out <file>` | ZIP 输出路径,仅用于 `pack`;默认为 `<root>/<publisher>-<name>-<version>.zip` |
|
|
137
|
+
| `--watch` | 持续构建统一器件包(`build`),或监听类型输入(`prepare`) |
|
|
135
138
|
|
|
136
139
|
```shell
|
|
137
140
|
pnpm exec mica build --root components
|
|
138
141
|
pnpm exec mica build --config mica.config.ts
|
|
142
|
+
pnpm exec mica build --watch
|
|
143
|
+
pnpm exec mica pack --out ./led.zip
|
|
139
144
|
pnpm exec mica prepare --source-dir content
|
|
140
145
|
```
|
|
141
146
|
|
|
142
147
|
默认从当前目录向上寻找最近的 `mica.config.ts`,其所在目录作为工程根目录。因此,在工程子目录中也能运行命令。`--root` 是查找起点,不一定是最终工程根目录。显式配置文件也必须命名为 `mica.config.ts`。
|
|
143
148
|
|
|
144
|
-
`prepare --watch` 只更新类型环境,不会持续构建 JS
|
|
149
|
+
`prepare --watch` 只更新类型环境,不会持续构建 JS。持续构建统一器件包使用 `mica build --watch`;当前 CLI 不提供开发服务器。
|
|
150
|
+
|
|
151
|
+
目录直连的场景不需要 ZIP:侧载工具直接消费 `outDir`(例如 `zerobit device sideload ./dist`),配合 `mica build --watch` 就能随源码更新。手动导入或上传需要 ZIP 时再运行 `mica pack`,由 mica 按 `release.json` 生成权威归档,避免自行打包把 `release.json` 算进发布身份或产生不一致字节。
|
|
145
152
|
|
|
146
153
|
配置了 `device` 的工程按[统一器件包](#统一器件包device-模式)构建;其余工程仍按下面的组件流程构建,两种模式的命令和参数完全相同。
|
|
147
154
|
|
|
@@ -375,6 +382,8 @@ const result = await buildDevice({
|
|
|
375
382
|
|
|
376
383
|
`mica build` 先执行与 `prepare` 相同的准备流程,准备失败则构建停止,不写出任何产物。`mica prepare --watch` 监听清单、DDL 与 `mica.config.ts`,串行重新生成;失败时报告错误并继续监听,上一次成功的类型文件保持不动。输入内容不变时不重写输出。
|
|
377
384
|
|
|
385
|
+
`mica build --watch` 先完整构建一次,随后监听 `sourceDir`(递归)、清单、DDL 与 `mica.config.ts`;变化经防抖后串行重建。配置或 `sourceDir` 改动会重新加载配置并重定向监听。每次成功构建输出一行 `mica build: complete <id>@<version> <integrity>`;失败只写 stderr,保留上一份完整产物并继续监听,初始构建失败也不退出。按 Ctrl+C 时等待当前构建结束后退出。
|
|
386
|
+
|
|
378
387
|
准备阶段额外校验 DDL 与清单的一致性,全部通过才生成类型:
|
|
379
388
|
|
|
380
389
|
- 引脚集合必须相等。
|
|
@@ -384,7 +393,7 @@ const result = await buildDevice({
|
|
|
384
393
|
|
|
385
394
|
### 产物与发布清单
|
|
386
395
|
|
|
387
|
-
`mica build` 清空 `outDir` 后写入包内文件,并生成 `release.json
|
|
396
|
+
`mica build` 清空 `outDir` 后写入包内文件,并生成 `release.json`。`mica build --watch` 先在临时目录完成构建,再整体替换 `outDir`,因此监听 `outDir` 的消费者不会读到半成品;任何失败都保留上一份完整产物:
|
|
388
397
|
|
|
389
398
|
```text
|
|
390
399
|
dist/
|
|
@@ -401,6 +410,12 @@ dist/
|
|
|
401
410
|
|
|
402
411
|
`outDir` 在删除前校验:它不能是文件系统根目录、主目录或其上级目录,也不能等于或包含工程根、`sourceDir`、清单文件、DDL 源文件或 `.mica`。`buildDevice()` 在准备前和写出前各校验一次,因此任何校验失败都不会删除已有内容。
|
|
403
412
|
|
|
413
|
+
### 打包已有产物
|
|
414
|
+
|
|
415
|
+
`mica pack` 不构建,只把 `outDir` 里已有的统一器件包打成 ZIP。归档内容严格等于 `release.json` 的 `files` 表加上根目录的 `release.json`:`release.json` 描述发布身份,本身不计入该身份。条目按包根相对路径排序并以 store-only(`level: 0`)写入,时间戳固定,因此同一份产物重复打包字节完全一致。
|
|
416
|
+
|
|
417
|
+
任一文件与 `release.json` 记录的大小或 `sha256` 不符、文件缺失,或 `outDir` 没有 `release.json` 时,命令直接失败且不写出 ZIP。默认输出到工程根目录的 `<publisher>-<name>-<version>.zip`,可用 `--out` 改路径;成功时输出一行 `mica pack: <绝对路径>`。
|
|
418
|
+
|
|
404
419
|
## 统一器件无头运行
|
|
405
420
|
|
|
406
421
|
`@mica-js/sdk/fused` 的 `createHeadlessRuntime({ document, registry, loadModule, step })` 使用正式电气求解器运行 `.sch v2`。`registry` 提供锁定包的文件表与字节;准备阶段校验身份、文件摘要、清单、拓扑及模型 IR 后,才创建模型。`loadModule` 接收已校验的包读取接口与模型入口路径,返回该模块的导出。
|
|
@@ -429,7 +444,7 @@ dist/
|
|
|
429
444
|
|
|
430
445
|
**TypeScript 无法导入 `./model.ddl`。** 先运行 `mica prepare`,确认生成了 `model.d.ddl.ts`,并启用 `allowArbitraryExtensions` 与 `moduleResolution: "Bundler"`。
|
|
431
446
|
|
|
432
|
-
**修改源码后产物没有更新。** 重新运行 `mica build
|
|
447
|
+
**修改源码后产物没有更新。** 重新运行 `mica build`,或使用 `mica build --watch` 自动重建;`prepare --watch` 只处理类型声明。
|
|
433
448
|
|
|
434
449
|
## API 入口
|
|
435
450
|
|
package/dist/build/cli.d.ts
CHANGED
|
@@ -9,12 +9,13 @@ export interface CliStreams {
|
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
11
|
export interface CliArguments {
|
|
12
|
-
readonly command: 'prepare' | 'build';
|
|
12
|
+
readonly command: 'prepare' | 'build' | 'pack';
|
|
13
13
|
readonly help: boolean;
|
|
14
14
|
readonly watch: boolean;
|
|
15
15
|
readonly root: string;
|
|
16
16
|
readonly config?: string;
|
|
17
17
|
readonly sourceDir?: string;
|
|
18
|
+
readonly out?: string;
|
|
18
19
|
}
|
|
19
20
|
/** Parses SDK commands without reading or executing project configuration. */
|
|
20
21
|
export declare function parseCliArguments(argv: readonly string[]): CliArguments;
|
package/dist/build/cli.js
CHANGED
|
@@ -9,6 +9,7 @@ const USAGE = `Usage: mica <command> [options]
|
|
|
9
9
|
|
|
10
10
|
Commands:
|
|
11
11
|
build build one unified device package or standalone component artifacts
|
|
12
|
+
pack archive the existing unified device build into a ZIP, without rebuilding
|
|
12
13
|
prepare generate the type environment of the configured project
|
|
13
14
|
|
|
14
15
|
A mica.config.ts file identifies the project; it is discovered upwards from --root.
|
|
@@ -18,7 +19,8 @@ Options:
|
|
|
18
19
|
--config <file> explicit mica.config.ts, relative to --root
|
|
19
20
|
--root <dir> discovery directory (default: current directory)
|
|
20
21
|
--source-dir <dir> override the configured source directory
|
|
21
|
-
--
|
|
22
|
+
--out <file> ZIP output path (pack; default: <root>/<publisher>-<name>-<version>.zip)
|
|
23
|
+
--watch rebuild device projects on change, or watch prepare inputs (prepare)
|
|
22
24
|
-h, --help show this help`;
|
|
23
25
|
/** Parses SDK commands without reading or executing project configuration. */
|
|
24
26
|
export function parseCliArguments(argv) {
|
|
@@ -28,11 +30,12 @@ export function parseCliArguments(argv) {
|
|
|
28
30
|
let root = process.cwd();
|
|
29
31
|
let sourceDir;
|
|
30
32
|
let config;
|
|
33
|
+
let out;
|
|
31
34
|
let command = 'prepare';
|
|
32
35
|
let index = 0;
|
|
33
36
|
const first = argv[0];
|
|
34
37
|
if (first !== undefined && !first.startsWith('-')) {
|
|
35
|
-
if (first !== 'prepare' && first !== 'build')
|
|
38
|
+
if (first !== 'prepare' && first !== 'build' && first !== 'pack')
|
|
36
39
|
throw new Error(`Unknown command '${first}'.`);
|
|
37
40
|
command = first;
|
|
38
41
|
index = 1;
|
|
@@ -76,12 +79,15 @@ export function parseCliArguments(argv) {
|
|
|
76
79
|
case '--config':
|
|
77
80
|
config = flagValue();
|
|
78
81
|
break;
|
|
82
|
+
case '--out':
|
|
83
|
+
out = flagValue();
|
|
84
|
+
break;
|
|
79
85
|
default:
|
|
80
86
|
throw new Error(`Unknown argument '${argument}'.`);
|
|
81
87
|
}
|
|
82
88
|
}
|
|
83
|
-
if (watch && command
|
|
84
|
-
throw new Error('--watch is
|
|
89
|
+
if (watch && command === 'pack')
|
|
90
|
+
throw new Error('--watch is not supported by mica pack.');
|
|
85
91
|
return {
|
|
86
92
|
command,
|
|
87
93
|
help,
|
|
@@ -89,6 +95,7 @@ export function parseCliArguments(argv) {
|
|
|
89
95
|
root: resolve(root),
|
|
90
96
|
...(sourceDir === undefined ? {} : { sourceDir }),
|
|
91
97
|
...(config === undefined ? {} : { config }),
|
|
98
|
+
...(out === undefined ? {} : { out }),
|
|
92
99
|
};
|
|
93
100
|
}
|
|
94
101
|
/** Runs one SDK command and returns its exit code. */
|
|
@@ -121,11 +128,23 @@ export async function runCli(argv, streams = { out: process.stdout, err: process
|
|
|
121
128
|
try {
|
|
122
129
|
const project = await load();
|
|
123
130
|
if (options.command === 'build') {
|
|
124
|
-
const { buildProject } = await import('./project.js');
|
|
131
|
+
const { buildProject, resolveDeviceBuildOptions } = await import('./project.js');
|
|
132
|
+
if (options.watch) {
|
|
133
|
+
if (project.config.device === undefined)
|
|
134
|
+
throw new Error('--watch requires a unified device project (device in mica.config.ts).');
|
|
135
|
+
const loadBuild = async () => resolveDeviceBuildOptions(await load());
|
|
136
|
+
return await watchBuildLoop(project, loadBuild, streams);
|
|
137
|
+
}
|
|
125
138
|
await buildProject(project);
|
|
126
139
|
streams.out.write('mica build: complete\n');
|
|
127
140
|
return 0;
|
|
128
141
|
}
|
|
142
|
+
if (options.command === 'pack') {
|
|
143
|
+
const { packDeviceProject } = await import('./pack.js');
|
|
144
|
+
const packed = await packDeviceProject(project, options.out === undefined ? {} : { outFile: resolve(options.out) });
|
|
145
|
+
streams.out.write(`mica pack: ${packed.outFile}\n`);
|
|
146
|
+
return 0;
|
|
147
|
+
}
|
|
129
148
|
if (project.config.device !== undefined) {
|
|
130
149
|
const loadDevice = async () => {
|
|
131
150
|
const next = await load();
|
|
@@ -181,7 +200,7 @@ function watchLoop(options, streams) {
|
|
|
181
200
|
settle(1);
|
|
182
201
|
return;
|
|
183
202
|
}
|
|
184
|
-
awaitWatch(watcher, streams, settle, () => resolve(options.root, options.sourceDir));
|
|
203
|
+
awaitWatch(watcher, streams, settle, 'prepare', () => resolve(options.root, options.sourceDir));
|
|
185
204
|
});
|
|
186
205
|
}
|
|
187
206
|
async function prepareDeviceOnce(options, streams) {
|
|
@@ -215,14 +234,34 @@ function watchDeviceLoop(load, initial, streams) {
|
|
|
215
234
|
settle(1);
|
|
216
235
|
return;
|
|
217
236
|
}
|
|
218
|
-
awaitWatch(watcher, streams, settle, () => [initial.manifest, initial.model, initial.configFile]
|
|
237
|
+
awaitWatch(watcher, streams, settle, 'prepare', () => [initial.manifest, initial.model, initial.configFile]
|
|
219
238
|
.filter((file) => file !== undefined)
|
|
220
239
|
.map((file) => relative(process.cwd(), resolve(initial.root, file)))
|
|
221
240
|
.join(', '));
|
|
222
241
|
});
|
|
223
242
|
}
|
|
243
|
+
async function watchBuildLoop(project, load, streams) {
|
|
244
|
+
const { watchDeviceBuild } = await import('./unified-build.js');
|
|
245
|
+
const sourceDir = resolve(project.root, project.config.sourceDir ?? 'src');
|
|
246
|
+
return new Promise((settle) => {
|
|
247
|
+
let watcher;
|
|
248
|
+
try {
|
|
249
|
+
watcher = watchDeviceBuild({
|
|
250
|
+
load,
|
|
251
|
+
onBuild: (summary) => streams.out.write(`mica build: complete ${summary}\n`),
|
|
252
|
+
onError: (error) => streams.err.write(`mica build: ${messageOf(error)}\n`),
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
streams.err.write(`mica build: ${messageOf(error)}\n`);
|
|
257
|
+
settle(1);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
awaitWatch(watcher, streams, settle, 'build', () => relative(process.cwd(), sourceDir) || '.');
|
|
261
|
+
});
|
|
262
|
+
}
|
|
224
263
|
/** Reports readiness and stops the watcher on the first termination signal. */
|
|
225
|
-
function awaitWatch(watcher, streams, settle, watching) {
|
|
264
|
+
function awaitWatch(watcher, streams, settle, command, watching) {
|
|
226
265
|
let closing = false;
|
|
227
266
|
const stop = () => {
|
|
228
267
|
// A second signal must not wait for a regeneration that may already be stuck.
|
|
@@ -232,7 +271,7 @@ function awaitWatch(watcher, streams, settle, watching) {
|
|
|
232
271
|
process.off('SIGINT', stop);
|
|
233
272
|
process.off('SIGTERM', stop);
|
|
234
273
|
void watcher.dispose().then(() => settle(0), (error) => {
|
|
235
|
-
streams.err.write(`mica
|
|
274
|
+
streams.err.write(`mica ${command}: ${messageOf(error)}\n`);
|
|
236
275
|
settle(1);
|
|
237
276
|
});
|
|
238
277
|
};
|
|
@@ -241,7 +280,7 @@ function awaitWatch(watcher, streams, settle, watching) {
|
|
|
241
280
|
void watcher.ready.then(() => {
|
|
242
281
|
if (closing)
|
|
243
282
|
return;
|
|
244
|
-
streams.out.write(`mica
|
|
283
|
+
streams.out.write(`mica ${command}: watching ${watching()} for changes\n`);
|
|
245
284
|
});
|
|
246
285
|
}
|
|
247
286
|
function report(files, streams) {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { loadProjectConfig } from './config.js';
|
|
2
|
+
import type { ReleaseManifest } from './unified-package.js';
|
|
3
|
+
type Project = Awaited<ReturnType<typeof loadProjectConfig>>;
|
|
4
|
+
export interface PackDeviceOptions {
|
|
5
|
+
/** Project root; the default archive is written beside it. */
|
|
6
|
+
readonly root: string;
|
|
7
|
+
/** Existing package directory that holds `release.json`. */
|
|
8
|
+
readonly outDir: string;
|
|
9
|
+
/** Archive path; relative paths resolve against the current directory. */
|
|
10
|
+
readonly outFile?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface PackDeviceResult {
|
|
13
|
+
/** Absolute path of the written archive. */
|
|
14
|
+
readonly outFile: string;
|
|
15
|
+
readonly release: ReleaseManifest;
|
|
16
|
+
/** Package paths written to the archive, including `release.json`. */
|
|
17
|
+
readonly files: readonly string[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Packs an existing unified device build into a deterministic ZIP without rebuilding.
|
|
21
|
+
*
|
|
22
|
+
* The archive contains exactly the files declared by `release.json` plus `release.json` itself,
|
|
23
|
+
* sorted by package path and stored without compression. `release.json` describes the published
|
|
24
|
+
* files and is not part of that set. Every declared file must still match its recorded size and
|
|
25
|
+
* SHA-256, so a package that no longer matches its release record is rejected instead of archived.
|
|
26
|
+
*
|
|
27
|
+
* @throws When `outDir` has no `release.json` or a declared file is missing or changed.
|
|
28
|
+
*/
|
|
29
|
+
export declare function packDevice(options: PackDeviceOptions): Promise<PackDeviceResult>;
|
|
30
|
+
/** Packs the unified device build that the project config declares. */
|
|
31
|
+
export declare function packDeviceProject(project: Project, options?: {
|
|
32
|
+
readonly outFile?: string;
|
|
33
|
+
}): Promise<PackDeviceResult>;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join, resolve } from 'node:path';
|
|
4
|
+
import { zipSync } from 'fflate';
|
|
5
|
+
import { isContained, packagePath } from './unified-paths.js';
|
|
6
|
+
/**
|
|
7
|
+
* Timestamp stored for every ZIP entry.
|
|
8
|
+
*
|
|
9
|
+
* Local components keep the bytes independent of the clock and the timezone, so packing the same
|
|
10
|
+
* build twice produces identical archives.
|
|
11
|
+
*/
|
|
12
|
+
const ZIP_MTIME = new Date(1980, 0, 1);
|
|
13
|
+
/** Name of the release record that ships at the package root and never describes itself. */
|
|
14
|
+
const RELEASE_FILE = 'release.json';
|
|
15
|
+
/**
|
|
16
|
+
* Packs an existing unified device build into a deterministic ZIP without rebuilding.
|
|
17
|
+
*
|
|
18
|
+
* The archive contains exactly the files declared by `release.json` plus `release.json` itself,
|
|
19
|
+
* sorted by package path and stored without compression. `release.json` describes the published
|
|
20
|
+
* files and is not part of that set. Every declared file must still match its recorded size and
|
|
21
|
+
* SHA-256, so a package that no longer matches its release record is rejected instead of archived.
|
|
22
|
+
*
|
|
23
|
+
* @throws When `outDir` has no `release.json` or a declared file is missing or changed.
|
|
24
|
+
*/
|
|
25
|
+
export async function packDevice(options) {
|
|
26
|
+
const outDir = resolve(options.outDir);
|
|
27
|
+
const releaseFile = join(outDir, RELEASE_FILE);
|
|
28
|
+
let releaseBytes;
|
|
29
|
+
try {
|
|
30
|
+
releaseBytes = await readFile(releaseFile);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error.code !== 'ENOENT')
|
|
34
|
+
throw error;
|
|
35
|
+
throw new Error(`Cannot find ${RELEASE_FILE} in ${outDir}; mica pack archives an existing unified device build, so run mica build first.`, { cause: error });
|
|
36
|
+
}
|
|
37
|
+
const release = parseRelease(releaseFile, releaseBytes.toString('utf8'));
|
|
38
|
+
const paths = [...Object.keys(release.files), RELEASE_FILE].sort(compare);
|
|
39
|
+
const entries = {};
|
|
40
|
+
for (const path of paths) {
|
|
41
|
+
entries[path] =
|
|
42
|
+
path === RELEASE_FILE
|
|
43
|
+
? new Uint8Array(releaseBytes)
|
|
44
|
+
: await readPackageFile(outDir, path, release.files[path]);
|
|
45
|
+
}
|
|
46
|
+
const archive = zipSync(entries, { level: 0, mtime: ZIP_MTIME });
|
|
47
|
+
const outFile = resolve(options.outFile ?? join(resolve(options.root), archiveName(release)));
|
|
48
|
+
await mkdir(dirname(outFile), { recursive: true });
|
|
49
|
+
const temporary = `${outFile}.${process.pid}.tmp`;
|
|
50
|
+
try {
|
|
51
|
+
await writeFile(temporary, archive);
|
|
52
|
+
await rename(temporary, outFile);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
await rm(temporary, { force: true }).catch(() => undefined);
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
return { outFile, release, files: paths };
|
|
59
|
+
}
|
|
60
|
+
/** Packs the unified device build that the project config declares. */
|
|
61
|
+
export function packDeviceProject(project, options = {}) {
|
|
62
|
+
return packDevice({
|
|
63
|
+
root: project.root,
|
|
64
|
+
outDir: resolve(project.root, project.config.outDir ?? 'dist'),
|
|
65
|
+
...(options.outFile === undefined ? {} : { outFile: options.outFile }),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** Default archive name: `<publisher>-<name>-<version>.zip`, derived from the release id. */
|
|
69
|
+
function archiveName(release) {
|
|
70
|
+
return `${release.id.replaceAll(':', '-')}-${release.version}.zip`;
|
|
71
|
+
}
|
|
72
|
+
function parseRelease(releaseFile, text) {
|
|
73
|
+
const invalid = (reason) => {
|
|
74
|
+
throw new Error(`Invalid ${releaseFile}: ${reason}`);
|
|
75
|
+
};
|
|
76
|
+
let value;
|
|
77
|
+
try {
|
|
78
|
+
value = JSON.parse(text);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
invalid(error instanceof Error ? error.message : String(error));
|
|
82
|
+
}
|
|
83
|
+
const root = value;
|
|
84
|
+
if (typeof root !== 'object' || root === null || Array.isArray(root))
|
|
85
|
+
invalid('expected an object');
|
|
86
|
+
const { id, version, files } = root;
|
|
87
|
+
if (typeof id !== 'string' || id.length === 0)
|
|
88
|
+
invalid('id must be a nonempty string');
|
|
89
|
+
if (typeof version !== 'string' || version.length === 0)
|
|
90
|
+
invalid('version must be a nonempty string');
|
|
91
|
+
if (typeof files !== 'object' || files === null || Array.isArray(files))
|
|
92
|
+
invalid('files must be an object');
|
|
93
|
+
const records = {};
|
|
94
|
+
for (const [path, entry] of Object.entries(files)) {
|
|
95
|
+
packagePath(path, `${releaseFile} file`);
|
|
96
|
+
if (typeof entry !== 'object' || entry === null)
|
|
97
|
+
invalid(`files['${path}'] must be an object`);
|
|
98
|
+
const { size, integrity } = entry;
|
|
99
|
+
if (typeof size !== 'number' || typeof integrity !== 'string')
|
|
100
|
+
invalid(`files['${path}'] must record size and integrity`);
|
|
101
|
+
records[path] = { size, integrity };
|
|
102
|
+
}
|
|
103
|
+
return { id, version, files: records };
|
|
104
|
+
}
|
|
105
|
+
/** Reads one declared package file and rejects content that `release.json` no longer describes. */
|
|
106
|
+
async function readPackageFile(outDir, path, record) {
|
|
107
|
+
const file = resolve(outDir, ...path.split('/'));
|
|
108
|
+
if (!isContained(outDir, file))
|
|
109
|
+
throw new Error(`Invalid release.json file path '${path}': it escapes ${outDir}`);
|
|
110
|
+
let data;
|
|
111
|
+
try {
|
|
112
|
+
data = await readFile(file);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error.code !== 'ENOENT')
|
|
116
|
+
throw error;
|
|
117
|
+
throw new Error(`${outDir} is missing '${path}'; run mica build again.`, { cause: error });
|
|
118
|
+
}
|
|
119
|
+
const integrity = `sha256:${createHash('sha256').update(data).digest('hex')}`;
|
|
120
|
+
if (data.byteLength !== record.size || integrity !== record.integrity)
|
|
121
|
+
throw new Error(`${outDir} at '${path}' no longer matches release.json; run mica build again.`);
|
|
122
|
+
return new Uint8Array(data);
|
|
123
|
+
}
|
|
124
|
+
function compare(left, right) {
|
|
125
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
126
|
+
}
|
package/dist/build/project.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { loadProjectConfig } from './config.js';
|
|
2
|
+
import { type BuildDeviceOptions } from './unified-build.js';
|
|
2
3
|
type Project = Awaited<ReturnType<typeof loadProjectConfig>>;
|
|
4
|
+
/** Resolves the unified device build of a project, including its validated output directory. */
|
|
5
|
+
export declare function resolveDeviceBuildOptions(project: Project): BuildDeviceOptions;
|
|
3
6
|
/**
|
|
4
7
|
* Builds the project.
|
|
5
8
|
*
|
package/dist/build/project.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isAbsolute, relative, resolve } from 'node:path';
|
|
2
2
|
import { build } from 'vite';
|
|
3
|
-
import {
|
|
3
|
+
import { buildDevice } from './unified-build.js';
|
|
4
4
|
import { micaSdk } from './vite.js';
|
|
5
5
|
function contains(parent, child) {
|
|
6
6
|
const path = relative(parent, child);
|
|
@@ -31,6 +31,22 @@ function viteConfig(project, directories) {
|
|
|
31
31
|
build: { outDir: directories.output },
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
|
+
/** Resolves the unified device build of a project, including its validated output directory. */
|
|
35
|
+
export function resolveDeviceBuildOptions(project) {
|
|
36
|
+
const directories = resolveDirectories(project);
|
|
37
|
+
const device = project.config.device;
|
|
38
|
+
if (device === undefined)
|
|
39
|
+
throw new Error('The project does not configure a unified device build.');
|
|
40
|
+
return {
|
|
41
|
+
root: project.root,
|
|
42
|
+
manifest: device.manifest,
|
|
43
|
+
outDir: directories.output,
|
|
44
|
+
sourceDir: project.config.sourceDir ?? 'src',
|
|
45
|
+
...(device.model === undefined ? {} : { model: device.model }),
|
|
46
|
+
configFile: project.configFile,
|
|
47
|
+
...(project.config.alias === undefined ? {} : { alias: project.config.alias }),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
34
50
|
/**
|
|
35
51
|
* Builds the project.
|
|
36
52
|
*
|
|
@@ -38,10 +54,9 @@ function viteConfig(project, directories) {
|
|
|
38
54
|
* standalone Device and Widget artifacts.
|
|
39
55
|
*/
|
|
40
56
|
export async function buildProject(project) {
|
|
41
|
-
const directories = resolveDirectories(project);
|
|
42
57
|
if (project.config.device !== undefined) {
|
|
43
|
-
await
|
|
58
|
+
await buildDevice(resolveDeviceBuildOptions(project));
|
|
44
59
|
return;
|
|
45
60
|
}
|
|
46
|
-
await build(viteConfig(project,
|
|
61
|
+
await build(viteConfig(project, resolveDirectories(project)));
|
|
47
62
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { loadProjectConfig } from './config.js';
|
|
2
|
-
import {
|
|
2
|
+
import type { PrepareWatcher } from './prepare.js';
|
|
3
|
+
import { type DevicePackageFile, type ReleaseManifest } from './unified-package.js';
|
|
3
4
|
type Project = Awaited<ReturnType<typeof loadProjectConfig>>;
|
|
4
5
|
export interface BuildDeviceOptions {
|
|
5
6
|
/** Project root that holds the manifest and the source directory. */
|
|
@@ -24,6 +25,8 @@ export interface BuildDeviceResult {
|
|
|
24
25
|
readonly outDir: string;
|
|
25
26
|
/** Absolute paths of the written package files. */
|
|
26
27
|
readonly files: readonly string[];
|
|
28
|
+
/** Release record that describes the published package files. */
|
|
29
|
+
readonly release: ReleaseManifest;
|
|
27
30
|
}
|
|
28
31
|
/**
|
|
29
32
|
* Prepares the type environment, builds every package file and writes the release.
|
|
@@ -35,4 +38,24 @@ export interface BuildDeviceResult {
|
|
|
35
38
|
export declare function buildDevice(options: BuildDeviceOptions): Promise<BuildDeviceResult>;
|
|
36
39
|
/** Builds the unified device package that the project config declares. */
|
|
37
40
|
export declare function buildDeviceProject(project: Project, outDir: string): Promise<BuildDeviceResult>;
|
|
41
|
+
export interface WatchDeviceBuildOptions {
|
|
42
|
+
/** Resolves the current build inputs; called again after every change. */
|
|
43
|
+
readonly load: () => Promise<BuildDeviceOptions>;
|
|
44
|
+
/** Coalescing window between a file event and the rebuild; defaults to 50 ms. */
|
|
45
|
+
readonly debounceMs?: number;
|
|
46
|
+
/** Receives `id@version integrity` after every successful build. */
|
|
47
|
+
readonly onBuild?: (summary: string) => void;
|
|
48
|
+
/** Receives build failures; watching continues afterwards. */
|
|
49
|
+
readonly onError?: (error: unknown) => void;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Builds the device once, then rebuilds after source, manifest, DDL or project config changes.
|
|
53
|
+
*
|
|
54
|
+
* Every build is staged below `.mica` and published only after it completes, so `outDir` always
|
|
55
|
+
* holds one complete package. A failing build reports the original error and leaves the previous
|
|
56
|
+
* package untouched; watching continues. Inputs are re-resolved on every run, so a config edit can
|
|
57
|
+
* retarget the source directory and the output. Build outputs (`.mica` and `outDir`) never feed
|
|
58
|
+
* back into the loop.
|
|
59
|
+
*/
|
|
60
|
+
export declare function watchDeviceBuild(options: WatchDeviceBuildOptions): PrepareWatcher;
|
|
38
61
|
export type { DevicePackageFile };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { mkdir, rename, rm, stat } from 'node:fs/promises';
|
|
2
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { assertSafeOutput, collectDevicePackage, createRelease, releaseIntegrity, writeDevicePackage, } from './unified-package.js';
|
|
4
|
+
import { DEVICE_TYPE_DIRECTORY, prepareDevice, watchPreparation } from './unified-prepare.js';
|
|
4
5
|
/**
|
|
5
6
|
* Prepares the type environment, builds every package file and writes the release.
|
|
6
7
|
*
|
|
@@ -11,13 +12,7 @@ import { prepareDevice } from './unified-prepare.js';
|
|
|
11
12
|
export async function buildDevice(options) {
|
|
12
13
|
const sourceDir = options.sourceDir ?? 'src';
|
|
13
14
|
const root = resolve(options.root);
|
|
14
|
-
const guarded =
|
|
15
|
-
root,
|
|
16
|
-
resolve(root, sourceDir),
|
|
17
|
-
resolve(root, options.manifest),
|
|
18
|
-
...(options.model === undefined ? [] : [resolve(root, options.model)]),
|
|
19
|
-
join(root, '.mica'),
|
|
20
|
-
];
|
|
15
|
+
const guarded = guardsOf(root, sourceDir, options);
|
|
21
16
|
// Validate before preparation writes anything, and again before the output reset.
|
|
22
17
|
const outDir = assertSafeOutput(options.outDir, guarded);
|
|
23
18
|
const prepared = await prepareDevice({
|
|
@@ -40,8 +35,19 @@ export async function buildDevice(options) {
|
|
|
40
35
|
return {
|
|
41
36
|
outDir,
|
|
42
37
|
files: files.map((file) => resolve(outDir, ...file.path.split('/'))),
|
|
38
|
+
release,
|
|
43
39
|
};
|
|
44
40
|
}
|
|
41
|
+
/** Absolute files and directories that an output reset must never touch. */
|
|
42
|
+
function guardsOf(root, sourceDir, options) {
|
|
43
|
+
return [
|
|
44
|
+
root,
|
|
45
|
+
resolve(root, sourceDir),
|
|
46
|
+
resolve(root, options.manifest),
|
|
47
|
+
...(options.model === undefined ? [] : [resolve(root, options.model)]),
|
|
48
|
+
join(root, DEVICE_TYPE_DIRECTORY),
|
|
49
|
+
];
|
|
50
|
+
}
|
|
45
51
|
/** Builds the unified device package that the project config declares. */
|
|
46
52
|
export async function buildDeviceProject(project, outDir) {
|
|
47
53
|
const device = project.config.device;
|
|
@@ -57,6 +63,109 @@ export async function buildDeviceProject(project, outDir) {
|
|
|
57
63
|
...(project.config.alias === undefined ? {} : { alias: project.config.alias }),
|
|
58
64
|
});
|
|
59
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Builds the device once, then rebuilds after source, manifest, DDL or project config changes.
|
|
68
|
+
*
|
|
69
|
+
* Every build is staged below `.mica` and published only after it completes, so `outDir` always
|
|
70
|
+
* holds one complete package. A failing build reports the original error and leaves the previous
|
|
71
|
+
* package untouched; watching continues. Inputs are re-resolved on every run, so a config edit can
|
|
72
|
+
* retarget the source directory and the output. Build outputs (`.mica` and `outDir`) never feed
|
|
73
|
+
* back into the loop.
|
|
74
|
+
*/
|
|
75
|
+
export function watchDeviceBuild(options) {
|
|
76
|
+
return watchPreparation({
|
|
77
|
+
load: options.load,
|
|
78
|
+
inputs: (build) => [
|
|
79
|
+
resolve(build.root, build.manifest),
|
|
80
|
+
...(build.model === undefined ? [] : [resolve(build.root, build.model)]),
|
|
81
|
+
...(build.configFile === undefined ? [] : [resolve(build.root, build.configFile)]),
|
|
82
|
+
],
|
|
83
|
+
directories: (build) => [resolve(build.root, build.sourceDir ?? 'src')],
|
|
84
|
+
ignored: (build) => [
|
|
85
|
+
resolve(build.outDir),
|
|
86
|
+
previousDirectory(build.outDir),
|
|
87
|
+
join(resolve(build.root), DEVICE_TYPE_DIRECTORY),
|
|
88
|
+
],
|
|
89
|
+
prepare: async (build) => [await buildDeviceStaged(build)],
|
|
90
|
+
...(options.debounceMs === undefined ? {} : { debounceMs: options.debounceMs }),
|
|
91
|
+
onPrepare: (lines) => {
|
|
92
|
+
const summary = lines[0];
|
|
93
|
+
if (summary !== undefined)
|
|
94
|
+
options.onBuild?.(summary);
|
|
95
|
+
},
|
|
96
|
+
...(options.onError === undefined ? {} : { onError: options.onError }),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** Builds into a staging directory and swaps it in, returning the `id@version integrity` summary. */
|
|
100
|
+
async function buildDeviceStaged(options) {
|
|
101
|
+
const root = resolve(options.root);
|
|
102
|
+
const sourceDir = options.sourceDir ?? 'src';
|
|
103
|
+
const guarded = guardsOf(root, sourceDir, options);
|
|
104
|
+
const outDir = assertSafeOutput(options.outDir, guarded);
|
|
105
|
+
const stagingRoot = join(root, DEVICE_TYPE_DIRECTORY, 'build-watch');
|
|
106
|
+
const staging = join(stagingRoot, 'package');
|
|
107
|
+
await rm(stagingRoot, { recursive: true, force: true });
|
|
108
|
+
try {
|
|
109
|
+
const result = await buildDevice({ ...options, outDir: staging });
|
|
110
|
+
await replaceDirectory(staging, outDir, guarded);
|
|
111
|
+
return `${result.release.id}@${result.release.version} ${releaseIntegrity(result.release)}`;
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
// A leftover staging directory is replaced by the next run, so cleanup never fails a build.
|
|
115
|
+
await rm(stagingRoot, { recursive: true, force: true }).catch(() => undefined);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Sibling directory that holds the previous package while a new one is swapped in. */
|
|
119
|
+
function previousDirectory(outDir) {
|
|
120
|
+
const target = resolve(outDir);
|
|
121
|
+
return join(dirname(target), `.${basename(target)}.mica-previous`);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Replaces `outDir` with `staging` as one unit: the previous directory is moved aside first, so a
|
|
125
|
+
* consumer never observes a partially written package, and is restored when the swap fails.
|
|
126
|
+
*/
|
|
127
|
+
async function replaceDirectory(staging, outDir, protect) {
|
|
128
|
+
const target = assertSafeOutput(outDir, protect);
|
|
129
|
+
await mkdir(dirname(target), { recursive: true });
|
|
130
|
+
const backup = previousDirectory(target);
|
|
131
|
+
await rm(backup, { recursive: true, force: true });
|
|
132
|
+
const existed = await exists(target);
|
|
133
|
+
let removable = true;
|
|
134
|
+
try {
|
|
135
|
+
if (existed)
|
|
136
|
+
await rename(target, backup);
|
|
137
|
+
try {
|
|
138
|
+
await rename(staging, target);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
if (existed) {
|
|
142
|
+
try {
|
|
143
|
+
await rename(backup, target);
|
|
144
|
+
}
|
|
145
|
+
catch (restoreError) {
|
|
146
|
+
removable = false;
|
|
147
|
+
throw new AggregateError([error, restoreError], `Cannot restore ${target}; the previous package is kept in ${backup}`, { cause: restoreError });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
if (removable)
|
|
155
|
+
await rm(backup, { recursive: true, force: true }).catch(() => undefined);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async function exists(path) {
|
|
159
|
+
try {
|
|
160
|
+
await stat(path);
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
if (error.code === 'ENOENT')
|
|
165
|
+
return false;
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
60
169
|
function resolveAliases(root, alias) {
|
|
61
170
|
const entries = Object.entries(alias);
|
|
62
171
|
if (entries.length === 0)
|
|
@@ -37,6 +37,8 @@ export declare function collectDevicePackage(options: CollectDeviceOptions): Pro
|
|
|
37
37
|
/** Builds the release file list with real byte sizes and SHA-256 digests. */
|
|
38
38
|
export declare function createRelease(manifest: UnifiedDeviceManifest, files: readonly DevicePackageFile[]): ReleaseManifest;
|
|
39
39
|
export declare function renderRelease(release: ReleaseManifest): string;
|
|
40
|
+
/** SHA-256 fingerprint of a rendered release file, identifying one published package build. */
|
|
41
|
+
export declare function releaseIntegrity(release: ReleaseManifest): string;
|
|
40
42
|
export interface WriteDeviceOptions {
|
|
41
43
|
/** Output directory that is reset before the package files are written. */
|
|
42
44
|
readonly outDir: string;
|
|
@@ -177,6 +177,10 @@ export function createRelease(manifest, files) {
|
|
|
177
177
|
export function renderRelease(release) {
|
|
178
178
|
return `${JSON.stringify(release, null, 2)}\n`;
|
|
179
179
|
}
|
|
180
|
+
/** SHA-256 fingerprint of a rendered release file, identifying one published package build. */
|
|
181
|
+
export function releaseIntegrity(release) {
|
|
182
|
+
return `sha256:${createHash('sha256').update(renderRelease(release)).digest('hex')}`;
|
|
183
|
+
}
|
|
180
184
|
/**
|
|
181
185
|
* Validates an output directory that is going to be deleted and recreated.
|
|
182
186
|
*
|
|
@@ -49,7 +49,12 @@ export declare function prepareDevice(options: PrepareDeviceOptions): Promise<Pr
|
|
|
49
49
|
export declare function watchDevice(options: WatchDeviceOptions): PrepareWatcher;
|
|
50
50
|
export interface WatchPreparationOptions<T> {
|
|
51
51
|
readonly load: () => Promise<T>;
|
|
52
|
+
/** Files whose changes trigger a run; each parent directory is watched non-recursively. */
|
|
52
53
|
readonly inputs: (loaded: T) => readonly string[];
|
|
54
|
+
/** Directories whose subtree triggers a run; watched recursively. */
|
|
55
|
+
readonly directories?: (loaded: T) => readonly string[];
|
|
56
|
+
/** Files or directories that never trigger a run, such as build outputs. */
|
|
57
|
+
readonly ignored?: (loaded: T) => readonly string[];
|
|
53
58
|
readonly prepare: (loaded: T) => Promise<readonly string[]>;
|
|
54
59
|
readonly debounceMs?: number;
|
|
55
60
|
readonly onPrepare?: (files: readonly string[]) => void;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { existsSync, watch } from 'node:fs';
|
|
1
|
+
import { existsSync, statSync, watch } from 'node:fs';
|
|
2
2
|
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
3
|
-
import { dirname, join, resolve
|
|
3
|
+
import { dirname, join, resolve } from 'node:path';
|
|
4
|
+
import { isContained } from './unified-paths.js';
|
|
4
5
|
import { loadDevicePackage } from './unified-manifest.js';
|
|
5
6
|
import { renderDeviceDeclaration, renderDeviceTypeScriptConfig } from './unified-types.js';
|
|
6
7
|
/** Directory below the project root that holds the generated type environment. */
|
|
@@ -61,7 +62,7 @@ export function watchPreparation(options) {
|
|
|
61
62
|
let queued = false;
|
|
62
63
|
let timer;
|
|
63
64
|
let current;
|
|
64
|
-
let targets = new Set();
|
|
65
|
+
let targets = { files: new Set(), directories: new Set(), ignored: [] };
|
|
65
66
|
const watchers = new Map();
|
|
66
67
|
const report = (error) => {
|
|
67
68
|
if (options.onError === undefined) {
|
|
@@ -76,34 +77,51 @@ export function watchPreparation(options) {
|
|
|
76
77
|
console.error('mica prepare --watch error handler failed:', reportingError);
|
|
77
78
|
}
|
|
78
79
|
};
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
const relevant = (changed) => {
|
|
81
|
+
if (targets.ignored.some((path) => isContained(path, changed)))
|
|
82
|
+
return false;
|
|
83
|
+
for (const file of targets.files)
|
|
84
|
+
if (isContained(changed, file) || isContained(file, changed))
|
|
85
|
+
return true;
|
|
86
|
+
for (const directory of targets.directories)
|
|
87
|
+
if (isContained(changed, directory) || isContained(directory, changed))
|
|
88
|
+
return true;
|
|
89
|
+
return false;
|
|
90
|
+
};
|
|
91
|
+
const syncWatchers = (loaded) => {
|
|
92
|
+
const files = new Set(options.inputs(loaded).map((file) => resolve(file)));
|
|
93
|
+
const directories = new Set((options.directories?.(loaded) ?? []).map((directory) => resolve(directory)));
|
|
94
|
+
const ignored = (options.ignored?.(loaded) ?? []).map((path) => resolve(path));
|
|
95
|
+
const roots = new Map();
|
|
96
|
+
const require = (path, recursive) => {
|
|
97
|
+
roots.set(path, recursive || roots.get(path) === true);
|
|
98
|
+
};
|
|
99
|
+
for (const file of files)
|
|
100
|
+
require(nearestExistingDirectory(dirname(file)), false);
|
|
101
|
+
for (const directory of directories)
|
|
102
|
+
require(nearestExistingDirectory(directory), true);
|
|
103
|
+
for (const [root, watched] of watchers) {
|
|
104
|
+
if (roots.get(root) === watched.recursive)
|
|
89
105
|
continue;
|
|
90
|
-
watcher.close();
|
|
91
|
-
watchers.delete(
|
|
106
|
+
watched.watcher.close();
|
|
107
|
+
watchers.delete(root);
|
|
92
108
|
}
|
|
93
|
-
targets =
|
|
94
|
-
for (const
|
|
95
|
-
if (watchers.has(
|
|
109
|
+
targets = { files, directories, ignored };
|
|
110
|
+
for (const [root, recursive] of roots) {
|
|
111
|
+
if (watchers.has(root))
|
|
96
112
|
continue;
|
|
97
113
|
let watcher;
|
|
98
114
|
try {
|
|
99
|
-
watcher = watch(
|
|
115
|
+
watcher = watch(root, { recursive }, (event, filename) => {
|
|
100
116
|
if (disposed)
|
|
101
117
|
return;
|
|
102
118
|
if (filename === null)
|
|
103
119
|
schedule();
|
|
104
120
|
else {
|
|
105
|
-
const changed = resolve(
|
|
106
|
-
if (
|
|
121
|
+
const changed = resolve(root, filename.toString());
|
|
122
|
+
if (isDirectoryChange(event, changed))
|
|
123
|
+
return;
|
|
124
|
+
if (relevant(changed))
|
|
107
125
|
schedule();
|
|
108
126
|
}
|
|
109
127
|
});
|
|
@@ -116,7 +134,7 @@ export function watchPreparation(options) {
|
|
|
116
134
|
if (!disposed)
|
|
117
135
|
report(error);
|
|
118
136
|
});
|
|
119
|
-
watchers.set(
|
|
137
|
+
watchers.set(root, { watcher, recursive });
|
|
120
138
|
}
|
|
121
139
|
};
|
|
122
140
|
const scan = async () => {
|
|
@@ -130,7 +148,7 @@ export function watchPreparation(options) {
|
|
|
130
148
|
return [];
|
|
131
149
|
}
|
|
132
150
|
try {
|
|
133
|
-
syncWatchers(
|
|
151
|
+
syncWatchers(loaded);
|
|
134
152
|
const files = await options.prepare(loaded);
|
|
135
153
|
options.onPrepare?.(files);
|
|
136
154
|
return files;
|
|
@@ -189,13 +207,35 @@ export function watchPreparation(options) {
|
|
|
189
207
|
clearTimeout(timer);
|
|
190
208
|
timer = undefined;
|
|
191
209
|
}
|
|
192
|
-
for (const
|
|
193
|
-
watcher.close();
|
|
210
|
+
for (const watched of watchers.values())
|
|
211
|
+
watched.watcher.close();
|
|
194
212
|
watchers.clear();
|
|
195
213
|
await current;
|
|
196
214
|
},
|
|
197
215
|
};
|
|
198
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* Windows can deliver a `change` event for a directory whose metadata was updated before watching
|
|
219
|
+
* started. A real edit inside the directory also events on the edited file, so a directory-level
|
|
220
|
+
* change alone never needs to trigger a run.
|
|
221
|
+
*/
|
|
222
|
+
function isDirectoryChange(event, path) {
|
|
223
|
+
if (event !== 'change')
|
|
224
|
+
return false;
|
|
225
|
+
try {
|
|
226
|
+
return statSync(path).isDirectory();
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/** Nearest existing ancestor directory, so a missing input can still be watched for creation. */
|
|
233
|
+
function nearestExistingDirectory(path) {
|
|
234
|
+
let directory = path;
|
|
235
|
+
while (!existsSync(directory) && dirname(directory) !== directory)
|
|
236
|
+
directory = dirname(directory);
|
|
237
|
+
return directory;
|
|
238
|
+
}
|
|
199
239
|
async function writeIfChanged(file, content) {
|
|
200
240
|
try {
|
|
201
241
|
if ((await readFile(file, 'utf8')) === content)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mica-js/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Device and widget author APIs with CLI-managed content builds",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"@mica-js/ddl": "0.3.1",
|
|
72
72
|
"@mica-js/device-model": "0.7.0",
|
|
73
73
|
"@mica-js/document": "0.3.0",
|
|
74
|
-
"@mica-js/simulator": "2.
|
|
74
|
+
"@mica-js/simulator": "2.7.0",
|
|
75
75
|
"@mica-js/widget": "0.5.0",
|
|
76
76
|
"@noble/hashes": "^2.4.0",
|
|
77
77
|
"ajv": "^8.17.1",
|