@platforma-sdk/block-tools 2.1.5
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 +82 -0
- package/bin/dev.cmd +3 -0
- package/bin/dev.js +6 -0
- package/bin/run.cmd +3 -0
- package/bin/run.js +7 -0
- package/dist/cli.cjs +786 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +58 -0
- package/dist/cli.d.ts +58 -0
- package/dist/cli.js +757 -0
- package/dist/cli.js.map +1 -0
- package/dist/lib.cjs +629 -0
- package/dist/lib.cjs.map +1 -0
- package/dist/lib.d.cts +2637 -0
- package/dist/lib.d.ts +2637 -0
- package/dist/lib.js +577 -0
- package/dist/lib.js.map +1 -0
- package/package.json +61 -0
package/dist/lib.cjs
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var zod = require('zod');
|
|
4
|
+
var plModelMiddleLayer = require('@milaboratories/pl-model-middle-layer');
|
|
5
|
+
require('yaml');
|
|
6
|
+
var fsp2 = require('fs/promises');
|
|
7
|
+
require('os');
|
|
8
|
+
var crypto = require('crypto');
|
|
9
|
+
var semver = require('semver/preload');
|
|
10
|
+
var pathPosix = require('path/posix');
|
|
11
|
+
var path = require('path');
|
|
12
|
+
var clientS3 = require('@aws-sdk/client-s3');
|
|
13
|
+
var fs = require('fs');
|
|
14
|
+
var mime = require('mime-types');
|
|
15
|
+
var tar = require('tar');
|
|
16
|
+
var tsHelpers = require('@milaboratories/ts-helpers');
|
|
17
|
+
|
|
18
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
|
+
|
|
20
|
+
function _interopNamespace(e) {
|
|
21
|
+
if (e && e.__esModule) return e;
|
|
22
|
+
var n = Object.create(null);
|
|
23
|
+
if (e) {
|
|
24
|
+
Object.keys(e).forEach(function (k) {
|
|
25
|
+
if (k !== 'default') {
|
|
26
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
27
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return e[k]; }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
n.default = e;
|
|
35
|
+
return Object.freeze(n);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var fsp2__default = /*#__PURE__*/_interopDefault(fsp2);
|
|
39
|
+
var semver__default = /*#__PURE__*/_interopDefault(semver);
|
|
40
|
+
var pathPosix__default = /*#__PURE__*/_interopDefault(pathPosix);
|
|
41
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
42
|
+
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
|
|
43
|
+
var mime__namespace = /*#__PURE__*/_interopNamespace(mime);
|
|
44
|
+
var tar__namespace = /*#__PURE__*/_interopNamespace(tar);
|
|
45
|
+
|
|
46
|
+
var __defProp = Object.defineProperty;
|
|
47
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
48
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
49
|
+
}) : x)(function(x) {
|
|
50
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
51
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
52
|
+
});
|
|
53
|
+
var __export = (target, all) => {
|
|
54
|
+
for (var name in all)
|
|
55
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/registry_v1/index.ts
|
|
59
|
+
var registry_v1_exports = {};
|
|
60
|
+
__export(registry_v1_exports, {
|
|
61
|
+
GlobalOverviewPath: () => GlobalOverviewPath,
|
|
62
|
+
MetaFile: () => MetaFile,
|
|
63
|
+
PlPackageConfigData: () => PlPackageConfigData,
|
|
64
|
+
PlPackageJsonConfigFile: () => PlPackageJsonConfigFile,
|
|
65
|
+
PlPackageYamlConfigFile: () => PlPackageYamlConfigFile,
|
|
66
|
+
PlRegCommonConfigData: () => PlRegCommonConfigData,
|
|
67
|
+
PlRegFullPackageConfigData: () => PlRegFullPackageConfigData,
|
|
68
|
+
PlRegPackageConfig: () => PlRegPackageConfig,
|
|
69
|
+
PlRegPackageConfigDataShard: () => PlRegPackageConfigDataShard,
|
|
70
|
+
packageContentPrefix: () => packageContentPrefix,
|
|
71
|
+
packageOverviewPath: () => packageOverviewPath,
|
|
72
|
+
payloadFilePath: () => payloadFilePath
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// src/registry_v1/v1_repo_schema.ts
|
|
76
|
+
var MainPrefix = "v1/";
|
|
77
|
+
function packageContentPrefix(bp) {
|
|
78
|
+
return `${MainPrefix}${bp.organization}/${bp.package}/${bp.version}`;
|
|
79
|
+
}
|
|
80
|
+
function payloadFilePath(bp, file) {
|
|
81
|
+
return `${MainPrefix}${bp.organization}/${bp.package}/${bp.version}/${file}`;
|
|
82
|
+
}
|
|
83
|
+
function packageOverviewPath(bp) {
|
|
84
|
+
return `${MainPrefix}${bp.organization}/${bp.package}/overview.json`;
|
|
85
|
+
}
|
|
86
|
+
var GlobalOverviewPath = `${MainPrefix}overview.json`;
|
|
87
|
+
var MetaFile = "meta.json";
|
|
88
|
+
var PlRegAddress = zod.z.string().regex(/^(?:s3:|file:)/);
|
|
89
|
+
var PlPackageConfigData = zod.z.object({
|
|
90
|
+
organization: zod.z.string(),
|
|
91
|
+
package: zod.z.string(),
|
|
92
|
+
version: plModelMiddleLayer.SemVer.optional(),
|
|
93
|
+
files: zod.z.record(zod.z.string().regex(/^[^\/]+$/), zod.z.string()).default({}),
|
|
94
|
+
meta: zod.z.object({}).passthrough()
|
|
95
|
+
});
|
|
96
|
+
var PlRegCommonConfigData = zod.z.object({
|
|
97
|
+
registries: zod.z.record(zod.z.string(), PlRegAddress).default({}),
|
|
98
|
+
registry: zod.z.string().optional()
|
|
99
|
+
});
|
|
100
|
+
var PlRegFullPackageConfigData = PlRegCommonConfigData.merge(PlPackageConfigData).required(
|
|
101
|
+
{ registry: true, version: true }
|
|
102
|
+
);
|
|
103
|
+
var PlRegPackageConfigDataShard = PlRegFullPackageConfigData.partial().required({
|
|
104
|
+
registries: true,
|
|
105
|
+
files: true
|
|
106
|
+
});
|
|
107
|
+
var PlPackageJsonConfigFile = "pl.package.json";
|
|
108
|
+
var PlPackageYamlConfigFile = "pl.package.yaml";
|
|
109
|
+
async function tryLoadFile(file, map) {
|
|
110
|
+
try {
|
|
111
|
+
return map(await fsp2__default.default.readFile(file));
|
|
112
|
+
} catch (err) {
|
|
113
|
+
if (err.code == "ENOENT") return void 0;
|
|
114
|
+
else throw new Error("", { cause: err });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
var VersionUpdatesPrefix = "_updates_v1/per_package_version/";
|
|
118
|
+
function packageUpdatePath(bp, seed) {
|
|
119
|
+
return `${VersionUpdatesPrefix}${bp.organization}/${bp.package}/${bp.version}/${seed}`;
|
|
120
|
+
}
|
|
121
|
+
var PackageUpdatePattern = /(?<packageKeyWithoutVersion>(?<organization>[^\/]+)\/(?<pkg>[^\/]+))\/(?<version>[^\/]+)\/(?<seed>[^\/]+)$/;
|
|
122
|
+
var GlobalUpdateSeedInFile = "_updates_v1/_global_update_in";
|
|
123
|
+
var GlobalUpdateSeedOutFile = "_updates_v1/_global_update_out";
|
|
124
|
+
var BlockRegistry = class {
|
|
125
|
+
constructor(storage, logger) {
|
|
126
|
+
this.storage = storage;
|
|
127
|
+
this.logger = logger;
|
|
128
|
+
}
|
|
129
|
+
constructNewPackage(pack) {
|
|
130
|
+
return new BlockRegistryPackConstructor(this.storage, pack);
|
|
131
|
+
}
|
|
132
|
+
async updateRegistry() {
|
|
133
|
+
this.logger?.info("Initiating registry refresh...");
|
|
134
|
+
const packagesToUpdate = /* @__PURE__ */ new Map();
|
|
135
|
+
const seedPaths = [];
|
|
136
|
+
const rawSeedPaths = await this.storage.listFiles(VersionUpdatesPrefix);
|
|
137
|
+
this.logger?.info("Packages to be updated:");
|
|
138
|
+
for (const seedPath of rawSeedPaths) {
|
|
139
|
+
const match = seedPath.match(PackageUpdatePattern);
|
|
140
|
+
if (!match) continue;
|
|
141
|
+
seedPaths.push(seedPath);
|
|
142
|
+
const { packageKeyWithoutVersion, organization, pkg, version, seed } = match.groups;
|
|
143
|
+
let update = packagesToUpdate.get(packageKeyWithoutVersion);
|
|
144
|
+
if (!update) {
|
|
145
|
+
packagesToUpdate.set(packageKeyWithoutVersion, {
|
|
146
|
+
package: { organization, package: pkg },
|
|
147
|
+
versions: /* @__PURE__ */ new Set([version])
|
|
148
|
+
});
|
|
149
|
+
} else if (!update.versions.has(version)) {
|
|
150
|
+
update.versions.add(version);
|
|
151
|
+
}
|
|
152
|
+
this.logger?.info(` - ${organization}:${pkg}:${version}`);
|
|
153
|
+
}
|
|
154
|
+
const overviewContent = await this.storage.getFile(GlobalOverviewPath);
|
|
155
|
+
let overview = overviewContent === void 0 ? [] : JSON.parse(overviewContent.toString());
|
|
156
|
+
this.logger?.info(`Global overview loaded, ${overview.length} records`);
|
|
157
|
+
for (const [, packageInfo] of packagesToUpdate.entries()) {
|
|
158
|
+
const overviewFile = packageOverviewPath(packageInfo.package);
|
|
159
|
+
const pOverviewContent = await this.storage.getFile(overviewFile);
|
|
160
|
+
let packageOverview = pOverviewContent === void 0 ? [] : JSON.parse(pOverviewContent.toString());
|
|
161
|
+
this.logger?.info(
|
|
162
|
+
`Updating ${packageInfo.package.organization}:${packageInfo.package.package} overview, ${packageOverview.length} records`
|
|
163
|
+
);
|
|
164
|
+
packageOverview = packageOverview.filter((e) => !packageInfo.versions.has(e.version));
|
|
165
|
+
for (const [v] of packageInfo.versions.entries()) {
|
|
166
|
+
const version = v.toString();
|
|
167
|
+
const metaContent = await this.storage.getFile(
|
|
168
|
+
payloadFilePath(
|
|
169
|
+
{
|
|
170
|
+
...packageInfo.package,
|
|
171
|
+
version
|
|
172
|
+
},
|
|
173
|
+
MetaFile
|
|
174
|
+
)
|
|
175
|
+
);
|
|
176
|
+
if (!metaContent) continue;
|
|
177
|
+
packageOverview.push({ version, meta: JSON.parse(metaContent.toString()) });
|
|
178
|
+
}
|
|
179
|
+
packageOverview.sort((e1, e2) => semver__default.default.compare(e2.version, e1.version));
|
|
180
|
+
await this.storage.putFile(overviewFile, Buffer.from(JSON.stringify(packageOverview)));
|
|
181
|
+
this.logger?.info(`Done (${packageOverview.length} records)`);
|
|
182
|
+
overview = overview.filter(
|
|
183
|
+
(e) => e.organization !== packageInfo.package.organization || e.package !== packageInfo.package.package
|
|
184
|
+
);
|
|
185
|
+
overview.push({
|
|
186
|
+
organization: packageInfo.package.organization,
|
|
187
|
+
package: packageInfo.package.package,
|
|
188
|
+
allVersions: packageOverview.map((e) => e.version).reverse(),
|
|
189
|
+
latestVersion: packageOverview[0].version,
|
|
190
|
+
latestMeta: packageOverview[0].meta
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
await this.storage.putFile(GlobalOverviewPath, Buffer.from(JSON.stringify(overview)));
|
|
194
|
+
this.logger?.info(`Global overview updated (${overview.length} records)`);
|
|
195
|
+
await this.storage.deleteFiles(...seedPaths.map((sp) => `${VersionUpdatesPrefix}${sp}`));
|
|
196
|
+
this.logger?.info(`Version update requests cleared`);
|
|
197
|
+
}
|
|
198
|
+
async updateIfNeeded(force = false) {
|
|
199
|
+
this.logger?.info(`Checking if registry requires refresh...`);
|
|
200
|
+
const updateRequestSeed = await this.storage.getFile(GlobalUpdateSeedInFile);
|
|
201
|
+
const currentUpdatedSeed = await this.storage.getFile(GlobalUpdateSeedOutFile);
|
|
202
|
+
if (!force && updateRequestSeed === void 0 && currentUpdatedSeed === void 0) return;
|
|
203
|
+
if (!force && updateRequestSeed !== void 0 && currentUpdatedSeed !== void 0 && updateRequestSeed.equals(currentUpdatedSeed))
|
|
204
|
+
return;
|
|
205
|
+
await this.updateRegistry();
|
|
206
|
+
if (updateRequestSeed) {
|
|
207
|
+
await this.storage.putFile(GlobalUpdateSeedOutFile, updateRequestSeed);
|
|
208
|
+
this.logger?.info(`Refresh finished`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async getPackageOverview(name) {
|
|
212
|
+
const content = await this.storage.getFile(packageOverviewPath(name));
|
|
213
|
+
if (content === void 0) return void 0;
|
|
214
|
+
return JSON.parse(content.toString());
|
|
215
|
+
}
|
|
216
|
+
async getGlobalOverview() {
|
|
217
|
+
const content = await this.storage.getFile(GlobalOverviewPath);
|
|
218
|
+
if (content === void 0) return void 0;
|
|
219
|
+
return JSON.parse(content.toString());
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var BlockRegistryPackConstructor = class {
|
|
223
|
+
constructor(storage, name) {
|
|
224
|
+
this.storage = storage;
|
|
225
|
+
this.name = name;
|
|
226
|
+
}
|
|
227
|
+
metaAdded = false;
|
|
228
|
+
seed = crypto.randomUUID();
|
|
229
|
+
async addFile(file, content) {
|
|
230
|
+
await this.storage.putFile(payloadFilePath(this.name, file), content);
|
|
231
|
+
}
|
|
232
|
+
async writeMeta(meta) {
|
|
233
|
+
await this.addFile(MetaFile, Buffer.from(JSON.stringify(meta)));
|
|
234
|
+
this.metaAdded = true;
|
|
235
|
+
}
|
|
236
|
+
async finish() {
|
|
237
|
+
if (!this.metaAdded) throw new Error("meta not added");
|
|
238
|
+
await this.storage.putFile(packageUpdatePath(this.name, this.seed), Buffer.of(0));
|
|
239
|
+
await this.storage.putFile(GlobalUpdateSeedInFile, Buffer.from(this.seed));
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
var S3Storage = class {
|
|
243
|
+
constructor(client, bucket, root) {
|
|
244
|
+
this.client = client;
|
|
245
|
+
this.bucket = bucket;
|
|
246
|
+
this.root = root;
|
|
247
|
+
}
|
|
248
|
+
async getFile(file) {
|
|
249
|
+
try {
|
|
250
|
+
return Buffer.from(
|
|
251
|
+
await (await this.client.getObject({
|
|
252
|
+
Bucket: this.bucket,
|
|
253
|
+
Key: pathPosix__default.default.join(this.root, file)
|
|
254
|
+
})).Body.transformToByteArray()
|
|
255
|
+
);
|
|
256
|
+
} catch (e) {
|
|
257
|
+
if (e.name === "NoSuchKey") return void 0;
|
|
258
|
+
else throw e;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async listFiles(prefix) {
|
|
262
|
+
const listRoot = pathPosix__default.default.join(this.root, prefix);
|
|
263
|
+
const paginator = clientS3.paginateListObjectsV2(
|
|
264
|
+
{ client: this.client },
|
|
265
|
+
{
|
|
266
|
+
Bucket: this.bucket,
|
|
267
|
+
Prefix: listRoot
|
|
268
|
+
}
|
|
269
|
+
);
|
|
270
|
+
const result = [];
|
|
271
|
+
for await (const page of paginator)
|
|
272
|
+
result.push(...page.Contents.map((e) => pathPosix__default.default.relative(listRoot, e.Key)));
|
|
273
|
+
return result;
|
|
274
|
+
}
|
|
275
|
+
async putFile(file, buffer) {
|
|
276
|
+
await this.client.putObject({
|
|
277
|
+
Bucket: this.bucket,
|
|
278
|
+
Key: pathPosix__default.default.join(this.root, file),
|
|
279
|
+
Body: buffer
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
async deleteFiles(...files) {
|
|
283
|
+
const results = await this.client.deleteObjects({
|
|
284
|
+
Bucket: this.bucket,
|
|
285
|
+
Delete: {
|
|
286
|
+
Objects: files.map((file) => ({
|
|
287
|
+
Key: pathPosix__default.default.join(this.root, file)
|
|
288
|
+
}))
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
if (results.Errors !== void 0 && results.Errors.length > 0)
|
|
292
|
+
throw new Error(`Errors encountered while deleting files: ${results.Errors.join("\n")}`);
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
var FSStorage = class {
|
|
296
|
+
/** Absolute path */
|
|
297
|
+
root;
|
|
298
|
+
constructor(_root) {
|
|
299
|
+
this.root = path__default.default.resolve(_root);
|
|
300
|
+
}
|
|
301
|
+
toAbsolutePath(localPath) {
|
|
302
|
+
if (pathPosix__default.default.isAbsolute(localPath)) throw new Error("absolute path");
|
|
303
|
+
return path__default.default.resolve(this.root, localPath.split(pathPosix__default.default.sep).join(path__default.default.sep));
|
|
304
|
+
}
|
|
305
|
+
async getFile(address) {
|
|
306
|
+
try {
|
|
307
|
+
return await fs__namespace.promises.readFile(this.toAbsolutePath(address));
|
|
308
|
+
} catch (err) {
|
|
309
|
+
if (err.code == "ENOENT") return void 0;
|
|
310
|
+
else throw new Error("", { cause: err });
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
async listFiles(prefix) {
|
|
314
|
+
try {
|
|
315
|
+
const listRoot = this.toAbsolutePath(prefix);
|
|
316
|
+
return (await fs__namespace.promises.readdir(listRoot, { recursive: true, withFileTypes: true })).filter((e) => e.isFile()).map(
|
|
317
|
+
(e) => path__default.default.relative(listRoot, path__default.default.resolve(e.path, e.name)).split(path__default.default.sep).join(pathPosix__default.default.sep)
|
|
318
|
+
);
|
|
319
|
+
} catch (err) {
|
|
320
|
+
if (err.code == "ENOENT") return [];
|
|
321
|
+
else throw new Error("", { cause: err });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async putFile(address, buffer) {
|
|
325
|
+
const absoluteAddress = this.toAbsolutePath(address);
|
|
326
|
+
await fs__namespace.promises.mkdir(path__default.default.dirname(absoluteAddress), { recursive: true });
|
|
327
|
+
await fs__namespace.promises.writeFile(absoluteAddress, buffer);
|
|
328
|
+
}
|
|
329
|
+
async deleteFiles(...files) {
|
|
330
|
+
for (const file of files) await fs__namespace.promises.rm(this.toAbsolutePath(file));
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
function storageByUrl(address) {
|
|
334
|
+
const url = new URL(address, `file:${path__default.default.resolve(".").split(path__default.default.sep).join(pathPosix__default.default.sep)}/`);
|
|
335
|
+
switch (url.protocol) {
|
|
336
|
+
case "file:":
|
|
337
|
+
const root = path__default.default.resolve(url.pathname);
|
|
338
|
+
return new FSStorage(root);
|
|
339
|
+
case "s3:":
|
|
340
|
+
const options = {};
|
|
341
|
+
const region = url.searchParams.get("region");
|
|
342
|
+
if (region) options.region = region;
|
|
343
|
+
const bucket = url.hostname;
|
|
344
|
+
return new S3Storage(new clientS3.S3(options), bucket, url.pathname.replace(/^\//, ""));
|
|
345
|
+
default:
|
|
346
|
+
throw new Error(`Unknown protocol: ${url.protocol}`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/registry_v1/config.ts
|
|
351
|
+
var PlRegPackageConfig = class {
|
|
352
|
+
constructor(conf) {
|
|
353
|
+
this.conf = conf;
|
|
354
|
+
}
|
|
355
|
+
createRegistry(logger) {
|
|
356
|
+
let address = this.conf.registry;
|
|
357
|
+
if (!address.startsWith("file:") && !address.startsWith("s3:")) {
|
|
358
|
+
const regByAlias = this.conf.registries[address];
|
|
359
|
+
if (!regByAlias) throw new Error(`Registry with alias "${address}" not found`);
|
|
360
|
+
address = regByAlias;
|
|
361
|
+
}
|
|
362
|
+
return new BlockRegistry(storageByUrl(address), logger);
|
|
363
|
+
}
|
|
364
|
+
get fullPackageName() {
|
|
365
|
+
return {
|
|
366
|
+
organization: this.conf.organization,
|
|
367
|
+
package: this.conf.package,
|
|
368
|
+
version: this.conf.version
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
function tryResolve(root, request) {
|
|
373
|
+
try {
|
|
374
|
+
return __require.resolve(request, {
|
|
375
|
+
paths: [root]
|
|
376
|
+
});
|
|
377
|
+
} catch (err) {
|
|
378
|
+
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
|
379
|
+
}
|
|
380
|
+
return void 0;
|
|
381
|
+
}
|
|
382
|
+
function ResolvedModuleFile(moduleRoot) {
|
|
383
|
+
return zod.z.string().transform((request, ctx) => {
|
|
384
|
+
const result = tryResolve(moduleRoot, request);
|
|
385
|
+
if (result === void 0) {
|
|
386
|
+
ctx.addIssue({
|
|
387
|
+
code: zod.z.ZodIssueCode.custom,
|
|
388
|
+
message: `Can't resolve ${request} against ${moduleRoot}`
|
|
389
|
+
});
|
|
390
|
+
return zod.z.NEVER;
|
|
391
|
+
}
|
|
392
|
+
return {
|
|
393
|
+
type: "absolute-file",
|
|
394
|
+
file: result
|
|
395
|
+
};
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
function ResolvedModuleFolder(moduleRoot, ...indexFilesToLookFor) {
|
|
399
|
+
return zod.z.string().transform((request, ctx) => {
|
|
400
|
+
const requestWithSlash = request.endsWith("/") ? request : `${request}/`;
|
|
401
|
+
for (const idxFile of indexFilesToLookFor) {
|
|
402
|
+
const result = tryResolve(moduleRoot, requestWithSlash + idxFile);
|
|
403
|
+
if (result !== void 0) {
|
|
404
|
+
if (!result.endsWith(idxFile))
|
|
405
|
+
throw new Error(`Unexpected resolve result ${result} with index file ${idxFile}`);
|
|
406
|
+
return {
|
|
407
|
+
type: "absolute-folder",
|
|
408
|
+
folder: result.slice(0, result.length - idxFile.length)
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
ctx.addIssue({
|
|
413
|
+
code: zod.z.ZodIssueCode.custom,
|
|
414
|
+
message: `Can't resolve ${request} folder against ${moduleRoot}, no index file found (${indexFilesToLookFor.join(", ")})`
|
|
415
|
+
});
|
|
416
|
+
return zod.z.NEVER;
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
function mapLocalToAbsolute(root) {
|
|
420
|
+
return (value) => value.type === "relative" ? { type: "absolute-file", file: path__default.default.resolve(root, value.path) } : value;
|
|
421
|
+
}
|
|
422
|
+
function absoluteToString() {
|
|
423
|
+
return async (value) => {
|
|
424
|
+
if (value.type === "absolute-file")
|
|
425
|
+
return await fsp2__default.default.readFile(value.file, { encoding: "utf-8" });
|
|
426
|
+
else return value.content;
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
function absoluteToBase64() {
|
|
430
|
+
return async (value) => {
|
|
431
|
+
if (value.type === "absolute-file") {
|
|
432
|
+
const mimeType = mime__namespace.lookup(value.file);
|
|
433
|
+
if (!mimeType) throw new Error(`Can't recognize mime type of the file: ${value.file}.`);
|
|
434
|
+
return {
|
|
435
|
+
type: "explicit-base64",
|
|
436
|
+
mimeType,
|
|
437
|
+
content: await fsp2__default.default.readFile(value.file, { encoding: "base64" })
|
|
438
|
+
};
|
|
439
|
+
} else return value;
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
function cpAbsoluteToRelative(dstFolder, fileAccumulator) {
|
|
443
|
+
return async (value) => {
|
|
444
|
+
if (value.type === "absolute-file") {
|
|
445
|
+
const fileName = path__default.default.basename(value.file);
|
|
446
|
+
const dst = path__default.default.resolve(dstFolder, fileName);
|
|
447
|
+
fileAccumulator?.push(fileName);
|
|
448
|
+
await fsp2__default.default.cp(value.file, dst);
|
|
449
|
+
return { type: "relative", path: fileName };
|
|
450
|
+
} else return value;
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
function packFolderToRelativeTgz(dstFolder, tgzName, fileAccumulator) {
|
|
454
|
+
if (!tgzName.endsWith(".tgz")) throw new Error(`Unexpected tgz file name: ${tgzName}`);
|
|
455
|
+
return async (value) => {
|
|
456
|
+
const dst = path__default.default.resolve(dstFolder, tgzName);
|
|
457
|
+
await tar__namespace.create(
|
|
458
|
+
{
|
|
459
|
+
gzip: true,
|
|
460
|
+
file: dst,
|
|
461
|
+
cwd: value.folder
|
|
462
|
+
},
|
|
463
|
+
[value.folder]
|
|
464
|
+
);
|
|
465
|
+
fileAccumulator?.push(tgzName);
|
|
466
|
+
return { type: "relative", path: tgzName };
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
function mapRemoteToAbsolute(rootUrl) {
|
|
470
|
+
const rootWithSlash = rootUrl.endsWith("/") ? rootUrl : `${rootUrl}/`;
|
|
471
|
+
return (value) => value.type === "relative" ? { type: "absolute-url", url: rootWithSlash + value.path } : value;
|
|
472
|
+
}
|
|
473
|
+
function BlockComponentsDescription(moduleRoot) {
|
|
474
|
+
return plModelMiddleLayer.BlockComponents(
|
|
475
|
+
ResolvedModuleFile(moduleRoot),
|
|
476
|
+
ResolvedModuleFolder(moduleRoot, "index.html")
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
function BlockComponentsConsolidate(dstFolder, fileAccumulator) {
|
|
480
|
+
return plModelMiddleLayer.BlockComponents(
|
|
481
|
+
plModelMiddleLayer.ContentAbsoluteBinaryLocal.transform(cpAbsoluteToRelative(dstFolder, fileAccumulator)),
|
|
482
|
+
plModelMiddleLayer.ContentAbsoluteFolder.transform(packFolderToRelativeTgz(dstFolder, "ui.tgz", fileAccumulator))
|
|
483
|
+
).pipe(plModelMiddleLayer.BlockComponentsManifest);
|
|
484
|
+
}
|
|
485
|
+
function BlockComponentsAbsoluteUrl(prefix) {
|
|
486
|
+
return plModelMiddleLayer.BlockComponents(
|
|
487
|
+
plModelMiddleLayer.ContentRelativeBinary.transform(mapRemoteToAbsolute(prefix)),
|
|
488
|
+
plModelMiddleLayer.ContentRelativeBinary.transform(mapRemoteToAbsolute(prefix))
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
function BlockPackMetaDescription(root) {
|
|
492
|
+
return plModelMiddleLayer.BlockPackMeta(
|
|
493
|
+
plModelMiddleLayer.DescriptionContentText.transform(mapLocalToAbsolute(root)),
|
|
494
|
+
plModelMiddleLayer.DescriptionContentBinary.transform(mapLocalToAbsolute(root))
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
function BlockPackMetaConsolidate(dstFolder, fileAccumulator) {
|
|
498
|
+
return plModelMiddleLayer.BlockPackMeta(
|
|
499
|
+
plModelMiddleLayer.ContentAbsoluteTextLocal.transform(cpAbsoluteToRelative(dstFolder, fileAccumulator)),
|
|
500
|
+
plModelMiddleLayer.ContentAbsoluteBinaryLocal.transform(cpAbsoluteToRelative(dstFolder, fileAccumulator))
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
plModelMiddleLayer.BlockPackMeta(
|
|
504
|
+
plModelMiddleLayer.ContentAbsoluteTextLocal.transform(absoluteToString()),
|
|
505
|
+
plModelMiddleLayer.ContentAbsoluteBinaryLocal.transform(absoluteToBase64())
|
|
506
|
+
).pipe(plModelMiddleLayer.BlockPackMetaEmbeddedContent);
|
|
507
|
+
|
|
508
|
+
// src/v2/model/index.ts
|
|
509
|
+
function ResolvedBlockPackDescriptionFromPackageJson(root) {
|
|
510
|
+
return plModelMiddleLayer.CreateBlockPackDescriptionSchema(
|
|
511
|
+
BlockComponentsDescription(root),
|
|
512
|
+
BlockPackMetaDescription(root)
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
function BlockPackDescriptionConsolidateToFolder(dstFolder, fileAccumulator) {
|
|
516
|
+
return plModelMiddleLayer.CreateBlockPackDescriptionSchema(
|
|
517
|
+
BlockComponentsConsolidate(dstFolder, fileAccumulator),
|
|
518
|
+
//BlockPackMetaToExplicit
|
|
519
|
+
BlockPackMetaConsolidate(dstFolder, fileAccumulator)
|
|
520
|
+
).pipe(BlockPackDescriptionManifest);
|
|
521
|
+
}
|
|
522
|
+
var BlockPackDescriptionManifest = plModelMiddleLayer.CreateBlockPackDescriptionSchema(
|
|
523
|
+
plModelMiddleLayer.BlockComponentsManifest,
|
|
524
|
+
plModelMiddleLayer.BlockPackMetaManifest
|
|
525
|
+
);
|
|
526
|
+
var BlockPackManifest = BlockPackDescriptionManifest.extend({
|
|
527
|
+
schema: zod.z.literal("v1"),
|
|
528
|
+
files: zod.z.array(zod.z.string())
|
|
529
|
+
});
|
|
530
|
+
var BlockPackManifestFile = "manifest.json";
|
|
531
|
+
async function buildBlockPackDist(description, dst) {
|
|
532
|
+
await fsp2__default.default.mkdir(dst, { recursive: true });
|
|
533
|
+
const files = [];
|
|
534
|
+
const descriptionRelative = await BlockPackDescriptionConsolidateToFolder(dst, files).parseAsync(
|
|
535
|
+
description
|
|
536
|
+
);
|
|
537
|
+
const manifest = BlockPackManifest.parse({
|
|
538
|
+
schema: "v1",
|
|
539
|
+
...descriptionRelative,
|
|
540
|
+
files
|
|
541
|
+
});
|
|
542
|
+
await fsp2__default.default.writeFile(path__default.default.resolve(dst, BlockPackManifestFile), JSON.stringify(manifest));
|
|
543
|
+
return manifest;
|
|
544
|
+
}
|
|
545
|
+
var BlockDescriptionPackageJsonField = "block";
|
|
546
|
+
var ConventionPackageNamePattern = /(?:@[a-zA-Z0-9-.]+\/)?(?<organization>[a-zA-Z0-9-]+)\.(?<name>[a-zA-Z0-9-]+)/;
|
|
547
|
+
function parsePackageName(packageName) {
|
|
548
|
+
const match = packageName.match(ConventionPackageNamePattern);
|
|
549
|
+
if (!match)
|
|
550
|
+
throw new Error(
|
|
551
|
+
`Malformed package name (${packageName}), can't infer organization and block pack name.`
|
|
552
|
+
);
|
|
553
|
+
const { name, organization } = match.groups;
|
|
554
|
+
return { name, organization };
|
|
555
|
+
}
|
|
556
|
+
async function tryLoadPackDescription(moduleRoot) {
|
|
557
|
+
const fullPackageJsonPath = path__default.default.resolve(moduleRoot, "package.json");
|
|
558
|
+
try {
|
|
559
|
+
const packageJson = await tryLoadFile(
|
|
560
|
+
fullPackageJsonPath,
|
|
561
|
+
(buf) => JSON.parse(buf.toString("utf-8"))
|
|
562
|
+
);
|
|
563
|
+
if (packageJson === void 0) return void 0;
|
|
564
|
+
const descriptionNotParsed = packageJson[BlockDescriptionPackageJsonField];
|
|
565
|
+
if (descriptionNotParsed === void 0) return void 0;
|
|
566
|
+
const descriptionRaw = {
|
|
567
|
+
...plModelMiddleLayer.BlockPackDescriptionFromPackageJsonRaw.parse(descriptionNotParsed),
|
|
568
|
+
id: {
|
|
569
|
+
...parsePackageName(
|
|
570
|
+
tsHelpers.notEmpty(packageJson["name"], `"name" not found in ${fullPackageJsonPath}`)
|
|
571
|
+
),
|
|
572
|
+
version: plModelMiddleLayer.SemVer.parse(packageJson["version"])
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
const descriptionParsingResult = await ResolvedBlockPackDescriptionFromPackageJson(moduleRoot).safeParseAsync(descriptionRaw);
|
|
576
|
+
if (descriptionParsingResult.success) return descriptionParsingResult.data;
|
|
577
|
+
return void 0;
|
|
578
|
+
} catch (e) {
|
|
579
|
+
return void 0;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
async function loadPackDescriptionRaw(moduleRoot) {
|
|
583
|
+
const fullPackageJsonPath = path__default.default.resolve(moduleRoot, "package.json");
|
|
584
|
+
const packageJson = JSON.parse(await fsp2__default.default.readFile(fullPackageJsonPath, { encoding: "utf-8" }));
|
|
585
|
+
const descriptionNotParsed = packageJson[BlockDescriptionPackageJsonField];
|
|
586
|
+
if (descriptionNotParsed === void 0)
|
|
587
|
+
throw new Error(
|
|
588
|
+
`Block description (field ${BlockDescriptionPackageJsonField}) not found in ${fullPackageJsonPath}.`
|
|
589
|
+
);
|
|
590
|
+
return {
|
|
591
|
+
...plModelMiddleLayer.BlockPackDescriptionFromPackageJsonRaw.parse(descriptionNotParsed),
|
|
592
|
+
id: {
|
|
593
|
+
...parsePackageName(
|
|
594
|
+
tsHelpers.notEmpty(packageJson["name"], `"name" not found in ${fullPackageJsonPath}`)
|
|
595
|
+
),
|
|
596
|
+
version: plModelMiddleLayer.SemVer.parse(packageJson["version"])
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
async function loadPackDescription(moduleRoot) {
|
|
601
|
+
const descriptionRaw = await loadPackDescriptionRaw(moduleRoot);
|
|
602
|
+
return await ResolvedBlockPackDescriptionFromPackageJson(moduleRoot).parseAsync(descriptionRaw);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
exports.BlockComponentsAbsoluteUrl = BlockComponentsAbsoluteUrl;
|
|
606
|
+
exports.BlockComponentsConsolidate = BlockComponentsConsolidate;
|
|
607
|
+
exports.BlockComponentsDescription = BlockComponentsDescription;
|
|
608
|
+
exports.BlockDescriptionPackageJsonField = BlockDescriptionPackageJsonField;
|
|
609
|
+
exports.BlockPackDescriptionConsolidateToFolder = BlockPackDescriptionConsolidateToFolder;
|
|
610
|
+
exports.BlockPackDescriptionManifest = BlockPackDescriptionManifest;
|
|
611
|
+
exports.BlockPackManifest = BlockPackManifest;
|
|
612
|
+
exports.BlockPackManifestFile = BlockPackManifestFile;
|
|
613
|
+
exports.RegistryV1 = registry_v1_exports;
|
|
614
|
+
exports.ResolvedBlockPackDescriptionFromPackageJson = ResolvedBlockPackDescriptionFromPackageJson;
|
|
615
|
+
exports.ResolvedModuleFile = ResolvedModuleFile;
|
|
616
|
+
exports.ResolvedModuleFolder = ResolvedModuleFolder;
|
|
617
|
+
exports.absoluteToBase64 = absoluteToBase64;
|
|
618
|
+
exports.absoluteToString = absoluteToString;
|
|
619
|
+
exports.buildBlockPackDist = buildBlockPackDist;
|
|
620
|
+
exports.cpAbsoluteToRelative = cpAbsoluteToRelative;
|
|
621
|
+
exports.loadPackDescription = loadPackDescription;
|
|
622
|
+
exports.loadPackDescriptionRaw = loadPackDescriptionRaw;
|
|
623
|
+
exports.mapLocalToAbsolute = mapLocalToAbsolute;
|
|
624
|
+
exports.mapRemoteToAbsolute = mapRemoteToAbsolute;
|
|
625
|
+
exports.packFolderToRelativeTgz = packFolderToRelativeTgz;
|
|
626
|
+
exports.parsePackageName = parsePackageName;
|
|
627
|
+
exports.tryLoadPackDescription = tryLoadPackDescription;
|
|
628
|
+
//# sourceMappingURL=lib.cjs.map
|
|
629
|
+
//# sourceMappingURL=lib.cjs.map
|