@orizm/cli 0.1.0 → 1.0.0
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/cli.js +246 -255
- package/package.json +3 -2
package/dist/cli.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -36,10 +37,15 @@ async function loadConfig(configFile) {
|
|
|
36
37
|
entryPoints: [configFile],
|
|
37
38
|
bundle: true,
|
|
38
39
|
write: false,
|
|
39
|
-
format: "
|
|
40
|
-
target: "node16"
|
|
40
|
+
format: "esm",
|
|
41
|
+
target: "node16",
|
|
42
|
+
platform: "node"
|
|
41
43
|
});
|
|
42
|
-
|
|
44
|
+
const moduleURL = `data:text/javascript,${encodeURIComponent(
|
|
45
|
+
buildResult.outputFiles[0].text
|
|
46
|
+
)}`;
|
|
47
|
+
const { default: orizmConfigRaw } = await import(moduleURL);
|
|
48
|
+
return v.parse(import_config_validator.OrizmConfigDef, orizmConfigRaw);
|
|
43
49
|
}
|
|
44
50
|
|
|
45
51
|
// src/lib/cli-spinner.ts
|
|
@@ -271,7 +277,6 @@ var cmsGenerateCommand = new import_commander.Command("generate").description("g
|
|
|
271
277
|
|
|
272
278
|
// src/cms/pull/command.ts
|
|
273
279
|
var import_commander2 = require("commander");
|
|
274
|
-
var cliColor2 = __toESM(require("cli-color"));
|
|
275
280
|
|
|
276
281
|
// src/api/developer-api-client.ts
|
|
277
282
|
var import_openapi_fetch = __toESM(require("openapi-fetch"));
|
|
@@ -286,33 +291,48 @@ function createDeveloperAPIClient(baseUrl, apiKey) {
|
|
|
286
291
|
});
|
|
287
292
|
}
|
|
288
293
|
|
|
289
|
-
// src/
|
|
294
|
+
// src/lib/fetch-definition-reference.ts
|
|
290
295
|
async function fetchDefinitionReference(options) {
|
|
291
296
|
const client = createDeveloperAPIClient(options.baseUrl, options.apiKey);
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
297
|
+
const abortController = new AbortController();
|
|
298
|
+
const timeout = setTimeout(() => {
|
|
299
|
+
abortController.abort();
|
|
300
|
+
}, 1e4);
|
|
301
|
+
try {
|
|
302
|
+
const response = await client.GET(
|
|
303
|
+
"/projects/{projectName}/definitions/current/reference",
|
|
304
|
+
{
|
|
305
|
+
params: { path: { projectName: options.projectName } },
|
|
306
|
+
signal: abortController.signal
|
|
307
|
+
}
|
|
308
|
+
);
|
|
309
|
+
if (response.data) {
|
|
310
|
+
return response.data.content;
|
|
296
311
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
312
|
+
if (response.response.status === 404) {
|
|
313
|
+
throw new Error(`Project ${options.projectName} not found.`);
|
|
314
|
+
}
|
|
315
|
+
if (response.response.status === 401) {
|
|
316
|
+
throw new Error(`API Key is invalid.`);
|
|
317
|
+
}
|
|
318
|
+
throw new Error(
|
|
319
|
+
`Unknown error occurred when fetching definitions for project ${options.projectName}.`
|
|
320
|
+
);
|
|
321
|
+
} catch (error2) {
|
|
322
|
+
if (error2 instanceof Error && error2.name === "AbortError") {
|
|
323
|
+
throw new Error("Request timed out.");
|
|
324
|
+
}
|
|
325
|
+
throw error2;
|
|
326
|
+
} finally {
|
|
327
|
+
clearTimeout(timeout);
|
|
306
328
|
}
|
|
307
|
-
throw new Error(
|
|
308
|
-
`Unknown error occurred when fetching definitions for project ${options.projectName}.`
|
|
309
|
-
);
|
|
310
329
|
}
|
|
311
330
|
|
|
312
|
-
// src/
|
|
331
|
+
// src/lib/load-env.ts
|
|
313
332
|
var import_promises2 = __toESM(require("fs/promises"));
|
|
314
333
|
var v3 = __toESM(require("valibot"));
|
|
315
334
|
var import_dotenv = __toESM(require("dotenv"));
|
|
335
|
+
var DEFAULT_ENV_FILE_PATH = ".env";
|
|
316
336
|
var EnvSchema = v3.object({
|
|
317
337
|
ORIZM_PROJECT: v3.pipe(
|
|
318
338
|
v3.string(),
|
|
@@ -333,22 +353,41 @@ var EnvSchema = v3.object({
|
|
|
333
353
|
)
|
|
334
354
|
});
|
|
335
355
|
async function loadEnv(envPath) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
356
|
+
let envFileContent;
|
|
357
|
+
if (envPath) {
|
|
358
|
+
envFileContent = await import_promises2.default.readFile(envPath, "utf-8").catch(() => null);
|
|
359
|
+
if (!envFileContent) {
|
|
360
|
+
throw new Error(`Environment variables file: '${envPath}' is not found.`);
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
envFileContent = await import_promises2.default.readFile(DEFAULT_ENV_FILE_PATH, "utf-8").catch(() => null);
|
|
364
|
+
}
|
|
365
|
+
if (envFileContent !== null) {
|
|
366
|
+
const dotenvParsed = import_dotenv.default.parse(envFileContent);
|
|
367
|
+
const envParseResult = v3.safeParse(EnvSchema, dotenvParsed);
|
|
368
|
+
if (!envParseResult.success) {
|
|
369
|
+
throw new Error(
|
|
370
|
+
`Environment variables are not valid.
|
|
371
|
+
` + envParseResult.issues.map((issue) => issue.message).join("\n")
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
return envParseResult.output;
|
|
375
|
+
}
|
|
376
|
+
const processEnvParseResult = v3.safeParse(EnvSchema, process.env);
|
|
377
|
+
if (!processEnvParseResult.success) {
|
|
340
378
|
throw new Error(
|
|
341
|
-
|
|
379
|
+
"Environment variables are not valid. Put .env file or set environment variables in process.env.\n" + processEnvParseResult.issues.map((issue) => issue.message).join("\n")
|
|
342
380
|
);
|
|
343
381
|
}
|
|
344
|
-
|
|
345
|
-
return env;
|
|
382
|
+
return processEnvParseResult.output;
|
|
346
383
|
}
|
|
347
384
|
|
|
348
385
|
// src/cms/pull/action.ts
|
|
349
386
|
var spinner2 = createSpinner();
|
|
350
387
|
async function pullAction(options) {
|
|
388
|
+
spinner2.start("Loading environment variables...");
|
|
351
389
|
const env = await loadEnv(options.envPath);
|
|
390
|
+
spinner2.succeed("Loaded environment variables.");
|
|
352
391
|
spinner2.start("Loading project schema...");
|
|
353
392
|
const definitionReference = await fetchDefinitionReference({
|
|
354
393
|
baseUrl: env.ORIZM_BASE_URL,
|
|
@@ -365,32 +404,24 @@ async function pullAction(options) {
|
|
|
365
404
|
}
|
|
366
405
|
|
|
367
406
|
// src/cms/pull/command.ts
|
|
368
|
-
var cmsPullCommand = new import_commander2.Command("pull").description("pull orizm schema and generate client and types.").option("-e, --env
|
|
407
|
+
var cmsPullCommand = new import_commander2.Command("pull").description("pull orizm schema and generate client and types.").option("-e, --env [path]", "env file path.").option("-o, --output <path>", "output directory.", "./src/orizm").action(async (options) => {
|
|
369
408
|
try {
|
|
370
409
|
await pullAction({
|
|
371
410
|
destination: options.output,
|
|
372
411
|
envPath: options.env
|
|
373
412
|
});
|
|
374
413
|
} catch (error2) {
|
|
375
|
-
|
|
376
|
-
console.error(cliColor2.red("Error:", error2.message));
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
console.error("An unexpected error occurred:", error2);
|
|
414
|
+
handleError(error2);
|
|
380
415
|
}
|
|
381
416
|
});
|
|
382
417
|
|
|
383
418
|
// src/consumer/generate/command.ts
|
|
384
419
|
var import_commander3 = require("commander");
|
|
385
420
|
|
|
386
|
-
// src/consumer/generate/action.ts
|
|
387
|
-
var import_cli_color3 = __toESM(require("cli-color"));
|
|
388
|
-
|
|
389
421
|
// src/consumer/pull/generate.ts
|
|
390
422
|
var import_promises3 = require("fs/promises");
|
|
391
423
|
var import_path2 = __toESM(require("path"));
|
|
392
424
|
var import_handlebars2 = __toESM(require("handlebars"));
|
|
393
|
-
var import_cli_color2 = __toESM(require("cli-color"));
|
|
394
425
|
|
|
395
426
|
// src/consumer/templates/client.ts.hbr
|
|
396
427
|
var client_ts_default2 = '// This file was automatically generated and should not be edited.\n\nimport { ConsumerClientOptions, OrizmConsumerClientBase } from "@orizm/consumer-sdk";\nimport * as Models from "./types";\n\nexport class OrizmClient extends OrizmConsumerClientBase {\n constructor(options: ConsumerClientOptions) {\n super(options);\n }\n\n readonly tables = {\n {{#each tables}}\n {{toEndpointName this.name}}: this.getTableClient<Models.{{toCodeTypeName this.name}}__Descriptor>("{{toEndpointName this.name}}"),\n {{/each}}\n };\n}\n';
|
|
@@ -400,11 +431,9 @@ var types_ts_default2 = '// This file was automatically generated and should no
|
|
|
400
431
|
|
|
401
432
|
// src/consumer/pull/generate.ts
|
|
402
433
|
async function generate2(options) {
|
|
403
|
-
console.log("generating ....");
|
|
404
434
|
await (0, import_promises3.mkdir)(options.output, { recursive: true });
|
|
405
435
|
await renderTypes2(options.definition, options.output);
|
|
406
436
|
await renderClient2(options.definition, options.output);
|
|
407
|
-
console.log("output to: ", options.output, " :", import_cli_color2.default.green("OK"));
|
|
408
437
|
}
|
|
409
438
|
import_handlebars2.default.registerHelper("toEndpointName", (str) => {
|
|
410
439
|
return str;
|
|
@@ -451,9 +480,12 @@ import_handlebars2.default.registerHelper("includeType", (column) => {
|
|
|
451
480
|
if (column.type === "module-array" && column.moduleName)
|
|
452
481
|
return `_ArrayModuleRef<${toCodeTypeName2(column.moduleName)}__Descriptor>`;
|
|
453
482
|
});
|
|
454
|
-
import_handlebars2.default.registerHelper(
|
|
455
|
-
|
|
456
|
-
|
|
483
|
+
import_handlebars2.default.registerHelper(
|
|
484
|
+
"filterUniqueIndexes",
|
|
485
|
+
(indexes) => {
|
|
486
|
+
return (indexes || []).filter((i) => i.unique);
|
|
487
|
+
}
|
|
488
|
+
);
|
|
457
489
|
import_handlebars2.default.registerHelper(
|
|
458
490
|
"getIndexColumns",
|
|
459
491
|
(table, index) => {
|
|
@@ -478,246 +510,120 @@ async function renderClient2(config, output) {
|
|
|
478
510
|
}
|
|
479
511
|
|
|
480
512
|
// src/consumer/generate/action.ts
|
|
513
|
+
var spinner3 = createSpinner();
|
|
481
514
|
async function generateAction(orizmConfigTs, destination) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
process.exit(1);
|
|
492
|
-
}
|
|
515
|
+
spinner3.start("Loading project schema...");
|
|
516
|
+
const definition = await loadConfig(orizmConfigTs);
|
|
517
|
+
spinner3.succeed("Loaded project schema.");
|
|
518
|
+
spinner3.start("Generating code...");
|
|
519
|
+
await generate2({
|
|
520
|
+
definition,
|
|
521
|
+
output: destination
|
|
522
|
+
});
|
|
523
|
+
spinner3.succeed(`Generated code to '${destination}'`);
|
|
493
524
|
}
|
|
494
525
|
|
|
495
526
|
// src/consumer/generate/command.ts
|
|
496
527
|
var consumerGenerateCommand = new import_commander3.Command("generate").description("generate orizm client and types.").option("-c, --config <path>", "orizm config file path.", "orizm.config.ts").option("-o, --output <path>", "output directory.", "./src/orizm").action(async (options) => {
|
|
497
|
-
|
|
528
|
+
try {
|
|
529
|
+
await generateAction(options.config, options.output);
|
|
530
|
+
} catch (error2) {
|
|
531
|
+
handleError(error2);
|
|
532
|
+
}
|
|
498
533
|
});
|
|
499
534
|
|
|
500
535
|
// src/consumer/pull/command.ts
|
|
501
536
|
var import_commander4 = require("commander");
|
|
502
537
|
|
|
503
538
|
// src/consumer/pull/action.ts
|
|
504
|
-
var
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
const
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
signal: abortController.signal
|
|
523
|
-
});
|
|
524
|
-
if (response.ok) {
|
|
525
|
-
const definition = await response.json().then((data) => data.content);
|
|
526
|
-
console.log(
|
|
527
|
-
`definitionVersion: ${definition.definitionVersion}`,
|
|
528
|
-
import_cli_color4.default.green("OK")
|
|
529
|
-
);
|
|
530
|
-
return definition;
|
|
531
|
-
} else {
|
|
532
|
-
throw new Error(
|
|
533
|
-
`Failed to pull definition : status code ${response.status}
|
|
534
|
-
${response.statusText}`
|
|
535
|
-
);
|
|
536
|
-
}
|
|
537
|
-
} catch (e) {
|
|
538
|
-
if (e instanceof TypeError || e instanceof DOMException && e.name === "AbortError") {
|
|
539
|
-
throw new Error("Failed to pull definition: " + String(e));
|
|
540
|
-
}
|
|
541
|
-
throw e;
|
|
542
|
-
} finally {
|
|
543
|
-
clearTimeout(abortTimer);
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
// src/consumer/pull/config.ts
|
|
548
|
-
var import_fs = require("fs");
|
|
549
|
-
var import_dotenv2 = require("dotenv");
|
|
550
|
-
async function loadConfig2(configPath) {
|
|
551
|
-
const config = (() => {
|
|
552
|
-
if ((0, import_fs.existsSync)(configPath)) {
|
|
553
|
-
const env = (0, import_fs.readFileSync)(configPath);
|
|
554
|
-
return (0, import_dotenv2.parse)(env);
|
|
555
|
-
} else {
|
|
556
|
-
return {};
|
|
557
|
-
}
|
|
558
|
-
})();
|
|
559
|
-
const projectName = config.ORIZM_PROJECT_NAME || process.env.ORIZM_PROJECT_NAME;
|
|
560
|
-
const consumerKey = config.ORIZM_CONSUMER_KEY || process.env.ORIZM_CONSUMER_KEY;
|
|
561
|
-
const baseUrl = config.ORIZM_BASE_URL || process.env.ORIZM_BASE_URL || "https://app.orizm.com";
|
|
562
|
-
console.log(`ORIZM_BASE_URL : ${baseUrl}`);
|
|
563
|
-
console.log(`ORIZM_PROJECT_NAME: ${projectName}`);
|
|
564
|
-
console.log(`ORIZM_CONSUMER_KEY: ${consumerKey ? "****" : void 0}`);
|
|
565
|
-
if (!projectName || !consumerKey) {
|
|
566
|
-
const errors = [];
|
|
567
|
-
if (!projectName) {
|
|
568
|
-
errors.push(
|
|
569
|
-
"ORIZM_CONSUMER_KEY is not set. Please set ORIZM_CONSUMER_KEY in .env.x-consumer-cli.local or environment variable."
|
|
570
|
-
);
|
|
571
|
-
}
|
|
572
|
-
if (!consumerKey) {
|
|
573
|
-
errors.push(
|
|
574
|
-
"ORIZM_PROJECT_NAME is not set. Please set ORIZM_PROJECT_NAME in .env.x-consumer-cli.local or environment variable."
|
|
575
|
-
);
|
|
576
|
-
}
|
|
577
|
-
throw new Error(errors.join("\n"));
|
|
578
|
-
}
|
|
579
|
-
return {
|
|
580
|
-
projectName,
|
|
581
|
-
consumerKey,
|
|
582
|
-
baseUrl
|
|
583
|
-
};
|
|
539
|
+
var spinner4 = createSpinner();
|
|
540
|
+
async function pullAction2(options) {
|
|
541
|
+
spinner4.start("Loading environment variables...");
|
|
542
|
+
const config = await loadEnv(options.envPath);
|
|
543
|
+
spinner4.succeed("Loaded environment variables.");
|
|
544
|
+
spinner4.start("Loading project schema...");
|
|
545
|
+
const definition = await fetchDefinitionReference({
|
|
546
|
+
baseUrl: config.ORIZM_BASE_URL,
|
|
547
|
+
apiKey: config.ORIZM_API_KEY,
|
|
548
|
+
projectName: config.ORIZM_PROJECT
|
|
549
|
+
});
|
|
550
|
+
spinner4.succeed("Loaded project schema.");
|
|
551
|
+
spinner4.start("Generating code...");
|
|
552
|
+
await generate2({
|
|
553
|
+
definition,
|
|
554
|
+
output: options.destination
|
|
555
|
+
});
|
|
556
|
+
spinner4.succeed(`Generated code to '${options.destination}'`);
|
|
584
557
|
}
|
|
585
558
|
|
|
586
|
-
// src/consumer/pull/
|
|
587
|
-
|
|
559
|
+
// src/consumer/pull/command.ts
|
|
560
|
+
var consumerPullCommand = new import_commander4.Command("pull").description("pull orizm schema and generate client and types.").option("-e, --env [path]", "env file path.").option("-o, --output <path>", "output directory.", "./src/orizm").action(async (options) => {
|
|
588
561
|
try {
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
definition,
|
|
593
|
-
output: options.destination
|
|
562
|
+
await pullAction2({
|
|
563
|
+
destination: options.output,
|
|
564
|
+
envPath: options.env
|
|
594
565
|
});
|
|
595
|
-
} catch (
|
|
596
|
-
|
|
597
|
-
process.exit(1);
|
|
566
|
+
} catch (error2) {
|
|
567
|
+
handleError(error2);
|
|
598
568
|
}
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
// src/consumer/pull/command.ts
|
|
602
|
-
var consumerPullCommand = new import_commander4.Command("pull").description("pull orizm schema and generate client and types.").option("-e, --env <path>", "env file path.", ".env.x-consumer-cli.local").option("-o, --output <path>", "output directory.", "./src/orizm").action(async (options) => {
|
|
603
|
-
await pullAction2({
|
|
604
|
-
destination: options.output,
|
|
605
|
-
envPath: options.env
|
|
606
|
-
});
|
|
607
569
|
});
|
|
608
570
|
|
|
609
571
|
// src/push/command.ts
|
|
610
572
|
var import_commander5 = require("commander");
|
|
611
|
-
var
|
|
612
|
-
|
|
613
|
-
// src/lib/env-manager.ts
|
|
614
|
-
var import_fs2 = __toESM(require("fs"));
|
|
615
|
-
var import_dotenv3 = __toESM(require("dotenv"));
|
|
616
|
-
var import_valibot = require("valibot");
|
|
617
|
-
var defaultEnvFilePath = ".env.x.local";
|
|
618
|
-
var schema = (0, import_valibot.object)({
|
|
619
|
-
ORIZM_PROJECT: (0, import_valibot.pipe)(
|
|
620
|
-
(0, import_valibot.string)(),
|
|
621
|
-
(0, import_valibot.minLength)(1, "ORIZM_PROJECT is required"),
|
|
622
|
-
(0, import_valibot.brand)("ORIZM_PROJECT")
|
|
623
|
-
),
|
|
624
|
-
ORIZM_BASE_URL: (0, import_valibot.pipe)(
|
|
625
|
-
(0, import_valibot.nullish)((0, import_valibot.string)(), "https://app.orizm.com"),
|
|
626
|
-
// if null, use default value
|
|
627
|
-
(0, import_valibot.transform)((v4) => v4.replace(/\/$/, "")),
|
|
628
|
-
// remove trailing slash
|
|
629
|
-
(0, import_valibot.brand)("ORIZM_BASE_URL")
|
|
630
|
-
),
|
|
631
|
-
ORIZM_API_KEY: (0, import_valibot.pipe)(
|
|
632
|
-
(0, import_valibot.string)(),
|
|
633
|
-
(0, import_valibot.minLength)(1, "ORIZM_API_KEY is required"),
|
|
634
|
-
(0, import_valibot.brand)("ORIZM_API_KEY")
|
|
635
|
-
)
|
|
636
|
-
});
|
|
637
|
-
var EnvManager = class {
|
|
638
|
-
parsed;
|
|
639
|
-
constructor(envFilePath) {
|
|
640
|
-
if (envFilePath && import_fs2.default.existsSync(envFilePath)) {
|
|
641
|
-
import_dotenv3.default.config({ path: envFilePath });
|
|
642
|
-
} else if (envFilePath !== defaultEnvFilePath) {
|
|
643
|
-
throw new Error(
|
|
644
|
-
`environment variables file: '${envFilePath}' is not found.`
|
|
645
|
-
);
|
|
646
|
-
} else {
|
|
647
|
-
import_dotenv3.default.config();
|
|
648
|
-
}
|
|
649
|
-
const result = (0, import_valibot.parse)(schema, process.env);
|
|
650
|
-
this.parsed = result;
|
|
651
|
-
}
|
|
652
|
-
get projectName() {
|
|
653
|
-
return this.parsed.ORIZM_PROJECT;
|
|
654
|
-
}
|
|
655
|
-
get baseUrl() {
|
|
656
|
-
return this.parsed.ORIZM_BASE_URL;
|
|
657
|
-
}
|
|
658
|
-
get apiKey() {
|
|
659
|
-
return this.parsed.ORIZM_API_KEY;
|
|
660
|
-
}
|
|
661
|
-
};
|
|
573
|
+
var import_cli_color3 = require("cli-color");
|
|
662
574
|
|
|
663
575
|
// src/lib/developer-api.ts
|
|
664
|
-
async function request(props) {
|
|
665
|
-
return await fetch(`${props.env.baseUrl}/api/dev/v1${props.path}`, {
|
|
666
|
-
method: props.method,
|
|
667
|
-
headers: {
|
|
668
|
-
"content-type": "application/json",
|
|
669
|
-
"x-api-key": props.env.apiKey
|
|
670
|
-
},
|
|
671
|
-
body: JSON.stringify(props.body)
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
576
|
async function applyDefinition(env, projectDef) {
|
|
675
|
-
const
|
|
676
|
-
env,
|
|
677
|
-
|
|
678
|
-
|
|
577
|
+
const client = createDeveloperAPIClient(
|
|
578
|
+
env.ORIZM_BASE_URL,
|
|
579
|
+
env.ORIZM_API_KEY
|
|
580
|
+
);
|
|
581
|
+
const response = await client.POST("/projects/{projectName}/definitions", {
|
|
582
|
+
params: { path: { projectName: env.ORIZM_PROJECT } },
|
|
583
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
679
584
|
body: { apply: true, project: projectDef }
|
|
680
585
|
});
|
|
681
|
-
if (!response.
|
|
682
|
-
const status = response.status;
|
|
586
|
+
if (!response.data) {
|
|
587
|
+
const status = response.response.status;
|
|
683
588
|
if (status === 401) {
|
|
684
589
|
throw new Error(
|
|
685
590
|
"401 Unauthorized. Please check your Developer API key and try again."
|
|
686
591
|
);
|
|
687
592
|
}
|
|
688
593
|
throw new Error(
|
|
689
|
-
`${status} ${response.statusText}:
|
|
594
|
+
`${status} ${response.response.statusText}: Unexpected error occurred while applying schema.`
|
|
690
595
|
);
|
|
691
596
|
}
|
|
692
597
|
}
|
|
693
598
|
var getChangeEvents = async (env, projectDef) => {
|
|
694
|
-
const
|
|
695
|
-
env,
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
throw new Error(`400 Bad Request:
|
|
705
|
-
${JSON.stringify(body, null, 2)}`);
|
|
599
|
+
const client = createDeveloperAPIClient(
|
|
600
|
+
env.ORIZM_BASE_URL,
|
|
601
|
+
env.ORIZM_API_KEY
|
|
602
|
+
);
|
|
603
|
+
const response = await client.POST(
|
|
604
|
+
"/projects/{projectName}/definitions/dry-run",
|
|
605
|
+
{
|
|
606
|
+
params: { path: { projectName: env.ORIZM_PROJECT } },
|
|
607
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
608
|
+
body: { project: projectDef }
|
|
706
609
|
}
|
|
610
|
+
);
|
|
611
|
+
if (!response.data) {
|
|
612
|
+
const status = response.response.status;
|
|
707
613
|
if (status === 401) {
|
|
708
614
|
throw new Error(
|
|
709
|
-
"401 Unauthorized
|
|
615
|
+
"401 Unauthorized. Please check your Developer API key and try again."
|
|
710
616
|
);
|
|
711
617
|
}
|
|
712
618
|
throw new Error(
|
|
713
|
-
`${status} ${response.statusText}:
|
|
619
|
+
`${status} ${response.response.statusText}: Unexpected error occurred while fetching schema change events.`
|
|
714
620
|
);
|
|
715
621
|
}
|
|
716
|
-
return
|
|
622
|
+
return response.data;
|
|
717
623
|
};
|
|
718
624
|
|
|
719
625
|
// src/lib/display-change-events.ts
|
|
720
|
-
var
|
|
626
|
+
var import_cli_color2 = require("cli-color");
|
|
721
627
|
|
|
722
628
|
// src/lib/cli-table.ts
|
|
723
629
|
var COLORS = {
|
|
@@ -819,10 +725,23 @@ var CliTable = class {
|
|
|
819
725
|
|
|
820
726
|
// src/lib/display-change-events.ts
|
|
821
727
|
async function displayChangeEvents(changeEvents) {
|
|
822
|
-
const
|
|
823
|
-
|
|
728
|
+
const {
|
|
729
|
+
tableChangeEvents = [],
|
|
730
|
+
bucketChangeEvents = [],
|
|
731
|
+
authorityChangeEvents = []
|
|
732
|
+
} = changeEvents;
|
|
733
|
+
if (tableChangeEvents.length === 0 && bucketChangeEvents.length === 0 && authorityChangeEvents.length === 0) {
|
|
824
734
|
console.log();
|
|
825
|
-
console.log((0,
|
|
735
|
+
console.log((0, import_cli_color2.green)("No changes."));
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
displayTableChangeEvents(tableChangeEvents);
|
|
739
|
+
displayBucketChangeEvents(bucketChangeEvents);
|
|
740
|
+
displayAuthorityChangeEvents(authorityChangeEvents);
|
|
741
|
+
console.log();
|
|
742
|
+
}
|
|
743
|
+
function displayTableChangeEvents(tableChangeEvents) {
|
|
744
|
+
if (tableChangeEvents.length === 0) {
|
|
826
745
|
return;
|
|
827
746
|
}
|
|
828
747
|
const eventsByTable = /* @__PURE__ */ new Map();
|
|
@@ -974,9 +893,81 @@ async function displayChangeEvents(changeEvents) {
|
|
|
974
893
|
}
|
|
975
894
|
}
|
|
976
895
|
table.print();
|
|
977
|
-
console.log();
|
|
978
896
|
}
|
|
979
897
|
}
|
|
898
|
+
function displayBucketChangeEvents(bucketChangeEvents) {
|
|
899
|
+
if (bucketChangeEvents.length === 0) {
|
|
900
|
+
return;
|
|
901
|
+
}
|
|
902
|
+
const eventsByBucket = /* @__PURE__ */ new Map();
|
|
903
|
+
for (const event of bucketChangeEvents) {
|
|
904
|
+
const bucketName = event.bucketDef.name;
|
|
905
|
+
const bucketEvents = eventsByBucket.get(bucketName) ?? [];
|
|
906
|
+
bucketEvents.push(event);
|
|
907
|
+
eventsByBucket.set(bucketName, bucketEvents);
|
|
908
|
+
}
|
|
909
|
+
const table = new CliTable(["Type", "Name", "Event", "Detail"]);
|
|
910
|
+
console.log();
|
|
911
|
+
console.log("\u26AB\uFE0EBuckets");
|
|
912
|
+
for (const [_, events] of eventsByBucket.entries()) {
|
|
913
|
+
for (const event of events) {
|
|
914
|
+
const eventName = event.event;
|
|
915
|
+
switch (eventName) {
|
|
916
|
+
case "bucket:create":
|
|
917
|
+
case "bucket:update": {
|
|
918
|
+
table.addRow(
|
|
919
|
+
[
|
|
920
|
+
"bucket",
|
|
921
|
+
event.bucketDef.name,
|
|
922
|
+
eventName,
|
|
923
|
+
JSON.stringify(event.bucketDef)
|
|
924
|
+
],
|
|
925
|
+
{
|
|
926
|
+
color: "green"
|
|
927
|
+
}
|
|
928
|
+
);
|
|
929
|
+
break;
|
|
930
|
+
}
|
|
931
|
+
case "bucket:delete": {
|
|
932
|
+
table.addRow(
|
|
933
|
+
[
|
|
934
|
+
"bucket",
|
|
935
|
+
event.bucketDef.name,
|
|
936
|
+
eventName,
|
|
937
|
+
JSON.stringify(event.bucketDef)
|
|
938
|
+
],
|
|
939
|
+
{
|
|
940
|
+
color: "red"
|
|
941
|
+
}
|
|
942
|
+
);
|
|
943
|
+
break;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
table.print();
|
|
949
|
+
}
|
|
950
|
+
function displayAuthorityChangeEvents(authorityChangeEvents) {
|
|
951
|
+
if (authorityChangeEvents.length === 0) {
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
const table = new CliTable(["Type", "Event", "Detail"]);
|
|
955
|
+
console.log();
|
|
956
|
+
console.log("\u26AB\uFE0EAuthorities");
|
|
957
|
+
for (const event of authorityChangeEvents) {
|
|
958
|
+
const eventName = event.event;
|
|
959
|
+
switch (eventName) {
|
|
960
|
+
case "authority:update": {
|
|
961
|
+
table.addRow(
|
|
962
|
+
["authority", eventName, JSON.stringify(event.authorityDef)],
|
|
963
|
+
{ color: "green" }
|
|
964
|
+
);
|
|
965
|
+
break;
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
table.print();
|
|
970
|
+
}
|
|
980
971
|
|
|
981
972
|
// src/lib/cli-input.ts
|
|
982
973
|
var import_promises4 = require("readline/promises");
|
|
@@ -992,20 +983,20 @@ async function input(prompt) {
|
|
|
992
983
|
}
|
|
993
984
|
|
|
994
985
|
// src/push/command.ts
|
|
995
|
-
var
|
|
996
|
-
var pushCommand = new import_commander5.Command("push").description("apply schema to project.").option("-e, --env <path>", "env file path."
|
|
986
|
+
var spinner5 = createSpinner();
|
|
987
|
+
var pushCommand = new import_commander5.Command("push").description("apply schema to project.").option("-e, --env <path>", "env file path.").option("-c, --config <path>", "orizm config file path.", "orizm.config.ts").option("--dry-run", "display change-events without applying schema.", false).option("-y, --yes", "apply schema without confirmation.", false).action(async (options) => {
|
|
997
988
|
try {
|
|
998
|
-
|
|
999
|
-
const env =
|
|
1000
|
-
|
|
1001
|
-
|
|
989
|
+
spinner5.start("Loading environment variables...");
|
|
990
|
+
const env = await loadEnv(options.env);
|
|
991
|
+
spinner5.succeed("Loaded environment variables.");
|
|
992
|
+
spinner5.start("Loading project schema...");
|
|
1002
993
|
const projectDef = await loadConfig(options.config);
|
|
1003
|
-
|
|
1004
|
-
|
|
994
|
+
spinner5.succeed("Loaded project schema.");
|
|
995
|
+
spinner5.start("Fetch schema change events...");
|
|
1005
996
|
const changeEvents = await getChangeEvents(env, projectDef);
|
|
1006
|
-
|
|
997
|
+
spinner5.succeed("Fetch schema change events.");
|
|
1007
998
|
await displayChangeEvents(changeEvents);
|
|
1008
|
-
if (changeEvents.tableChangeEvents.length === 0) {
|
|
999
|
+
if (changeEvents.tableChangeEvents.length === 0 && changeEvents.bucketChangeEvents.length === 0 && changeEvents.authorityChangeEvents.length === 0) {
|
|
1009
1000
|
return;
|
|
1010
1001
|
}
|
|
1011
1002
|
if (options.dryRun) {
|
|
@@ -1014,13 +1005,13 @@ var pushCommand = new import_commander5.Command("push").description("apply schem
|
|
|
1014
1005
|
if (!options.yes) {
|
|
1015
1006
|
const confirm = await input("Do you want to apply schema? (y/N): ");
|
|
1016
1007
|
if (confirm !== "y") {
|
|
1017
|
-
console.log((0,
|
|
1008
|
+
console.log((0, import_cli_color3.red)("Canceled."));
|
|
1018
1009
|
return;
|
|
1019
1010
|
}
|
|
1020
1011
|
}
|
|
1021
|
-
|
|
1012
|
+
spinner5.start("Applying schema...");
|
|
1022
1013
|
await applyDefinition(env, projectDef);
|
|
1023
|
-
|
|
1014
|
+
spinner5.succeed("Schema applied.");
|
|
1024
1015
|
} catch (error2) {
|
|
1025
1016
|
handleError(error2);
|
|
1026
1017
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orizm/cli",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Orizm CLI",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"handlebars": "^4.7.7",
|
|
20
20
|
"openapi-fetch": "^0.12.2",
|
|
21
21
|
"valibot": "^1.0.0",
|
|
22
|
-
"@orizm/common": "1.
|
|
22
|
+
"@orizm/common": "1.12.0"
|
|
23
23
|
},
|
|
24
24
|
"bin": {
|
|
25
25
|
"orizm": "bin/index.js"
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
},
|
|
32
32
|
"license": "Apache-2.0",
|
|
33
33
|
"scripts": {
|
|
34
|
+
"dev": "tsup --watch",
|
|
34
35
|
"build": "tsup",
|
|
35
36
|
"typecheck": "tsc --noEmit",
|
|
36
37
|
"generate": "openapi-typescript ../../apps/server/openapi/developerAPI.yaml --output src/api/developer-api.d.ts"
|