@slicemachine/manager 0.16.1-dev-next-release.2 → 0.16.1-dev-environments.2
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/dist/auth/PrismicAuthManager.cjs +1 -1
- package/dist/auth/PrismicAuthManager.cjs.map +1 -1
- package/dist/auth/PrismicAuthManager.js +2 -2
- package/dist/auth/PrismicAuthManager.js.map +1 -1
- package/dist/client/index.d.ts +2 -1
- package/dist/client.cjs +1 -0
- package/dist/client.cjs.map +1 -1
- package/dist/client.js +2 -1
- package/dist/constants/API_ENDPOINTS.cjs +6 -3
- package/dist/constants/API_ENDPOINTS.cjs.map +1 -1
- package/dist/constants/API_ENDPOINTS.d.ts +1 -0
- package/dist/constants/API_ENDPOINTS.js +6 -3
- package/dist/constants/API_ENDPOINTS.js.map +1 -1
- package/dist/errors.cjs +20 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +20 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +9 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/managers/customTypes/CustomTypesManager.cjs +4 -4
- package/dist/managers/customTypes/CustomTypesManager.cjs.map +1 -1
- package/dist/managers/customTypes/CustomTypesManager.js +4 -4
- package/dist/managers/customTypes/CustomTypesManager.js.map +1 -1
- package/dist/managers/prismicRepository/PrismicRepositoryManager.cjs +37 -4
- package/dist/managers/prismicRepository/PrismicRepositoryManager.cjs.map +1 -1
- package/dist/managers/prismicRepository/PrismicRepositoryManager.d.ts +2 -1
- package/dist/managers/prismicRepository/PrismicRepositoryManager.js +38 -5
- package/dist/managers/prismicRepository/PrismicRepositoryManager.js.map +1 -1
- package/dist/managers/prismicRepository/sortEnvironments.cjs +36 -0
- package/dist/managers/prismicRepository/sortEnvironments.cjs.map +1 -0
- package/dist/managers/prismicRepository/sortEnvironments.d.ts +17 -0
- package/dist/managers/prismicRepository/sortEnvironments.js +36 -0
- package/dist/managers/prismicRepository/sortEnvironments.js.map +1 -0
- package/dist/managers/prismicRepository/types.cjs +9 -0
- package/dist/managers/prismicRepository/types.cjs.map +1 -1
- package/dist/managers/prismicRepository/types.d.ts +9 -0
- package/dist/managers/prismicRepository/types.js +9 -0
- package/dist/managers/prismicRepository/types.js.map +1 -1
- package/dist/managers/project/ProjectManager.cjs +67 -0
- package/dist/managers/project/ProjectManager.cjs.map +1 -1
- package/dist/managers/project/ProjectManager.d.ts +33 -1
- package/dist/managers/project/ProjectManager.js +67 -1
- package/dist/managers/project/ProjectManager.js.map +1 -1
- package/dist/managers/screenshots/ScreenshotsManager.cjs +2 -2
- package/dist/managers/screenshots/ScreenshotsManager.cjs.map +1 -1
- package/dist/managers/screenshots/ScreenshotsManager.js +2 -2
- package/dist/managers/screenshots/ScreenshotsManager.js.map +1 -1
- package/dist/managers/slices/SlicesManager.cjs +4 -4
- package/dist/managers/slices/SlicesManager.cjs.map +1 -1
- package/dist/managers/slices/SlicesManager.js +4 -4
- package/dist/managers/slices/SlicesManager.js.map +1 -1
- package/package.json +3 -3
- package/src/auth/PrismicAuthManager.ts +1 -1
- package/src/client/index.ts +3 -0
- package/src/constants/API_ENDPOINTS.ts +6 -0
- package/src/errors.ts +4 -0
- package/src/index.ts +13 -0
- package/src/managers/customTypes/CustomTypesManager.ts +4 -4
- package/src/managers/prismicRepository/PrismicRepositoryManager.ts +55 -4
- package/src/managers/prismicRepository/sortEnvironments.ts +55 -0
- package/src/managers/prismicRepository/types.ts +12 -0
- package/src/managers/project/ProjectManager.ts +131 -3
- package/src/managers/screenshots/ScreenshotsManager.ts +2 -2
- package/src/managers/slices/SlicesManager.ts +4 -4
|
@@ -4,16 +4,28 @@ import * as path from "node:path";
|
|
|
4
4
|
import { createRequire } from "node:module";
|
|
5
5
|
import { detect as niDetect } from "@antfu/ni";
|
|
6
6
|
import { ExecaChildProcess } from "execa";
|
|
7
|
-
|
|
7
|
+
import {
|
|
8
|
+
HookError,
|
|
9
|
+
CallHookReturnType,
|
|
10
|
+
ProjectEnvironmentUpdateHook,
|
|
11
|
+
} from "@slicemachine/plugin-kit";
|
|
12
|
+
import * as t from "io-ts";
|
|
13
|
+
|
|
14
|
+
import { DecodeError } from "../../lib/DecodeError";
|
|
8
15
|
import { assertPluginsInitialized } from "../../lib/assertPluginsInitialized";
|
|
16
|
+
import { decodeHookResult } from "../../lib/decodeHookResult";
|
|
9
17
|
import { decodeSliceMachineConfig } from "../../lib/decodeSliceMachineConfig";
|
|
10
18
|
import { format } from "../../lib/format";
|
|
11
19
|
import { installDependencies } from "../../lib/installDependencies";
|
|
12
20
|
import { locateFileUpward } from "../../lib/locateFileUpward";
|
|
13
21
|
|
|
14
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
PackageManager,
|
|
24
|
+
SliceMachineConfig,
|
|
25
|
+
OnlyHookErrors,
|
|
26
|
+
} from "../../types";
|
|
15
27
|
|
|
16
|
-
import { SliceMachineError, InternalError } from "../../errors";
|
|
28
|
+
import { SliceMachineError, InternalError, PluginError } from "../../errors";
|
|
17
29
|
|
|
18
30
|
import { SLICE_MACHINE_CONFIG_FILENAME } from "../../constants/SLICE_MACHINE_CONFIG_FILENAME";
|
|
19
31
|
import { TS_CONFIG_FILENAME } from "../../constants/TS_CONFIG_FILENAME";
|
|
@@ -57,6 +69,15 @@ type ProjectManagerInstallDependenciesReturnType = {
|
|
|
57
69
|
execaProcess: ExecaChildProcess;
|
|
58
70
|
};
|
|
59
71
|
|
|
72
|
+
type ProjectManagerReadEnvironmentReturnType = {
|
|
73
|
+
environment: string | undefined;
|
|
74
|
+
errors: (DecodeError | HookError)[];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
type ProjectManagerUpdateEnvironmentArgs = {
|
|
78
|
+
environment: string | undefined;
|
|
79
|
+
};
|
|
80
|
+
|
|
60
81
|
export class ProjectManager extends BaseManager {
|
|
61
82
|
private _cachedRoot: string | undefined;
|
|
62
83
|
private _cachedSliceMachineConfigPath: string | undefined;
|
|
@@ -195,12 +216,43 @@ export class ProjectManager extends BaseManager {
|
|
|
195
216
|
return path.dirname(sliceMachinePackageJSONPath);
|
|
196
217
|
}
|
|
197
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Returns the project's repository name (i.e. the production environment). It
|
|
221
|
+
* ignores the currently selected environment.
|
|
222
|
+
*
|
|
223
|
+
* Use this method to retrieve the production environment domain.
|
|
224
|
+
*
|
|
225
|
+
* @returns The project's repository name.
|
|
226
|
+
*/
|
|
198
227
|
async getRepositoryName(): Promise<string> {
|
|
199
228
|
const sliceMachineConfig = await this.getSliceMachineConfig();
|
|
200
229
|
|
|
201
230
|
return sliceMachineConfig.repositoryName;
|
|
202
231
|
}
|
|
203
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Returns the currently selected environment domain if set. If an environment
|
|
235
|
+
* is not set, it returns the project's repository name (the production
|
|
236
|
+
* environment).
|
|
237
|
+
*
|
|
238
|
+
* Use this method to retrieve the repository name to be sent with Prismic API
|
|
239
|
+
* requests.
|
|
240
|
+
*
|
|
241
|
+
* @returns The resolved repository name.
|
|
242
|
+
*/
|
|
243
|
+
async getResolvedRepositoryName(): Promise<string> {
|
|
244
|
+
const repositoryName = await this.getRepositoryName();
|
|
245
|
+
|
|
246
|
+
const supportsEnvironments = this.project.checkSupportsEnvironments();
|
|
247
|
+
if (!supportsEnvironments) {
|
|
248
|
+
return repositoryName;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const { environment } = await this.project.readEnvironment();
|
|
252
|
+
|
|
253
|
+
return environment ?? repositoryName;
|
|
254
|
+
}
|
|
255
|
+
|
|
204
256
|
async getAdapterName(): Promise<string> {
|
|
205
257
|
const sliceMachineConfig = await this.getSliceMachineConfig();
|
|
206
258
|
const adapterName =
|
|
@@ -312,4 +364,80 @@ export class ProjectManager extends BaseManager {
|
|
|
312
364
|
throw error;
|
|
313
365
|
}
|
|
314
366
|
}
|
|
367
|
+
|
|
368
|
+
checkSupportsEnvironments(): boolean {
|
|
369
|
+
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
|
370
|
+
|
|
371
|
+
return (
|
|
372
|
+
this.sliceMachinePluginRunner.hooksForType("project:environment:read")
|
|
373
|
+
.length > 0 &&
|
|
374
|
+
this.sliceMachinePluginRunner.hooksForType("project:environment:update")
|
|
375
|
+
.length > 0
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async readEnvironment(): Promise<ProjectManagerReadEnvironmentReturnType> {
|
|
380
|
+
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
|
381
|
+
|
|
382
|
+
await this._assertAdapterSupportsEnvironments();
|
|
383
|
+
|
|
384
|
+
const hookResult = await this.sliceMachinePluginRunner.callHook(
|
|
385
|
+
"project:environment:read",
|
|
386
|
+
undefined,
|
|
387
|
+
);
|
|
388
|
+
const { data, errors } = decodeHookResult(
|
|
389
|
+
t.type({
|
|
390
|
+
environment: t.union([t.undefined, t.string]),
|
|
391
|
+
}),
|
|
392
|
+
hookResult,
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
// An undefined value is equivalent to the production environment.
|
|
396
|
+
// We cast to undefined.
|
|
397
|
+
const repositoryName = await this.project.getRepositoryName();
|
|
398
|
+
const environmentDomain =
|
|
399
|
+
data[0]?.environment === repositoryName
|
|
400
|
+
? undefined
|
|
401
|
+
: data[0]?.environment;
|
|
402
|
+
|
|
403
|
+
return {
|
|
404
|
+
environment: environmentDomain,
|
|
405
|
+
errors,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async updateEnvironment(
|
|
410
|
+
args: ProjectManagerUpdateEnvironmentArgs,
|
|
411
|
+
): Promise<OnlyHookErrors<CallHookReturnType<ProjectEnvironmentUpdateHook>>> {
|
|
412
|
+
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
|
413
|
+
|
|
414
|
+
await this._assertAdapterSupportsEnvironments();
|
|
415
|
+
|
|
416
|
+
const repositoryName = await this.project.getRepositoryName();
|
|
417
|
+
const environment =
|
|
418
|
+
args.environment === repositoryName ? undefined : args.environment;
|
|
419
|
+
|
|
420
|
+
const hookResult = await this.sliceMachinePluginRunner.callHook(
|
|
421
|
+
"project:environment:update",
|
|
422
|
+
{ environment },
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
errors: hookResult.errors,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private async _assertAdapterSupportsEnvironments(): Promise<void> {
|
|
431
|
+
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
|
432
|
+
|
|
433
|
+
const supportsEnvironments = this.checkSupportsEnvironments();
|
|
434
|
+
|
|
435
|
+
if (!supportsEnvironments) {
|
|
436
|
+
const adapterName = await this.project.getAdapterName();
|
|
437
|
+
|
|
438
|
+
throw new PluginError(
|
|
439
|
+
`${adapterName} does not support environments. Use an adapter that implements the \`project:environment:read\` and \`project:environment:update\` hooks to use environments.`,
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
315
443
|
}
|
|
@@ -183,13 +183,13 @@ export class ScreenshotsManager extends BaseManager {
|
|
|
183
183
|
body?: unknown;
|
|
184
184
|
}): Promise<Response> {
|
|
185
185
|
const authenticationToken = await this.user.getAuthenticationToken();
|
|
186
|
-
const
|
|
186
|
+
const repositoryName = await this.project.getRepositoryName();
|
|
187
187
|
|
|
188
188
|
return await fetch(args.url, {
|
|
189
189
|
body: args.body ? JSON.stringify(args.body) : undefined,
|
|
190
190
|
headers: {
|
|
191
191
|
Authorization: `Bearer ${authenticationToken}`,
|
|
192
|
-
Repository:
|
|
192
|
+
Repository: repositoryName,
|
|
193
193
|
"User-Agent": SLICE_MACHINE_USER_AGENT,
|
|
194
194
|
...(args.body ? { "Content-Type": "application/json" } : {}),
|
|
195
195
|
},
|
|
@@ -764,12 +764,12 @@ export class SlicesManager extends BaseManager {
|
|
|
764
764
|
});
|
|
765
765
|
|
|
766
766
|
const authenticationToken = await this.user.getAuthenticationToken();
|
|
767
|
-
const
|
|
767
|
+
const repositoryName = await this.project.getResolvedRepositoryName();
|
|
768
768
|
|
|
769
769
|
// TODO: Create a single shared client.
|
|
770
770
|
const client = prismicCustomTypesClient.createClient({
|
|
771
771
|
endpoint: API_ENDPOINTS.PrismicModels,
|
|
772
|
-
repositoryName
|
|
772
|
+
repositoryName,
|
|
773
773
|
token: authenticationToken,
|
|
774
774
|
userAgent: args.userAgent || SLICE_MACHINE_USER_AGENT,
|
|
775
775
|
fetch,
|
|
@@ -981,11 +981,11 @@ export class SlicesManager extends BaseManager {
|
|
|
981
981
|
|
|
982
982
|
async fetchRemoteSlices(): Promise<SharedSlice[]> {
|
|
983
983
|
const authenticationToken = await this.user.getAuthenticationToken();
|
|
984
|
-
const
|
|
984
|
+
const repositoryName = await this.project.getResolvedRepositoryName();
|
|
985
985
|
|
|
986
986
|
const client = prismicCustomTypesClient.createClient({
|
|
987
987
|
endpoint: API_ENDPOINTS.PrismicModels,
|
|
988
|
-
repositoryName
|
|
988
|
+
repositoryName,
|
|
989
989
|
token: authenticationToken,
|
|
990
990
|
userAgent: SLICE_MACHINE_USER_AGENT,
|
|
991
991
|
fetch,
|