-
Notifications
You must be signed in to change notification settings - Fork 0
/
npm-packager-proxy.ts
218 lines (204 loc) · 6.6 KB
/
npm-packager-proxy.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
DeepClient,
SerialOperation,
} from '@deep-foundation/deeplinks/imports/client.js';
import { BoolExpLink } from '@deep-foundation/deeplinks/imports/client_types.js';
import { createSerialOperation } from '@deep-foundation/deeplinks/imports/gql/index.js';
import { MinilinkCollection, Minilinks } from '@deep-foundation/deeplinks/imports/minilinks';
import debug from 'debug';
/**
* Proxy for package management
*
* @example
```ts
const npmPackagerProxy = new NpmPackagerProxy(deep);
await npmPackagerProxy.applyMinilinks();
await npmPackagerProxy.install('@deep-foundation/logger');
```
*/
export class NpmPackagerProxy {
private deep: DeepClient;
constructor(deep: DeepClient) {
this.deep = deep;
}
/**
* Installs packages
*
* @remarks
* {@link REQUIRED_PACKAGES} must be in minilinks. It is recommended to use {@link applyMinilinks} for this
*
* @throws
* If {@link REQUIRED_PACKAGES} is not in minilinks
* If installation failed
*/
public async install(...packageNames: Array<string>): Promise<any> {
const log = debug(`@deep-foundation/npm-packager:${NpmPackagerProxy.name}:${this.install.name}`)
log({packageNames})
const operations = await this.makeInstallPackagesOperations(...packageNames);
log({operations})
const serialResult = await this.deep.serial({
operations: operations.flatMap(operation => operation.operations)
})
log({serialResult})
const promisesToAwaitInstallation = operations.map(async (operation) => await this.deep.await(operation.installLinkId));
log({promisesToAwaitInstallation})
await Promise.all(promisesToAwaitInstallation);
const intalledTypeLinkId = this.deep.idLocal(RequiredPackages.NpmPackager, "Installed");
log({intalledTypeLinkId})
const {data: failedInstallLinkIds} = await this.deep.select({
_or: operations.map(operation => (
{
id: operation.installLinkId,
out: {
_not: {
type_id: intalledTypeLinkId,
to: {
id: {
_id: [operation.packageName]
}
}
}
}
}
))
})
log({failedInstallLinkIds})
if(failedInstallLinkIds.length > 0) {
throw new Error(`Failed to install ${failedInstallLinkIds.join(', ')}`)
}
}
/**
* Makes operations to install packages
*
* @remarks
* {@link REQUIRED_PACKAGES} must be in minilinks. It is recommended to use {@link applyMinilinks} for this
*
* @throws
* If {@link REQUIRED_PACKAGES} is not in minilinks
*/
public async makeInstallPackagesOperations(...packageNames: Array<string>): Promise<MakeInstallPackagesOperationsReturnType> {
const log = debug(`@deep-foundation/npm-packager:${NpmPackagerProxy.name}:${this.makeInstallPackagesOperations.name}`)
this.ensureRequiredPackagesAreInMinilinks()
const containTypeLinkId = this.deep.idLocal(
RequiredPackages.Core,
'Contain'
);
log({containTypeLinkId})
const reservedLinkIds = await this.deep.reserve(packageNames.length * 2)
log({reservedLinkIds})
const result = packageNames.map((packageName) => {
const packageQueryLinkId = reservedLinkIds.pop()!;
const installLinkId = reservedLinkIds.pop()!;
return {
packageName,
packageQueryLinkId,
installLinkId,
operations: [
createSerialOperation({
type: 'insert',
table: 'links',
objects: {
id: packageQueryLinkId,
type_id: this.deep.idLocal(
RequiredPackages.Core,
'PackageQuery'
),
}
}),
createSerialOperation({
type: 'insert',
table: 'strings',
objects: {
link_id: packageQueryLinkId,
value: packageName
}
}),
createSerialOperation({
type: 'insert',
table: 'links',
objects: {
type_id: containTypeLinkId,
from_id: this.deep.linkId,
to_id: packageQueryLinkId
}
}),
createSerialOperation({
type: 'insert',
table: 'links',
objects: {
id: installLinkId,
type_id: this.deep.idLocal(
RequiredPackages.NpmPackager,
'Install'
),
from_id: this.deep.linkId,
to_id: packageQueryLinkId
}
}),
createSerialOperation({
type: 'insert',
table: 'links',
objects: {
type_id: containTypeLinkId,
from_id: this.deep.linkId,
to_id: installLinkId
}
}),
]
}
})
log({result})
return result
}
public ensureRequiredPackagesAreInMinilinks() {
for (const requiredPackageName of Object.values(RequiredPackages)) {
try {
this.deep.idLocal(requiredPackageName);
} catch (error) {
throw new Error(`Required package ${requiredPackageName} is not in minilinks`)
}
}
}
/**
* Puts {@link REQUIRED_PACKAGES} to minilinks
*/
public async applyMinilinks() {
const log = debug(`@deep-foundation/npm-packager:${NpmPackagerProxy.name}:${this.applyMinilinks.name}`)
const requiredPackageLinks = await this.getRequiredPackagesLinks();
log({requiredPackageLinks})
const applyMinilinksResult = this.deep.minilinks.apply(requiredPackageLinks.data);
log({applyMinilinksResult})
return applyMinilinksResult
}
/**
* Gets all links down in contain tree to {@link REQUIRED_PACKAGES}
*/
public async getRequiredPackagesLinks() {
const log = debug(`@deep-foundation/npm-packager:${NpmPackagerProxy.name}:${this.getRequiredPackagesLinks.name}`)
const packageNamesToApply = Object.values(RequiredPackages);
log({packageNamesToApply})
const selectData: BoolExpLink = {
up: {
tree_id: {
_id: ["@deep-foundation/core", "containTree"]
},
parent: {
_or: packageNamesToApply.map(packageName => ({
id: {
_id: [packageName]
}
}))
}
}
}
log({selectData})
const result = await this.deep.select(selectData);
log({result})
return result;
}
}
export enum RequiredPackages {
Core = '@deep-foundation/core',
NpmPackager = '@deep-foundation/npm-packager',
}
export type MakeInstallPackagesOperationsReturnType = Array<{packageName: string; packageQueryLinkId: number; installLinkId: number; operations: Array<SerialOperation>}>