@extrahorizon/exh-cli 1.12.0-dev-139-711ee70 → 1.12.0-dev-141-b72e89f

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.
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildTemplates = void 0;
4
4
  const templateRepository = require("../../../repositories/templates");
5
+ const templateV2Repository = require("../../../repositories/templatesV2");
6
+ const utils_1 = require("./utils");
5
7
  async function buildTemplates(templateFilesByName) {
6
8
  const templates = [];
7
9
  for (const name of Object.keys(templateFilesByName)) {
@@ -11,41 +13,98 @@ async function buildTemplates(templateFilesByName) {
11
13
  }
12
14
  exports.buildTemplates = buildTemplates;
13
15
  async function buildTemplate(name, templateFilesByName, callChain = []) {
16
+ const newCallChain = [...callChain, name];
14
17
  if (callChain.includes(name)) {
15
- throw new Error(`Circular extension detected. ${[...callChain, name].reverse().map((n) => `'${n}'`).join(' > ')}`);
18
+ throw new Error(`Circular extension detected. ${renderCallChain(newCallChain)}`);
16
19
  }
17
- let templateFile = templateFilesByName[name];
18
- if (templateFile === undefined) {
19
- templateFile = await templateRepository.findByName(name);
20
- if (!templateFile) {
21
- throw new Error(`Template file dependency ${name} not found!`);
22
- }
20
+ const templateFile = templateFilesByName[name];
21
+ if (!templateFile) {
22
+ throw new Error(`Template file dependency ${name} not found!`);
23
+ }
24
+ if (templateFile.extends_template) {
25
+ return await extendV1Template(name, templateFilesByName, newCallChain);
26
+ }
27
+ if (templateFile.extendsTemplate) {
28
+ return await extendV2Template(name, templateFilesByName, newCallChain);
29
+ }
30
+ return { ...templateFile, name };
31
+ }
32
+ async function extendV1Template(name, templateFilesByName, callChain) {
33
+ const { extends_template, description, schema, fields } = templateFilesByName[name];
34
+ if (!templateFilesByName[extends_template]) {
35
+ templateFilesByName[extends_template] = await templateRepository.findByName(extends_template);
36
+ }
37
+ const extendingTemplate = await buildTemplate(extends_template, templateFilesByName, callChain);
38
+ if (!(0, utils_1.isV1Template)(extendingTemplate)) {
39
+ throw v1ExtendingV2Error(extends_template, callChain);
23
40
  }
24
- const { description, extends_template, schema } = templateFile;
25
- let { fields } = templateFile;
26
- if (extends_template) {
27
- const extendingTemplate = await buildTemplate(extends_template, templateFilesByName, [...callChain, name]);
28
- const match = (_fullMatch, variableName) => {
29
- const value = fields[variableName];
30
- if (typeof value === 'undefined') {
31
- throw new Error(`Could not find a value to fill '$content.${variableName}' ` +
32
- `while extending '${extends_template}' ` +
33
- `in ${[...callChain, name].reverse().map(n => `'${n}'`).join(' > ')}`);
34
- }
35
- return value;
36
- };
37
- const extendedFields = {};
38
- for (const extendingField of Object.keys(extendingTemplate.fields)) {
39
- const extendedValue = extendingTemplate.fields[extendingField]
40
- .replace(/\$content\.([a-z0-9_-]+)/ig, match);
41
- extendedFields[extendingField] = extendedValue;
41
+ const resolveMatch = (_fullMatch, variableName) => {
42
+ const value = fields[variableName];
43
+ if (typeof value === 'undefined') {
44
+ throw v1VariableNotFoundError(variableName, extends_template, callChain);
42
45
  }
43
- fields = extendedFields;
46
+ return value;
47
+ };
48
+ const extendedFields = {};
49
+ for (const extendingField of Object.keys(extendingTemplate.fields)) {
50
+ const extendedValue = extendingTemplate.fields[extendingField]
51
+ .replace(/\$content\.([a-z0-9_-]+)/ig, resolveMatch);
52
+ extendedFields[extendingField] = extendedValue;
44
53
  }
45
54
  return {
46
55
  name,
47
56
  description,
48
57
  schema,
49
- fields,
58
+ fields: extendedFields,
59
+ };
60
+ }
61
+ async function extendV2Template(name, templateFilesByName, callChain) {
62
+ const { extendsTemplate, description, properties, outputs } = templateFilesByName[name];
63
+ if (!templateFilesByName[extendsTemplate]) {
64
+ templateFilesByName[extendsTemplate] = await templateV2Repository.findByName(extendsTemplate);
65
+ }
66
+ const extendingTemplate = await buildTemplate(extendsTemplate, templateFilesByName, callChain);
67
+ if ((0, utils_1.isV1Template)(extendingTemplate)) {
68
+ throw v2ExtendingV1Error(extendsTemplate, callChain);
69
+ }
70
+ const resolveMatch = (_fullMatch, variableName) => {
71
+ const value = outputs[variableName];
72
+ if (typeof value === 'undefined') {
73
+ throw v2VariableNotFoundError(variableName, extendsTemplate, callChain);
74
+ }
75
+ return value;
76
+ };
77
+ const newOutputs = {};
78
+ for (const output of Object.keys(extendingTemplate.outputs)) {
79
+ const extendedValue = extendingTemplate.outputs[output]
80
+ .replace(/{{[ ]*@data\.([a-zA-Z0-9_-]+)[ ]*}}/g, resolveMatch);
81
+ newOutputs[output] = extendedValue;
82
+ }
83
+ return {
84
+ name,
85
+ description,
86
+ properties,
87
+ outputs: newOutputs,
50
88
  };
51
89
  }
90
+ function v1ExtendingV2Error(extendsTemplate, callChain) {
91
+ return new Error(`You cannot extend a v2 template ('${extendsTemplate}') in a v1 template!` +
92
+ `In ${renderCallChain(callChain)}`);
93
+ }
94
+ function v1VariableNotFoundError(variableName, extendsTemplate, callChain) {
95
+ return new Error(`Could not find a value to fill '$content.${variableName}' ` +
96
+ `while extending '${extendsTemplate}' ` +
97
+ `in ${renderCallChain(callChain)}`);
98
+ }
99
+ function v2ExtendingV1Error(extendsTemplate, callChain) {
100
+ return new Error(`You cannot extend a v1 template ('${extendsTemplate}') in a v2 template!` +
101
+ `In ${renderCallChain(callChain)}`);
102
+ }
103
+ function v2VariableNotFoundError(variableName, extendsTemplate, callChain) {
104
+ return new Error(`Could not find a value to fill '{{@data.${variableName}}}' ` +
105
+ `while extending '${extendsTemplate}' ` +
106
+ `in ${renderCallChain(callChain)}`);
107
+ }
108
+ function renderCallChain(callChain) {
109
+ return callChain.reverse().map(part => `'${part}'`).join(' > ');
110
+ }
@@ -26,27 +26,32 @@ async function readTemplateJson(fileName) {
26
26
  exports.readTemplateJson = readTemplateJson;
27
27
  async function readTemplateFolder(subFolder) {
28
28
  const templateFile = await readTemplateJson(path.join(subFolder, 'template.json'));
29
- const templateFields = await readTemplateFolderFieldFiles(subFolder);
30
- if (!templateFile.fields) {
31
- templateFile.fields = templateFields;
32
- }
33
- else {
29
+ const extraFiles = await readExtraTemplateFolderFiles(subFolder);
30
+ if ((0, utils_1.isV1Template)(templateFile)) {
34
31
  templateFile.fields = {
35
32
  ...templateFile.fields,
36
- ...templateFields,
33
+ ...extraFiles,
34
+ };
35
+ }
36
+ else {
37
+ templateFile.outputs = {
38
+ ...templateFile.outputs,
39
+ ...extraFiles,
37
40
  };
38
41
  }
39
42
  return templateFile;
40
43
  }
41
44
  exports.readTemplateFolder = readTemplateFolder;
42
- async function readTemplateFolderFieldFiles(subFolder) {
43
- const templateFields = {};
44
- const folderFileNames = await (0, utils_1.listFolderContent)(subFolder);
45
- const fieldFileNames = folderFileNames.filter((fileName) => fileName !== 'template.json');
46
- for (const fieldFileName of fieldFileNames) {
47
- const fieldName = (0, utils_1.removeFileNameExtension)(fieldFileName);
48
- const fieldContent = await (0, utils_1.readTextFile)(path.join(subFolder, fieldFileName));
49
- templateFields[fieldName] = fieldContent;
45
+ async function readExtraTemplateFolderFiles(subFolder) {
46
+ const extraFiles = {};
47
+ const fileNames = await (0, utils_1.listFolderContent)(subFolder);
48
+ for (const fileName of fileNames) {
49
+ if (fileName === 'template.json') {
50
+ continue;
51
+ }
52
+ const name = (0, utils_1.removeFileNameExtension)(fileName);
53
+ const content = await (0, utils_1.readTextFile)(path.join(subFolder, fileName));
54
+ extraFiles[name] = content;
50
55
  }
51
- return templateFields;
56
+ return extraFiles;
52
57
  }
@@ -2,7 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.uploadTemplate = void 0;
4
4
  const templateRepository = require("../../../repositories/templates");
5
+ const templateV2Repository = require("../../../repositories/templatesV2");
6
+ const utils_1 = require("./utils");
5
7
  async function uploadTemplate(template) {
8
+ if ((0, utils_1.isV1Template)(template)) {
9
+ await uploadV1Template(template);
10
+ }
11
+ else {
12
+ await uploadV2Template(template);
13
+ }
14
+ }
15
+ exports.uploadTemplate = uploadTemplate;
16
+ async function uploadV1Template(template) {
6
17
  const existingTemplate = await templateRepository.findByName(template.name);
7
18
  try {
8
19
  if (!existingTemplate) {
@@ -18,4 +29,19 @@ async function uploadTemplate(template) {
18
29
  throw new Error(`Error creating or updating template: ${JSON.stringify(err.response)}`);
19
30
  }
20
31
  }
21
- exports.uploadTemplate = uploadTemplate;
32
+ async function uploadV2Template(template) {
33
+ const existingTemplate = await templateV2Repository.findByName(template.name);
34
+ try {
35
+ if (!existingTemplate) {
36
+ console.log(`Creating new template '${template.name}'`);
37
+ await templateV2Repository.create(template);
38
+ }
39
+ else {
40
+ console.log(`Updating existing template '${template.name}'`);
41
+ await templateV2Repository.update(existingTemplate.id, template);
42
+ }
43
+ }
44
+ catch (err) {
45
+ throw new Error(`Error creating or updating template: ${JSON.stringify(err.response)}`);
46
+ }
47
+ }
@@ -2,3 +2,4 @@ export declare function listFolderContent(path: string): Promise<string[]>;
2
2
  export declare function readJsonFile(path: string): Promise<any>;
3
3
  export declare function readTextFile(path: string): Promise<string>;
4
4
  export declare function removeFileNameExtension(fileName: string): string;
5
+ export declare function isV1Template(template: any): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeFileNameExtension = exports.readTextFile = exports.readJsonFile = exports.listFolderContent = void 0;
3
+ exports.isV1Template = exports.removeFileNameExtension = exports.readTextFile = exports.readJsonFile = exports.listFolderContent = void 0;
4
4
  const promises_1 = require("fs/promises");
5
5
  async function listFolderContent(path) {
6
6
  return await (0, promises_1.readdir)(path);
@@ -20,3 +20,7 @@ function removeFileNameExtension(fileName) {
20
20
  return fileName.split('.')[0];
21
21
  }
22
22
  exports.removeFileNameExtension = removeFileNameExtension;
23
+ function isV1Template(template) {
24
+ return !!template.schema || !!template.fields;
25
+ }
26
+ exports.isV1Template = isV1Template;
@@ -1,7 +1,7 @@
1
1
  import type { TemplateIn } from '@extrahorizon/javascript-sdk';
2
- export declare function findByName(name: string): Promise<import("@extrahorizon/javascript-sdk").TemplateOut>;
3
- export declare function findById(id: string): Promise<import("@extrahorizon/javascript-sdk").TemplateOut>;
4
- export declare function findAll(): Promise<import("@extrahorizon/javascript-sdk").TemplateOut[]>;
5
- export declare function create(data: TemplateIn): Promise<import("@extrahorizon/javascript-sdk").TemplateOut>;
6
- export declare function update(id: string, data: TemplateIn): Promise<import("@extrahorizon/javascript-sdk").TemplateOut>;
2
+ export declare function findByName(name: string): Promise<import("@extrahorizon/javascript-sdk").Template>;
3
+ export declare function findById(id: string): Promise<import("@extrahorizon/javascript-sdk").Template>;
4
+ export declare function findAll(): Promise<import("@extrahorizon/javascript-sdk").Template[]>;
5
+ export declare function create(data: TemplateIn): Promise<import("@extrahorizon/javascript-sdk").Template>;
6
+ export declare function update(id: string, data: TemplateIn): Promise<import("@extrahorizon/javascript-sdk").Template>;
7
7
  export declare function remove(id: string): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
@@ -0,0 +1,7 @@
1
+ import type { TemplateV2Creation } from '@extrahorizon/javascript-sdk';
2
+ export declare function findByName(name: string): Promise<import("@extrahorizon/javascript-sdk").TemplateV2>;
3
+ export declare function findById(id: string): Promise<import("@extrahorizon/javascript-sdk").TemplateV2>;
4
+ export declare function findAll(): Promise<import("@extrahorizon/javascript-sdk").TemplateV2[]>;
5
+ export declare function create(data: TemplateV2Creation): Promise<import("@extrahorizon/javascript-sdk").TemplateV2>;
6
+ export declare function update(id: string, data: Partial<TemplateV2Creation>): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
7
+ export declare function remove(id: string): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.remove = exports.update = exports.create = exports.findAll = exports.findById = exports.findByName = void 0;
4
+ const exh_1 = require("../exh");
5
+ async function findByName(name) {
6
+ return await (0, exh_1.getSdk)().templatesV2.findByName(name);
7
+ }
8
+ exports.findByName = findByName;
9
+ async function findById(id) {
10
+ return await (0, exh_1.getSdk)().templatesV2.findById(id);
11
+ }
12
+ exports.findById = findById;
13
+ async function findAll() {
14
+ return await (0, exh_1.getSdk)().templatesV2.findAll();
15
+ }
16
+ exports.findAll = findAll;
17
+ async function create(data) {
18
+ return await (0, exh_1.getSdk)().templatesV2.create(data);
19
+ }
20
+ exports.create = create;
21
+ async function update(id, data) {
22
+ return await (0, exh_1.getSdk)().templatesV2.update(id, data);
23
+ }
24
+ exports.update = update;
25
+ async function remove(id) {
26
+ return await (0, exh_1.getSdk)().templatesV2.remove(id);
27
+ }
28
+ exports.remove = remove;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@extrahorizon/exh-cli",
3
- "version": "1.12.0-dev-139-711ee70",
3
+ "version": "1.12.0-dev-141-b72e89f",
4
4
  "main": "build/index.js",
5
5
  "exports": "./build/index.js",
6
6
  "license": "MIT",
@@ -41,7 +41,7 @@
41
41
  "typescript": "4.5.5"
42
42
  },
43
43
  "dependencies": {
44
- "@extrahorizon/javascript-sdk": "^8.9.0-dev-138-3022f93",
44
+ "@extrahorizon/javascript-sdk": "8.9.0-dev-139-729209a",
45
45
  "ajv": "^8.11.0",
46
46
  "archiver": "^7.0.1",
47
47
  "chalk": "^4.0.0",