@contrail/cli 1.0.3 → 1.0.4
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/lib/{create-app → apps/create-app}/create.d.ts +0 -0
- package/lib/{create-app → apps/create-app}/create.js +1 -5
- package/lib/{create-app → apps/create-app}/setup-template.d.ts +0 -0
- package/lib/{create-app → apps/create-app}/setup-template.js +1 -1
- package/lib/{deploy-app → apps/deploy-app}/actions-deployer.d.ts +0 -0
- package/lib/{deploy-app → apps/deploy-app}/actions-deployer.js +4 -4
- package/lib/{deploy-app → apps/deploy-app}/app-deployer.d.ts +0 -0
- package/lib/{deploy-app → apps/deploy-app}/app-deployer.js +1 -1
- package/lib/{deploy-app → apps/deploy-app}/app-template-parsers.d.ts +0 -0
- package/lib/{deploy-app → apps/deploy-app}/app-template-parsers.js +0 -0
- package/lib/{deploy-app → apps/deploy-app}/deploy.d.ts +0 -0
- package/lib/{deploy-app → apps/deploy-app}/deploy.js +4 -2
- package/lib/{deploy-app → apps/deploy-app}/interfaces.d.ts +0 -0
- package/lib/{deploy-app → apps/deploy-app}/interfaces.js +0 -0
- package/lib/auth/login.d.ts +2 -1
- package/lib/auth/login.js +3 -2
- package/lib/cli.js +9 -3
- package/lib/common.d.ts +1 -0
- package/lib/common.js +5 -0
- package/lib/types/interfaces.d.ts +51 -0
- package/lib/types/interfaces.js +0 -0
- package/lib/types/option-set-uploader.d.ts +1 -0
- package/lib/types/option-set-uploader.js +75 -0
- package/lib/types/types-extractor.d.ts +1 -0
- package/lib/types/types-extractor.js +93 -0
- package/lib/types/types-uploader.d.ts +1 -0
- package/lib/types/types-uploader.js +171 -0
- package/package.json +1 -1
- package/src/{create-app → apps/create-app}/create.ts +0 -0
- package/src/{create-app → apps/create-app}/setup-template.ts +1 -1
- package/src/{deploy-app → apps/deploy-app}/actions-deployer.ts +5 -4
- package/src/{deploy-app → apps/deploy-app}/app-deployer.ts +1 -1
- package/src/{deploy-app → apps/deploy-app}/app-template-parsers.ts +0 -0
- package/src/{deploy-app → apps/deploy-app}/deploy.ts +4 -2
- package/src/{deploy-app → apps/deploy-app}/interfaces.ts +0 -0
- package/src/auth/login.ts +7 -5
- package/src/cli.ts +9 -3
- package/src/common.ts +5 -0
- package/src/types/interfaces.ts +54 -0
- package/src/types/option-set-uploader.ts +83 -0
- package/src/types/types-extractor.ts +116 -0
- package/src/types/types-uploader.ts +198 -0
- package/tsconfig.json +10 -2
- package/templates/javascript/set_auth.bat +0 -3
- package/templates/javascript/set_auth.sh +0 -3
- package/templates/typescript/set_auth.bat +0 -3
- package/templates/typescript/set_auth.sh +0 -3
|
File without changes
|
|
@@ -37,11 +37,7 @@ async function promptForMissingOptions(options) {
|
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
39
|
const answers = await inquirer.prompt(questions);
|
|
40
|
-
return {
|
|
41
|
-
...options,
|
|
42
|
-
appName: options.appName || answers.appName,
|
|
43
|
-
template: options.template || answers.template,
|
|
44
|
-
};
|
|
40
|
+
return Object.assign(Object.assign({}, options), { appName: options.appName || answers.appName, template: options.template || answers.template });
|
|
45
41
|
}
|
|
46
42
|
export async function create(args) {
|
|
47
43
|
let options = parseArgumentsIntoOptions(args);
|
|
File without changes
|
|
@@ -12,7 +12,7 @@ function copyTemplate(templateDir) {
|
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
14
|
export async function setUpTemplate(options) {
|
|
15
|
-
const templateDir = path.resolve(new URL(currentFileUrl).pathname,
|
|
15
|
+
const templateDir = path.resolve(new URL(currentFileUrl).pathname, `../../../../templates/${options.template.toLocaleLowerCase()}`).replace(/\\/g, '\\').replace(/(.):\\(.):/g, '').replace(/%20/g, ' ');
|
|
16
16
|
try {
|
|
17
17
|
const tasks = new Listr([
|
|
18
18
|
{
|
|
File without changes
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Entities } from '@contrail/sdk';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
3
|
import { ConfigPropertiesParser } from './app-template-parsers';
|
|
4
|
+
import { getSeparatorByPlatform } from '../../common';
|
|
4
5
|
import fs from 'fs';
|
|
5
6
|
export class ActionsDeployer {
|
|
6
7
|
async deployActions(appDeclaration, app) {
|
|
@@ -77,10 +78,9 @@ export class ActionsDeployer {
|
|
|
77
78
|
throw new Error('Action handler must be a .js file.');
|
|
78
79
|
}
|
|
79
80
|
const CURRENT_DIR = process.cwd();
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const
|
|
83
|
-
const content = fs.readFileSync(functionLocaiton);
|
|
81
|
+
const separator = getSeparatorByPlatform();
|
|
82
|
+
const functionLocation = CURRENT_DIR + separator + actionDeclaration.handler;
|
|
83
|
+
const content = fs.readFileSync(functionLocation);
|
|
84
84
|
return content.toString();
|
|
85
85
|
}
|
|
86
86
|
}
|
|
File without changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Entities } from '@contrail/sdk';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
|
-
import { Auth } from '
|
|
3
|
+
import { Auth } from '../../auth/auth';
|
|
4
4
|
import { ActionsDeployer } from './actions-deployer';
|
|
5
5
|
import { TriggerKeyDefinitionParser, ConfigPropertiesParser } from './app-template-parsers';
|
|
6
6
|
export class AppDeployer {
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
-
import { authUser } from '
|
|
3
|
+
import { authUser } from '../../auth/login';
|
|
4
4
|
import { AppDeployer } from './app-deployer';
|
|
5
|
+
import { getSeparatorByPlatform } from '../../common';
|
|
5
6
|
const yaml = require('js-yaml');
|
|
6
7
|
function loadAppConfig() {
|
|
7
8
|
const TARGET_DIR = process.cwd();
|
|
8
9
|
try {
|
|
9
|
-
const
|
|
10
|
+
const separator = getSeparatorByPlatform();
|
|
11
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'app.yml', 'utf8'));
|
|
10
12
|
console.log(doc);
|
|
11
13
|
return doc;
|
|
12
14
|
}
|
|
File without changes
|
|
File without changes
|
package/lib/auth/login.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { AuthContext } from './auth';
|
|
2
|
+
export declare function authUser(): Promise<AuthContext>;
|
package/lib/auth/login.js
CHANGED
|
@@ -60,8 +60,8 @@ export async function authUser() {
|
|
|
60
60
|
cognitoUser.authenticateUser(authDetails, {
|
|
61
61
|
onSuccess: async (result) => {
|
|
62
62
|
const user = result;
|
|
63
|
-
await setAuthContext(user, params);
|
|
64
|
-
resolve(
|
|
63
|
+
const config = await setAuthContext(user, params);
|
|
64
|
+
resolve(config);
|
|
65
65
|
},
|
|
66
66
|
onFailure: (err) => {
|
|
67
67
|
reject(err);
|
|
@@ -109,4 +109,5 @@ async function setAuthContext(user, params) {
|
|
|
109
109
|
if (org) {
|
|
110
110
|
Auth.config.orgId = org.orgId || org.id;
|
|
111
111
|
}
|
|
112
|
+
return Object.assign({}, Auth.config);
|
|
112
113
|
}
|
package/lib/cli.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { create } from './create-app/create';
|
|
2
|
-
import { deployApp, removeApp } from './deploy-app/deploy';
|
|
1
|
+
import { create } from './apps/create-app/create';
|
|
2
|
+
import { deployApp, removeApp } from './apps/deploy-app/deploy';
|
|
3
|
+
import { getTypes } from './types/types-extractor';
|
|
4
|
+
import { uploadTypes } from './types/types-uploader';
|
|
3
5
|
/**
|
|
4
6
|
* Built using documentation here:
|
|
5
7
|
* https://www.twilio.com/blog/how-to-build-a-cli-with-node-js
|
|
@@ -20,7 +22,11 @@ export async function cli(args) {
|
|
|
20
22
|
break;
|
|
21
23
|
}
|
|
22
24
|
case 'extract-types': {
|
|
23
|
-
|
|
25
|
+
getTypes(args);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
case 'upload-types': {
|
|
29
|
+
uploadTypes(args);
|
|
24
30
|
break;
|
|
25
31
|
}
|
|
26
32
|
default: {
|
package/lib/common.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getSeparatorByPlatform(): "\\" | "/";
|
package/lib/common.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export interface ReducedType {
|
|
2
|
+
slug: string;
|
|
3
|
+
label: string;
|
|
4
|
+
typePath: string;
|
|
5
|
+
isArchived: boolean;
|
|
6
|
+
typeProperties: ReducedType[];
|
|
7
|
+
}
|
|
8
|
+
export interface ReducedTypeProperties {
|
|
9
|
+
id: string;
|
|
10
|
+
isArchived: boolean;
|
|
11
|
+
propertyType: PropertyType;
|
|
12
|
+
slug: string;
|
|
13
|
+
label: string;
|
|
14
|
+
editable: boolean;
|
|
15
|
+
core: boolean;
|
|
16
|
+
typePropertyOptionSetId: string;
|
|
17
|
+
formula: string;
|
|
18
|
+
referencedTypeRootSlug: string;
|
|
19
|
+
defaultValue: string;
|
|
20
|
+
propertyLevel: string;
|
|
21
|
+
inherited: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface ReducedOptionSet {
|
|
24
|
+
id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
isArchived: boolean;
|
|
27
|
+
optionSet: any[];
|
|
28
|
+
}
|
|
29
|
+
export declare enum PropertyType {
|
|
30
|
+
Text = "text",
|
|
31
|
+
String = "string",
|
|
32
|
+
Number = "number",
|
|
33
|
+
Date = "date",
|
|
34
|
+
Boolean = "boolean",
|
|
35
|
+
MultiSelect = "multi_select",
|
|
36
|
+
SingleSelect = "choice",
|
|
37
|
+
Url = "url",
|
|
38
|
+
Currency = "currency",
|
|
39
|
+
Percent = "percent",
|
|
40
|
+
Rating = "rating",
|
|
41
|
+
Formula = "formula",
|
|
42
|
+
ObjectReference = "object_reference",
|
|
43
|
+
ObjectReferenceList = "object_ref_list",
|
|
44
|
+
DataGrid = "data_grid",
|
|
45
|
+
CascadingSelect = "cascading_select",
|
|
46
|
+
Sequence = "sequence",
|
|
47
|
+
Constant = "constant",
|
|
48
|
+
Composite = "composite",
|
|
49
|
+
UserList = "userList",
|
|
50
|
+
Image = "image"
|
|
51
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uploadOptionSets(): Promise<void>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import { getSeparatorByPlatform } from '../common';
|
|
4
|
+
const yaml = require('js-yaml');
|
|
5
|
+
function loadOptionSets() {
|
|
6
|
+
const TARGET_DIR = process.cwd();
|
|
7
|
+
try {
|
|
8
|
+
const separator = getSeparatorByPlatform();
|
|
9
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'option_sets.yml', 'utf8'));
|
|
10
|
+
return doc.optionSets;
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export async function uploadOptionSets() {
|
|
17
|
+
const optionSets = loadOptionSets();
|
|
18
|
+
const currentOptionSets = await new Entities().get({
|
|
19
|
+
entityName: 'type-property-option-set',
|
|
20
|
+
});
|
|
21
|
+
const currentOptionSetsByName = {};
|
|
22
|
+
for (const currentOptionSet of currentOptionSets) {
|
|
23
|
+
currentOptionSetsByName[currentOptionSet.name] = currentOptionSet;
|
|
24
|
+
}
|
|
25
|
+
const newOptionSetsByName = {};
|
|
26
|
+
for (const optionSet of optionSets) {
|
|
27
|
+
newOptionSetsByName[optionSet.name] = optionSet;
|
|
28
|
+
}
|
|
29
|
+
for (const optionSet of optionSets) {
|
|
30
|
+
try {
|
|
31
|
+
const found = currentOptionSetsByName[optionSet.name];
|
|
32
|
+
if (!found) {
|
|
33
|
+
console.log('Creating: ', optionSet.name);
|
|
34
|
+
await new Entities().create({
|
|
35
|
+
entityName: 'type-property-option-set',
|
|
36
|
+
object: {
|
|
37
|
+
name: optionSet.name,
|
|
38
|
+
optionSet: optionSet.optionSet,
|
|
39
|
+
isArchived: optionSet.isArchived,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
console.log('Updating: ', optionSet.name);
|
|
45
|
+
await new Entities().update({
|
|
46
|
+
entityName: 'type-property-option-set',
|
|
47
|
+
id: found.id,
|
|
48
|
+
object: {
|
|
49
|
+
name: optionSet.name,
|
|
50
|
+
optionSet: optionSet.optionSet,
|
|
51
|
+
isArchived: optionSet.isArchived,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
throw new Error(`Error Uploading OptionSets: ${err}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (const currentOptionSet of currentOptionSets) {
|
|
61
|
+
try {
|
|
62
|
+
const found = newOptionSetsByName[currentOptionSet.name];
|
|
63
|
+
if (!found) {
|
|
64
|
+
console.log('Deleting: ', currentOptionSet.name);
|
|
65
|
+
await new Entities().delete({
|
|
66
|
+
entityName: 'type-property-option-set',
|
|
67
|
+
id: currentOptionSet.id,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
throw new Error(`Error Deleting OptionSets: ${err}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getTypes(args: any): Promise<void>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { authUser } from '../auth/login';
|
|
5
|
+
import { getSeparatorByPlatform } from '../common';
|
|
6
|
+
const yaml = require('js-yaml');
|
|
7
|
+
export async function getTypes(args) {
|
|
8
|
+
try {
|
|
9
|
+
const userConfig = await authUser();
|
|
10
|
+
console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
|
|
11
|
+
console.log(`%s Retrieving`, chalk.yellow.bold('OPTION SETS'));
|
|
12
|
+
await getOptionSets();
|
|
13
|
+
console.log(`%s Retrieved`, chalk.yellow.bold('OPTION SETS'));
|
|
14
|
+
console.log(`%s Retrieving`, chalk.yellow.bold('TYPES'));
|
|
15
|
+
await getReducedTypes();
|
|
16
|
+
console.log(`%s Retrieved`, chalk.yellow.bold('TYPES'));
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
console.log(`%s Types Retrieval Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
console.log('%s Types Retrieval Complete.', chalk.green.bold('COMPLETE'));
|
|
23
|
+
}
|
|
24
|
+
async function getOptionSets() {
|
|
25
|
+
const optionSets = await new Entities().get({
|
|
26
|
+
entityName: 'type-property-option-set',
|
|
27
|
+
});
|
|
28
|
+
const reducedOptionSets = [];
|
|
29
|
+
for (const optionSet of optionSets) {
|
|
30
|
+
const reducedOptionSet = {
|
|
31
|
+
id: optionSet.id,
|
|
32
|
+
name: optionSet.name,
|
|
33
|
+
isArchived: optionSet.isArchived,
|
|
34
|
+
optionSet: optionSet.optionSet,
|
|
35
|
+
};
|
|
36
|
+
reducedOptionSets.push(reducedOptionSet);
|
|
37
|
+
}
|
|
38
|
+
const optionSetsHolder = {
|
|
39
|
+
optionSets: reducedOptionSets,
|
|
40
|
+
};
|
|
41
|
+
const CURRENT_DIR = process.cwd();
|
|
42
|
+
const separator = getSeparatorByPlatform();
|
|
43
|
+
const yamlStr = yaml.dump(optionSetsHolder);
|
|
44
|
+
fs.writeFileSync(CURRENT_DIR + separator + 'option_sets.yml', yamlStr, 'utf8');
|
|
45
|
+
}
|
|
46
|
+
async function getReducedTypes() {
|
|
47
|
+
const types = await new Entities().get({
|
|
48
|
+
entityName: 'type',
|
|
49
|
+
});
|
|
50
|
+
console.log('Found types: ', types.length);
|
|
51
|
+
const reducedTypes = [];
|
|
52
|
+
for (const type of types) {
|
|
53
|
+
const typeWithProperties = await new Entities().get({
|
|
54
|
+
entityName: 'type',
|
|
55
|
+
id: type.id,
|
|
56
|
+
relations: ['typeProperties'],
|
|
57
|
+
});
|
|
58
|
+
const reducedTypeProperties = [];
|
|
59
|
+
for (const typeProperty of typeWithProperties.typeProperties) {
|
|
60
|
+
const reducedTypeProperty = {
|
|
61
|
+
id: typeProperty.id,
|
|
62
|
+
isArchived: typeProperty.isArchived,
|
|
63
|
+
propertyType: typeProperty.propertyType,
|
|
64
|
+
slug: typeProperty.slug,
|
|
65
|
+
label: typeProperty.label,
|
|
66
|
+
editable: typeProperty.editable,
|
|
67
|
+
core: typeProperty.core,
|
|
68
|
+
typePropertyOptionSetId: typeProperty.typePropertyOptionSetId,
|
|
69
|
+
formula: typeProperty.formula,
|
|
70
|
+
referencedTypeRootSlug: typeProperty.referencedTypeRootSlug,
|
|
71
|
+
defaultValue: typeProperty.defaultValue,
|
|
72
|
+
propertyLevel: typeProperty.propertyLevel,
|
|
73
|
+
inherited: typeProperty.inherited,
|
|
74
|
+
};
|
|
75
|
+
reducedTypeProperties.push(reducedTypeProperty);
|
|
76
|
+
}
|
|
77
|
+
const reducedType = {
|
|
78
|
+
slug: typeWithProperties.slug,
|
|
79
|
+
label: typeWithProperties.label,
|
|
80
|
+
typePath: typeWithProperties.typePath,
|
|
81
|
+
isArchived: typeWithProperties.isArchived,
|
|
82
|
+
typeProperties: reducedTypeProperties,
|
|
83
|
+
};
|
|
84
|
+
reducedTypes.push(reducedType);
|
|
85
|
+
}
|
|
86
|
+
const typesHolder = {
|
|
87
|
+
types: reducedTypes,
|
|
88
|
+
};
|
|
89
|
+
const CURRENT_DIR = process.cwd();
|
|
90
|
+
const separator = getSeparatorByPlatform();
|
|
91
|
+
const yamlStr = yaml.dump(typesHolder);
|
|
92
|
+
fs.writeFileSync(CURRENT_DIR + separator + 'types.yml', yamlStr, 'utf8');
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uploadTypes(args: any): Promise<void>;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { Entities, Request } from '@contrail/sdk';
|
|
2
|
+
import { uploadOptionSets } from './option-set-uploader';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import { authUser } from '../auth/login';
|
|
6
|
+
import { getSeparatorByPlatform } from '../common';
|
|
7
|
+
const yaml = require('js-yaml');
|
|
8
|
+
export async function uploadTypes(args) {
|
|
9
|
+
try {
|
|
10
|
+
const userConfig = await authUser();
|
|
11
|
+
console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
|
|
12
|
+
console.log(`%s Uploading`, chalk.yellow.bold('OPTION SETS'));
|
|
13
|
+
await uploadOptionSets();
|
|
14
|
+
console.log(`%s Uploaded`, chalk.yellow.bold('OPTION SETS'));
|
|
15
|
+
console.log(`%s Uploading`, chalk.yellow.bold('TYPES'));
|
|
16
|
+
await uploadReducedTypes();
|
|
17
|
+
console.log(`%s Uploaded`, chalk.yellow.bold('TYPES'));
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
console.log(`%s Types Upload Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
console.log('%s Types Upload Complete.', chalk.green.bold('COMPLETE'));
|
|
24
|
+
}
|
|
25
|
+
function loadReducedTypes() {
|
|
26
|
+
const TARGET_DIR = process.cwd();
|
|
27
|
+
try {
|
|
28
|
+
const separator = getSeparatorByPlatform();
|
|
29
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
|
|
30
|
+
return doc.types;
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function uploadReducedTypes() {
|
|
37
|
+
const types = loadReducedTypes();
|
|
38
|
+
const currentTypesWithoutProperties = await new Entities().get({
|
|
39
|
+
entityName: 'type',
|
|
40
|
+
});
|
|
41
|
+
const currentTypes = [];
|
|
42
|
+
for (const currentTypeWithoutProperties of currentTypesWithoutProperties) {
|
|
43
|
+
const typeWithProperties = await new Entities().get({
|
|
44
|
+
entityName: 'type',
|
|
45
|
+
id: currentTypeWithoutProperties.id,
|
|
46
|
+
relations: ['typeProperties'],
|
|
47
|
+
});
|
|
48
|
+
currentTypes.push(typeWithProperties);
|
|
49
|
+
}
|
|
50
|
+
const currentTypesBySlug = {};
|
|
51
|
+
for (const currentType of currentTypes) {
|
|
52
|
+
currentTypesBySlug[currentType.slug] = currentType;
|
|
53
|
+
}
|
|
54
|
+
const newTypesBySlug = {};
|
|
55
|
+
for (const type of types) {
|
|
56
|
+
newTypesBySlug[type.slug] = type;
|
|
57
|
+
}
|
|
58
|
+
for (const type of types) {
|
|
59
|
+
try {
|
|
60
|
+
const found = currentTypesBySlug[type.slug];
|
|
61
|
+
if (!found) {
|
|
62
|
+
if (!type.typePath.includes(':')) {
|
|
63
|
+
console.log('Cannot create a base type. Skipping: ', type.slug);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
console.log('Creating Type: ', type.slug);
|
|
67
|
+
const createdType = await new Entities().create({
|
|
68
|
+
entityName: 'type',
|
|
69
|
+
object: {
|
|
70
|
+
slug: type.slug,
|
|
71
|
+
label: type.label,
|
|
72
|
+
typePath: type.typePath,
|
|
73
|
+
isArchived: type.isArchived,
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
await uploadReducedTypeProperties(createdType, [], type.typeProperties);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
console.log('Updating Type: ', type.slug);
|
|
80
|
+
const updatedType = await new Entities().update({
|
|
81
|
+
entityName: 'type',
|
|
82
|
+
id: found.id,
|
|
83
|
+
object: {
|
|
84
|
+
slug: type.slug,
|
|
85
|
+
label: type.label,
|
|
86
|
+
typePath: type.typePath,
|
|
87
|
+
isArchived: type.isArchived,
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
await uploadReducedTypeProperties(updatedType, found.typeProperties, type.typeProperties);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
console.log('Error Uploading Types: ', err);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
for (const currentType of currentTypes) {
|
|
98
|
+
try {
|
|
99
|
+
const found = newTypesBySlug[currentType.slug];
|
|
100
|
+
if (!found) {
|
|
101
|
+
console.log('Deleting Type: ', currentType.slug);
|
|
102
|
+
await new Entities().delete({
|
|
103
|
+
entityName: 'type',
|
|
104
|
+
id: currentType.id,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
throw new Error(`Error Deleting Types: ${err}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function uploadReducedTypeProperties(type, currentPropertyTypes, newPropertyTypes) {
|
|
114
|
+
const currentPropertyTypeBySlug = {};
|
|
115
|
+
for (const currentPropertyType of currentPropertyTypes) {
|
|
116
|
+
currentPropertyTypeBySlug[currentPropertyType.slug] = currentPropertyType;
|
|
117
|
+
}
|
|
118
|
+
const newPropertyTypesBySlug = {};
|
|
119
|
+
for (const newPropertyType of newPropertyTypes) {
|
|
120
|
+
newPropertyTypesBySlug[newPropertyType.slug] = newPropertyType;
|
|
121
|
+
}
|
|
122
|
+
for (const newPropertyType of newPropertyTypes) {
|
|
123
|
+
try {
|
|
124
|
+
const found = currentPropertyTypeBySlug[newPropertyType.slug];
|
|
125
|
+
if (!found && !newPropertyType.inherited) {
|
|
126
|
+
console.log('Creating Type-Property: ', newPropertyType.slug);
|
|
127
|
+
await Request.request(`/types/${type.id}/type-properties`, {
|
|
128
|
+
method: 'POST',
|
|
129
|
+
body: JSON.stringify({
|
|
130
|
+
isArchived: newPropertyType.isArchived,
|
|
131
|
+
propertyType: newPropertyType.propertyType,
|
|
132
|
+
slug: newPropertyType.slug,
|
|
133
|
+
label: newPropertyType.label,
|
|
134
|
+
editable: newPropertyType.editable,
|
|
135
|
+
core: newPropertyType.core,
|
|
136
|
+
typePropertyOptionSetId: newPropertyType.typePropertyOptionSetId,
|
|
137
|
+
formula: newPropertyType.formula,
|
|
138
|
+
referencedTypeRootSlug: newPropertyType.referencedTypeRootSlug,
|
|
139
|
+
defaultValue: newPropertyType.defaultValue,
|
|
140
|
+
propertyLevel: newPropertyType.propertyLevel,
|
|
141
|
+
}),
|
|
142
|
+
headers: {
|
|
143
|
+
'Accept': 'application/json',
|
|
144
|
+
'Content-Type': 'application/json',
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
console.log('Error Creating Properties: ', err);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
for (const currentPropertyType of currentPropertyTypes) {
|
|
154
|
+
try {
|
|
155
|
+
const found = newPropertyTypesBySlug[currentPropertyType.slug];
|
|
156
|
+
if (!found && !currentPropertyType.inherited) {
|
|
157
|
+
console.log('Deleting Type-Property: ', currentPropertyType.slug);
|
|
158
|
+
await Request.request(`/types/${type.id}/type-properties/${currentPropertyType.id}`, {
|
|
159
|
+
method: 'DELETE',
|
|
160
|
+
headers: {
|
|
161
|
+
'Accept': 'application/json',
|
|
162
|
+
'Content-Type': 'application/json',
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
throw new Error(`Error Deleting Properties: ${err}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
package/package.json
CHANGED
|
File without changes
|
|
@@ -16,7 +16,7 @@ function copyTemplate(templateDir){
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export async function setUpTemplate(options) {
|
|
19
|
-
const templateDir = path.resolve(new URL(currentFileUrl).pathname,
|
|
19
|
+
const templateDir = path.resolve(new URL(currentFileUrl).pathname, `../../../../templates/${options.template.toLocaleLowerCase()}`).replace(/\\/g, '\\').replace(/(.):\\(.):/g, '').replace(/%20/g, ' ');
|
|
20
20
|
try {
|
|
21
21
|
const tasks = new Listr([
|
|
22
22
|
{
|
|
@@ -2,6 +2,8 @@ import { Entities } from '@contrail/sdk';
|
|
|
2
2
|
import chalk from 'chalk';
|
|
3
3
|
import { App, AppDeclaration, AppActionDeclaration, EventWorkflowActionDefinition } from './interfaces';
|
|
4
4
|
import { ConfigPropertiesParser } from './app-template-parsers';
|
|
5
|
+
import { getSeparatorByPlatform } from '../../common';
|
|
6
|
+
|
|
5
7
|
import fs from 'fs';
|
|
6
8
|
|
|
7
9
|
export class ActionsDeployer {
|
|
@@ -99,11 +101,10 @@ export class ActionsDeployer {
|
|
|
99
101
|
throw new Error('Action handler must be a .js file.');
|
|
100
102
|
}
|
|
101
103
|
const CURRENT_DIR = process.cwd();
|
|
102
|
-
const
|
|
103
|
-
const separator = isWin ? '\\' : '/';
|
|
104
|
+
const separator = getSeparatorByPlatform();
|
|
104
105
|
|
|
105
|
-
const
|
|
106
|
-
const content = fs.readFileSync(
|
|
106
|
+
const functionLocation = CURRENT_DIR + separator + actionDeclaration.handler;
|
|
107
|
+
const content = fs.readFileSync(functionLocation);
|
|
107
108
|
return content.toString();
|
|
108
109
|
}
|
|
109
110
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Entities } from '@contrail/sdk';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
|
-
import { Auth } from '
|
|
3
|
+
import { Auth } from '../../auth/auth';
|
|
4
4
|
import { ActionsDeployer } from './actions-deployer';
|
|
5
5
|
import { App, AppDeclaration } from './interfaces';
|
|
6
6
|
import { TriggerKeyDefinitionParser, ConfigPropertiesParser } from './app-template-parsers';
|
|
File without changes
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
-
import { authUser } from '
|
|
3
|
+
import { authUser } from '../../auth/login';
|
|
4
4
|
import { AppDeployer } from './app-deployer';
|
|
5
5
|
import { AppDeclaration } from './interfaces';
|
|
6
|
+
import { getSeparatorByPlatform } from '../../common';
|
|
6
7
|
const yaml = require('js-yaml');
|
|
7
8
|
|
|
8
9
|
function loadAppConfig(){
|
|
9
10
|
const TARGET_DIR = process.cwd();
|
|
10
11
|
try {
|
|
11
|
-
const
|
|
12
|
+
const separator = getSeparatorByPlatform();
|
|
13
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'app.yml', 'utf8'));
|
|
12
14
|
console.log(doc);
|
|
13
15
|
return doc;
|
|
14
16
|
} catch (e) {
|
|
File without changes
|
package/src/auth/login.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
|
|
2
2
|
import inquirer from 'inquirer';
|
|
3
3
|
import { Request, setConfig } from '@contrail/sdk';
|
|
4
|
-
import { Auth } from './auth';
|
|
4
|
+
import { Auth, AuthContext } from './auth';
|
|
5
5
|
|
|
6
6
|
const CONFIGURATION = {
|
|
7
7
|
userPoolId: 'us-east-1_x8hXZUsL6',
|
|
8
8
|
clientId: process.env.CONTRAIL_COGNITO_CLIENT_ID || '2oubek96sue5rsncovtqhs3scm',
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
export async function authUser(){
|
|
11
|
+
export async function authUser(): Promise<AuthContext>{
|
|
12
12
|
const questions = [];
|
|
13
13
|
if (!process.env.CONTRAIL_CLI_EMAIL) {
|
|
14
14
|
questions.push({
|
|
@@ -68,9 +68,9 @@ export async function authUser(){
|
|
|
68
68
|
cognitoUser.authenticateUser(authDetails, {
|
|
69
69
|
onSuccess: async (result) => {
|
|
70
70
|
const user = result;
|
|
71
|
-
await setAuthContext(user, params);
|
|
71
|
+
const config = await setAuthContext(user, params);
|
|
72
72
|
|
|
73
|
-
resolve(
|
|
73
|
+
resolve(config);
|
|
74
74
|
},
|
|
75
75
|
onFailure: (err) => {
|
|
76
76
|
reject(err);
|
|
@@ -79,7 +79,7 @@ export async function authUser(){
|
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
async function setAuthContext(user, params){
|
|
82
|
+
async function setAuthContext(user, params): Promise<AuthContext> {
|
|
83
83
|
|
|
84
84
|
// TODO Dont allow user to set this - they could set dev
|
|
85
85
|
const questions = [];
|
|
@@ -122,4 +122,6 @@ async function setAuthContext(user, params){
|
|
|
122
122
|
if (org) {
|
|
123
123
|
Auth.config.orgId = org.orgId || org.id;
|
|
124
124
|
}
|
|
125
|
+
|
|
126
|
+
return Object.assign({}, Auth.config);
|
|
125
127
|
}
|
package/src/cli.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
|
|
2
|
-
import { create } from './create-app/create';
|
|
3
|
-
import { deployApp, removeApp } from './deploy-app/deploy';
|
|
2
|
+
import { create } from './apps/create-app/create';
|
|
3
|
+
import { deployApp, removeApp } from './apps/deploy-app/deploy';
|
|
4
|
+
import { getTypes } from './types/types-extractor';
|
|
5
|
+
import { uploadTypes } from './types/types-uploader';
|
|
4
6
|
/**
|
|
5
7
|
* Built using documentation here:
|
|
6
8
|
* https://www.twilio.com/blog/how-to-build-a-cli-with-node-js
|
|
@@ -22,7 +24,11 @@ export async function cli(args) {
|
|
|
22
24
|
break;
|
|
23
25
|
}
|
|
24
26
|
case 'extract-types': {
|
|
25
|
-
|
|
27
|
+
getTypes(args);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case 'upload-types': {
|
|
31
|
+
uploadTypes(args);
|
|
26
32
|
break;
|
|
27
33
|
}
|
|
28
34
|
default: {
|
package/src/common.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface ReducedType {
|
|
2
|
+
slug: string;
|
|
3
|
+
label: string;
|
|
4
|
+
typePath: string;
|
|
5
|
+
isArchived: boolean;
|
|
6
|
+
typeProperties: ReducedType[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ReducedTypeProperties {
|
|
10
|
+
id: string;
|
|
11
|
+
isArchived: boolean;
|
|
12
|
+
propertyType: PropertyType;
|
|
13
|
+
slug: string;
|
|
14
|
+
label: string;
|
|
15
|
+
editable: boolean;
|
|
16
|
+
core: boolean;
|
|
17
|
+
typePropertyOptionSetId: string;
|
|
18
|
+
formula: string;
|
|
19
|
+
referencedTypeRootSlug: string;
|
|
20
|
+
defaultValue: string;
|
|
21
|
+
propertyLevel: string;
|
|
22
|
+
inherited: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ReducedOptionSet {
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
isArchived: boolean;
|
|
29
|
+
optionSet: any[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export declare enum PropertyType {
|
|
33
|
+
Text = 'text',
|
|
34
|
+
String = 'string',
|
|
35
|
+
Number = 'number',
|
|
36
|
+
Date = 'date',
|
|
37
|
+
Boolean = 'boolean',
|
|
38
|
+
MultiSelect = 'multi_select',
|
|
39
|
+
SingleSelect = 'choice',
|
|
40
|
+
Url = 'url',
|
|
41
|
+
Currency = 'currency',
|
|
42
|
+
Percent = 'percent',
|
|
43
|
+
Rating = 'rating',
|
|
44
|
+
Formula = 'formula',
|
|
45
|
+
ObjectReference = 'object_reference',
|
|
46
|
+
ObjectReferenceList = 'object_ref_list',
|
|
47
|
+
DataGrid = 'data_grid',
|
|
48
|
+
CascadingSelect = 'cascading_select',
|
|
49
|
+
Sequence = 'sequence',
|
|
50
|
+
Constant = 'constant',
|
|
51
|
+
Composite = 'composite',
|
|
52
|
+
UserList = 'userList',
|
|
53
|
+
Image = 'image',
|
|
54
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import { ReducedOptionSet } from './interfaces';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { getSeparatorByPlatform } from '../common';
|
|
5
|
+
|
|
6
|
+
const yaml = require('js-yaml');
|
|
7
|
+
|
|
8
|
+
function loadOptionSets(){
|
|
9
|
+
const TARGET_DIR = process.cwd();
|
|
10
|
+
try {
|
|
11
|
+
const separator = getSeparatorByPlatform();
|
|
12
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'option_sets.yml', 'utf8'));
|
|
13
|
+
return doc.optionSets;
|
|
14
|
+
} catch (e) {
|
|
15
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function uploadOptionSets() {
|
|
20
|
+
const optionSets: ReducedOptionSet[] = loadOptionSets();
|
|
21
|
+
|
|
22
|
+
const currentOptionSets = await new Entities().get({
|
|
23
|
+
entityName: 'type-property-option-set',
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const currentOptionSetsByName = {};
|
|
27
|
+
for (const currentOptionSet of currentOptionSets) {
|
|
28
|
+
currentOptionSetsByName[currentOptionSet.name] = currentOptionSet;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const newOptionSetsByName = {};
|
|
32
|
+
for (const optionSet of optionSets) {
|
|
33
|
+
newOptionSetsByName[optionSet.name] = optionSet;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for (const optionSet of optionSets) {
|
|
37
|
+
try {
|
|
38
|
+
const found = currentOptionSetsByName[optionSet.name];
|
|
39
|
+
if (!found) {
|
|
40
|
+
console.log('Creating: ', optionSet.name);
|
|
41
|
+
await new Entities().create({
|
|
42
|
+
entityName: 'type-property-option-set',
|
|
43
|
+
object: {
|
|
44
|
+
name: optionSet.name,
|
|
45
|
+
optionSet: optionSet.optionSet,
|
|
46
|
+
isArchived: optionSet.isArchived,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
else
|
|
51
|
+
{
|
|
52
|
+
console.log('Updating: ', optionSet.name);
|
|
53
|
+
await new Entities().update({
|
|
54
|
+
entityName: 'type-property-option-set',
|
|
55
|
+
id: found.id,
|
|
56
|
+
object: {
|
|
57
|
+
name: optionSet.name,
|
|
58
|
+
optionSet: optionSet.optionSet,
|
|
59
|
+
isArchived: optionSet.isArchived,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
} catch (err) {
|
|
64
|
+
throw new Error(`Error Uploading OptionSets: ${err}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
for (const currentOptionSet of currentOptionSets) {
|
|
69
|
+
try {
|
|
70
|
+
const found = newOptionSetsByName[currentOptionSet.name];
|
|
71
|
+
if (!found) {
|
|
72
|
+
console.log('Deleting: ', currentOptionSet.name);
|
|
73
|
+
await new Entities().delete({
|
|
74
|
+
entityName: 'type-property-option-set',
|
|
75
|
+
id: currentOptionSet.id,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
} catch (err) {
|
|
79
|
+
throw new Error(`Error Deleting OptionSets: ${err}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Entities } from '@contrail/sdk';
|
|
2
|
+
import { ReducedType, ReducedOptionSet, ReducedTypeProperties } from './interfaces';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import { authUser } from '../auth/login';
|
|
6
|
+
import { getSeparatorByPlatform } from '../common';
|
|
7
|
+
|
|
8
|
+
const yaml = require('js-yaml');
|
|
9
|
+
|
|
10
|
+
export async function getTypes(args) {
|
|
11
|
+
try {
|
|
12
|
+
const userConfig = await authUser();
|
|
13
|
+
console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
|
|
14
|
+
|
|
15
|
+
console.log(`%s Retrieving`, chalk.yellow.bold('OPTION SETS'));
|
|
16
|
+
await getOptionSets();
|
|
17
|
+
console.log(`%s Retrieved`, chalk.yellow.bold('OPTION SETS'));
|
|
18
|
+
|
|
19
|
+
console.log(`%s Retrieving`, chalk.yellow.bold('TYPES'));
|
|
20
|
+
await getReducedTypes();
|
|
21
|
+
console.log(`%s Retrieved`, chalk.yellow.bold('TYPES'));
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
console.log(`%s Types Retrieval Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
console.log('%s Types Retrieval Complete.', chalk.green.bold('COMPLETE'));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function getOptionSets() {
|
|
33
|
+
const optionSets = await new Entities().get({
|
|
34
|
+
entityName: 'type-property-option-set',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const reducedOptionSets = [];
|
|
38
|
+
for (const optionSet of optionSets) {
|
|
39
|
+
const reducedOptionSet: ReducedOptionSet = {
|
|
40
|
+
id: optionSet.id,
|
|
41
|
+
name: optionSet.name,
|
|
42
|
+
isArchived: optionSet.isArchived,
|
|
43
|
+
optionSet: optionSet.optionSet,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
reducedOptionSets.push(reducedOptionSet);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const optionSetsHolder = {
|
|
50
|
+
optionSets: reducedOptionSets,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const CURRENT_DIR = process.cwd();
|
|
54
|
+
const separator = getSeparatorByPlatform();
|
|
55
|
+
|
|
56
|
+
const yamlStr = yaml.dump(optionSetsHolder);
|
|
57
|
+
fs.writeFileSync(CURRENT_DIR + separator + 'option_sets.yml', yamlStr, 'utf8');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function getReducedTypes() {
|
|
61
|
+
const types = await new Entities().get({
|
|
62
|
+
entityName: 'type',
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
console.log('Found types: ', types.length);
|
|
66
|
+
|
|
67
|
+
const reducedTypes = [];
|
|
68
|
+
for (const type of types) {
|
|
69
|
+
const typeWithProperties = await new Entities().get({
|
|
70
|
+
entityName: 'type',
|
|
71
|
+
id: type.id,
|
|
72
|
+
relations: ['typeProperties'],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const reducedTypeProperties = [];
|
|
76
|
+
for (const typeProperty of typeWithProperties.typeProperties) {
|
|
77
|
+
const reducedTypeProperty: ReducedTypeProperties = {
|
|
78
|
+
id: typeProperty.id,
|
|
79
|
+
isArchived: typeProperty.isArchived,
|
|
80
|
+
propertyType: typeProperty.propertyType,
|
|
81
|
+
slug: typeProperty.slug,
|
|
82
|
+
label: typeProperty.label,
|
|
83
|
+
editable: typeProperty.editable,
|
|
84
|
+
core: typeProperty.core,
|
|
85
|
+
typePropertyOptionSetId: typeProperty.typePropertyOptionSetId,
|
|
86
|
+
formula: typeProperty.formula,
|
|
87
|
+
referencedTypeRootSlug: typeProperty.referencedTypeRootSlug,
|
|
88
|
+
defaultValue: typeProperty.defaultValue,
|
|
89
|
+
propertyLevel: typeProperty.propertyLevel,
|
|
90
|
+
inherited: typeProperty.inherited,
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
reducedTypeProperties.push(reducedTypeProperty);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const reducedType: ReducedType = {
|
|
97
|
+
slug: typeWithProperties.slug,
|
|
98
|
+
label: typeWithProperties.label,
|
|
99
|
+
typePath: typeWithProperties.typePath,
|
|
100
|
+
isArchived: typeWithProperties.isArchived,
|
|
101
|
+
typeProperties: reducedTypeProperties,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
reducedTypes.push(reducedType);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const typesHolder = {
|
|
108
|
+
types: reducedTypes,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const CURRENT_DIR = process.cwd();
|
|
112
|
+
const separator = getSeparatorByPlatform();
|
|
113
|
+
|
|
114
|
+
const yamlStr = yaml.dump(typesHolder);
|
|
115
|
+
fs.writeFileSync(CURRENT_DIR + separator + 'types.yml', yamlStr, 'utf8');
|
|
116
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { Entities, Request } from '@contrail/sdk';
|
|
2
|
+
import { ReducedType, ReducedTypeProperties } from './interfaces';
|
|
3
|
+
import { uploadOptionSets } from './option-set-uploader';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
import { authUser } from '../auth/login';
|
|
7
|
+
import { getSeparatorByPlatform } from '../common';
|
|
8
|
+
|
|
9
|
+
const yaml = require('js-yaml');
|
|
10
|
+
|
|
11
|
+
export async function uploadTypes(args) {
|
|
12
|
+
try {
|
|
13
|
+
const userConfig = await authUser();
|
|
14
|
+
console.log(`%s Getting all types for orgId: ${userConfig.orgId}`, chalk.blue.bold('START'));
|
|
15
|
+
|
|
16
|
+
console.log(`%s Uploading`, chalk.yellow.bold('OPTION SETS'));
|
|
17
|
+
await uploadOptionSets();
|
|
18
|
+
console.log(`%s Uploaded`, chalk.yellow.bold('OPTION SETS'));
|
|
19
|
+
|
|
20
|
+
console.log(`%s Uploading`, chalk.yellow.bold('TYPES'));
|
|
21
|
+
await uploadReducedTypes();
|
|
22
|
+
console.log(`%s Uploaded`, chalk.yellow.bold('TYPES'));
|
|
23
|
+
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
console.log(`%s Types Upload Failed. ${e}`, chalk.red.bold('FAIL'));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
console.log('%s Types Upload Complete.', chalk.green.bold('COMPLETE'));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function loadReducedTypes(){
|
|
34
|
+
const TARGET_DIR = process.cwd();
|
|
35
|
+
try {
|
|
36
|
+
const separator = getSeparatorByPlatform();
|
|
37
|
+
const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'types.yml', 'utf8'));
|
|
38
|
+
return doc.types;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
throw new Error(`ERROR PARSING YAML: ${e}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function uploadReducedTypes() {
|
|
45
|
+
const types: ReducedType[] = loadReducedTypes();
|
|
46
|
+
|
|
47
|
+
const currentTypesWithoutProperties = await new Entities().get({
|
|
48
|
+
entityName: 'type',
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const currentTypes = [];
|
|
52
|
+
for (const currentTypeWithoutProperties of currentTypesWithoutProperties) {
|
|
53
|
+
const typeWithProperties = await new Entities().get({
|
|
54
|
+
entityName: 'type',
|
|
55
|
+
id: currentTypeWithoutProperties.id,
|
|
56
|
+
relations: ['typeProperties'],
|
|
57
|
+
});
|
|
58
|
+
currentTypes.push(typeWithProperties);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const currentTypesBySlug = {};
|
|
62
|
+
for (const currentType of currentTypes) {
|
|
63
|
+
currentTypesBySlug[currentType.slug] = currentType;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const newTypesBySlug = {};
|
|
67
|
+
for (const type of types) {
|
|
68
|
+
newTypesBySlug[type.slug] = type;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
for (const type of types) {
|
|
72
|
+
try {
|
|
73
|
+
const found = currentTypesBySlug[type.slug];
|
|
74
|
+
if (!found) {
|
|
75
|
+
if (!type.typePath.includes(':')) {
|
|
76
|
+
console.log('Cannot create a base type. Skipping: ', type.slug);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
console.log('Creating Type: ', type.slug);
|
|
81
|
+
|
|
82
|
+
const createdType = await new Entities().create({
|
|
83
|
+
entityName: 'type',
|
|
84
|
+
object: {
|
|
85
|
+
slug: type.slug,
|
|
86
|
+
label: type.label,
|
|
87
|
+
typePath: type.typePath,
|
|
88
|
+
isArchived: type.isArchived,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
await uploadReducedTypeProperties(createdType, [], type.typeProperties);
|
|
93
|
+
}
|
|
94
|
+
else
|
|
95
|
+
{
|
|
96
|
+
console.log('Updating Type: ', type.slug);
|
|
97
|
+
const updatedType = await new Entities().update({
|
|
98
|
+
entityName: 'type',
|
|
99
|
+
id: found.id,
|
|
100
|
+
object: {
|
|
101
|
+
slug: type.slug,
|
|
102
|
+
label: type.label,
|
|
103
|
+
typePath: type.typePath,
|
|
104
|
+
isArchived: type.isArchived,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
await uploadReducedTypeProperties(updatedType, found.typeProperties, type.typeProperties);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
} catch (err) {
|
|
112
|
+
console.log('Error Uploading Types: ', err);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const currentType of currentTypes) {
|
|
117
|
+
try {
|
|
118
|
+
const found = newTypesBySlug[currentType.slug];
|
|
119
|
+
if (!found) {
|
|
120
|
+
console.log('Deleting Type: ', currentType.slug);
|
|
121
|
+
await new Entities().delete({
|
|
122
|
+
entityName: 'type',
|
|
123
|
+
id: currentType.id,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
} catch (err) {
|
|
127
|
+
throw new Error(`Error Deleting Types: ${err}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function uploadReducedTypeProperties(type, currentPropertyTypes, newPropertyTypes) {
|
|
134
|
+
const currentPropertyTypeBySlug = {};
|
|
135
|
+
for (const currentPropertyType of currentPropertyTypes) {
|
|
136
|
+
currentPropertyTypeBySlug[currentPropertyType.slug] = currentPropertyType;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const newPropertyTypesBySlug = {};
|
|
140
|
+
for (const newPropertyType of newPropertyTypes) {
|
|
141
|
+
newPropertyTypesBySlug[newPropertyType.slug] = newPropertyType;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (const newPropertyType of newPropertyTypes) {
|
|
145
|
+
try {
|
|
146
|
+
const found = currentPropertyTypeBySlug[newPropertyType.slug];
|
|
147
|
+
if (!found && !newPropertyType.inherited) {
|
|
148
|
+
console.log('Creating Type-Property: ', newPropertyType.slug);
|
|
149
|
+
|
|
150
|
+
await Request.request(
|
|
151
|
+
`/types/${type.id}/type-properties`,
|
|
152
|
+
{
|
|
153
|
+
method: 'POST',
|
|
154
|
+
body: JSON.stringify({
|
|
155
|
+
isArchived: newPropertyType.isArchived,
|
|
156
|
+
propertyType: newPropertyType.propertyType,
|
|
157
|
+
slug: newPropertyType.slug,
|
|
158
|
+
label: newPropertyType.label,
|
|
159
|
+
editable: newPropertyType.editable,
|
|
160
|
+
core: newPropertyType.core,
|
|
161
|
+
typePropertyOptionSetId: newPropertyType.typePropertyOptionSetId,
|
|
162
|
+
formula: newPropertyType.formula,
|
|
163
|
+
referencedTypeRootSlug: newPropertyType.referencedTypeRootSlug,
|
|
164
|
+
defaultValue: newPropertyType.defaultValue,
|
|
165
|
+
propertyLevel: newPropertyType.propertyLevel,
|
|
166
|
+
}),
|
|
167
|
+
headers: {
|
|
168
|
+
'Accept': 'application/json',
|
|
169
|
+
'Content-Type': 'application/json',
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
} catch (err) {
|
|
175
|
+
console.log('Error Creating Properties: ', err);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
for (const currentPropertyType of currentPropertyTypes) {
|
|
180
|
+
try {
|
|
181
|
+
const found = newPropertyTypesBySlug[currentPropertyType.slug];
|
|
182
|
+
if (!found && !currentPropertyType.inherited) {
|
|
183
|
+
console.log('Deleting Type-Property: ', currentPropertyType.slug);
|
|
184
|
+
await Request.request(
|
|
185
|
+
`/types/${type.id}/type-properties/${currentPropertyType.id}`,
|
|
186
|
+
{
|
|
187
|
+
method: 'DELETE',
|
|
188
|
+
headers: {
|
|
189
|
+
'Accept': 'application/json',
|
|
190
|
+
'Content-Type': 'application/json',
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
} catch (err) {
|
|
195
|
+
throw new Error(`Error Deleting Properties: ${err}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
3
|
+
"target": "es2017",
|
|
4
4
|
"module": "esnext",
|
|
5
5
|
"declaration": true,
|
|
6
6
|
"esModuleInterop": true,
|
|
@@ -11,7 +11,15 @@
|
|
|
11
11
|
"noImplicitAny": false,
|
|
12
12
|
"emitDecoratorMetadata": true,
|
|
13
13
|
"skipLibCheck": true,
|
|
14
|
-
"moduleResolution": "node"
|
|
14
|
+
"moduleResolution": "node",
|
|
15
|
+
"lib": [
|
|
16
|
+
"es5",
|
|
17
|
+
"ES2015",
|
|
18
|
+
"ES2016",
|
|
19
|
+
"ES2017",
|
|
20
|
+
"dom",
|
|
21
|
+
"esnext.asynciterable"
|
|
22
|
+
],
|
|
15
23
|
},
|
|
16
24
|
"include": [
|
|
17
25
|
"src"
|