@fragmentsx/cli 0.0.0-stage → 0.1.0-dev-20261126025247

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/dist/bin.js ADDED
@@ -0,0 +1,1032 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ fragmentFileHeader,
4
+ fragmentStylesHeader,
5
+ genFileHeader,
6
+ isFragmentDirName,
7
+ isGeneratedPath,
8
+ readOutState,
9
+ sanitizeFragmentName,
10
+ sanitizeServerMessage,
11
+ typesFileHeader
12
+ } from "./chunk-2UXLILUH.js";
13
+
14
+ // src/pull.ts
15
+ import path3 from "path";
16
+
17
+ // src/config.ts
18
+ import fs from "fs";
19
+ import path from "path";
20
+ var ALLOWED_LOCAL_HOSTS = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
21
+ var MAX_ROOTS = 20;
22
+ var KEY_PATTERN = /^[\x21-\x7E]+$/;
23
+ function assertOutInsideCwd(cwd, outDir) {
24
+ const message = "\u041A\u0430\u0442\u0430\u043B\u043E\u0433 out \u0434\u043E\u043B\u0436\u0435\u043D \u043D\u0430\u0445\u043E\u0434\u0438\u0442\u044C\u0441\u044F \u0441\u0442\u0440\u043E\u0433\u043E \u0432\u043D\u0443\u0442\u0440\u0438 \u043A\u0430\u0442\u0430\u043B\u043E\u0433\u0430 \u0437\u0430\u043F\u0443\u0441\u043A\u0430";
25
+ const realCwd = fs.realpathSync(cwd);
26
+ let probe = path.resolve(outDir);
27
+ while (!fs.lstatSync(probe, { throwIfNoEntry: false })) {
28
+ const parent = path.dirname(probe);
29
+ if (parent === probe) {
30
+ break;
31
+ }
32
+ probe = parent;
33
+ }
34
+ let realProbe;
35
+ try {
36
+ realProbe = fs.realpathSync(probe);
37
+ } catch {
38
+ throw new Error(message);
39
+ }
40
+ const rel = path.relative(realCwd, realProbe);
41
+ if (rel === ".." || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
42
+ throw new Error(message);
43
+ }
44
+ }
45
+ function readPullConfig(cwd, env) {
46
+ const configPath = path.resolve(cwd, "fragments.config.json");
47
+ if (!fs.existsSync(configPath)) {
48
+ throw new Error("\u0424\u0430\u0439\u043B fragments.config.json \u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D");
49
+ }
50
+ let parsed;
51
+ try {
52
+ parsed = JSON.parse(fs.readFileSync(configPath, "utf-8"));
53
+ } catch {
54
+ throw new Error("\u041D\u0435\u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u0439 JSON \u0432 fragments.config.json");
55
+ }
56
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
57
+ throw new Error("fragments.config.json \u0434\u043E\u043B\u0436\u0435\u043D \u0441\u043E\u0434\u0435\u0440\u0436\u0430\u0442\u044C JSON-\u043E\u0431\u044A\u0435\u043A\u0442");
58
+ }
59
+ const cfg = parsed;
60
+ if (typeof cfg.project !== "number" || !Number.isInteger(cfg.project) || cfg.project <= 0) {
61
+ throw new Error(
62
+ "fragments.config.json: \u043F\u043E\u043B\u0435 project \u0434\u043E\u043B\u0436\u043D\u043E \u0431\u044B\u0442\u044C \u043F\u043E\u043B\u043E\u0436\u0438\u0442\u0435\u043B\u044C\u043D\u044B\u043C \u0446\u0435\u043B\u044B\u043C \u0447\u0438\u0441\u043B\u043E\u043C"
63
+ );
64
+ }
65
+ if (!Array.isArray(cfg.fragments) || cfg.fragments.length === 0 || !cfg.fragments.every(
66
+ (id) => typeof id === "number" && Number.isInteger(id) && id > 0
67
+ )) {
68
+ throw new Error(
69
+ "fragments.config.json: \u043F\u043E\u043B\u0435 fragments \u0434\u043E\u043B\u0436\u043D\u043E \u0431\u044B\u0442\u044C \u043D\u0435\u043F\u0443\u0441\u0442\u044B\u043C \u043C\u0430\u0441\u0441\u0438\u0432\u043E\u043C \u043F\u043E\u043B\u043E\u0436\u0438\u0442\u0435\u043B\u044C\u043D\u044B\u0445 \u0446\u0435\u043B\u044B\u0445 \u0447\u0438\u0441\u0435\u043B"
70
+ );
71
+ }
72
+ const fragments = Array.from(new Set(cfg.fragments));
73
+ if (fragments.length > MAX_ROOTS) {
74
+ throw new Error(
75
+ `fragments.config.json: \u0432 fragments \u043D\u0435 \u0431\u043E\u043B\u044C\u0448\u0435 ${MAX_ROOTS} \u0443\u043D\u0438\u043A\u0430\u043B\u044C\u043D\u044B\u0445 \u043A\u043E\u0440\u043D\u0435\u0439, \u0443\u043A\u0430\u0437\u0430\u043D\u043E ${fragments.length}`
76
+ );
77
+ }
78
+ if (typeof cfg.out !== "string" || cfg.out.trim().length === 0) {
79
+ throw new Error("fragments.config.json: \u043F\u043E\u043B\u0435 out \u043E\u0431\u044F\u0437\u0430\u0442\u0435\u043B\u044C\u043D\u043E");
80
+ }
81
+ const realCwd = fs.realpathSync(cwd);
82
+ const resolvedOut = path.resolve(realCwd, cfg.out);
83
+ const relative = path.relative(realCwd, resolvedOut);
84
+ if (relative === "" || relative.startsWith("..") || path.isAbsolute(relative)) {
85
+ throw new Error("\u041A\u0430\u0442\u0430\u043B\u043E\u0433 out \u0434\u043E\u043B\u0436\u0435\u043D \u043D\u0430\u0445\u043E\u0434\u0438\u0442\u044C\u0441\u044F \u0441\u0442\u0440\u043E\u0433\u043E \u0432\u043D\u0443\u0442\u0440\u0438 \u043A\u0430\u0442\u0430\u043B\u043E\u0433\u0430 \u0437\u0430\u043F\u0443\u0441\u043A\u0430");
86
+ }
87
+ assertOutInsideCwd(realCwd, resolvedOut);
88
+ const rawUrl = env.FRAGMENTS_API_URL;
89
+ if (!rawUrl) {
90
+ throw new Error("\u041F\u0435\u0440\u0435\u043C\u0435\u043D\u043D\u0430\u044F \u043E\u043A\u0440\u0443\u0436\u0435\u043D\u0438\u044F FRAGMENTS_API_URL \u043D\u0435 \u0437\u0430\u0434\u0430\u043D\u0430");
91
+ }
92
+ let parsedUrl;
93
+ try {
94
+ parsedUrl = new URL(rawUrl);
95
+ } catch {
96
+ throw new Error("FRAGMENTS_API_URL \u0441\u043E\u0434\u0435\u0440\u0436\u0438\u0442 \u043D\u0435\u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u0439 URL");
97
+ }
98
+ if (parsedUrl.protocol !== "https:" && parsedUrl.protocol !== "http:") {
99
+ throw new Error(
100
+ "FRAGMENTS_API_URL \u0434\u043E\u043B\u0436\u0435\u043D \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u044C \u043F\u0440\u043E\u0442\u043E\u043A\u043E\u043B https (\u0438\u043B\u0438 http \u0434\u043B\u044F localhost)"
101
+ );
102
+ }
103
+ if (parsedUrl.protocol === "http:" && !ALLOWED_LOCAL_HOSTS.has(parsedUrl.hostname)) {
104
+ throw new Error(
105
+ "FRAGMENTS_API_URL \u0441 \u043F\u0440\u043E\u0442\u043E\u043A\u043E\u043B\u043E\u043C http \u0440\u0430\u0437\u0440\u0435\u0448\u0451\u043D \u0442\u043E\u043B\u044C\u043A\u043E \u0434\u043B\u044F localhost, 127.0.0.1 \u0438\u043B\u0438 ::1"
106
+ );
107
+ }
108
+ if (parsedUrl.username || parsedUrl.password) {
109
+ throw new Error(
110
+ "FRAGMENTS_API_URL \u043D\u0435 \u0434\u043E\u043B\u0436\u0435\u043D \u0441\u043E\u0434\u0435\u0440\u0436\u0430\u0442\u044C \u0438\u043C\u044F \u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u0442\u0435\u043B\u044F \u0438\u043B\u0438 \u043F\u0430\u0440\u043E\u043B\u044C"
111
+ );
112
+ }
113
+ if (parsedUrl.search) {
114
+ throw new Error(
115
+ "FRAGMENTS_API_URL \u043D\u0435 \u0434\u043E\u043B\u0436\u0435\u043D \u0441\u043E\u0434\u0435\u0440\u0436\u0430\u0442\u044C \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u044B \u0437\u0430\u043F\u0440\u043E\u0441\u0430 (?query)"
116
+ );
117
+ }
118
+ if (parsedUrl.hash) {
119
+ throw new Error("FRAGMENTS_API_URL \u043D\u0435 \u0434\u043E\u043B\u0436\u0435\u043D \u0441\u043E\u0434\u0435\u0440\u0436\u0430\u0442\u044C \u044F\u043A\u043E\u0440\u044C (#hash)");
120
+ }
121
+ const rawKey = env.FRAGMENTS_API_KEY;
122
+ if (!rawKey || rawKey.trim().length === 0) {
123
+ throw new Error("\u041F\u0435\u0440\u0435\u043C\u0435\u043D\u043D\u0430\u044F \u043E\u043A\u0440\u0443\u0436\u0435\u043D\u0438\u044F FRAGMENTS_API_KEY \u043D\u0435 \u0437\u0430\u0434\u0430\u043D\u0430");
124
+ }
125
+ const apiKey = rawKey.trim();
126
+ if (!KEY_PATTERN.test(apiKey)) {
127
+ throw new Error(
128
+ "\u041F\u0435\u0440\u0435\u043C\u0435\u043D\u043D\u0430\u044F \u043E\u043A\u0440\u0443\u0436\u0435\u043D\u0438\u044F FRAGMENTS_API_KEY \u0441\u043E\u0434\u0435\u0440\u0436\u0438\u0442 \u043D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u044B\u0435 \u0441\u0438\u043C\u0432\u043E\u043B\u044B"
129
+ );
130
+ }
131
+ const apiBaseUrl = parsedUrl.href.replace(/\/+$/, "");
132
+ return {
133
+ project: cfg.project,
134
+ outDir: resolvedOut,
135
+ fragments,
136
+ apiBaseUrl,
137
+ apiKey
138
+ };
139
+ }
140
+
141
+ // src/bundle.ts
142
+ var EXPORT_BUNDLE_FORMAT_VERSION = 1;
143
+ function parseExportBundle(json) {
144
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
145
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: root");
146
+ }
147
+ const b = json;
148
+ if (b.formatVersion !== EXPORT_BUNDLE_FORMAT_VERSION) {
149
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: formatVersion");
150
+ }
151
+ const projectObj = b.project;
152
+ if (typeof projectObj !== "object" || projectObj === null || Array.isArray(projectObj) || typeof projectObj.id !== "number" || !Number.isInteger(projectObj.id) || projectObj.id <= 0) {
153
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: project");
154
+ }
155
+ if (!Array.isArray(b.roots) || !b.roots.every((r) => typeof r === "number" && Number.isInteger(r) && r > 0)) {
156
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: roots");
157
+ }
158
+ if (!Array.isArray(b.fragments)) {
159
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragments");
160
+ }
161
+ const fragmentIds = /* @__PURE__ */ new Set();
162
+ for (const f of b.fragments) {
163
+ if (typeof f !== "object" || f === null || Array.isArray(f)) {
164
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment");
165
+ }
166
+ const frag = f;
167
+ if (typeof frag.id !== "number" || !Number.isInteger(frag.id) || frag.id <= 0) {
168
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment.id");
169
+ }
170
+ fragmentIds.add(frag.id);
171
+ if (typeof frag.name !== "string") {
172
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment.name");
173
+ }
174
+ if (typeof frag.version !== "number" || !Number.isInteger(frag.version) || frag.version <= 0) {
175
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment.version");
176
+ }
177
+ if (typeof frag.contentHash !== "string") {
178
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment.contentHash");
179
+ }
180
+ if (typeof frag.scopeClass !== "string" || !/^fx-[A-Za-z0-9_-]+$/.test(frag.scopeClass)) {
181
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: scopeClass");
182
+ }
183
+ if (typeof frag.tsxSource !== "string") {
184
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment.tsxSource");
185
+ }
186
+ if (typeof frag.css !== "string") {
187
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment.css");
188
+ }
189
+ if (frag.schema !== null && (typeof frag.schema !== "object" || Array.isArray(frag.schema))) {
190
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment.schema");
191
+ }
192
+ if (!Array.isArray(frag.imports) || !frag.imports.every((imp) => typeof imp === "number" && Number.isInteger(imp) && imp > 0)) {
193
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: fragment.imports");
194
+ }
195
+ if (!Array.isArray(frag.importSites)) {
196
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
197
+ }
198
+ let prevEnd = -1;
199
+ const siteFragmentIds = [];
200
+ const tsx = frag.tsxSource;
201
+ for (const site of frag.importSites) {
202
+ if (typeof site !== "object" || site === null || Array.isArray(site) || typeof site.fragmentId !== "number" || !Number.isInteger(site.fragmentId) || site.fragmentId <= 0 || typeof site.start !== "number" || !Number.isInteger(site.start) || site.start < 0 || typeof site.end !== "number" || !Number.isInteger(site.end) || site.end <= site.start + 1) {
203
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
204
+ }
205
+ const s = site;
206
+ if (s.start < prevEnd) {
207
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
208
+ }
209
+ prevEnd = s.end;
210
+ if (s.end > tsx.length) {
211
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
212
+ }
213
+ const quote = tsx[s.start];
214
+ if (quote !== '"' && quote !== "'") {
215
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
216
+ }
217
+ if (tsx[s.end - 1] !== quote) {
218
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
219
+ }
220
+ const slice = tsx.slice(s.start + 1, s.end - 1);
221
+ if (slice !== `fragment:${s.fragmentId}`) {
222
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
223
+ }
224
+ siteFragmentIds.push(s.fragmentId);
225
+ }
226
+ const expectedImports = Array.from(new Set(siteFragmentIds)).sort(
227
+ (a, b2) => a - b2
228
+ );
229
+ const actualImports = frag.imports;
230
+ if (actualImports.length !== expectedImports.length) {
231
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
232
+ }
233
+ for (let i = 0; i < expectedImports.length; i++) {
234
+ if (actualImports[i] !== expectedImports[i]) {
235
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: importSites");
236
+ }
237
+ }
238
+ }
239
+ for (const root of b.roots) {
240
+ if (!fragmentIds.has(root)) {
241
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: roots");
242
+ }
243
+ }
244
+ for (const f of b.fragments) {
245
+ for (const imp of f.imports) {
246
+ if (!fragmentIds.has(imp)) {
247
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: imports");
248
+ }
249
+ }
250
+ }
251
+ if (typeof b.sources !== "object" || b.sources === null || Array.isArray(b.sources)) {
252
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: sources");
253
+ }
254
+ for (const [, val] of Object.entries(b.sources)) {
255
+ if (typeof val !== "object" || val === null || Array.isArray(val)) {
256
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: sources");
257
+ }
258
+ const s = val;
259
+ if (typeof s.kind !== "string" || typeof s.baseUrl !== "string" || typeof s.transport !== "object" || s.transport === null || Array.isArray(s.transport)) {
260
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: sources");
261
+ }
262
+ }
263
+ if (typeof b.projectProperties !== "object" || b.projectProperties === null || Array.isArray(b.projectProperties)) {
264
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: projectProperties");
265
+ }
266
+ if (typeof b.types !== "string") {
267
+ throw new Error("\u043F\u0430\u043A\u0435\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043F\u043E\u0432\u0440\u0435\u0436\u0434\u0451\u043D: types");
268
+ }
269
+ return b;
270
+ }
271
+ var EXPORT_CONE_UNAVAILABLE = "EXPORT_CONE_UNAVAILABLE";
272
+ var VALID_CONE_STATES = /* @__PURE__ */ new Set([
273
+ "not_found",
274
+ "deleted",
275
+ "never_published",
276
+ "not_exportable",
277
+ "artifact_missing",
278
+ "too_large"
279
+ ]);
280
+ function parseExportConeError(json) {
281
+ if (typeof json !== "object" || json === null || Array.isArray(json)) {
282
+ return null;
283
+ }
284
+ const obj = json;
285
+ if (obj.code !== EXPORT_CONE_UNAVAILABLE) {
286
+ return null;
287
+ }
288
+ if (obj.statusCode !== 422) {
289
+ return null;
290
+ }
291
+ if (typeof obj.state !== "string" || !VALID_CONE_STATES.has(obj.state)) {
292
+ return null;
293
+ }
294
+ if (typeof obj.message !== "string") {
295
+ return null;
296
+ }
297
+ if (!Array.isArray(obj.chain)) {
298
+ return null;
299
+ }
300
+ const chain = [];
301
+ for (const node of obj.chain) {
302
+ if (typeof node !== "object" || node === null || Array.isArray(node)) {
303
+ return null;
304
+ }
305
+ const n = node;
306
+ if (typeof n.id !== "number" || !Number.isInteger(n.id) || n.id <= 0) {
307
+ return null;
308
+ }
309
+ if (n.name !== null && typeof n.name !== "string") {
310
+ return null;
311
+ }
312
+ chain.push({
313
+ id: n.id,
314
+ name: n.name
315
+ });
316
+ }
317
+ return {
318
+ code: EXPORT_CONE_UNAVAILABLE,
319
+ statusCode: 422,
320
+ state: obj.state,
321
+ chain,
322
+ message: obj.message
323
+ };
324
+ }
325
+
326
+ // src/emit.ts
327
+ function rewriteImportSites(fragment) {
328
+ if (!fragment.importSites || fragment.importSites.length === 0) {
329
+ return fragment.tsxSource;
330
+ }
331
+ const sortedSites = [...fragment.importSites].sort((a, b) => b.start - a.start);
332
+ let tsx = fragment.tsxSource;
333
+ for (const site of sortedSites) {
334
+ tsx = tsx.slice(0, site.start + 1) + `../${site.fragmentId}/fragment` + tsx.slice(site.end - 1);
335
+ }
336
+ return tsx;
337
+ }
338
+ function versionedScopeClass(scopeClass, version) {
339
+ return `${scopeClass}-v${version}`;
340
+ }
341
+ function versionScopeCss(css, scopeClass, version) {
342
+ const vClass = versionedScopeClass(scopeClass, version);
343
+ const escaped = scopeClass.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
344
+ const replacePattern = new RegExp(
345
+ `(?<![A-Za-z0-9_-])${escaped}(?!-v${version}(?![A-Za-z0-9_]))(?![A-Za-z0-9_])`,
346
+ "g"
347
+ );
348
+ const result = css.replace(replacePattern, vClass);
349
+ const unversionedPattern = new RegExp(
350
+ `(?<![A-Za-z0-9_-])${escaped}(?!-v${version})(?![A-Za-z0-9_])`
351
+ );
352
+ if (unversionedPattern.test(result)) {
353
+ throw new Error(`\u0412 CSS \u043E\u0431\u043D\u0430\u0440\u0443\u0436\u0435\u043D\u043E \u043D\u0435\u0432\u044B\u0432\u0435\u0434\u0435\u043D\u043D\u043E\u0435 \u0432\u0445\u043E\u0436\u0434\u0435\u043D\u0438\u0435 \u043A\u043B\u0430\u0441\u0441\u0430 ${scopeClass}`);
354
+ }
355
+ return result;
356
+ }
357
+ function rootCone(bundle, rootId) {
358
+ const byId = /* @__PURE__ */ new Map();
359
+ for (const f of bundle.fragments) {
360
+ byId.set(f.id, f);
361
+ }
362
+ if (!byId.has(rootId)) {
363
+ throw new Error(`\u041A\u043E\u0440\u0435\u043D\u044C ${rootId} \u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D \u0432 \u043F\u0430\u043A\u0435\u0442\u0435 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438`);
364
+ }
365
+ const visited = /* @__PURE__ */ new Set([rootId]);
366
+ const queue = [rootId];
367
+ while (queue.length > 0) {
368
+ const currentId = queue.shift();
369
+ const fragment = byId.get(currentId);
370
+ if (!fragment) continue;
371
+ for (const importId of fragment.imports) {
372
+ if (!visited.has(importId)) {
373
+ visited.add(importId);
374
+ queue.push(importId);
375
+ }
376
+ }
377
+ }
378
+ const otherIds = Array.from(visited).filter((id) => id !== rootId).sort((a, b) => a - b);
379
+ return [rootId, ...otherIds];
380
+ }
381
+ function emitRootIndex(bundle, rootId) {
382
+ const rootFrag = bundle.fragments.find((f) => f.id === rootId);
383
+ if (!rootFrag) {
384
+ throw new Error(`\u041A\u043E\u0440\u0435\u043D\u044C ${rootId} \u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D \u0432 \u043F\u0430\u043A\u0435\u0442\u0435 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438`);
385
+ }
386
+ const header = fragmentFileHeader(
387
+ rootFrag.id,
388
+ rootFrag.name,
389
+ rootFrag.version
390
+ );
391
+ const coneIds = rootCone(bundle, rootId);
392
+ const byId = new Map(bundle.fragments.map((f) => [f.id, f]));
393
+ const cssImports = [];
394
+ const scopeClasses = [];
395
+ for (const id of coneIds) {
396
+ const frag = byId.get(id);
397
+ if (!frag) continue;
398
+ scopeClasses.push(versionedScopeClass(frag.scopeClass, frag.version));
399
+ if (id === rootId) {
400
+ cssImports.push('import "./styles.css";');
401
+ } else {
402
+ cssImports.push(`import "../${id}/styles.css";`);
403
+ }
404
+ }
405
+ const wrapper = `<StandaloneFragmentProvider projectProperties={projectProperties} sources={sources}>
406
+ <div className="${scopeClasses.join(" ")}" style={{ display: "contents" }}>
407
+ <__Root {...${rootFrag.schema ? "resolveRootProps(__schema, props)" : "props"}} />
408
+ </div>
409
+ </StandaloneFragmentProvider>`;
410
+ if (!rootFrag.schema) {
411
+ return `${header}import type { ComponentProps } from "react";
412
+ import { StandaloneFragmentProvider } from "@fragmentsx/runtime-react";
413
+ import __Root from "./fragment";
414
+ import { projectProperties, sources } from "../fragments.gen";
415
+ ${cssImports.join("\n")}
416
+
417
+ export default function FragmentRoot(props: ComponentProps<typeof __Root>) {
418
+ return (
419
+ ${wrapper}
420
+ );
421
+ }
422
+ `;
423
+ }
424
+ return `${header}import { StandaloneFragmentProvider, resolveRootProps } from "@fragmentsx/runtime-react";
425
+ import type { ConsumerPropsFromSchema } from "@fragmentsx/runtime-react";
426
+ import __Root, { schema as __schema } from "./fragment";
427
+ import { projectProperties, sources } from "../fragments.gen";
428
+ ${cssImports.join("\n")}
429
+
430
+ export default function FragmentRoot(props: ConsumerPropsFromSchema<typeof __schema>) {
431
+ return (
432
+ ${wrapper}
433
+ );
434
+ }
435
+ `;
436
+ }
437
+ function deepSortKeys(value) {
438
+ if (value === null || typeof value !== "object") {
439
+ return value;
440
+ }
441
+ if (Array.isArray(value)) {
442
+ return value.map(deepSortKeys);
443
+ }
444
+ const obj = value;
445
+ const sorted = {};
446
+ const keys = Object.keys(obj).sort();
447
+ for (const key of keys) {
448
+ sorted[key] = deepSortKeys(obj[key]);
449
+ }
450
+ return sorted;
451
+ }
452
+ function emitGenFile(bundle) {
453
+ const header = genFileHeader(bundle.fragments);
454
+ const sortedProps = deepSortKeys(bundle.projectProperties);
455
+ const sortedSources = deepSortKeys(bundle.sources);
456
+ return `${header}export const projectProperties = ${JSON.stringify(sortedProps, null, 2)};
457
+
458
+ export const sources = ${JSON.stringify(sortedSources, null, 2)};
459
+ `;
460
+ }
461
+ function emitTypesFile(bundle) {
462
+ const header = typesFileHeader(bundle.fragments);
463
+ const typesContent = bundle.types && bundle.types.trim().length > 0 ? bundle.types : "export {};\n";
464
+ const body = typesContent.endsWith("\n") ? typesContent : `${typesContent}
465
+ `;
466
+ return `${header}
467
+ ${body}`;
468
+ }
469
+ function planFragmentFiles(bundle) {
470
+ const files = /* @__PURE__ */ new Map();
471
+ const sortedFragments = [...bundle.fragments].sort((a, b) => a.id - b.id);
472
+ for (const fragment of sortedFragments) {
473
+ const rewrittenTsx = rewriteImportSites(fragment);
474
+ files.set(
475
+ `${fragment.id}/fragment.tsx`,
476
+ fragmentFileHeader(fragment.id, fragment.name, fragment.version) + rewrittenTsx
477
+ );
478
+ const scopedCss = versionScopeCss(
479
+ fragment.css,
480
+ fragment.scopeClass,
481
+ fragment.version
482
+ );
483
+ files.set(
484
+ `${fragment.id}/styles.css`,
485
+ fragmentStylesHeader(fragment.id, fragment.name, fragment.version) + scopedCss
486
+ );
487
+ }
488
+ const sortedRoots = Array.from(new Set(bundle.roots)).sort((a, b) => a - b);
489
+ for (const rootId of sortedRoots) {
490
+ const rootFrag = bundle.fragments.find((f) => f.id === rootId);
491
+ if (rootFrag) {
492
+ files.set(`${rootId}/index.tsx`, emitRootIndex(bundle, rootId));
493
+ }
494
+ }
495
+ files.set("fragments.gen.ts", emitGenFile(bundle));
496
+ files.set("fragments.d.ts", emitTypesFile(bundle));
497
+ return files;
498
+ }
499
+
500
+ // src/fetch-bundle.ts
501
+ var STATE_REASONS = {
502
+ never_published: "\u043D\u0435 \u043E\u043F\u0443\u0431\u043B\u0438\u043A\u043E\u0432\u0430\u043D",
503
+ deleted: "\u0443\u0434\u0430\u043B\u0451\u043D",
504
+ not_found: "\u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D \u0432 \u043F\u0440\u043E\u0435\u043A\u0442\u0435 \u043A\u043B\u044E\u0447\u0430",
505
+ not_exportable: "\u043D\u0435 \u043C\u043E\u0436\u0435\u0442 \u0431\u044B\u0442\u044C \u044D\u043A\u0441\u043F\u043E\u0440\u0442\u0438\u0440\u043E\u0432\u0430\u043D",
506
+ artifact_missing: "\u0430\u0440\u0442\u0435\u0444\u0430\u043A\u0442 \u043E\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442",
507
+ too_large: "\u043A\u043E\u043D\u0443\u0441 \u0431\u043E\u043B\u044C\u0448\u0435 50 \u0444\u0440\u0430\u0433\u043C\u0435\u043D\u0442\u043E\u0432"
508
+ };
509
+ var label = (node) => node.name !== null ? `${node.id} \xAB${sanitizeFragmentName(node.name)}\xBB` : `${node.id}`;
510
+ function formatConeError(error) {
511
+ const lines = [];
512
+ if (error.state === "too_large") {
513
+ lines.push("\u0412\u044B\u0433\u0440\u0443\u0437\u043A\u0430 \u043E\u0441\u0442\u0430\u043D\u043E\u0432\u043B\u0435\u043D\u0430: \u043A\u043E\u043D\u0443\u0441 \u0431\u043E\u043B\u044C\u0448\u0435 50 \u0444\u0440\u0430\u0433\u043C\u0435\u043D\u0442\u043E\u0432");
514
+ return lines;
515
+ }
516
+ const target = error.chain[error.chain.length - 1];
517
+ if (!target) {
518
+ lines.push(
519
+ `\u0412\u044B\u0433\u0440\u0443\u0437\u043A\u0430 \u043E\u0441\u0442\u0430\u043D\u043E\u0432\u043B\u0435\u043D\u0430: ${STATE_REASONS[error.state] ?? error.message}`
520
+ );
521
+ return lines;
522
+ }
523
+ const reason = STATE_REASONS[error.state] ?? "\u043D\u0435\u0434\u043E\u0441\u0442\u0443\u043F\u0435\u043D \u0434\u043B\u044F \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438";
524
+ if (error.state === "not_found" || target.name === null) {
525
+ lines.push(`\u0412\u044B\u0433\u0440\u0443\u0437\u043A\u0430 \u043E\u0441\u0442\u0430\u043D\u043E\u0432\u043B\u0435\u043D\u0430: \u0444\u0440\u0430\u0433\u043C\u0435\u043D\u0442 ${target.id} ${reason}`);
526
+ } else {
527
+ lines.push(`\u0412\u044B\u0433\u0440\u0443\u0437\u043A\u0430 \u043E\u0441\u0442\u0430\u043D\u043E\u0432\u043B\u0435\u043D\u0430: \u0444\u0440\u0430\u0433\u043C\u0435\u043D\u0442 ${label(target)} ${reason}`);
528
+ }
529
+ if (error.chain.length > 0) {
530
+ const chainStr = error.chain.map(label).join(" \u2192 ");
531
+ lines.push(`\u0426\u0435\u043F\u043E\u0447\u043A\u0430: ${chainStr}`);
532
+ }
533
+ return lines;
534
+ }
535
+ async function fetchBundle(config, options = {}) {
536
+ const log = options.log ?? console;
537
+ const fetchImpl = options.fetchImpl ?? fetch;
538
+ const baseUrl = config.apiBaseUrl ?? config.apiOrigin ?? "";
539
+ const queryParams = new URLSearchParams({
540
+ fragments: config.fragments.join(",")
541
+ });
542
+ const baseWithSlash = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
543
+ const url = new URL(`export/bundle?${queryParams.toString()}`, baseWithSlash);
544
+ let response;
545
+ try {
546
+ response = await fetchImpl(url.href, {
547
+ method: "GET",
548
+ headers: {
549
+ Authorization: `Bearer ${config.apiKey}`,
550
+ Accept: "application/json"
551
+ },
552
+ signal: AbortSignal.timeout(3e4)
553
+ });
554
+ } catch (err) {
555
+ if (err instanceof Error) {
556
+ if (err.name === "TimeoutError" || err.name === "AbortError") {
557
+ log.error("\u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043D\u0435 \u043E\u0442\u0432\u0435\u0442\u0438\u043B \u0437\u0430 30 \u0441");
558
+ return null;
559
+ }
560
+ const cause = err.cause;
561
+ if (cause && typeof cause.code === "string") {
562
+ log.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0441\u0435\u0442\u0438 (${err.name}): ${cause.code}`);
563
+ } else {
564
+ log.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u0441\u0435\u0442\u0438: ${err.name}`);
565
+ }
566
+ } else {
567
+ log.error("\u041E\u0448\u0438\u0431\u043A\u0430 \u0441\u0435\u0442\u0438");
568
+ }
569
+ return null;
570
+ }
571
+ if (response.status === 401) {
572
+ log.error("\u041A\u043B\u044E\u0447 \u043D\u0435 \u043F\u0440\u0438\u043D\u044F\u0442 (401)");
573
+ return null;
574
+ }
575
+ if (response.status === 404) {
576
+ log.error(
577
+ "\u042D\u043D\u0434\u043F\u043E\u0438\u043D\u0442 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043D\u0435\u0434\u043E\u0441\u0442\u0443\u043F\u0435\u043D (404): \u043F\u0440\u043E\u0432\u0435\u0440\u044C\u0442\u0435 FRAGMENTS_API_URL \u0438 \u0432\u043A\u043B\u044E\u0447\u0451\u043D \u043B\u0438 \u044D\u043D\u0434\u043F\u043E\u0438\u043D\u0442 \u043D\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435"
578
+ );
579
+ return null;
580
+ }
581
+ if (response.status === 429) {
582
+ log.error("\u041F\u0440\u0435\u0432\u044B\u0448\u0435\u043D \u043B\u0438\u043C\u0438\u0442 \u0437\u0430\u043F\u0440\u043E\u0441\u043E\u0432 (429), \u043F\u043E\u0432\u0442\u043E\u0440\u0438\u0442\u0435 \u043F\u043E\u0437\u0436\u0435");
583
+ return null;
584
+ }
585
+ if (response.status === 400) {
586
+ let reason = "";
587
+ try {
588
+ const body = await response.json();
589
+ if (typeof body?.message === "string") {
590
+ reason = sanitizeServerMessage(body.message);
591
+ }
592
+ } catch {
593
+ }
594
+ log.error(
595
+ reason ? `\u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043E\u0442\u043A\u043B\u043E\u043D\u0438\u043B \u0437\u0430\u043F\u0440\u043E\u0441 (400): ${reason}` : "\u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u043E\u0442\u043A\u043B\u043E\u043D\u0438\u043B \u0437\u0430\u043F\u0440\u043E\u0441 (400)"
596
+ );
597
+ return null;
598
+ }
599
+ if (response.status === 422) {
600
+ let json;
601
+ try {
602
+ json = await response.json();
603
+ } catch {
604
+ log.error("\u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u0432\u0435\u0440\u043D\u0443\u043B \u0441\u0442\u0430\u0442\u0443\u0441 422");
605
+ return null;
606
+ }
607
+ const coneError = parseExportConeError(json);
608
+ if (!coneError) {
609
+ log.error("\u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u0432\u0435\u0440\u043D\u0443\u043B \u0441\u0442\u0430\u0442\u0443\u0441 422");
610
+ return null;
611
+ }
612
+ const lines = formatConeError(coneError);
613
+ for (const line of lines) {
614
+ log.error(line);
615
+ }
616
+ return null;
617
+ }
618
+ if (!response.ok) {
619
+ log.error(`\u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438 \u0432\u0435\u0440\u043D\u0443\u043B \u0441\u0442\u0430\u0442\u0443\u0441 ${response.status}`);
620
+ return null;
621
+ }
622
+ try {
623
+ return await response.json();
624
+ } catch {
625
+ log.error("\u041D\u0435 \u0443\u0434\u0430\u043B\u043E\u0441\u044C \u0440\u0430\u0437\u043E\u0431\u0440\u0430\u0442\u044C JSON \u043E\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432\u044B\u0433\u0440\u0443\u0437\u043A\u0438");
626
+ return null;
627
+ }
628
+ }
629
+
630
+ // src/plan.ts
631
+ function sortedUnique(ids) {
632
+ return Array.from(new Set(ids)).sort((a, b) => a - b);
633
+ }
634
+ async function buildPlan(options) {
635
+ const log = options.log ?? console;
636
+ let config;
637
+ try {
638
+ config = readPullConfig(options.cwd, options.env);
639
+ } catch (err) {
640
+ log.error(err.message);
641
+ return null;
642
+ }
643
+ const json = await fetchBundle(config, {
644
+ fetchImpl: options.fetchImpl,
645
+ log
646
+ });
647
+ if (json === null) {
648
+ return null;
649
+ }
650
+ let bundle;
651
+ try {
652
+ bundle = parseExportBundle(json);
653
+ } catch (err) {
654
+ log.error(err.message);
655
+ return null;
656
+ }
657
+ if (bundle.project.id !== config.project) {
658
+ log.error(
659
+ `\u041F\u0440\u043E\u0435\u043A\u0442 \u043F\u0430\u043A\u0435\u0442\u0430 (${bundle.project.id}) \u043D\u0435 \u0441\u043E\u0432\u043F\u0430\u0434\u0430\u0435\u0442 \u0441 \u043F\u0440\u043E\u0435\u043A\u0442\u043E\u043C \u043A\u043E\u043D\u0444\u0438\u0433\u0430 (${config.project})`
660
+ );
661
+ return null;
662
+ }
663
+ const wantRoots = sortedUnique(config.fragments);
664
+ const gotRoots = sortedUnique(bundle.roots);
665
+ if (wantRoots.length !== gotRoots.length || wantRoots.some((id, i) => gotRoots[i] !== id)) {
666
+ log.error(
667
+ `\u041A\u043E\u0440\u043D\u0438 \u043F\u0430\u043A\u0435\u0442\u0430 (${gotRoots.join(", ")}) \u043D\u0435 \u0441\u043E\u0432\u043F\u0430\u0434\u0430\u044E\u0442 \u0441 \u043A\u043E\u0440\u043D\u044F\u043C\u0438 \u043A\u043E\u043D\u0444\u0438\u0433\u0430 (${wantRoots.join(", ")})`
668
+ );
669
+ return null;
670
+ }
671
+ let files;
672
+ try {
673
+ files = planFragmentFiles(bundle);
674
+ } catch (err) {
675
+ log.error(err.message);
676
+ return null;
677
+ }
678
+ return { config, bundle, files };
679
+ }
680
+
681
+ // src/write.ts
682
+ import crypto from "crypto";
683
+ import fs2 from "fs";
684
+ import path2 from "path";
685
+ var SEGMENT_PATTERN = /^[A-Za-z0-9._-]+$/;
686
+ var PlanReplaceError = class extends Error {
687
+ code;
688
+ failedPath;
689
+ replaced;
690
+ constructor(cause, failedPath, replaced) {
691
+ super("\u0421\u0431\u043E\u0439 \u043F\u0440\u0438 \u0437\u0430\u043C\u0435\u043D\u0435 \u0444\u0430\u0439\u043B\u043E\u0432");
692
+ this.name = "PlanReplaceError";
693
+ this.code = cause?.code;
694
+ this.failedPath = failedPath;
695
+ this.replaced = replaced;
696
+ }
697
+ };
698
+ var PlanConflictError = class extends Error {
699
+ paths;
700
+ constructor(paths) {
701
+ super(`\u041A\u043E\u043D\u0444\u043B\u0438\u043A\u0442 \u043F\u043B\u0430\u043D\u0430: ${paths.join(", ")}`);
702
+ this.name = "PlanConflictError";
703
+ this.paths = paths;
704
+ }
705
+ };
706
+ async function applyPlan(outDir, files, state) {
707
+ try {
708
+ const rootStat = await fs2.promises.lstat(outDir);
709
+ if (rootStat.isSymbolicLink()) {
710
+ throw new Error("\u043A\u0430\u0442\u0430\u043B\u043E\u0433 out \u043D\u0435 \u0434\u043E\u043B\u0436\u0435\u043D \u0431\u044B\u0442\u044C \u0441\u0438\u043C\u0432\u043E\u043B\u0438\u0447\u0435\u0441\u043A\u043E\u0439 \u0441\u0441\u044B\u043B\u043A\u043E\u0439");
711
+ }
712
+ } catch (err) {
713
+ if (err.code !== "ENOENT") {
714
+ throw err;
715
+ }
716
+ }
717
+ for (const [filePath] of files) {
718
+ if (path2.isAbsolute(filePath)) {
719
+ throw new Error(`\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u044B\u0439 \u0430\u0431\u0441\u043E\u043B\u044E\u0442\u043D\u044B\u0439 \u043F\u0443\u0442\u044C: ${filePath}`);
720
+ }
721
+ const segments = filePath.split(/[/\\]/);
722
+ for (const segment of segments) {
723
+ if (segment === "" || segment === "." || segment === ".." || !SEGMENT_PATTERN.test(segment)) {
724
+ throw new Error(`\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u044B\u0439 \u0441\u0435\u0433\u043C\u0435\u043D\u0442 \u043F\u0443\u0442\u0438: ${segment}`);
725
+ }
726
+ }
727
+ const fullPath = path2.resolve(outDir, filePath);
728
+ const rel = path2.relative(outDir, fullPath);
729
+ if (rel.startsWith("..") || path2.isAbsolute(rel)) {
730
+ throw new Error(`\u041F\u0443\u0442\u044C \u0432\u044B\u0445\u043E\u0434\u0438\u0442 \u0437\u0430 \u043F\u0440\u0435\u0434\u0435\u043B\u044B outDir: ${filePath}`);
731
+ }
732
+ }
733
+ const conflicts = [];
734
+ for (const [filePath] of files) {
735
+ const segments = filePath.split("/");
736
+ let prefixConflict = false;
737
+ let currentPrefix = "";
738
+ for (let i = 0; i < segments.length - 1; i++) {
739
+ currentPrefix = currentPrefix ? `${currentPrefix}/${segments[i]}` : segments[i];
740
+ const prefixKind = state.get(currentPrefix);
741
+ if (prefixKind !== void 0 && prefixKind !== "dir") {
742
+ conflicts.push(filePath);
743
+ prefixConflict = true;
744
+ break;
745
+ }
746
+ }
747
+ if (prefixConflict) {
748
+ continue;
749
+ }
750
+ const kind = state.get(filePath);
751
+ if (kind === "foreign" || kind === "symlink" || kind === "dir") {
752
+ conflicts.push(filePath);
753
+ }
754
+ }
755
+ if (conflicts.length > 0) {
756
+ conflicts.sort();
757
+ throw new PlanConflictError(conflicts);
758
+ }
759
+ const stagedFiles = [];
760
+ try {
761
+ for (const [filePath, content] of files) {
762
+ const fullPath = path2.resolve(outDir, filePath);
763
+ const parentDir = path2.dirname(fullPath);
764
+ await fs2.promises.mkdir(parentDir, { recursive: true });
765
+ const fileName = path2.basename(fullPath);
766
+ const randHex = crypto.randomBytes(4).toString("hex");
767
+ const tmpName = `${fileName}.fragments-tmp-${process.pid}-${randHex}`;
768
+ const tmpPath = path2.join(parentDir, tmpName);
769
+ await fs2.promises.writeFile(tmpPath, content, { encoding: "utf-8" });
770
+ stagedFiles.push({ tmpPath, dstPath: fullPath });
771
+ }
772
+ } catch (err) {
773
+ for (const { tmpPath } of stagedFiles) {
774
+ try {
775
+ await fs2.promises.unlink(tmpPath);
776
+ } catch {
777
+ }
778
+ }
779
+ throw err;
780
+ }
781
+ const replaced = [];
782
+ for (const { tmpPath, dstPath } of stagedFiles) {
783
+ const relDst = path2.relative(outDir, dstPath).split(path2.sep).join("/");
784
+ try {
785
+ await fs2.promises.rename(tmpPath, dstPath);
786
+ replaced.push(relDst);
787
+ } catch (err) {
788
+ for (const staged of stagedFiles) {
789
+ try {
790
+ await fs2.promises.unlink(staged.tmpPath);
791
+ } catch {
792
+ }
793
+ }
794
+ throw new PlanReplaceError(err, relDst, replaced);
795
+ }
796
+ }
797
+ const removed = [];
798
+ for (const [diskPath, kind] of state) {
799
+ if (kind === "own" && !files.has(diskPath) && isGeneratedPath(diskPath)) {
800
+ const fullPath = path2.resolve(outDir, diskPath);
801
+ try {
802
+ await fs2.promises.unlink(fullPath);
803
+ removed.push(diskPath);
804
+ } catch (err) {
805
+ if (err.code !== "ENOENT") {
806
+ throw err;
807
+ }
808
+ }
809
+ }
810
+ }
811
+ const candidateDirs = /* @__PURE__ */ new Set();
812
+ for (const removedPath of removed) {
813
+ const parts = removedPath.split("/");
814
+ let cur = "";
815
+ for (let i = 0; i < parts.length - 1; i++) {
816
+ cur = cur ? `${cur}/${parts[i]}` : parts[i];
817
+ candidateDirs.add(cur);
818
+ }
819
+ }
820
+ for (const [diskPath, kind] of state) {
821
+ if (kind === "dir" && isFragmentDirName(diskPath)) {
822
+ const hasPlanFiles = Array.from(files.keys()).some(
823
+ (f) => f === diskPath || f.startsWith(`${diskPath}/`)
824
+ );
825
+ if (!hasPlanFiles) {
826
+ candidateDirs.add(diskPath);
827
+ }
828
+ }
829
+ }
830
+ const sortedDirs = Array.from(candidateDirs).sort((a, b) => {
831
+ const depthDiff = b.split("/").length - a.split("/").length;
832
+ return depthDiff !== 0 ? depthDiff : b.localeCompare(a);
833
+ });
834
+ for (const dirRel of sortedDirs) {
835
+ const dirFull = path2.resolve(outDir, dirRel);
836
+ try {
837
+ await fs2.promises.rmdir(dirFull);
838
+ } catch {
839
+ }
840
+ }
841
+ removed.sort();
842
+ return {
843
+ written: files.size,
844
+ removed
845
+ };
846
+ }
847
+
848
+ // src/pull.ts
849
+ function formatWriteError(name, code, failedPath) {
850
+ const parts = [`\u041E\u0448\u0438\u0431\u043A\u0430 \u0437\u0430\u043F\u0438\u0441\u0438 \u0444\u0430\u0439\u043B\u043E\u0432: ${name}`];
851
+ if (code) {
852
+ parts.push(code);
853
+ }
854
+ if (failedPath) {
855
+ parts.push(failedPath);
856
+ }
857
+ return parts.join(", ");
858
+ }
859
+ async function runPull(options) {
860
+ const log = options.log ?? console;
861
+ const plan = await buildPlan(options);
862
+ if (!plan) {
863
+ return 2;
864
+ }
865
+ let state;
866
+ try {
867
+ assertOutInsideCwd(options.cwd, plan.config.outDir);
868
+ state = await readOutState(plan.config.outDir);
869
+ } catch (err) {
870
+ log.error(err.message);
871
+ return 2;
872
+ }
873
+ let result;
874
+ try {
875
+ result = await applyPlan(plan.config.outDir, plan.files, state);
876
+ } catch (err) {
877
+ if (err instanceof PlanConflictError) {
878
+ for (const p of err.paths) {
879
+ log.error(`\u041F\u0443\u0442\u044C \u0437\u0430\u043D\u044F\u0442 \u0444\u0430\u0439\u043B\u043E\u043C, \u043A\u043E\u0442\u043E\u0440\u044B\u0439 fragments pull \u043D\u0435 \u0441\u043E\u0437\u0434\u0430\u0432\u0430\u043B: ${p}`);
880
+ }
881
+ return 2;
882
+ }
883
+ if (err instanceof PlanReplaceError) {
884
+ log.error(formatWriteError("Error", err.code, err.failedPath));
885
+ if (err.replaced.length > 0) {
886
+ log.error(
887
+ "\u0427\u0430\u0441\u0442\u044C \u0444\u0430\u0439\u043B\u043E\u0432 \u0443\u0436\u0435 \u0437\u0430\u043C\u0435\u043D\u0435\u043D\u0430, out \u0441\u043E\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u043C\u0435\u0441\u044C \u0432\u0435\u0440\u0441\u0438\u0439; \u043F\u043E\u0441\u043B\u0435 \u0443\u0441\u0442\u0440\u0430\u043D\u0435\u043D\u0438\u044F \u043F\u0440\u0438\u0447\u0438\u043D\u044B \u0437\u0430\u043F\u0443\u0441\u0442\u0438\u0442\u0435 fragments pull \u0435\u0449\u0451 \u0440\u0430\u0437. \u0417\u0430\u043C\u0435\u043D\u0435\u043D\u044B:"
888
+ );
889
+ for (const p of err.replaced) {
890
+ log.error(` ${p}`);
891
+ }
892
+ }
893
+ return 2;
894
+ }
895
+ const e = err;
896
+ const failed = e?.dest ?? e?.path;
897
+ log.error(
898
+ formatWriteError(
899
+ e?.name || "Error",
900
+ e?.code,
901
+ failed ? path3.relative(plan.config.outDir, failed) : void 0
902
+ )
903
+ );
904
+ return 2;
905
+ }
906
+ log.log(`\u0423\u0441\u043F\u0435\u0448\u043D\u043E \u0432\u044B\u0433\u0440\u0443\u0436\u0435\u043D\u043E \u0444\u0440\u0430\u0433\u043C\u0435\u043D\u0442\u043E\u0432: ${plan.bundle.fragments.length}`);
907
+ if (result.removed.length > 0) {
908
+ for (const removedPath of result.removed) {
909
+ log.log(`\u0443\u0434\u0430\u043B\u0451\u043D ${removedPath}`);
910
+ }
911
+ log.log(`\u0423\u0434\u0430\u043B\u0435\u043D\u043E \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0445 \u0444\u0430\u0439\u043B\u043E\u0432: ${result.removed.length}`);
912
+ }
913
+ return 0;
914
+ }
915
+
916
+ // src/check.ts
917
+ import fs3 from "fs";
918
+ import path4 from "path";
919
+ var STATUS_NAMES = {
920
+ changed: "\u0438\u0437\u043C\u0435\u043D\u0451\u043D",
921
+ missing: "\u043E\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442",
922
+ extra: "\u043B\u0438\u0448\u043D\u0438\u0439",
923
+ conflict: "\u0437\u0430\u043D\u044F\u0442 \u0447\u0443\u0436\u0438\u043C \u0444\u0430\u0439\u043B\u043E\u043C"
924
+ };
925
+ async function compareWithDisk(outDir, files, state) {
926
+ const result = [];
927
+ for (const [filePath, content] of files) {
928
+ const segments = filePath.split("/");
929
+ let prefixConflict = false;
930
+ let currentPrefix = "";
931
+ for (let i = 0; i < segments.length - 1; i++) {
932
+ currentPrefix = currentPrefix ? `${currentPrefix}/${segments[i]}` : segments[i];
933
+ const prefixKind = state.get(currentPrefix);
934
+ if (prefixKind !== void 0 && prefixKind !== "dir") {
935
+ result.push({ path: filePath, status: "conflict" });
936
+ prefixConflict = true;
937
+ break;
938
+ }
939
+ }
940
+ if (prefixConflict) {
941
+ continue;
942
+ }
943
+ const kind = state.get(filePath);
944
+ if (kind === void 0) {
945
+ result.push({ path: filePath, status: "missing" });
946
+ } else if (kind === "foreign" || kind === "symlink" || kind === "dir") {
947
+ result.push({ path: filePath, status: "conflict" });
948
+ } else if (kind === "own") {
949
+ const fullPath = path4.resolve(outDir, filePath);
950
+ try {
951
+ const diskBytes = await fs3.promises.readFile(fullPath);
952
+ const expectedBytes = Buffer.from(content, "utf8");
953
+ if (Buffer.compare(diskBytes, expectedBytes) !== 0) {
954
+ result.push({ path: filePath, status: "changed" });
955
+ }
956
+ } catch {
957
+ result.push({ path: filePath, status: "changed" });
958
+ }
959
+ }
960
+ }
961
+ for (const [diskPath, kind] of state) {
962
+ if (kind === "own" && !files.has(diskPath)) {
963
+ result.push({ path: diskPath, status: "extra" });
964
+ }
965
+ }
966
+ result.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
967
+ return result;
968
+ }
969
+ async function executeCheck(options) {
970
+ const log = options.log ?? console;
971
+ const plan = await buildPlan(options);
972
+ if (!plan) {
973
+ return 2;
974
+ }
975
+ let entries;
976
+ try {
977
+ const state = await readOutState(plan.config.outDir);
978
+ entries = await compareWithDisk(plan.config.outDir, plan.files, state);
979
+ } catch (err) {
980
+ log.error(err.message);
981
+ return 2;
982
+ }
983
+ if (entries.length === 0) {
984
+ log.log("\u0424\u0430\u0439\u043B\u044B \u0441\u043E\u0432\u043F\u0430\u0434\u0430\u044E\u0442 \u0441 \u043E\u043F\u0443\u0431\u043B\u0438\u043A\u043E\u0432\u0430\u043D\u043D\u044B\u043C");
985
+ return 0;
986
+ }
987
+ const hasConflict = entries.some((e) => e.status === "conflict");
988
+ if (hasConflict) {
989
+ for (const entry of entries) {
990
+ log.error(`${STATUS_NAMES[entry.status]} ${entry.path}`);
991
+ }
992
+ log.error("\u041F\u0443\u0442\u044C \u043F\u043B\u0430\u043D\u0430 \u0437\u0430\u043D\u044F\u0442 \u0447\u0443\u0436\u0438\u043C \u0444\u0430\u0439\u043B\u043E\u043C");
993
+ return 2;
994
+ }
995
+ for (const entry of entries) {
996
+ log.log(`${STATUS_NAMES[entry.status]} ${entry.path}`);
997
+ }
998
+ log.log(`\u0420\u0430\u0441\u0445\u043E\u0436\u0434\u0435\u043D\u0438\u0439: ${entries.length} \u2014 \u0437\u0430\u043F\u0443\u0441\u0442\u0438\u0442\u0435 fragments pull`);
999
+ return 1;
1000
+ }
1001
+ function runCheck(options) {
1002
+ return executeCheck(options);
1003
+ }
1004
+
1005
+ // src/bin.ts
1006
+ var COMMANDS = {
1007
+ pull: runPull,
1008
+ check: runCheck
1009
+ };
1010
+ async function main() {
1011
+ const command = process.argv[2];
1012
+ const handler = command ? COMMANDS[command] : void 0;
1013
+ if (handler) {
1014
+ try {
1015
+ const exitCode = await handler({
1016
+ cwd: process.cwd(),
1017
+ env: process.env
1018
+ });
1019
+ process.exitCode = exitCode;
1020
+ } catch (err) {
1021
+ console.error(`\u041E\u0448\u0438\u0431\u043A\u0430 \u043A\u043E\u043C\u0430\u043D\u0434\u044B ${command}:`, err.message);
1022
+ process.exitCode = 2;
1023
+ }
1024
+ return;
1025
+ }
1026
+ console.error("\u0418\u0441\u043F\u043E\u043B\u044C\u0437\u043E\u0432\u0430\u043D\u0438\u0435: fragments pull | check");
1027
+ process.exitCode = 2;
1028
+ }
1029
+ main().catch((err) => {
1030
+ console.error("\u041D\u0435\u043E\u0436\u0438\u0434\u0430\u043D\u043D\u0430\u044F \u043E\u0448\u0438\u0431\u043A\u0430:", err);
1031
+ process.exitCode = 2;
1032
+ });