@adelin-io/cli 0.0.0-stage → 0.1.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 +76 -2
- package/authoring-release.json +174 -0
- package/package.json +30 -4
- package/runtime-integrity.json +9 -0
- package/scripts/adelin.mjs +7768 -0
- package/scripts/assemble-checkout.mjs +93 -0
- package/scripts/local-save.mjs +513 -0
- package/scripts/materialize.mjs +319 -0
- package/scripts/runtime.mjs +8950 -0
- package/scripts/typecheck.mjs +72 -0
- package/scripts/workspace-state.mjs +183 -0
- package/skill/README.md +77 -0
- package/skill/SKILL.md +100 -0
- package/skill/authoring/access.md +193 -0
- package/skill/authoring/analytics.md +20 -0
- package/skill/authoring/app-memory.md +128 -0
- package/skill/authoring/app-requirements.md +99 -0
- package/skill/authoring/app-structure.md +265 -0
- package/skill/authoring/assets.md +117 -0
- package/skill/authoring/auth.md +263 -0
- package/skill/authoring/content-binding.md +26 -0
- package/skill/authoring/i18n.md +184 -0
- package/skill/authoring/language.md +503 -0
- package/skill/authoring/local-workflow.md +1667 -0
- package/skill/authoring/offline/connectivity.md +50 -0
- package/skill/authoring/offline/example.md +102 -0
- package/skill/authoring/offline/history-sync.md +39 -0
- package/skill/authoring/offline/index.md +29 -0
- package/skill/authoring/offline/package-storage.md +37 -0
- package/skill/authoring/offline/platforms.md +35 -0
- package/skill/authoring/offline/startup-navigation.md +53 -0
- package/skill/authoring/platform-files.md +616 -0
- package/skill/authoring/player/casting.md +47 -0
- package/skill/authoring/player/controls.md +95 -0
- package/skill/authoring/player/downloads.md +90 -0
- package/skill/authoring/player/drm.md +31 -0
- package/skill/authoring/player/index.md +39 -0
- package/skill/authoring/player/live-dvr.md +74 -0
- package/skill/authoring/player/platforms.md +47 -0
- package/skill/authoring/player/playback.md +43 -0
- package/skill/authoring/player/tracks-subtitles.md +32 -0
- package/skill/authoring/profiles.md +213 -0
- package/skill/authoring/remote-keys.md +139 -0
- package/skill/authoring/search.md +27 -0
- package/skill/authoring/store.md +85 -0
- package/skill/authoring/tv-text-entry.md +146 -0
- package/skill/design/approved/README.md +14 -0
- package/skill/design/approved/vesper-phone-home.jpg +0 -0
- package/skill/design/approved/vesper-tv-detail.jpg +0 -0
- package/skill/design/approved/vesper-tv-home.jpg +0 -0
- package/skill/design/design-guide.md +610 -0
- package/skill/design/edge-to-edge.md +66 -0
- package/skill/design/icons.md +50 -0
- package/skill/design/responsive-layout.md +175 -0
- package/skill/design/safe-area.md +221 -0
- package/skill/design/streaming-playbook.md +131 -0
- package/skill/design/visual-review.md +150 -0
- package/skill/examples/README.md +24 -0
- package/skill/examples/cinema/DESIGN.md +58 -0
- package/skill/examples/cinema/app.tsx +17 -0
- package/skill/examples/cinema/assets/logo.png +0 -0
- package/skill/examples/cinema/components/Action.tsx +40 -0
- package/skill/examples/cinema/components/Action.tv.tsx +44 -0
- package/skill/examples/cinema/components/CatalogGrid.tsx +31 -0
- package/skill/examples/cinema/components/ContinueRail.tsx +98 -0
- package/skill/examples/cinema/components/Feedback.tsx +28 -0
- package/skill/examples/cinema/components/Field.tsx +41 -0
- package/skill/examples/cinema/components/LibraryRail.tsx +46 -0
- package/skill/examples/cinema/components/PlayAction.tsx +29 -0
- package/skill/examples/cinema/components/Root.tsx +59 -0
- package/skill/examples/cinema/components/Shell.tsx +70 -0
- package/skill/examples/cinema/components/Tab.tsx +22 -0
- package/skill/examples/cinema/components/TitleCard.tsx +68 -0
- package/skill/examples/cinema/components/TitleMeta.tsx +36 -0
- package/skill/examples/cinema/locales/en.ts +211 -0
- package/skill/examples/cinema/locales/lt.ts +211 -0
- package/skill/examples/cinema/preview/phone-home.jpg +0 -0
- package/skill/examples/cinema/preview/tv-detail.jpg +0 -0
- package/skill/examples/cinema/preview/tv-home.jpg +0 -0
- package/skill/examples/cinema/screens/portrait/Account.tsx +110 -0
- package/skill/examples/cinema/screens/portrait/Browse.tsx +73 -0
- package/skill/examples/cinema/screens/portrait/Detail.tsx +159 -0
- package/skill/examples/cinema/screens/portrait/Home.tsx +196 -0
- package/skill/examples/cinema/screens/portrait/Player.tsx +299 -0
- package/skill/examples/cinema/screens/portrait/Search.tsx +46 -0
- package/skill/examples/cinema/screens/portrait/Search.tv.tsx +55 -0
- package/skill/examples/cinema/screens/portrait/SignIn.tsx +101 -0
- package/skill/examples/cinema/screens/portrait/SignUp.tsx +106 -0
- package/skill/examples/cinema/store.tsx +132 -0
- package/skill/examples/cinema/theme.ts +32 -0
- package/skill/examples/documentary/DESIGN.md +33 -0
- package/skill/examples/documentary/app.tsx +17 -0
- package/skill/examples/documentary/components/Action.tsx +43 -0
- package/skill/examples/documentary/components/CatalogGrid.tsx +47 -0
- package/skill/examples/documentary/components/Feedback.tsx +27 -0
- package/skill/examples/documentary/components/Field.tsx +33 -0
- package/skill/examples/documentary/components/LibraryRail.tsx +47 -0
- package/skill/examples/documentary/components/Root.tsx +62 -0
- package/skill/examples/documentary/components/Shell.tsx +93 -0
- package/skill/examples/documentary/components/StoryCard.tsx +53 -0
- package/skill/examples/documentary/components/StoryMeta.tsx +22 -0
- package/skill/examples/documentary/preview/phone-home.jpg +0 -0
- package/skill/examples/documentary/preview/tv-detail.jpg +0 -0
- package/skill/examples/documentary/preview/tv-home.jpg +0 -0
- package/skill/examples/documentary/screens/portrait/Account.tsx +97 -0
- package/skill/examples/documentary/screens/portrait/Browse.tsx +71 -0
- package/skill/examples/documentary/screens/portrait/Detail.tsx +133 -0
- package/skill/examples/documentary/screens/portrait/Home.tsx +89 -0
- package/skill/examples/documentary/screens/portrait/Player.tsx +116 -0
- package/skill/examples/documentary/screens/portrait/Search.tsx +48 -0
- package/skill/examples/documentary/screens/portrait/SignIn.tsx +96 -0
- package/skill/examples/documentary/screens/portrait/SignUp.tsx +101 -0
- package/skill/examples/documentary/store.tsx +115 -0
- package/skill/examples/documentary/theme.ts +20 -0
- package/skill/examples/series/DESIGN.md +55 -0
- package/skill/examples/series/app.tsx +17 -0
- package/skill/examples/series/components/Action.tsx +42 -0
- package/skill/examples/series/components/CatalogGrid.tsx +43 -0
- package/skill/examples/series/components/ContinueRail.tsx +78 -0
- package/skill/examples/series/components/Feedback.tsx +26 -0
- package/skill/examples/series/components/Field.tsx +34 -0
- package/skill/examples/series/components/LibraryRail.tsx +38 -0
- package/skill/examples/series/components/OutcomeNote.tsx +28 -0
- package/skill/examples/series/components/PlayAction.tsx +29 -0
- package/skill/examples/series/components/RestoreAction.tsx +33 -0
- package/skill/examples/series/components/Root.tsx +75 -0
- package/skill/examples/series/components/SaveTitle.tsx +34 -0
- package/skill/examples/series/components/Shell.tsx +75 -0
- package/skill/examples/series/components/TitleCard.tsx +62 -0
- package/skill/examples/series/components/UnlockAction.tsx +10 -0
- package/skill/examples/series/preview/phone-home.jpg +0 -0
- package/skill/examples/series/preview/tv-detail.jpg +0 -0
- package/skill/examples/series/preview/tv-home.jpg +0 -0
- package/skill/examples/series/screens/portrait/Account.tsx +125 -0
- package/skill/examples/series/screens/portrait/Browse.tsx +61 -0
- package/skill/examples/series/screens/portrait/Detail.tsx +231 -0
- package/skill/examples/series/screens/portrait/Home.tsx +109 -0
- package/skill/examples/series/screens/portrait/MyList.tsx +44 -0
- package/skill/examples/series/screens/portrait/Plans.tsx +131 -0
- package/skill/examples/series/screens/portrait/Player.tsx +138 -0
- package/skill/examples/series/screens/portrait/Profiles.tsx +103 -0
- package/skill/examples/series/screens/portrait/Search.tsx +50 -0
- package/skill/examples/series/screens/portrait/SignIn.tsx +94 -0
- package/skill/examples/series/screens/portrait/SignUp.tsx +100 -0
- package/skill/examples/series/store.tsx +192 -0
- package/skill/examples/series/theme.ts +32 -0
- package/skill/guides/final-pricing-capabilities.md +13 -0
- package/skill/reference/api.md +842 -0
- package/skill/reference/components.md +1782 -0
- package/skill/reference/packages.md +24 -0
- package/skill/scripts/assemble-checkout.mjs +93 -0
- package/skill/scripts/local-save.mjs +513 -0
- package/skill/scripts/materialize.mjs +319 -0
- package/skill/scripts/workspace-state.mjs +183 -0
- package/types/adelin-access.d.ts +70 -0
- package/types/adelin-analytics.d.ts +5 -0
- package/types/adelin-assets.d.ts +26 -0
- package/types/adelin-auth.d.ts +37 -0
- package/types/adelin-content.d.ts +120 -0
- package/types/adelin-device.d.ts +195 -0
- package/types/adelin-downloads.d.ts +47 -0
- package/types/adelin-env.d.ts +8 -0
- package/types/adelin-globals.d.ts +19 -0
- package/types/adelin-i18n.d.ts +31 -0
- package/types/adelin-native-stack.d.ts +178 -0
- package/types/adelin-navigation.d.ts +264 -0
- package/types/adelin-profiles.d.ts +51 -0
- package/types/adelin-react.d.ts +49 -0
- package/types/adelin-reanimated.d.ts +376 -0
- package/types/adelin-storage.d.ts +9 -0
- package/types/adelin-store.d.ts +74 -0
- package/types/adelin-ui.d.ts +1518 -0
- package/types/adelin-utils.d.ts +7 -0
- package/types/adelin-worklets.d.ts +15 -0
- package/workspace-tsconfig.json +53 -0
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { readdirSync, mkdirSync, rmdirSync, writeFileSync, linkSync, unlinkSync, renameSync, lstatSync } from 'node:fs';
|
|
3
|
+
import { basename, dirname, join, resolve } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { MAX_BUNDLE_BYTES, MANIFEST, PENDING, UUID, SOURCE_PATH, DECLARATION_PATH,
|
|
6
|
+
refuse, sha256, lstatIfPresent, assertNoSymlinkAncestors, assertSafeTarget,
|
|
7
|
+
readSafeFile, hashSafeFile, parseManifest, manifestStamp, putManifestStamp, withWorkspaceMutation } from './workspace-state.mjs';
|
|
8
|
+
|
|
9
|
+
function readBundle(bundlePath) {
|
|
10
|
+
let value;
|
|
11
|
+
try { value = JSON.parse(readSafeFile(resolve(bundlePath)).toString('utf8')); }
|
|
12
|
+
catch { refuse('Invalid checkout bundle file.'); }
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function validate(bundle, expectedAppId) {
|
|
17
|
+
if (!UUID.test(expectedAppId) || !bundle || typeof bundle !== 'object' ||
|
|
18
|
+
Array.isArray(bundle) || bundle.schema !== 1 || bundle.appId !== expectedAppId ||
|
|
19
|
+
!['full', 'targeted'].includes(bundle.mode) ||
|
|
20
|
+
Object.keys(bundle).some(key => !['schema', 'appId', 'mode', 'requestedPaths', 'files', 'typeDeclarations', 'tsconfig'].includes(key)) ||
|
|
21
|
+
!Array.isArray(bundle.files) || bundle.files.length > 500 ||
|
|
22
|
+
!Array.isArray(bundle.typeDeclarations) || bundle.typeDeclarations.length < 1 ||
|
|
23
|
+
bundle.typeDeclarations.length > 100 || typeof bundle.tsconfig !== 'string')
|
|
24
|
+
refuse('Checkout bundle does not match this app.');
|
|
25
|
+
if (bundle.mode === 'full' && Object.hasOwn(bundle, 'requestedPaths')) refuse('Invalid full checkout selection.');
|
|
26
|
+
if (bundle.mode === 'targeted' &&
|
|
27
|
+
(!Array.isArray(bundle.requestedPaths) || bundle.requestedPaths.length < 1 || bundle.requestedPaths.length > 50 ||
|
|
28
|
+
new Set(bundle.requestedPaths).size !== bundle.requestedPaths.length ||
|
|
29
|
+
bundle.requestedPaths.some(path => typeof path !== 'string' || !SOURCE_PATH.test(path))))
|
|
30
|
+
refuse('Invalid targeted checkout selection.');
|
|
31
|
+
const seen = new Set();
|
|
32
|
+
for (const file of bundle.files) {
|
|
33
|
+
if (!file || typeof file !== 'object' || typeof file.path !== 'string' ||
|
|
34
|
+
Array.isArray(file) || Object.keys(file).some(key => !['path', 'content', 'version'].includes(key)) ||
|
|
35
|
+
!SOURCE_PATH.test(file.path) ||
|
|
36
|
+
typeof file.content !== 'string' ||
|
|
37
|
+
!Number.isSafeInteger(file.version) || file.version < 0 ||
|
|
38
|
+
seen.has(file.path))
|
|
39
|
+
refuse('Checkout bundle contains an unsafe file.');
|
|
40
|
+
seen.add(file.path);
|
|
41
|
+
}
|
|
42
|
+
for (const file of bundle.typeDeclarations) {
|
|
43
|
+
if (!file || typeof file !== 'object' || typeof file.path !== 'string' ||
|
|
44
|
+
Array.isArray(file) || Object.keys(file).some(key => !['path', 'content'].includes(key)) ||
|
|
45
|
+
!DECLARATION_PATH.test(file.path) || typeof file.content !== 'string' ||
|
|
46
|
+
file.content.length === 0 || seen.has(file.path))
|
|
47
|
+
refuse('Checkout bundle contains an unsafe declaration.');
|
|
48
|
+
seen.add(file.path);
|
|
49
|
+
}
|
|
50
|
+
let config;
|
|
51
|
+
try { config = JSON.parse(bundle.tsconfig); }
|
|
52
|
+
catch { refuse('Checkout bundle contains an invalid tsconfig.'); }
|
|
53
|
+
if (!config || typeof config !== 'object' || Array.isArray(config) ||
|
|
54
|
+
Object.keys(config).some(key => !['compilerOptions', 'include'].includes(key)) ||
|
|
55
|
+
!config.compilerOptions || typeof config.compilerOptions !== 'object' ||
|
|
56
|
+
Array.isArray(config.compilerOptions) || 'plugins' in config.compilerOptions ||
|
|
57
|
+
!Array.isArray(config.include) || !config.include.every(item => typeof item === 'string'))
|
|
58
|
+
refuse('Checkout bundle contains an invalid tsconfig.');
|
|
59
|
+
if (Buffer.byteLength(JSON.stringify(bundle), 'utf8') > MAX_BUNDLE_BYTES)
|
|
60
|
+
refuse('Checkout bundle is too large.');
|
|
61
|
+
if (bundle.mode === 'targeted' &&
|
|
62
|
+
(bundle.files.length !== bundle.requestedPaths.length ||
|
|
63
|
+
bundle.files.some(file => !bundle.requestedPaths.includes(file.path))))
|
|
64
|
+
refuse('Targeted checkout does not match requested paths.');
|
|
65
|
+
return [...bundle.files, ...bundle.typeDeclarations, { path: 'tsconfig.json', content: bundle.tsconfig }];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function ensureFolders(root, relativePath, created) {
|
|
69
|
+
const parts = dirname(relativePath).split('/');
|
|
70
|
+
if (parts.length === 1 && parts[0] === '.') return;
|
|
71
|
+
let cursor = root;
|
|
72
|
+
for (const part of parts) {
|
|
73
|
+
cursor = join(cursor, part);
|
|
74
|
+
const stat = lstatIfPresent(cursor);
|
|
75
|
+
if (stat && !stat.isDirectory()) refuse('Occupied checkout path.');
|
|
76
|
+
if (!stat) { mkdirSync(cursor); created.push(cursor); }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function assertStage(stage, content) {
|
|
81
|
+
const stat = lstatIfPresent(stage);
|
|
82
|
+
if (!stat?.isFile() || stat.isSymbolicLink() || stat.nlink !== 1 ||
|
|
83
|
+
sha256(readSafeFile(stage)) !== sha256(Buffer.from(content, 'utf8')))
|
|
84
|
+
refuse('Checkout stage was replaced or changed.');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function removeOwnedStage(stage) {
|
|
88
|
+
assertNoSymlinkAncestors(dirname(stage));
|
|
89
|
+
if (lstatIfPresent(stage)) unlinkSync(stage);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function removeEmptyOwnedFolders(folders) {
|
|
93
|
+
for (const folder of folders.reverse()) {
|
|
94
|
+
assertNoSymlinkAncestors(dirname(folder));
|
|
95
|
+
if (lstatIfPresent(folder)?.isDirectory() && readdirSync(folder).length === 0) rmdirSync(folder);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function initialInstall(root, files, expectedAppId, options) {
|
|
100
|
+
const writeStage = options.writeStage ?? writeFileSync;
|
|
101
|
+
const stages = [];
|
|
102
|
+
const installed = [];
|
|
103
|
+
const createdFolders = [];
|
|
104
|
+
try {
|
|
105
|
+
const record = { schema: 1, appId: expectedAppId, files: {}, support: {} };
|
|
106
|
+
for (const file of files) {
|
|
107
|
+
const stage = join(root, `.${randomUUID()}.tmp`);
|
|
108
|
+
stages.push(stage);
|
|
109
|
+
writeStage(stage, file.content, { flag: 'wx', mode: 0o600 });
|
|
110
|
+
assertStage(stage, file.content);
|
|
111
|
+
putManifestStamp(record, file.path, file.version === undefined
|
|
112
|
+
? { sha256: sha256(Buffer.from(file.content, 'utf8')) }
|
|
113
|
+
: { version: file.version, sha256: sha256(Buffer.from(file.content, 'utf8')) });
|
|
114
|
+
}
|
|
115
|
+
const manifestStage = join(root, `.${randomUUID()}.tmp`);
|
|
116
|
+
stages.push(manifestStage);
|
|
117
|
+
const manifestContent = JSON.stringify(record, null, 2) + '\n';
|
|
118
|
+
writeStage(manifestStage, manifestContent, { flag: 'wx', mode: 0o600 });
|
|
119
|
+
assertStage(manifestStage, manifestContent);
|
|
120
|
+
|
|
121
|
+
for (let i = 0; i < files.length + 1; i++) {
|
|
122
|
+
const relativePath = i < files.length ? files[i].path : MANIFEST;
|
|
123
|
+
const target = join(root, relativePath);
|
|
124
|
+
ensureFolders(root, relativePath, createdFolders);
|
|
125
|
+
assertNoSymlinkAncestors(dirname(target));
|
|
126
|
+
if (lstatIfPresent(target)) refuse('Occupied checkout path.');
|
|
127
|
+
assertStage(stages[i], i < files.length ? files[i].content : manifestContent);
|
|
128
|
+
linkSync(stages[i], target);
|
|
129
|
+
installed.push({ path: target, ino: lstatSync(stages[i]).ino });
|
|
130
|
+
}
|
|
131
|
+
} catch (error) {
|
|
132
|
+
for (const item of installed.reverse()) {
|
|
133
|
+
assertNoSymlinkAncestors(dirname(item.path));
|
|
134
|
+
if (lstatIfPresent(item.path)?.ino === item.ino) unlinkSync(item.path);
|
|
135
|
+
}
|
|
136
|
+
throw error;
|
|
137
|
+
} finally {
|
|
138
|
+
for (const stage of stages) removeOwnedStage(stage);
|
|
139
|
+
removeEmptyOwnedFolders(createdFolders);
|
|
140
|
+
}
|
|
141
|
+
return { appId: expectedAppId, fileCount: files.filter(file => file.version !== undefined).length };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function manifestText(value) { return JSON.stringify(value, null, 2) + '\n'; }
|
|
145
|
+
|
|
146
|
+
function readManifest(root, expectedAppId) {
|
|
147
|
+
const path = assertSafeTarget(root, MANIFEST);
|
|
148
|
+
return parseManifest(readSafeFile(path), expectedAppId);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function readPending(root, appId) {
|
|
152
|
+
const path = assertSafeTarget(root, PENDING);
|
|
153
|
+
if (!lstatIfPresent(path)) return null;
|
|
154
|
+
let value;
|
|
155
|
+
try { value = JSON.parse(readSafeFile(path).toString('utf8')); }
|
|
156
|
+
catch { refuse('Invalid pending checkout journal.'); }
|
|
157
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) ||
|
|
158
|
+
Object.keys(value).sort().join(',') !== 'appId,entries,manifestSha256,schema' ||
|
|
159
|
+
value.schema !== 1 || value.appId !== appId ||
|
|
160
|
+
!/^[a-f0-9]{64}$/.test(value.manifestSha256) ||
|
|
161
|
+
!Array.isArray(value.entries) || value.entries.length > 601)
|
|
162
|
+
refuse('Invalid pending checkout journal.');
|
|
163
|
+
const seen = new Set();
|
|
164
|
+
for (const entry of value.entries) {
|
|
165
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry) ||
|
|
166
|
+
Object.keys(entry).sort().join(',') !== 'next,old,path' ||
|
|
167
|
+
typeof entry.path !== 'string' || seen.has(entry.path) ||
|
|
168
|
+
(!SOURCE_PATH.test(entry.path) && !DECLARATION_PATH.test(entry.path) && entry.path !== 'tsconfig.json') ||
|
|
169
|
+
(entry.old !== null && !validJournalStamp(entry.old, entry.path)) ||
|
|
170
|
+
!validJournalStamp(entry.next, entry.path)) refuse('Invalid pending checkout journal.');
|
|
171
|
+
seen.add(entry.path);
|
|
172
|
+
assertSafeTarget(root, entry.path);
|
|
173
|
+
}
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function validJournalStamp(stamp, path) {
|
|
178
|
+
return stamp && typeof stamp === 'object' && !Array.isArray(stamp) &&
|
|
179
|
+
Object.keys(stamp).sort().join(',') === (SOURCE_PATH.test(path) ? 'sha256,version' : 'sha256') &&
|
|
180
|
+
/^[a-f0-9]{64}$/.test(stamp.sha256) &&
|
|
181
|
+
(!SOURCE_PATH.test(path) || (Number.isSafeInteger(stamp.version) && stamp.version >= 0));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function writeManifest(root, manifest, options) {
|
|
185
|
+
const target = assertSafeTarget(root, MANIFEST);
|
|
186
|
+
const stage = join(root, `.${randomUUID()}.tmp`);
|
|
187
|
+
try {
|
|
188
|
+
const content = manifestText(manifest);
|
|
189
|
+
(options.writeStage ?? writeFileSync)(stage, content, { flag: 'wx', mode: 0o600 });
|
|
190
|
+
assertStage(stage, content);
|
|
191
|
+
assertSafeTarget(root, MANIFEST);
|
|
192
|
+
assertStage(stage, content);
|
|
193
|
+
(options.renameFile ?? renameSync)(stage, target);
|
|
194
|
+
} finally { removeOwnedStage(stage); }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function reconcilePending(root, manifest, appId, options) {
|
|
198
|
+
const pending = readPending(root, appId);
|
|
199
|
+
if (!pending) return manifest;
|
|
200
|
+
const currentHash = sha256(Buffer.from(manifestText(manifest), 'utf8'));
|
|
201
|
+
if (currentHash !== pending.manifestSha256) {
|
|
202
|
+
const alreadySettled = pending.entries.every(entry => {
|
|
203
|
+
const stamp = manifestStamp(manifest, entry.path);
|
|
204
|
+
return JSON.stringify(stamp ?? null) === JSON.stringify(entry.next);
|
|
205
|
+
});
|
|
206
|
+
if (!alreadySettled) refuse('Pending checkout journal does not match manifest.');
|
|
207
|
+
assertSafeTarget(root, PENDING);
|
|
208
|
+
unlinkSync(join(root, PENDING));
|
|
209
|
+
return manifest;
|
|
210
|
+
}
|
|
211
|
+
for (const entry of pending.entries) {
|
|
212
|
+
if (JSON.stringify(manifestStamp(manifest, entry.path) ?? null) !== JSON.stringify(entry.old))
|
|
213
|
+
refuse('Pending checkout journal has changed base.');
|
|
214
|
+
const target = assertSafeTarget(root, entry.path);
|
|
215
|
+
const stat = lstatIfPresent(target);
|
|
216
|
+
if (stat && hashSafeFile(target) === entry.next.sha256)
|
|
217
|
+
putManifestStamp(manifest, entry.path, entry.next);
|
|
218
|
+
}
|
|
219
|
+
writeManifest(root, manifest, options);
|
|
220
|
+
assertSafeTarget(root, PENDING);
|
|
221
|
+
unlinkSync(join(root, PENDING));
|
|
222
|
+
return manifest;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function refresh(root, files, appId, options) {
|
|
226
|
+
let manifest = readManifest(root, appId);
|
|
227
|
+
for (const file of files) assertSafeTarget(root, file.path);
|
|
228
|
+
assertSafeTarget(root, PENDING);
|
|
229
|
+
manifest = reconcilePending(root, manifest, appId, options);
|
|
230
|
+
const priorHash = sha256(Buffer.from(manifestText(manifest), 'utf8'));
|
|
231
|
+
const outcomes = [];
|
|
232
|
+
const eligible = [];
|
|
233
|
+
for (const file of files) {
|
|
234
|
+
const target = assertSafeTarget(root, file.path);
|
|
235
|
+
const old = manifestStamp(manifest, file.path) ?? null;
|
|
236
|
+
const stat = lstatIfPresent(target);
|
|
237
|
+
const incomingVersion = file.version;
|
|
238
|
+
if (!stat && old) { outcomes.push({ path: file.path, status: 'LOCAL_REMOVED', ...(incomingVersion === undefined ? {} : { incomingVersion }) }); continue; }
|
|
239
|
+
if (stat && !old) { outcomes.push({ path: file.path, status: 'UNTRACKED_COLLISION', ...(incomingVersion === undefined ? {} : { incomingVersion }) }); continue; }
|
|
240
|
+
if (stat && hashSafeFile(target) !== old.sha256) {
|
|
241
|
+
outcomes.push({ path: file.path, status: 'DIRTY_LOCAL_FILE', ...(incomingVersion === undefined ? {} : { incomingVersion }) });
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
const next = file.version === undefined
|
|
245
|
+
? { sha256: sha256(Buffer.from(file.content, 'utf8')) }
|
|
246
|
+
: { version: file.version, sha256: sha256(Buffer.from(file.content, 'utf8')) };
|
|
247
|
+
if (old && JSON.stringify(old) === JSON.stringify(next)) {
|
|
248
|
+
outcomes.push({ path: file.path, status: 'unchanged', ...(incomingVersion === undefined ? {} : { incomingVersion }) });
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
eligible.push({ file, target, old, next });
|
|
252
|
+
outcomes.push({ path: file.path, status: 'updated', ...(incomingVersion === undefined ? {} : { incomingVersion }) });
|
|
253
|
+
}
|
|
254
|
+
if (eligible.length === 0) return { appId, outcomes };
|
|
255
|
+
const stages = [];
|
|
256
|
+
const createdFolders = [];
|
|
257
|
+
let journalOwned = false;
|
|
258
|
+
try {
|
|
259
|
+
for (const item of eligible) {
|
|
260
|
+
ensureFolders(root, item.file.path, createdFolders);
|
|
261
|
+
assertSafeTarget(root, item.file.path);
|
|
262
|
+
const stage = join(dirname(item.target), `.${basename(item.target)}.${randomUUID()}.tmp`);
|
|
263
|
+
stages.push({ path: stage, item });
|
|
264
|
+
(options.writeStage ?? writeFileSync)(stage, item.file.content, { flag: 'wx', mode: 0o600 });
|
|
265
|
+
assertStage(stage, item.file.content);
|
|
266
|
+
}
|
|
267
|
+
const journal = { schema: 1, appId, manifestSha256: priorHash,
|
|
268
|
+
entries: eligible.map(({ file, old, next }) => ({ path: file.path, old, next })) };
|
|
269
|
+
assertSafeTarget(root, PENDING);
|
|
270
|
+
writeFileSync(join(root, PENDING), manifestText(journal), { flag: 'wx', mode: 0o600 });
|
|
271
|
+
journalOwned = true;
|
|
272
|
+
for (const { path: stage, item } of stages) {
|
|
273
|
+
const target = assertSafeTarget(root, item.file.path);
|
|
274
|
+
const present = lstatIfPresent(target);
|
|
275
|
+
if ((!present && item.old) || (present && !item.old) ||
|
|
276
|
+
(present && hashSafeFile(target) !== item.old.sha256))
|
|
277
|
+
refuse('Checkout path changed during refresh.');
|
|
278
|
+
assertStage(stage, item.file.content);
|
|
279
|
+
(options.renameFile ?? renameSync)(stage, target);
|
|
280
|
+
}
|
|
281
|
+
for (const { file, next } of eligible) putManifestStamp(manifest, file.path, next);
|
|
282
|
+
if (sha256(Buffer.from(manifestText(readManifest(root, appId)), 'utf8')) !== priorHash)
|
|
283
|
+
refuse('Workspace manifest changed during refresh.');
|
|
284
|
+
writeManifest(root, manifest, options);
|
|
285
|
+
assertSafeTarget(root, PENDING);
|
|
286
|
+
unlinkSync(join(root, PENDING));
|
|
287
|
+
journalOwned = false;
|
|
288
|
+
} finally {
|
|
289
|
+
for (const { path } of stages) removeOwnedStage(path);
|
|
290
|
+
if (!journalOwned) removeEmptyOwnedFolders(createdFolders);
|
|
291
|
+
}
|
|
292
|
+
return { appId, outcomes };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Install or refresh an app-bound checkout without replacing local edits. */
|
|
296
|
+
export function materialize(bundlePath, destination, expectedAppId, options = {}) {
|
|
297
|
+
const bundle = readBundle(bundlePath);
|
|
298
|
+
const files = validate(bundle, expectedAppId);
|
|
299
|
+
const root = resolve(destination);
|
|
300
|
+
assertNoSymlinkAncestors(root);
|
|
301
|
+
const existing = lstatIfPresent(root);
|
|
302
|
+
if (existing && !existing.isDirectory()) refuse('Occupied checkout path.');
|
|
303
|
+
const manifest = existing ? assertSafeTarget(root, MANIFEST) : join(root, MANIFEST);
|
|
304
|
+
if (existing && lstatIfPresent(manifest)) return withWorkspaceMutation(root, () => refresh(root, files, expectedAppId, options));
|
|
305
|
+
if (bundle.mode !== 'full' || (existing && readdirSync(root).length !== 0))
|
|
306
|
+
refuse('Checkout destination must be an empty directory for initial checkout.');
|
|
307
|
+
if (!existing) mkdirSync(root);
|
|
308
|
+
return initialInstall(root, files, expectedAppId, options);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
312
|
+
try {
|
|
313
|
+
if (process.argv.length !== 5) refuse('Usage: materialize.mjs <bundle.json> <empty-directory> <app-id>');
|
|
314
|
+
process.stdout.write(JSON.stringify(materialize(process.argv[2], process.argv[3], process.argv[4])) + '\n');
|
|
315
|
+
} catch (error) {
|
|
316
|
+
process.stderr.write(`${error.message}\n`);
|
|
317
|
+
process.exitCode = 1;
|
|
318
|
+
}
|
|
319
|
+
}
|