-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
200 lines (183 loc) · 4.38 KB
/
db.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* eslint-disable no-console */
const { GraphQLClient } = require('graphql-request');
const endpoint = 'https://graphql.fauna.com/graphql';
const graphQLClient = new GraphQLClient(endpoint, {
headers: {
authorization: 'Bearer fnADYc2oY-ACAjR9EnRZT2RrGt5FifqJDpT2UM8H',
},
});
// Playground authorization: 'Bearer fnADYe67ZIACAq281apQH3KMD0YkIikg8vGMCuMg'
async function getNpmSyncSeq() {
const query = /* GraphQL */ `
{
findNpmSyncByID(id: "243701427429442053") {
seq
_id
}
}
`;
try {
const data = await graphQLClient.request(query);
return data.findNpmSyncByID.seq;
} catch (err) {
console.log('## Could not execute `updateNpmSync` on db');
}
return 0;
}
async function updateNpmSyncSeq(seq) {
const query = /* GraphQL */ `
mutation {
updateNpmSync(id: "243701427429442053", data: { seq: ${seq} }) {
seq
_id
}
}
`;
try {
await graphQLClient.request(query);
} catch (err) {
console.log('## Could not execute `updateNpmSync` on db');
}
}
function getLibrary(deps) {
if (Object.keys(deps).includes('lit-element')) {
return { connect: '243701321298870785' }; // LitElement 2.x
}
return { connect: '243701293377389063' }; // HTMLElement
}
/**
* Object => GraphQL query
*
* @param {mixed} obj
* @return template string.
*/
const queryfy = obj => {
if (typeof obj === 'number') {
return obj;
}
if (Array.isArray(obj)) {
const props = obj.map(value => `${queryfy(value)}`).join(',');
return `[${props}]`;
}
if (typeof obj === 'object') {
const props = Object.keys(obj)
.map(key => `${key}:${queryfy(obj[key])}`)
.join(',');
return `{${props}}`;
}
return JSON.stringify(obj);
};
async function createPackage(pkg, version, rawCustomElements) {
const library = getLibrary(pkg.versions[version].dependencies);
const customElementsCreate = [];
rawCustomElements.tags.forEach(tag => {
const attributesCreate = [];
tag.attributes.forEach(attribute => {
const newAttribute = {
name: attribute.name,
type: attribute.type,
};
if (attribute.values) {
newAttribute.values = { create: attribute.values };
}
attributesCreate.push(newAttribute);
});
const propertiesCreate = [];
tag.properties.forEach(property => {
const newProperty = {
name: property.name,
type: property.type,
};
if (property.attribute) {
newProperty.attribute = property.attribute;
}
if (property.reflect) {
newProperty.reflect = property.reflect;
}
if (property.values) {
newProperty.values = { create: property.values };
}
propertiesCreate.push(newProperty);
});
const newTag = {
name: tag.name,
library,
attributes: {
create: attributesCreate,
},
properties: {
create: propertiesCreate,
},
};
customElementsCreate.push(newTag);
});
const data = {
name: pkg.name,
version,
library,
packageJsonString: JSON.stringify(pkg.versions[version]),
customElementsString: JSON.stringify(rawCustomElements),
customElements: {
create: customElementsCreate,
},
};
if (pkg.description) {
data.description = pkg.description;
}
if (pkg.homepage) {
data.homepage = pkg.homepage;
}
if (pkg.license) {
data.license = pkg.license;
}
if (pkg.module) {
data.module = pkg.module;
}
if (pkg.main) {
data.main = pkg.main;
}
const query = `
mutation createPackage {
createPackage(
data: ${queryfy(data)}
) {
name
_id
customElements {
data {
name
attributes {
data {
name
type
}
}
properties {
data {
name
type
attribute
reflect
}
}
}
}
}
}
`;
console.log(`Adding Package ${pkg.name}@${version}`);
rawCustomElements.tags.forEach(tag => {
console.log(`Adding CustomElement ${tag.name}`);
});
console.log('---');
try {
await graphQLClient.request(query);
} catch (err) {
console.log('## Could not execute `createPackage` on db', err);
}
}
module.exports = {
getNpmSyncSeq,
updateNpmSyncSeq,
createPackage,
};