-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschema.js
128 lines (113 loc) · 3.37 KB
/
schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const fs = require('fs-extra');
const path = require('path');
const prettier = require('prettier');
const { getCwd } = require('./path');
const { logger } = require('./logger');
const { fetchSchemas, fetchSchema } = require('./api/schema');
const chalk = require('chalk');
const { table, getBorderCharacters } = require('table');
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const logSchemas = schemas => {
const data = schemas
.map(r => [r.labels.singular, r.name, r.objectTypeId || ''])
.sort((a, b) => (a[1] > b[1] ? 1 : -1));
data.unshift([
chalk.bold('Label'),
chalk.bold('Name'),
chalk.bold('objectTypeId'),
]);
const tableConfig = {
singleLine: true,
border: getBorderCharacters('honeywell'),
};
logger.log(data.length ? table(data, tableConfig) : 'No Schemas were found');
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const cleanSchema = schema => {
const parsedSchema = {};
parsedSchema.name = schema.name;
parsedSchema.labels = schema.labels;
parsedSchema.requiredProperties = schema.requiredProperties;
parsedSchema.searchableProperties = schema.searchableProperties;
parsedSchema.primaryDisplayProperty = schema.primaryDisplayProperty;
parsedSchema.associatedObjects = schema.associatedObjects;
parsedSchema.properties = schema.properties
.filter(p => !p.name.startsWith('hs_'))
.map(p => ({
name: p.name,
label: p.label,
type: p.type,
fieldType: p.fieldType,
description: p.description,
}));
return parsedSchema;
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const getResolvedPath = (dest, name) => {
if (name) return path.resolve(getCwd(), dest || '', `${name}.json`);
return path.resolve(getCwd(), dest || '');
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const writeSchemaToDisk = (schema, dest) =>
fs.outputFileSync(
getResolvedPath(dest, schema.name),
prettier.format(JSON.stringify(cleanSchema(schema)), {
parser: 'json',
})
);
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const listSchemas = async accountId => {
const response = await fetchSchemas(accountId);
logSchemas(response.results);
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const downloadSchemas = async (accountId, dest) => {
const response = await fetchSchemas(accountId);
logSchemas(response.results);
if (response.results.length) {
response.results.forEach(r => writeSchemaToDisk(r, dest));
}
return;
};
/**
* @deprecated
* Use the corresponding export from @hubspot/local-dev-lib
* https://github.com/HubSpot/hubspot-local-dev-lib
*/
const downloadSchema = async (accountId, schemaObjectType, dest) => {
const response = await fetchSchema(accountId, schemaObjectType);
writeSchemaToDisk(response, dest);
};
module.exports = {
writeSchemaToDisk,
getResolvedPath,
listSchemas,
cleanSchema,
downloadSchemas,
downloadSchema,
logSchemas,
};