@intelligentgraphics/ig.gfx.packager 2.3.4 → 3.0.0-alpha.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.
@@ -0,0 +1,577 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ var path = require('path');
5
+ var fs = require('fs');
6
+ var promises = require('stream/promises');
7
+ var require$$1 = require('child_process');
8
+ var cli = require('../cli.js');
9
+ var index = require('./build.js');
10
+ var versionFile = require('../versionFile.js');
11
+ var axios = require('axios');
12
+ var Ajv = require('ajv');
13
+ var JSZip = require('jszip');
14
+ var terser = require('terser');
15
+ var dependencies = require('../dependencies.js');
16
+ require('update-notifier');
17
+ require('yargs');
18
+ require('tslib');
19
+ require('os');
20
+ require('assert');
21
+ require('events');
22
+ require('buffer');
23
+ require('stream');
24
+ require('util');
25
+ require('constants');
26
+ require('write-pkg');
27
+ require('inquirer');
28
+ require('glob');
29
+ require('typescript');
30
+ require('../scripts.js');
31
+ require('typedoc');
32
+ require('simple-git');
33
+ require('resolve');
34
+
35
+ function _interopNamespaceDefault(e) {
36
+ var n = Object.create(null);
37
+ if (e) {
38
+ Object.keys(e).forEach(function (k) {
39
+ if (k !== 'default') {
40
+ var d = Object.getOwnPropertyDescriptor(e, k);
41
+ Object.defineProperty(n, k, d.get ? d : {
42
+ enumerable: true,
43
+ get: function () { return e[k]; }
44
+ });
45
+ }
46
+ });
47
+ }
48
+ n.default = e;
49
+ return Object.freeze(n);
50
+ }
51
+
52
+ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
53
+ var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
54
+ var terser__namespace = /*#__PURE__*/_interopNamespaceDefault(terser);
55
+
56
+ const PLUGIN_ID = "0feba3a0-b6d1-11e6-9598-0800200c9a66";
57
+ /**
58
+ * Starts an asset service session and returns the sessionId
59
+ *
60
+ * @param {SessionStartParams} params
61
+ * @returns
62
+ */
63
+ const startSession = async ({
64
+ url,
65
+ authentication,
66
+ ...params
67
+ }) => {
68
+ const payload = {
69
+ ...params,
70
+ user: undefined,
71
+ password: undefined,
72
+ license: undefined,
73
+ plugin: PLUGIN_ID
74
+ };
75
+ if (authentication.type === "credentials") {
76
+ payload.user = authentication.username;
77
+ payload.password = authentication.password;
78
+ } else if (authentication.type === "license") {
79
+ payload.license = authentication.license;
80
+ }
81
+ const {
82
+ data: {
83
+ session: sessionId,
84
+ state
85
+ }
86
+ } = await axios.post(`Session/Start2`, JSON.stringify(payload), {
87
+ baseURL: url
88
+ });
89
+ if (state !== "SUCCESS") {
90
+ throw new Error(`Could not start session: Asset server responded with ${state}`);
91
+ }
92
+ return {
93
+ url,
94
+ sessionId,
95
+ domain: params.domain,
96
+ subDomain: params.subDomain
97
+ };
98
+ };
99
+ const closeSession = async session => {
100
+ await axios.get(`Session/Close/${session.sessionId}`, {
101
+ baseURL: session.url
102
+ });
103
+ };
104
+ const uploadPackage = async (session, {
105
+ name,
106
+ version
107
+ }, zipFilePath) => {
108
+ try {
109
+ await uploadPackageToUrl(session.url, `UploadPackage/${session.sessionId}/${name}_${version}`, zipFilePath);
110
+ } catch (err) {
111
+ await uploadPackageToUrl(session.url, `UploadPackage/${session.sessionId}/${name}_${version}/`, zipFilePath);
112
+ }
113
+ };
114
+ const getExistingPackages = async session => axios.get(`Script/GetInformation/${session.sessionId}`, {
115
+ baseURL: session.url,
116
+ validateStatus: status => status === 404 || status === 200
117
+ }).then(({
118
+ data
119
+ }) => Array.isArray(data) ? data : undefined);
120
+ const uploadPackageToUrl = (url, path, zipFilePath) => axios.post(path, fs__namespace.createReadStream(zipFilePath), {
121
+ baseURL: url
122
+ }).then(({
123
+ data,
124
+ status
125
+ }) => {
126
+ let objectBody;
127
+ if (typeof data === "string") {
128
+ try {
129
+ objectBody = JSON.parse(data);
130
+ } catch (err) {}
131
+ } else if (typeof data === "object") {
132
+ objectBody = data;
133
+ }
134
+ if (objectBody !== undefined) {
135
+ if ("state" in objectBody && objectBody.state !== "SUCCESS") {
136
+ throw new Error(objectBody.response ?? objectBody.state);
137
+ }
138
+ }
139
+ if (status >= 400) {
140
+ if (objectBody !== undefined) {
141
+ let text = "";
142
+ for (const key in objectBody) {
143
+ text += key + ": \n";
144
+ if (typeof objectBody[key] === "object") {
145
+ text += JSON.stringify(objectBody[key], undefined, 2);
146
+ } else {
147
+ text += objectBody[key];
148
+ }
149
+ text += "\n\n";
150
+ }
151
+ throw new Error(text);
152
+ }
153
+ throw new Error(data);
154
+ }
155
+ return data;
156
+ });
157
+
158
+ const buildArchiveFromPublishedPackage = (location, manifest, creatorPackage) => {
159
+ const archive = new JSZip();
160
+ archive.file("_Package.json", JSON.stringify(creatorPackage, null, 2));
161
+ const index = dependencies.readPublishedPackageCreatorIndex(location);
162
+ if (index !== undefined) {
163
+ archive.file("_Index.json", JSON.stringify(index, null, 2));
164
+ }
165
+ archive.file(manifest.main, fs__namespace.createReadStream(path__namespace.join(location.path, manifest.main)));
166
+ return archive;
167
+ };
168
+ let validateSchema;
169
+ const runtimeScripts = ["Interactor", "Core", "Mixed"];
170
+ const notRuntimeScripts = ["Context", "Evaluator"];
171
+ const buildArchiveFromPackage = async (workspaceLocation, packageLocation, data) => {
172
+ const {
173
+ domain,
174
+ subdomain
175
+ } = cli.parseCreatorPackageName(data);
176
+ const logStep = step => index.logPackageMessage(data.Package, step);
177
+ if (validateSchema === undefined) {
178
+ validateSchema = await axios.get("https://archive.intelligentgraphics.biz/schemas/gfx/animation.json").then(({
179
+ data
180
+ }) => new Ajv({
181
+ coerceTypes: true,
182
+ allErrors: true,
183
+ removeAdditional: true,
184
+ useDefaults: "empty",
185
+ validateSchema: "log"
186
+ }).compile(data));
187
+ }
188
+ const libFilePath = path__namespace.join(cli.getWorkspaceOutputPath(workspaceLocation), `${data.Package}.min.js`);
189
+ const scriptDirectories = [packageLocation.path, packageLocation.scriptsDir];
190
+ if (subdomain === "GFX.Standard") {
191
+ logStep(`Including Images folder`);
192
+ scriptDirectories.push(path__namespace.join(packageLocation.path, "Images"));
193
+ }
194
+ const manifest = cli.readPackageCreatorManifest(packageLocation);
195
+ if (manifest !== undefined) {
196
+ if (manifest.RunTime && notRuntimeScripts.includes(manifest.Type)) {
197
+ logStep("Setting script RunTime to false because of script type");
198
+ cli.writePackageCreatorManifest(packageLocation, {
199
+ ...manifest,
200
+ RunTime: false
201
+ });
202
+ } else if (!manifest.RunTime && runtimeScripts.includes(manifest.Type)) {
203
+ logStep("Setting script RunTime to true because of script type");
204
+ cli.writePackageCreatorManifest(packageLocation, {
205
+ ...manifest,
206
+ RunTime: true
207
+ });
208
+ }
209
+ }
210
+
211
+ // if (index === undefined) {
212
+ // throw new Error(
213
+ // `Could not find an "${INDEX_FILE}" file in "${primaryScriptDir}"`,
214
+ // );
215
+ // }
216
+
217
+ const animations = new Map();
218
+ for (const scriptFilePath of cli.readPackageAnimationList(packageLocation)) {
219
+ const content = fs__namespace.readFileSync(scriptFilePath, {
220
+ encoding: "utf8"
221
+ });
222
+ let data;
223
+ try {
224
+ data = JSON.parse(content);
225
+ } catch (err) {
226
+ const relativePath = path__namespace.relative(workspaceLocation.path, scriptFilePath);
227
+ if (err instanceof SyntaxError) {
228
+ throw new Error(`Encountered invalid syntax in file "${relativePath}": ${String(err)}`);
229
+ }
230
+ throw new Error(`Encountered an unexpected error while parsing animation json file at path "${relativePath}"`, {
231
+ cause: err
232
+ });
233
+ }
234
+ validateSchema(data);
235
+ animations.set(data.Id, JSON.stringify(data));
236
+ }
237
+ let libFile;
238
+ try {
239
+ libFile = fs__namespace.readFileSync(libFilePath, {
240
+ encoding: "utf8"
241
+ });
242
+ } catch (err) {
243
+ if (!cli.isErrorENOENT(err)) {
244
+ throw err;
245
+ }
246
+ }
247
+ if (libFile === undefined) {
248
+ if (animations.size === 0) {
249
+ throw new Error(`Could not find a javascript file at "${libFilePath}".
250
+ Packaging without a javascript file is only allowed when animation json files are available`);
251
+ }
252
+ }
253
+ const archive = new JSZip();
254
+ let library = "";
255
+ if (libFile) {
256
+ library = libFile;
257
+ }
258
+ if (!library) {
259
+ const date = new Date(Date.now());
260
+ library = `/* This file is part of the ${domain} Data Packages.
261
+ * Copyright (C) ${date.getFullYear()} intelligentgraphics. All Rights Reserved. */`;
262
+ }
263
+ if (animations.size > 0) {
264
+ const scopeParts = data.Scope.split(".");
265
+ const scope = data.Scope;
266
+ library += createNamespace(scopeParts);
267
+ for (const [name, data] of animations) {
268
+ library += `${scope}.${name} = ` + data + ";";
269
+ console.log(`${name}: Added animation ${scope}.${name}`);
270
+ }
271
+ }
272
+ const minifyResult = terser__namespace.minify(library, {
273
+ ecma: 5
274
+ });
275
+ archive.file(`${data.Package}.js`, minifyResult.code);
276
+ archive.file(`_Package.json`, JSON.stringify(data, null, 2));
277
+ const creatorIndex = cli.readPackageCreatorIndex(packageLocation);
278
+ if (creatorIndex !== undefined) {
279
+ archive.file(`_Index.json`, JSON.stringify(creatorIndex, null, 2));
280
+ }
281
+ for (const directory of scriptDirectories) {
282
+ try {
283
+ for (const file of fs__namespace.readdirSync(directory)) {
284
+ const {
285
+ ext
286
+ } = path__namespace.parse(file);
287
+ switch (ext) {
288
+ case ".png":
289
+ case ".jpeg":
290
+ case ".jpg":
291
+ break;
292
+ default:
293
+ continue;
294
+ }
295
+ archive.file(file, fs__namespace.createReadStream(path__namespace.join(directory, file)));
296
+ }
297
+ } catch (err) {
298
+ console.error(`Script directory "${directory}" does not exist`);
299
+ }
300
+ }
301
+ return archive;
302
+ };
303
+ const createNamespace = parts => {
304
+ let code = `var ${parts[0]};`;
305
+ for (let index = 0; index < parts.length; index++) {
306
+ const path = parts.slice(0, index + 1).join(".");
307
+ code += `\n(${path} = ${path} || {});`;
308
+ }
309
+ return code;
310
+ };
311
+
312
+ const releaseFolder = async options => {
313
+ const workspace = cli.detectWorkspace(options.cwd);
314
+ const location = cli.detectPackage(workspace, options.directory);
315
+ const {
316
+ write: writeVersionFile,
317
+ reset: resetVersionFile
318
+ } = versionFile.getVersionFileHandler(location);
319
+ const packageDescription = cli.readPackageCreatorManifest(location);
320
+ const fullPackageName = packageDescription.Package;
321
+ const {
322
+ domain,
323
+ subdomain
324
+ } = cli.parseCreatorPackageName(packageDescription);
325
+ const publishDomain = options.domain ?? domain;
326
+ const publishSubdomain = options.subdomain ?? subdomain;
327
+ const sharedPackageJson = cli.readWorkspaceNpmManifest(workspace);
328
+ let newVersion;
329
+ try {
330
+ newVersion = versionFile.parseVersionFromString(options.newVersion);
331
+ } catch (err) {
332
+ throw new Error(`Please enter a version in this format 1.0.0.100`);
333
+ }
334
+ if (newVersion.buildNumber < 100) {
335
+ newVersion.preRelease = {
336
+ type: "beta",
337
+ version: newVersion.buildNumber
338
+ };
339
+ } else if (newVersion.buildNumber > 100) {
340
+ newVersion.preRelease = {
341
+ type: "patch",
342
+ version: newVersion.buildNumber - 100
343
+ };
344
+ }
345
+ if (sharedPackageJson !== undefined) {
346
+ index.logPackageMessage(packageDescription.Package, `Running npm install to make sure all dependencies are up to date`);
347
+ require$$1.execSync(`npm install`, {
348
+ encoding: "utf-8",
349
+ cwd: workspace.path
350
+ });
351
+ }
352
+ const binDir = cli.getWorkspaceOutputPath(workspace);
353
+ fs__namespace.mkdirSync(binDir, {
354
+ recursive: true
355
+ });
356
+ let assetServerPackageDetails;
357
+ let packageNameWithVersion;
358
+ {
359
+ const versionWithoutPrelease = newVersion.clone();
360
+ versionWithoutPrelease.preRelease = undefined;
361
+ const newVersionString = versionWithoutPrelease.toVersionString({
362
+ buildNumber: true
363
+ });
364
+ packageNameWithVersion = `${packageDescription.Package}_${newVersionString}`;
365
+ assetServerPackageDetails = {
366
+ name: packageDescription.Package,
367
+ version: newVersionString
368
+ };
369
+ }
370
+ const zipFilePath = path__namespace.join(binDir, packageNameWithVersion + ".zip");
371
+ try {
372
+ if (options.pushOnly) {
373
+ if (!fs__namespace.existsSync(zipFilePath)) {
374
+ throw new Error(`Expected a zip file to exist at path ${zipFilePath} since pushOnly is specified`);
375
+ }
376
+ } else {
377
+ const gitVersionInformation = await versionFile.getVersionInformationFromGit(workspace, location);
378
+ writeVersionFile(fullPackageName, newVersion);
379
+ const bannerText = sharedPackageJson !== undefined ? versionFile.getWorkspaceBannerText(sharedPackageJson) : undefined;
380
+ await index.buildFolders({
381
+ ...options,
382
+ directories: [location.path],
383
+ skipPackagesWithoutTsFiles: true,
384
+ banner: options.banner ? {
385
+ text: bannerText,
386
+ commit: gitVersionInformation.commit,
387
+ commitDirty: gitVersionInformation.dirty,
388
+ version: newVersion.toVersionString({
389
+ buildNumber: true
390
+ }),
391
+ date: new Date(Date.now())
392
+ // gitVersionInformation.commitDate
393
+ // ? new Date(gitVersionInformation.commitDate)
394
+ // : new Date(Date.now()),
395
+ } : undefined
396
+ });
397
+ newVersion.preRelease = undefined;
398
+ try {
399
+ fs__namespace.rmSync(zipFilePath);
400
+ } catch (err) {
401
+ if (!cli.isErrorENOENT(err)) {
402
+ throw err;
403
+ }
404
+ }
405
+ const archive = await buildArchiveFromPackage(workspace, location, packageDescription);
406
+ const zipOutputStream = fs__namespace.createWriteStream(zipFilePath);
407
+ await promises.pipeline(archive.generateNodeStream(), zipOutputStream);
408
+ }
409
+ if (!options.noUpload) {
410
+ if (!options.authentication) {
411
+ throw new Error(`Expected authentication to be available`);
412
+ }
413
+ index.logPackageMessage(packageDescription.Package, `Opening connection to asset server`);
414
+ const sessionManager = await createSessionManager({
415
+ url: options.service,
416
+ address: options.address,
417
+ domain: publishDomain,
418
+ subDomain: publishSubdomain,
419
+ authentication: options.authentication
420
+ });
421
+ index.logPackageMessage(packageDescription.Package, `Making sure all dependencies are available with an acceptable version`);
422
+ if (!(await ensureRequiredVersions(workspace, packageDescription, sessionManager, options.prompter))) {
423
+ index.logPackageMessage(packageDescription.Package, "AssetService does not yet support required apis for dependency check");
424
+ }
425
+ index.logPackageMessage(packageDescription.Package, `Uploading package to ${publishDomain}.${publishSubdomain}`);
426
+ await uploadPackage(sessionManager.getTargetSession(), assetServerPackageDetails, zipFilePath);
427
+ await sessionManager.destroy().catch(err => {
428
+ index.logPackageMessage(packageDescription.Package, `Failed to close asset service session(s): ${err}`);
429
+ });
430
+ }
431
+ } catch (err) {
432
+ resetVersionFile();
433
+ throw err;
434
+ }
435
+ if (newVersion.buildNumber >= 100 && !options.pushOnly) {
436
+ index.logPackageMessage(fullPackageName, cli.translate("messages.movingReleaseZip"));
437
+ const zipFileName = `${packageNameWithVersion}.zip`;
438
+ const releasesPath = cli.getPackageReleasesDirectory(location);
439
+ fs__namespace.mkdirSync(releasesPath, {
440
+ recursive: true
441
+ });
442
+ fs__namespace.copyFileSync(zipFilePath, path__namespace.join(releasesPath, zipFileName));
443
+ }
444
+ };
445
+ const ensureRequiredVersions = async (workspaceLocation, creatorPackage, sessionManager, prompter) => {
446
+ const targetSession = sessionManager.getTargetSession();
447
+ const rawUploadedPackages = await getExistingPackages(targetSession);
448
+ if (rawUploadedPackages === undefined) {
449
+ return false;
450
+ }
451
+ const uploadedPackages = rawUploadedPackages.map(entry => {
452
+ let version;
453
+ try {
454
+ version = versionFile.parseVersionFromString(entry.informalVersion);
455
+ } catch (err) {
456
+ throw new Error(`Encountered invalid format for version ${entry.informalVersion}`);
457
+ }
458
+ if (version.buildNumber < 100) {
459
+ version.preRelease = {
460
+ type: "beta",
461
+ version: version.buildNumber
462
+ };
463
+ } else if (version.buildNumber > 100) {
464
+ version.preRelease = {
465
+ type: "patch",
466
+ version: version.buildNumber - 100
467
+ };
468
+ }
469
+ return {
470
+ ...entry,
471
+ version
472
+ };
473
+ });
474
+ const libraries = dependencies.determineWorkspaceIGLibraries(workspaceLocation);
475
+ for (const libraryLocation of libraries) {
476
+ const libraryManifest = dependencies.readPublishedPackageNpmManifest(libraryLocation);
477
+ const libraryCreatorPackage = dependencies.readPublishedPackageCreatorManifest(libraryLocation);
478
+ if (libraryCreatorPackage === undefined || libraryManifest.main === undefined || libraryCreatorPackage.Package === creatorPackage.Package) {
479
+ continue;
480
+ }
481
+ const libraryVersion = cli.build.PackageVersion.extractFromLine(libraryManifest.version);
482
+ if (libraryVersion.preRelease) {
483
+ libraryVersion.buildNumber = libraryVersion.preRelease.version;
484
+ libraryVersion.preRelease = undefined;
485
+ } else {
486
+ libraryVersion.buildNumber = 100;
487
+ }
488
+ let uploadedPackageInBasics;
489
+ let uploadedPackageInTarget;
490
+ for (const uploadedPackage of uploadedPackages) {
491
+ if (uploadedPackage.scope === libraryCreatorPackage.Package) {
492
+ if (uploadedPackage.support) {
493
+ uploadedPackageInBasics = uploadedPackage;
494
+ } else {
495
+ uploadedPackageInTarget = uploadedPackage;
496
+ }
497
+ }
498
+ }
499
+ if (targetSession.subDomain !== "Basics" && (uploadedPackageInBasics !== undefined || uploadedPackageInTarget !== undefined)) {
500
+ index.logPackageMessage(creatorPackage.Package, `Package ${libraryCreatorPackage.Package} is uploaded both for Basics and ${targetSession.subDomain}. The package within ${targetSession.subDomain} will be used.`);
501
+ }
502
+ const validInBasics = uploadedPackageInBasics !== undefined && !uploadedPackageInBasics.version.isLesserThan(libraryVersion);
503
+ const validInTarget = uploadedPackageInTarget !== undefined && !uploadedPackageInTarget.version.isLesserThan(libraryVersion);
504
+ if (validInBasics || validInTarget) {
505
+ index.logPackageMessage(creatorPackage.Package, `Skipping upload for package ${libraryCreatorPackage.Package} as it is already uploaded and up to date.`);
506
+ continue;
507
+ }
508
+ if (uploadedPackageInBasics) {
509
+ index.logPackageMessage(creatorPackage.Package, `Found invalid version for package ${libraryCreatorPackage.Package} in Basics.`);
510
+ }
511
+ if (targetSession.subDomain !== "Basics" && uploadedPackageInTarget) {
512
+ index.logPackageMessage(creatorPackage.Package, `Found invalid version for package ${libraryCreatorPackage.Package} in ${targetSession.subDomain}.`);
513
+ }
514
+ const possibleTargets = ["Basics"];
515
+ if (targetSession.subDomain !== "Basics") {
516
+ possibleTargets.push(targetSession.subDomain);
517
+ }
518
+ const uploadTargetScope = await prompter.ask({
519
+ message: `${libraryCreatorPackage.Package} is not available at all or has an invalid version. Select the scope to upload it to`,
520
+ options: [...possibleTargets, "Skip"],
521
+ default: possibleTargets[0]
522
+ });
523
+ if (uploadTargetScope === "Skip") {
524
+ continue;
525
+ }
526
+ const archive = buildArchiveFromPublishedPackage(libraryLocation, libraryManifest, libraryCreatorPackage);
527
+ const newVersionString = libraryVersion.toVersionString({
528
+ buildNumber: true
529
+ });
530
+ const packageNameWithVersion = `${libraryCreatorPackage.Package}_${newVersionString}`;
531
+ const zipFilePath = path__namespace.join(cli.getWorkspaceOutputPath(workspaceLocation), `${packageNameWithVersion}.zip`);
532
+ try {
533
+ fs__namespace.rmSync(zipFilePath);
534
+ } catch (err) {
535
+ if (!cli.isErrorENOENT(err)) {
536
+ throw err;
537
+ }
538
+ }
539
+ const zipOutputStream = fs__namespace.createWriteStream(zipFilePath);
540
+ await promises.pipeline(archive.generateNodeStream(), zipOutputStream);
541
+ const session = uploadTargetScope === "Basics" ? await sessionManager.getBasicsSession() : targetSession;
542
+ index.logPackageMessage(creatorPackage.Package, `Uploading package ${libraryCreatorPackage.Package} with version ${newVersionString} to ${session.domain}.${session.subDomain}`);
543
+ await uploadPackage(session, {
544
+ name: libraryCreatorPackage.Package,
545
+ version: newVersionString
546
+ }, zipFilePath);
547
+ }
548
+ return true;
549
+ };
550
+ const createSessionManager = async params => {
551
+ const targetSession = await startSession(params);
552
+ let basicsSession;
553
+ return {
554
+ getBasicsSession: async () => {
555
+ if (targetSession.subDomain === "Basics") {
556
+ return targetSession;
557
+ }
558
+ if (basicsSession === undefined) {
559
+ basicsSession = await startSession({
560
+ ...params,
561
+ subDomain: "Basics"
562
+ });
563
+ }
564
+ return basicsSession;
565
+ },
566
+ getTargetSession: () => targetSession,
567
+ destroy: async () => {
568
+ await closeSession(targetSession);
569
+ if (basicsSession !== undefined) {
570
+ await closeSession(basicsSession);
571
+ }
572
+ }
573
+ };
574
+ };
575
+
576
+ exports.releaseFolder = releaseFolder;
577
+ //# sourceMappingURL=release.js.map