@slicemachine/manager 0.15.4-dev-environments.0 → 0.15.4-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/client/index.d.ts +1 -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 +3 -5
- package/dist/constants/API_ENDPOINTS.cjs.map +1 -1
- package/dist/constants/API_ENDPOINTS.d.ts +1 -1
- package/dist/constants/API_ENDPOINTS.js +3 -5
- package/dist/constants/API_ENDPOINTS.js.map +1 -1
- package/dist/errors.cjs +4 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +4 -0
- package/dist/errors.js.map +1 -1
- package/dist/managers/prismicRepository/PrismicRepositoryManager.cjs +17 -5
- package/dist/managers/prismicRepository/PrismicRepositoryManager.cjs.map +1 -1
- package/dist/managers/prismicRepository/PrismicRepositoryManager.js +18 -6
- 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/project/ProjectManager.cjs.map +1 -1
- package/dist/managers/project/ProjectManager.js.map +1 -1
- package/package.json +3 -3
- package/src/client/index.ts +1 -0
- package/src/constants/API_ENDPOINTS.ts +6 -6
- package/src/errors.ts +4 -0
- package/src/managers/prismicRepository/PrismicRepositoryManager.ts +25 -6
- package/src/managers/prismicRepository/sortEnvironments.ts +55 -0
- package/src/managers/project/ProjectManager.ts +2 -2
package/dist/client/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ export type { CreateSliceMachineManagerClientArgs, SliceMachineManagerClient, }
|
|
|
3
3
|
export type { SimulatorManagerReadSliceSimulatorSetupStep, SimulatorManagerReadSliceSimulatorSetupStepsReturnType, } from "../managers/simulator/SimulatorManager";
|
|
4
4
|
export type { Environment } from "../managers/prismicRepository/types";
|
|
5
5
|
export type { SliceMachineManagerPushSliceReturnType } from "../managers/slices/SlicesManager";
|
|
6
|
-
export { InternalError, NotFoundError, SliceMachineError, UnauthenticatedError, UnauthorizedError, UnexpectedDataError, isInternalError, isNotFoundError, isSliceMachineError, isUnauthenticatedError, isUnauthorizedError, isUnexpectedDataError, } from "../errors";
|
|
6
|
+
export { InternalError, NotFoundError, SliceMachineError, UnauthenticatedError, UnauthorizedError, UnexpectedDataError, isInternalError, isNotFoundError, isPluginError, isSliceMachineError, isUnauthenticatedError, isUnauthorizedError, isUnexpectedDataError, } from "../errors";
|
|
7
7
|
export { DecodeError } from "../lib/DecodeError";
|
package/dist/client.cjs
CHANGED
|
@@ -12,6 +12,7 @@ exports.UnauthorizedError = errors.UnauthorizedError;
|
|
|
12
12
|
exports.UnexpectedDataError = errors.UnexpectedDataError;
|
|
13
13
|
exports.isInternalError = errors.isInternalError;
|
|
14
14
|
exports.isNotFoundError = errors.isNotFoundError;
|
|
15
|
+
exports.isPluginError = errors.isPluginError;
|
|
15
16
|
exports.isSliceMachineError = errors.isSliceMachineError;
|
|
16
17
|
exports.isUnauthenticatedError = errors.isUnauthenticatedError;
|
|
17
18
|
exports.isUnauthorizedError = errors.isUnauthorizedError;
|
package/dist/client.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;"}
|
package/dist/client.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createSliceMachineManagerClient } from "./managers/createSliceMachineManagerClient.js";
|
|
2
|
-
import { InternalError, NotFoundError, SliceMachineError, UnauthenticatedError, UnauthorizedError, UnexpectedDataError, isInternalError, isNotFoundError, isSliceMachineError, isUnauthenticatedError, isUnauthorizedError, isUnexpectedDataError } from "./errors.js";
|
|
2
|
+
import { InternalError, NotFoundError, SliceMachineError, UnauthenticatedError, UnauthorizedError, UnexpectedDataError, isInternalError, isNotFoundError, isPluginError, isSliceMachineError, isUnauthenticatedError, isUnauthorizedError, isUnexpectedDataError } from "./errors.js";
|
|
3
3
|
import { DecodeError } from "./lib/DecodeError.js";
|
|
4
4
|
export {
|
|
5
5
|
DecodeError,
|
|
@@ -12,6 +12,7 @@ export {
|
|
|
12
12
|
createSliceMachineManagerClient,
|
|
13
13
|
isInternalError,
|
|
14
14
|
isNotFoundError,
|
|
15
|
+
isPluginError,
|
|
15
16
|
isSliceMachineError,
|
|
16
17
|
isUnauthenticatedError,
|
|
17
18
|
isUnauthorizedError,
|
|
@@ -13,7 +13,7 @@ const API_ENDPOINTS = (() => {
|
|
|
13
13
|
AwsAclProvider: trailingSlash.addTrailingSlash(process.env.acl_provider_endpoint),
|
|
14
14
|
PrismicOembed: trailingSlash.removeTrailingSlash(process.env.oembed_endpoint ?? "https://oembed.wroom.io"),
|
|
15
15
|
PrismicUnsplash: trailingSlash.removeTrailingSlash(process.env.unsplash_endpoint ?? "https://unsplash.wroom.io"),
|
|
16
|
-
|
|
16
|
+
SliceMachineV1: trailingSlash.addTrailingSlash(process.env.slice_machine_v1_endpoint)
|
|
17
17
|
};
|
|
18
18
|
const missingAPIEndpoints = Object.keys(apiEndpoints).filter((key) => {
|
|
19
19
|
return !apiEndpoints[key];
|
|
@@ -33,8 +33,7 @@ const API_ENDPOINTS = (() => {
|
|
|
33
33
|
AwsAclProvider: "https://2iamcvnxf4.execute-api.us-east-1.amazonaws.com/stage/",
|
|
34
34
|
PrismicOembed: "https://oembed.wroom.io",
|
|
35
35
|
PrismicUnsplash: "https://unsplash.wroom.io",
|
|
36
|
-
|
|
37
|
-
SliceMachine: "https://slice-machine-api-staging.example"
|
|
36
|
+
SliceMachineV1: "https://mc5qopc07a.execute-api.us-east-1.amazonaws.com/v1/"
|
|
38
37
|
};
|
|
39
38
|
}
|
|
40
39
|
case APPLICATION_MODE.APPLICATION_MODE.Production:
|
|
@@ -47,8 +46,7 @@ const API_ENDPOINTS = (() => {
|
|
|
47
46
|
AwsAclProvider: "https://0yyeb2g040.execute-api.us-east-1.amazonaws.com/prod/",
|
|
48
47
|
PrismicOembed: "https://oembed.prismic.io",
|
|
49
48
|
PrismicUnsplash: "https://unsplash.prismic.io",
|
|
50
|
-
|
|
51
|
-
SliceMachine: "https://slice-machine-api.example"
|
|
49
|
+
SliceMachineV1: "https://21vvgrh0s6.execute-api.us-east-1.amazonaws.com/v1/"
|
|
52
50
|
};
|
|
53
51
|
}
|
|
54
52
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"API_ENDPOINTS.cjs","sources":["../../../src/constants/API_ENDPOINTS.ts"],"sourcesContent":["import { addTrailingSlash, removeTrailingSlash } from \"../lib/trailingSlash\";\nimport { APPLICATION_MODE } from \"./APPLICATION_MODE\";\n\nexport type APIEndpoints = {\n\tPrismicWroom: string;\n\tPrismicAuthentication: string;\n\tPrismicModels: string;\n\tPrismicUser: string;\n\tAwsAclProvider: string;\n\tPrismicOembed: string;\n\tPrismicUnsplash: string;\n\
|
|
1
|
+
{"version":3,"file":"API_ENDPOINTS.cjs","sources":["../../../src/constants/API_ENDPOINTS.ts"],"sourcesContent":["import { addTrailingSlash, removeTrailingSlash } from \"../lib/trailingSlash\";\nimport { APPLICATION_MODE } from \"./APPLICATION_MODE\";\n\nexport type APIEndpoints = {\n\tPrismicWroom: string;\n\tPrismicAuthentication: string;\n\tPrismicModels: string;\n\tPrismicUser: string;\n\tAwsAclProvider: string;\n\tPrismicOembed: string;\n\tPrismicUnsplash: string;\n\tSliceMachineV1: string;\n};\n\nexport const API_ENDPOINTS: APIEndpoints = (() => {\n\tswitch (process.env.SM_ENV) {\n\t\tcase APPLICATION_MODE.Development: {\n\t\t\tconst apiEndpoints = {\n\t\t\t\tPrismicWroom: addTrailingSlash(process.env.wroom_endpoint),\n\t\t\t\tPrismicAuthentication: addTrailingSlash(\n\t\t\t\t\tprocess.env.authentication_server_endpoint,\n\t\t\t\t),\n\t\t\t\tPrismicModels: addTrailingSlash(process.env.customtypesapi_endpoint),\n\t\t\t\tPrismicUser: addTrailingSlash(process.env.user_service_endpoint),\n\t\t\t\tAwsAclProvider: addTrailingSlash(process.env.acl_provider_endpoint),\n\t\t\t\tPrismicOembed: removeTrailingSlash(\n\t\t\t\t\tprocess.env.oembed_endpoint ?? \"https://oembed.wroom.io\",\n\t\t\t\t),\n\t\t\t\tPrismicUnsplash: removeTrailingSlash(\n\t\t\t\t\tprocess.env.unsplash_endpoint ?? \"https://unsplash.wroom.io\",\n\t\t\t\t),\n\t\t\t\tSliceMachineV1: addTrailingSlash(process.env.slice_machine_v1_endpoint),\n\t\t\t};\n\n\t\t\tconst missingAPIEndpoints = Object.keys(apiEndpoints).filter((key) => {\n\t\t\t\treturn !apiEndpoints[key as keyof typeof apiEndpoints];\n\t\t\t});\n\n\t\t\tif (missingAPIEndpoints.length > 0) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`You are running Slice Machine in development mode where API endpoints are configured via environment variables. The following endpoints were not configured: ${missingAPIEndpoints.join(\n\t\t\t\t\t\t\", \",\n\t\t\t\t\t)}. Configure them before continuing.`,\n\t\t\t\t);\n\n\t\t\t\tprocess.exit(1);\n\t\t\t}\n\n\t\t\treturn apiEndpoints as APIEndpoints;\n\t\t}\n\n\t\tcase APPLICATION_MODE.Staging: {\n\t\t\treturn {\n\t\t\t\tPrismicWroom: \"https://wroom.io/\",\n\t\t\t\tPrismicAuthentication: \"https://auth.wroom.io/\",\n\t\t\t\tPrismicModels: \"https://customtypes.wroom.io/\",\n\t\t\t\tPrismicUser: \"https://user.wroom.io/\",\n\t\t\t\tAwsAclProvider:\n\t\t\t\t\t\"https://2iamcvnxf4.execute-api.us-east-1.amazonaws.com/stage/\",\n\t\t\t\tPrismicOembed: \"https://oembed.wroom.io\",\n\t\t\t\tPrismicUnsplash: \"https://unsplash.wroom.io\",\n\t\t\t\tSliceMachineV1:\n\t\t\t\t\t\"https://mc5qopc07a.execute-api.us-east-1.amazonaws.com/v1/\",\n\t\t\t};\n\t\t}\n\n\t\tcase APPLICATION_MODE.Production:\n\t\tdefault: {\n\t\t\treturn {\n\t\t\t\tPrismicWroom: \"https://prismic.io/\",\n\t\t\t\tPrismicAuthentication: \"https://auth.prismic.io/\",\n\t\t\t\tPrismicModels: \"https://customtypes.prismic.io/\",\n\t\t\t\tPrismicUser: \"https://user.internal-prismic.io/\",\n\t\t\t\tAwsAclProvider:\n\t\t\t\t\t\"https://0yyeb2g040.execute-api.us-east-1.amazonaws.com/prod/\",\n\t\t\t\tPrismicOembed: \"https://oembed.prismic.io\",\n\t\t\t\tPrismicUnsplash: \"https://unsplash.prismic.io\",\n\t\t\t\tSliceMachineV1:\n\t\t\t\t\t\"https://21vvgrh0s6.execute-api.us-east-1.amazonaws.com/v1/\",\n\t\t\t};\n\t\t}\n\t}\n})();\n"],"names":["APPLICATION_MODE","addTrailingSlash","removeTrailingSlash"],"mappings":";;;;AAcO,MAAM,iBAA+B,MAAK;AACxC,UAAA,QAAQ,IAAI,QAAQ;AAAA,IAC3B,KAAKA,iBAAAA,iBAAiB,aAAa;AAClC,YAAM,eAAe;AAAA,QACpB,cAAcC,cAAA,iBAAiB,QAAQ,IAAI,cAAc;AAAA,QACzD,uBAAuBA,cAAA,iBACtB,QAAQ,IAAI,8BAA8B;AAAA,QAE3C,eAAeA,cAAA,iBAAiB,QAAQ,IAAI,uBAAuB;AAAA,QACnE,aAAaA,cAAA,iBAAiB,QAAQ,IAAI,qBAAqB;AAAA,QAC/D,gBAAgBA,cAAA,iBAAiB,QAAQ,IAAI,qBAAqB;AAAA,QAClE,eAAeC,cAAAA,oBACd,QAAQ,IAAI,mBAAmB,yBAAyB;AAAA,QAEzD,iBAAiBA,cAAAA,oBAChB,QAAQ,IAAI,qBAAqB,2BAA2B;AAAA,QAE7D,gBAAgBD,cAAA,iBAAiB,QAAQ,IAAI,yBAAyB;AAAA,MAAA;AAGvE,YAAM,sBAAsB,OAAO,KAAK,YAAY,EAAE,OAAO,CAAC,QAAO;AAC7D,eAAA,CAAC,aAAa,GAAgC;AAAA,MAAA,CACrD;AAEG,UAAA,oBAAoB,SAAS,GAAG;AACnC,gBAAQ,MACP,gKAAgK,oBAAoB,KACnL,IAAI,sCACiC;AAGvC,gBAAQ,KAAK,CAAC;AAAA,MACd;AAEM,aAAA;AAAA,IACP;AAAA,IAED,KAAKD,iBAAAA,iBAAiB,SAAS;AACvB,aAAA;AAAA,QACN,cAAc;AAAA,QACd,uBAAuB;AAAA,QACvB,eAAe;AAAA,QACf,aAAa;AAAA,QACb,gBACC;AAAA,QACD,eAAe;AAAA,QACf,iBAAiB;AAAA,QACjB,gBACC;AAAA,MAAA;AAAA,IAEF;AAAA,IAED,KAAKA,iBAAAA,iBAAiB;AAAA,IACtB,SAAS;AACD,aAAA;AAAA,QACN,cAAc;AAAA,QACd,uBAAuB;AAAA,QACvB,eAAe;AAAA,QACf,aAAa;AAAA,QACb,gBACC;AAAA,QACD,eAAe;AAAA,QACf,iBAAiB;AAAA,QACjB,gBACC;AAAA,MAAA;AAAA,IAEF;AAAA,EACD;AACF;;"}
|
|
@@ -11,7 +11,7 @@ const API_ENDPOINTS = (() => {
|
|
|
11
11
|
AwsAclProvider: addTrailingSlash(process.env.acl_provider_endpoint),
|
|
12
12
|
PrismicOembed: removeTrailingSlash(process.env.oembed_endpoint ?? "https://oembed.wroom.io"),
|
|
13
13
|
PrismicUnsplash: removeTrailingSlash(process.env.unsplash_endpoint ?? "https://unsplash.wroom.io"),
|
|
14
|
-
|
|
14
|
+
SliceMachineV1: addTrailingSlash(process.env.slice_machine_v1_endpoint)
|
|
15
15
|
};
|
|
16
16
|
const missingAPIEndpoints = Object.keys(apiEndpoints).filter((key) => {
|
|
17
17
|
return !apiEndpoints[key];
|
|
@@ -31,8 +31,7 @@ const API_ENDPOINTS = (() => {
|
|
|
31
31
|
AwsAclProvider: "https://2iamcvnxf4.execute-api.us-east-1.amazonaws.com/stage/",
|
|
32
32
|
PrismicOembed: "https://oembed.wroom.io",
|
|
33
33
|
PrismicUnsplash: "https://unsplash.wroom.io",
|
|
34
|
-
|
|
35
|
-
SliceMachine: "https://slice-machine-api-staging.example"
|
|
34
|
+
SliceMachineV1: "https://mc5qopc07a.execute-api.us-east-1.amazonaws.com/v1/"
|
|
36
35
|
};
|
|
37
36
|
}
|
|
38
37
|
case APPLICATION_MODE.Production:
|
|
@@ -45,8 +44,7 @@ const API_ENDPOINTS = (() => {
|
|
|
45
44
|
AwsAclProvider: "https://0yyeb2g040.execute-api.us-east-1.amazonaws.com/prod/",
|
|
46
45
|
PrismicOembed: "https://oembed.prismic.io",
|
|
47
46
|
PrismicUnsplash: "https://unsplash.prismic.io",
|
|
48
|
-
|
|
49
|
-
SliceMachine: "https://slice-machine-api.example"
|
|
47
|
+
SliceMachineV1: "https://21vvgrh0s6.execute-api.us-east-1.amazonaws.com/v1/"
|
|
50
48
|
};
|
|
51
49
|
}
|
|
52
50
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"API_ENDPOINTS.js","sources":["../../../src/constants/API_ENDPOINTS.ts"],"sourcesContent":["import { addTrailingSlash, removeTrailingSlash } from \"../lib/trailingSlash\";\nimport { APPLICATION_MODE } from \"./APPLICATION_MODE\";\n\nexport type APIEndpoints = {\n\tPrismicWroom: string;\n\tPrismicAuthentication: string;\n\tPrismicModels: string;\n\tPrismicUser: string;\n\tAwsAclProvider: string;\n\tPrismicOembed: string;\n\tPrismicUnsplash: string;\n\
|
|
1
|
+
{"version":3,"file":"API_ENDPOINTS.js","sources":["../../../src/constants/API_ENDPOINTS.ts"],"sourcesContent":["import { addTrailingSlash, removeTrailingSlash } from \"../lib/trailingSlash\";\nimport { APPLICATION_MODE } from \"./APPLICATION_MODE\";\n\nexport type APIEndpoints = {\n\tPrismicWroom: string;\n\tPrismicAuthentication: string;\n\tPrismicModels: string;\n\tPrismicUser: string;\n\tAwsAclProvider: string;\n\tPrismicOembed: string;\n\tPrismicUnsplash: string;\n\tSliceMachineV1: string;\n};\n\nexport const API_ENDPOINTS: APIEndpoints = (() => {\n\tswitch (process.env.SM_ENV) {\n\t\tcase APPLICATION_MODE.Development: {\n\t\t\tconst apiEndpoints = {\n\t\t\t\tPrismicWroom: addTrailingSlash(process.env.wroom_endpoint),\n\t\t\t\tPrismicAuthentication: addTrailingSlash(\n\t\t\t\t\tprocess.env.authentication_server_endpoint,\n\t\t\t\t),\n\t\t\t\tPrismicModels: addTrailingSlash(process.env.customtypesapi_endpoint),\n\t\t\t\tPrismicUser: addTrailingSlash(process.env.user_service_endpoint),\n\t\t\t\tAwsAclProvider: addTrailingSlash(process.env.acl_provider_endpoint),\n\t\t\t\tPrismicOembed: removeTrailingSlash(\n\t\t\t\t\tprocess.env.oembed_endpoint ?? \"https://oembed.wroom.io\",\n\t\t\t\t),\n\t\t\t\tPrismicUnsplash: removeTrailingSlash(\n\t\t\t\t\tprocess.env.unsplash_endpoint ?? \"https://unsplash.wroom.io\",\n\t\t\t\t),\n\t\t\t\tSliceMachineV1: addTrailingSlash(process.env.slice_machine_v1_endpoint),\n\t\t\t};\n\n\t\t\tconst missingAPIEndpoints = Object.keys(apiEndpoints).filter((key) => {\n\t\t\t\treturn !apiEndpoints[key as keyof typeof apiEndpoints];\n\t\t\t});\n\n\t\t\tif (missingAPIEndpoints.length > 0) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`You are running Slice Machine in development mode where API endpoints are configured via environment variables. The following endpoints were not configured: ${missingAPIEndpoints.join(\n\t\t\t\t\t\t\", \",\n\t\t\t\t\t)}. Configure them before continuing.`,\n\t\t\t\t);\n\n\t\t\t\tprocess.exit(1);\n\t\t\t}\n\n\t\t\treturn apiEndpoints as APIEndpoints;\n\t\t}\n\n\t\tcase APPLICATION_MODE.Staging: {\n\t\t\treturn {\n\t\t\t\tPrismicWroom: \"https://wroom.io/\",\n\t\t\t\tPrismicAuthentication: \"https://auth.wroom.io/\",\n\t\t\t\tPrismicModels: \"https://customtypes.wroom.io/\",\n\t\t\t\tPrismicUser: \"https://user.wroom.io/\",\n\t\t\t\tAwsAclProvider:\n\t\t\t\t\t\"https://2iamcvnxf4.execute-api.us-east-1.amazonaws.com/stage/\",\n\t\t\t\tPrismicOembed: \"https://oembed.wroom.io\",\n\t\t\t\tPrismicUnsplash: \"https://unsplash.wroom.io\",\n\t\t\t\tSliceMachineV1:\n\t\t\t\t\t\"https://mc5qopc07a.execute-api.us-east-1.amazonaws.com/v1/\",\n\t\t\t};\n\t\t}\n\n\t\tcase APPLICATION_MODE.Production:\n\t\tdefault: {\n\t\t\treturn {\n\t\t\t\tPrismicWroom: \"https://prismic.io/\",\n\t\t\t\tPrismicAuthentication: \"https://auth.prismic.io/\",\n\t\t\t\tPrismicModels: \"https://customtypes.prismic.io/\",\n\t\t\t\tPrismicUser: \"https://user.internal-prismic.io/\",\n\t\t\t\tAwsAclProvider:\n\t\t\t\t\t\"https://0yyeb2g040.execute-api.us-east-1.amazonaws.com/prod/\",\n\t\t\t\tPrismicOembed: \"https://oembed.prismic.io\",\n\t\t\t\tPrismicUnsplash: \"https://unsplash.prismic.io\",\n\t\t\t\tSliceMachineV1:\n\t\t\t\t\t\"https://21vvgrh0s6.execute-api.us-east-1.amazonaws.com/v1/\",\n\t\t\t};\n\t\t}\n\t}\n})();\n"],"names":[],"mappings":";;AAcO,MAAM,iBAA+B,MAAK;AACxC,UAAA,QAAQ,IAAI,QAAQ;AAAA,IAC3B,KAAK,iBAAiB,aAAa;AAClC,YAAM,eAAe;AAAA,QACpB,cAAc,iBAAiB,QAAQ,IAAI,cAAc;AAAA,QACzD,uBAAuB,iBACtB,QAAQ,IAAI,8BAA8B;AAAA,QAE3C,eAAe,iBAAiB,QAAQ,IAAI,uBAAuB;AAAA,QACnE,aAAa,iBAAiB,QAAQ,IAAI,qBAAqB;AAAA,QAC/D,gBAAgB,iBAAiB,QAAQ,IAAI,qBAAqB;AAAA,QAClE,eAAe,oBACd,QAAQ,IAAI,mBAAmB,yBAAyB;AAAA,QAEzD,iBAAiB,oBAChB,QAAQ,IAAI,qBAAqB,2BAA2B;AAAA,QAE7D,gBAAgB,iBAAiB,QAAQ,IAAI,yBAAyB;AAAA,MAAA;AAGvE,YAAM,sBAAsB,OAAO,KAAK,YAAY,EAAE,OAAO,CAAC,QAAO;AAC7D,eAAA,CAAC,aAAa,GAAgC;AAAA,MAAA,CACrD;AAEG,UAAA,oBAAoB,SAAS,GAAG;AACnC,gBAAQ,MACP,gKAAgK,oBAAoB,KACnL,IAAI,sCACiC;AAGvC,gBAAQ,KAAK,CAAC;AAAA,MACd;AAEM,aAAA;AAAA,IACP;AAAA,IAED,KAAK,iBAAiB,SAAS;AACvB,aAAA;AAAA,QACN,cAAc;AAAA,QACd,uBAAuB;AAAA,QACvB,eAAe;AAAA,QACf,aAAa;AAAA,QACb,gBACC;AAAA,QACD,eAAe;AAAA,QACf,iBAAiB;AAAA,QACjB,gBACC;AAAA,MAAA;AAAA,IAEF;AAAA,IAED,KAAK,iBAAiB;AAAA,IACtB,SAAS;AACD,aAAA;AAAA,QACN,cAAc;AAAA,QACd,uBAAuB;AAAA,QACvB,eAAe;AAAA,QACf,aAAa;AAAA,QACb,gBACC;AAAA,QACD,eAAe;AAAA,QACf,iBAAiB;AAAA,QACjB,gBACC;AAAA,MAAA;AAAA,IAEF;AAAA,EACD;AACF;"}
|
package/dist/errors.cjs
CHANGED
|
@@ -80,6 +80,9 @@ const isUnexpectedDataError = (error) => {
|
|
|
80
80
|
const isInternalError = (error) => {
|
|
81
81
|
return isSliceMachineError(error) && error.name === InternalError.name;
|
|
82
82
|
};
|
|
83
|
+
const isPluginError = (error) => {
|
|
84
|
+
return isSliceMachineError(error) && error.name === PluginError.name;
|
|
85
|
+
};
|
|
83
86
|
exports.InternalError = InternalError;
|
|
84
87
|
exports.NotFoundError = NotFoundError;
|
|
85
88
|
exports.PluginError = PluginError;
|
|
@@ -90,6 +93,7 @@ exports.UnauthorizedError = UnauthorizedError;
|
|
|
90
93
|
exports.UnexpectedDataError = UnexpectedDataError;
|
|
91
94
|
exports.isInternalError = isInternalError;
|
|
92
95
|
exports.isNotFoundError = isNotFoundError;
|
|
96
|
+
exports.isPluginError = isPluginError;
|
|
93
97
|
exports.isSliceMachineError = isSliceMachineError;
|
|
94
98
|
exports.isUnauthenticatedError = isUnauthenticatedError;
|
|
95
99
|
exports.isUnauthorizedError = isUnauthorizedError;
|
package/dist/errors.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.cjs","sources":["../../src/errors.ts"],"sourcesContent":["import { HookError } from \"@slicemachine/plugin-kit\";\n\nexport class SliceMachineError extends Error {\n\t_sliceMachineError = true;\n\tname = \"SliceMachineError\";\n}\nexport class UnauthorizedError extends SliceMachineError {\n\tname = \"UnauthorizedError\";\n}\nexport class UnauthenticatedError extends SliceMachineError {\n\tname = \"UnauthenticatedError\";\n\tmessage = \"Authenticate before trying again.\";\n}\nexport class NotFoundError extends SliceMachineError {\n\tname = \"NotFoundError\";\n}\nexport class UnexpectedDataError extends SliceMachineError {\n\tname = \"UnexpectedDataError\";\n}\nexport class InternalError extends SliceMachineError {\n\tname = \"InternalError\";\n}\nexport class PluginError extends SliceMachineError {\n\tname = \"PluginError\";\n}\nexport class PluginHookResultError extends SliceMachineError {\n\tname = \"PluginHookResultError\";\n\n\tconstructor(errors: HookError[]) {\n\t\tsuper(\n\t\t\t`${errors.length} error${\n\t\t\t\terrors.length === 1 ? \"\" : \"s\"\n\t\t\t} were returned by one or more plugins.`,\n\t\t\t{\n\t\t\t\tcause: errors,\n\t\t\t},\n\t\t);\n\t}\n}\n\nexport const isSliceMachineError = (\n\terror: unknown,\n): error is SliceMachineError => {\n\t// TODO: Discuss a stronger way to serialize error for the client to detect with r19\n\t// @ts-expect-error We don't want to add \"dom\" to tsconfig \"lib\" because of the TODO\n\tif (typeof window !== \"undefined\") {\n\t\treturn typeof error === \"object\" && error !== null;\n\t} else {\n\t\treturn (\n\t\t\ttypeof error === \"object\" &&\n\t\t\terror !== null &&\n\t\t\t\"_sliceMachineError\" in error\n\t\t);\n\t}\n};\n\nexport const isUnauthorizedError = (\n\terror: unknown,\n): error is UnauthorizedError => {\n\treturn isSliceMachineError(error) && error.name === UnauthorizedError.name;\n};\n\nexport const isUnauthenticatedError = (\n\terror: unknown,\n): error is UnauthenticatedError => {\n\treturn isSliceMachineError(error) && error.name === UnauthenticatedError.name;\n};\n\nexport const isNotFoundError = (error: unknown): error is NotFoundError => {\n\treturn isSliceMachineError(error) && error.name === NotFoundError.name;\n};\n\nexport const isUnexpectedDataError = (\n\terror: unknown,\n): error is UnexpectedDataError => {\n\treturn isSliceMachineError(error) && error.name === UnexpectedDataError.name;\n};\n\nexport const isInternalError = (error: unknown): error is InternalError => {\n\treturn isSliceMachineError(error) && error.name === InternalError.name;\n};\n"],"names":[],"mappings":";;;;;;;;AAEM,MAAO,0BAA0B,MAAK;AAAA,EAAtC;AAAA;AACL,8CAAqB;AACrB,gCAAO;AAAA;AACP;AACK,MAAO,0BAA0B,kBAAiB;AAAA,EAAlD;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,6BAA6B,kBAAiB;AAAA,EAArD;AAAA;AACL,gCAAO;AACP,mCAAU;AAAA;AACV;AACK,MAAO,sBAAsB,kBAAiB;AAAA,EAA9C;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,4BAA4B,kBAAiB;AAAA,EAApD;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,sBAAsB,kBAAiB;AAAA,EAA9C;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,oBAAoB,kBAAiB;AAAA,EAA5C;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,8BAA8B,kBAAiB;AAAA,EAG3D,YAAY,QAAmB;AAC9B,UACC,GAAG,OAAO,eACT,OAAO,WAAW,IAAI,KAAK,6CAE5B;AAAA,MACC,OAAO;AAAA,IAAA,CACP;AATH,gCAAO;AAAA,EAWP;AACA;AAEY,MAAA,sBAAsB,CAClC,UAC+B;AAG3B,MAAA,OAAO,WAAW,aAAa;AAC3B,WAAA,OAAO,UAAU,YAAY,UAAU;AAAA,EAAA,OACxC;AACN,WACC,OAAO,UAAU,YACjB,UAAU,QACV,wBAAwB;AAAA,EAEzB;AACF;AAEa,MAAA,sBAAsB,CAClC,UAC+B;AAC/B,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,kBAAkB;AACvE;AAEa,MAAA,yBAAyB,CACrC,UACkC;AAClC,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,qBAAqB;AAC1E;AAEa,MAAA,kBAAkB,CAAC,UAA0C;AACzE,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,cAAc;AACnE;AAEa,MAAA,wBAAwB,CACpC,UACiC;AACjC,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,oBAAoB;AACzE;AAEa,MAAA,kBAAkB,CAAC,UAA0C;AACzE,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,cAAc;AACnE
|
|
1
|
+
{"version":3,"file":"errors.cjs","sources":["../../src/errors.ts"],"sourcesContent":["import { HookError } from \"@slicemachine/plugin-kit\";\n\nexport class SliceMachineError extends Error {\n\t_sliceMachineError = true;\n\tname = \"SliceMachineError\";\n}\nexport class UnauthorizedError extends SliceMachineError {\n\tname = \"UnauthorizedError\";\n}\nexport class UnauthenticatedError extends SliceMachineError {\n\tname = \"UnauthenticatedError\";\n\tmessage = \"Authenticate before trying again.\";\n}\nexport class NotFoundError extends SliceMachineError {\n\tname = \"NotFoundError\";\n}\nexport class UnexpectedDataError extends SliceMachineError {\n\tname = \"UnexpectedDataError\";\n}\nexport class InternalError extends SliceMachineError {\n\tname = \"InternalError\";\n}\nexport class PluginError extends SliceMachineError {\n\tname = \"PluginError\";\n}\nexport class PluginHookResultError extends SliceMachineError {\n\tname = \"PluginHookResultError\";\n\n\tconstructor(errors: HookError[]) {\n\t\tsuper(\n\t\t\t`${errors.length} error${\n\t\t\t\terrors.length === 1 ? \"\" : \"s\"\n\t\t\t} were returned by one or more plugins.`,\n\t\t\t{\n\t\t\t\tcause: errors,\n\t\t\t},\n\t\t);\n\t}\n}\n\nexport const isSliceMachineError = (\n\terror: unknown,\n): error is SliceMachineError => {\n\t// TODO: Discuss a stronger way to serialize error for the client to detect with r19\n\t// @ts-expect-error We don't want to add \"dom\" to tsconfig \"lib\" because of the TODO\n\tif (typeof window !== \"undefined\") {\n\t\treturn typeof error === \"object\" && error !== null;\n\t} else {\n\t\treturn (\n\t\t\ttypeof error === \"object\" &&\n\t\t\terror !== null &&\n\t\t\t\"_sliceMachineError\" in error\n\t\t);\n\t}\n};\n\nexport const isUnauthorizedError = (\n\terror: unknown,\n): error is UnauthorizedError => {\n\treturn isSliceMachineError(error) && error.name === UnauthorizedError.name;\n};\n\nexport const isUnauthenticatedError = (\n\terror: unknown,\n): error is UnauthenticatedError => {\n\treturn isSliceMachineError(error) && error.name === UnauthenticatedError.name;\n};\n\nexport const isNotFoundError = (error: unknown): error is NotFoundError => {\n\treturn isSliceMachineError(error) && error.name === NotFoundError.name;\n};\n\nexport const isUnexpectedDataError = (\n\terror: unknown,\n): error is UnexpectedDataError => {\n\treturn isSliceMachineError(error) && error.name === UnexpectedDataError.name;\n};\n\nexport const isInternalError = (error: unknown): error is InternalError => {\n\treturn isSliceMachineError(error) && error.name === InternalError.name;\n};\n\nexport const isPluginError = (error: unknown): error is PluginError => {\n\treturn isSliceMachineError(error) && error.name === PluginError.name;\n};\n"],"names":[],"mappings":";;;;;;;;AAEM,MAAO,0BAA0B,MAAK;AAAA,EAAtC;AAAA;AACL,8CAAqB;AACrB,gCAAO;AAAA;AACP;AACK,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,cAAc;AACnE;AAEa,MAAA,gBAAgB,CAAC,UAAwC;AACrE,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,YAAY;AACjE;;;;;;;;;;;;;;;;"}
|
package/dist/errors.d.ts
CHANGED
|
@@ -32,3 +32,4 @@ export declare const isUnauthenticatedError: (error: unknown) => error is Unauth
|
|
|
32
32
|
export declare const isNotFoundError: (error: unknown) => error is NotFoundError;
|
|
33
33
|
export declare const isUnexpectedDataError: (error: unknown) => error is UnexpectedDataError;
|
|
34
34
|
export declare const isInternalError: (error: unknown) => error is InternalError;
|
|
35
|
+
export declare const isPluginError: (error: unknown) => error is PluginError;
|
package/dist/errors.js
CHANGED
|
@@ -78,6 +78,9 @@ const isUnexpectedDataError = (error) => {
|
|
|
78
78
|
const isInternalError = (error) => {
|
|
79
79
|
return isSliceMachineError(error) && error.name === InternalError.name;
|
|
80
80
|
};
|
|
81
|
+
const isPluginError = (error) => {
|
|
82
|
+
return isSliceMachineError(error) && error.name === PluginError.name;
|
|
83
|
+
};
|
|
81
84
|
export {
|
|
82
85
|
InternalError,
|
|
83
86
|
NotFoundError,
|
|
@@ -89,6 +92,7 @@ export {
|
|
|
89
92
|
UnexpectedDataError,
|
|
90
93
|
isInternalError,
|
|
91
94
|
isNotFoundError,
|
|
95
|
+
isPluginError,
|
|
92
96
|
isSliceMachineError,
|
|
93
97
|
isUnauthenticatedError,
|
|
94
98
|
isUnauthorizedError,
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sources":["../../src/errors.ts"],"sourcesContent":["import { HookError } from \"@slicemachine/plugin-kit\";\n\nexport class SliceMachineError extends Error {\n\t_sliceMachineError = true;\n\tname = \"SliceMachineError\";\n}\nexport class UnauthorizedError extends SliceMachineError {\n\tname = \"UnauthorizedError\";\n}\nexport class UnauthenticatedError extends SliceMachineError {\n\tname = \"UnauthenticatedError\";\n\tmessage = \"Authenticate before trying again.\";\n}\nexport class NotFoundError extends SliceMachineError {\n\tname = \"NotFoundError\";\n}\nexport class UnexpectedDataError extends SliceMachineError {\n\tname = \"UnexpectedDataError\";\n}\nexport class InternalError extends SliceMachineError {\n\tname = \"InternalError\";\n}\nexport class PluginError extends SliceMachineError {\n\tname = \"PluginError\";\n}\nexport class PluginHookResultError extends SliceMachineError {\n\tname = \"PluginHookResultError\";\n\n\tconstructor(errors: HookError[]) {\n\t\tsuper(\n\t\t\t`${errors.length} error${\n\t\t\t\terrors.length === 1 ? \"\" : \"s\"\n\t\t\t} were returned by one or more plugins.`,\n\t\t\t{\n\t\t\t\tcause: errors,\n\t\t\t},\n\t\t);\n\t}\n}\n\nexport const isSliceMachineError = (\n\terror: unknown,\n): error is SliceMachineError => {\n\t// TODO: Discuss a stronger way to serialize error for the client to detect with r19\n\t// @ts-expect-error We don't want to add \"dom\" to tsconfig \"lib\" because of the TODO\n\tif (typeof window !== \"undefined\") {\n\t\treturn typeof error === \"object\" && error !== null;\n\t} else {\n\t\treturn (\n\t\t\ttypeof error === \"object\" &&\n\t\t\terror !== null &&\n\t\t\t\"_sliceMachineError\" in error\n\t\t);\n\t}\n};\n\nexport const isUnauthorizedError = (\n\terror: unknown,\n): error is UnauthorizedError => {\n\treturn isSliceMachineError(error) && error.name === UnauthorizedError.name;\n};\n\nexport const isUnauthenticatedError = (\n\terror: unknown,\n): error is UnauthenticatedError => {\n\treturn isSliceMachineError(error) && error.name === UnauthenticatedError.name;\n};\n\nexport const isNotFoundError = (error: unknown): error is NotFoundError => {\n\treturn isSliceMachineError(error) && error.name === NotFoundError.name;\n};\n\nexport const isUnexpectedDataError = (\n\terror: unknown,\n): error is UnexpectedDataError => {\n\treturn isSliceMachineError(error) && error.name === UnexpectedDataError.name;\n};\n\nexport const isInternalError = (error: unknown): error is InternalError => {\n\treturn isSliceMachineError(error) && error.name === InternalError.name;\n};\n"],"names":[],"mappings":";;;;;;AAEM,MAAO,0BAA0B,MAAK;AAAA,EAAtC;AAAA;AACL,8CAAqB;A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AoB,KAAK,KAAK,MAAM,SAAS,cAAc;AACnE;"}
|
|
1
|
+
{"version":3,"file":"errors.js","sources":["../../src/errors.ts"],"sourcesContent":["import { HookError } from \"@slicemachine/plugin-kit\";\n\nexport class SliceMachineError extends Error {\n\t_sliceMachineError = true;\n\tname = \"SliceMachineError\";\n}\nexport class UnauthorizedError extends SliceMachineError {\n\tname = \"UnauthorizedError\";\n}\nexport class UnauthenticatedError extends SliceMachineError {\n\tname = \"UnauthenticatedError\";\n\tmessage = \"Authenticate before trying again.\";\n}\nexport class NotFoundError extends SliceMachineError {\n\tname = \"NotFoundError\";\n}\nexport class UnexpectedDataError extends SliceMachineError {\n\tname = \"UnexpectedDataError\";\n}\nexport class InternalError extends SliceMachineError {\n\tname = \"InternalError\";\n}\nexport class PluginError extends SliceMachineError {\n\tname = \"PluginError\";\n}\nexport class PluginHookResultError extends SliceMachineError {\n\tname = \"PluginHookResultError\";\n\n\tconstructor(errors: HookError[]) {\n\t\tsuper(\n\t\t\t`${errors.length} error${\n\t\t\t\terrors.length === 1 ? \"\" : \"s\"\n\t\t\t} were returned by one or more plugins.`,\n\t\t\t{\n\t\t\t\tcause: errors,\n\t\t\t},\n\t\t);\n\t}\n}\n\nexport const isSliceMachineError = (\n\terror: unknown,\n): error is SliceMachineError => {\n\t// TODO: Discuss a stronger way to serialize error for the client to detect with r19\n\t// @ts-expect-error We don't want to add \"dom\" to tsconfig \"lib\" because of the TODO\n\tif (typeof window !== \"undefined\") {\n\t\treturn typeof error === \"object\" && error !== null;\n\t} else {\n\t\treturn (\n\t\t\ttypeof error === \"object\" &&\n\t\t\terror !== null &&\n\t\t\t\"_sliceMachineError\" in error\n\t\t);\n\t}\n};\n\nexport const isUnauthorizedError = (\n\terror: unknown,\n): error is UnauthorizedError => {\n\treturn isSliceMachineError(error) && error.name === UnauthorizedError.name;\n};\n\nexport const isUnauthenticatedError = (\n\terror: unknown,\n): error is UnauthenticatedError => {\n\treturn isSliceMachineError(error) && error.name === UnauthenticatedError.name;\n};\n\nexport const isNotFoundError = (error: unknown): error is NotFoundError => {\n\treturn isSliceMachineError(error) && error.name === NotFoundError.name;\n};\n\nexport const isUnexpectedDataError = (\n\terror: unknown,\n): error is UnexpectedDataError => {\n\treturn isSliceMachineError(error) && error.name === UnexpectedDataError.name;\n};\n\nexport const isInternalError = (error: unknown): error is InternalError => {\n\treturn isSliceMachineError(error) && error.name === InternalError.name;\n};\n\nexport const isPluginError = (error: unknown): error is PluginError => {\n\treturn isSliceMachineError(error) && error.name === PluginError.name;\n};\n"],"names":[],"mappings":";;;;;;AAEM,MAAO,0BAA0B,MAAK;AAAA,EAAtC;AAAA;AACL,8CAAqB;AACrB,gCAAO;AAAA;AACP;AACK,MAAO,0BAA0B,kBAAiB;AAAA,EAAlD;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,6BAA6B,kBAAiB;AAAA,EAArD;AAAA;AACL,gCAAO;AACP,mCAAU;AAAA;AACV;AACK,MAAO,sBAAsB,kBAAiB;AAAA,EAA9C;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,4BAA4B,kBAAiB;AAAA,EAApD;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,sBAAsB,kBAAiB;AAAA,EAA9C;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,oBAAoB,kBAAiB;AAAA,EAA5C;AAAA;AACL,gCAAO;AAAA;AACP;AACK,MAAO,8BAA8B,kBAAiB;AAAA,EAG3D,YAAY,QAAmB;AAC9B,UACC,GAAG,OAAO,eACT,OAAO,WAAW,IAAI,KAAK,6CAE5B;AAAA,MACC,OAAO;AAAA,IAAA,CACP;AATH,gCAAO;AAAA,EAWP;AACA;AAEY,MAAA,sBAAsB,CAClC,UAC+B;AAG3B,MAAA,OAAO,WAAW,aAAa;AAC3B,WAAA,OAAO,UAAU,YAAY,UAAU;AAAA,EAAA,OACxC;AACN,WACC,OAAO,UAAU,YACjB,UAAU,QACV,wBAAwB;AAAA,EAEzB;AACF;AAEa,MAAA,sBAAsB,CAClC,UAC+B;AAC/B,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,kBAAkB;AACvE;AAEa,MAAA,yBAAyB,CACrC,UACkC;AAClC,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,qBAAqB;AAC1E;AAEa,MAAA,kBAAkB,CAAC,UAA0C;AACzE,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,cAAc;AACnE;AAEa,MAAA,wBAAwB,CACpC,UACiC;AACjC,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,oBAAoB;AACzE;AAEa,MAAA,kBAAkB,CAAC,UAA0C;AACzE,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,cAAc;AACnE;AAEa,MAAA,gBAAgB,CAAC,UAAwC;AACrE,SAAO,oBAAoB,KAAK,KAAK,MAAM,SAAS,YAAY;AACjE;"}
|
|
@@ -12,6 +12,7 @@ const REPOSITORY_NAME_VALIDATION = require("../../constants/REPOSITORY_NAME_VALI
|
|
|
12
12
|
const errors = require("../../errors.cjs");
|
|
13
13
|
const BaseManager = require("../BaseManager.cjs");
|
|
14
14
|
const types = require("./types.cjs");
|
|
15
|
+
const sortEnvironments = require("./sortEnvironments.cjs");
|
|
15
16
|
function _interopNamespaceDefault(e) {
|
|
16
17
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
17
18
|
if (e) {
|
|
@@ -314,17 +315,28 @@ class PrismicRepositoryManager extends BaseManager.BaseManager {
|
|
|
314
315
|
}
|
|
315
316
|
async fetchEnvironments() {
|
|
316
317
|
const repositoryName = await this.project.getRepositoryName();
|
|
317
|
-
const url = new URL(
|
|
318
|
-
|
|
318
|
+
const url = new URL(`./environments`, API_ENDPOINTS.API_ENDPOINTS.SliceMachineV1);
|
|
319
|
+
url.searchParams.set("repository", repositoryName);
|
|
320
|
+
const res = await this._fetch({ url });
|
|
319
321
|
if (res.ok) {
|
|
320
322
|
const json = await res.json();
|
|
321
|
-
const { value
|
|
323
|
+
const { value, error } = decode.decode(t__namespace.union([
|
|
324
|
+
t__namespace.type({
|
|
325
|
+
results: t__namespace.array(types.Environment)
|
|
326
|
+
}),
|
|
327
|
+
t__namespace.type({
|
|
328
|
+
error: t__namespace.literal("invalid_token")
|
|
329
|
+
})
|
|
330
|
+
]), json);
|
|
322
331
|
if (error) {
|
|
323
|
-
throw new
|
|
332
|
+
throw new errors.UnexpectedDataError(`Failed to decode environments: ${error.errors.join(", ")}`);
|
|
333
|
+
}
|
|
334
|
+
if ("results" in value) {
|
|
335
|
+
return sortEnvironments.sortEnvironments(value.results);
|
|
324
336
|
}
|
|
325
|
-
return environments;
|
|
326
337
|
}
|
|
327
338
|
switch (res.status) {
|
|
339
|
+
case 400:
|
|
328
340
|
case 401:
|
|
329
341
|
throw new errors.UnauthenticatedError();
|
|
330
342
|
case 403:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRepositoryManager.cjs","sources":["../../../../src/managers/prismicRepository/PrismicRepositoryManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport fetch, { Response } from \"../../lib/fetch\";\nimport { fold } from \"fp-ts/Either\";\n\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decode } from \"../../lib/decode\";\nimport { serializeCookies } from \"../../lib/serializeCookies\";\n\nimport { SLICE_MACHINE_USER_AGENT } from \"../../constants/SLICE_MACHINE_USER_AGENT\";\nimport { API_ENDPOINTS } from \"../../constants/API_ENDPOINTS\";\nimport { REPOSITORY_NAME_VALIDATION } from \"../../constants/REPOSITORY_NAME_VALIDATION\";\n\nimport { UnauthenticatedError, UnauthorizedError } from \"../../errors\";\n\nimport { BaseManager } from \"../BaseManager\";\n\nimport {\n\tAllChangeTypes,\n\tBulkBody,\n\tChangeTypes,\n\tClientError,\n\tLimit,\n\tLimitType,\n\tPrismicRepository,\n\tPrismicRepositoryRole,\n\tPrismicRepositoryUserAgent,\n\tPrismicRepositoryUserAgents,\n\tRawLimit,\n\tTransactionalMergeArgs,\n\tTransactionalMergeReturnType,\n\tFrameworkWroomTelemetryID,\n\tStarterId,\n\tEnvironment,\n} from \"./types\";\n\nconst DEFAULT_REPOSITORY_SETTINGS = {\n\tplan: \"personal\",\n\tisAnnual: \"false\",\n\trole: \"developer\",\n};\n\ntype PrismicRepositoryManagerCheckExistsArgs = {\n\tdomain: string;\n};\n\ntype PrismicRepositoryManagerCreateArgs = {\n\tdomain: string;\n\tframework: FrameworkWroomTelemetryID;\n\tstarterId?: StarterId;\n};\n\ntype PrismicRepositoryManagerDeleteArgs = {\n\tdomain: string;\n\tpassword: string;\n};\n\ntype PrismicRepositoryManagerPushDocumentsArgs = {\n\tdomain: string;\n\tsignature: string;\n\tdocuments: Record<string, unknown>; // TODO: Type unknown if possible(?)\n};\n\nexport class PrismicRepositoryManager extends BaseManager {\n\t// TODO: Add methods for repository-specific actions. E.g. creating a\n\t// new repository.\n\n\tasync readAll(): Promise<PrismicRepository[]> {\n\t\tconst url = new URL(\"./repositories\", API_ENDPOINTS.PrismicUser);\n\t\tconst res = await this._fetch({ url });\n\n\t\tif (res.ok) {\n\t\t\tconst json = await res.json();\n\t\t\tconst { value: repositories, error } = decode(\n\t\t\t\tt.array(PrismicRepository),\n\t\t\t\tjson,\n\t\t\t);\n\n\t\t\tif (error) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to decode repositories: ${error.errors.join(\", \")}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn repositories;\n\t\t} else {\n\t\t\tconst text = await res.text();\n\t\t\tthrow new Error(`Failed to read repositories`, { cause: text });\n\t\t}\n\t}\n\n\t// Should this be in manager? It's one of the few sync method\n\t//\n\t// Reply from Angelo 2022-12-22: I think it should be in manager\n\t// because we shouldn't be exporting root-level utilities from this\n\t// package. If we want to make it more inline with the other methods,\n\t// we could simplify the API by changing its signature to the\n\t// following:\n\t//\n\t// ```ts\n\t// (repositoryName: string) => Promise<boolean>\n\t// ```\n\t//\n\t// This method would:\n\t//\n\t// 1. Fetch the list of repositories for the user using `readAll()`.\n\t// The list would be cached.\n\t// 2. Determine if the user has write access to the given repository.\n\t//\n\t// This version has the following benefits:\n\t//\n\t// - Does not expect the consumer to supply a repository object; it\n\t// only requires a repository name, which could be sourced from\n\t// anything (incl. the project's `slicemachine.config.json`).\n\t//\n\t// - Similarly, it does not expect the consumer to call `readAll()`\n\t// before calling this method.\n\t//\n\t// - Works for repositories that the user does not have access to. For\n\t// example, I could use it to check if I have access to \"qwerty\",\n\t// even if I am not added as a user. The purpose of the method is\n\t// still valid: do I have write access to a given repository?\n\thasWriteAccess(repository: PrismicRepository): boolean {\n\t\tswitch (repository.role) {\n\t\t\tcase PrismicRepositoryRole.SuperUser:\n\t\t\tcase PrismicRepositoryRole.Owner:\n\t\t\tcase PrismicRepositoryRole.Administrator:\n\t\t\t\treturn true;\n\n\t\t\tdefault:\n\t\t\t\treturn false;\n\t\t}\n\t}\n\n\tasync checkExists(\n\t\targs: PrismicRepositoryManagerCheckExistsArgs,\n\t): Promise<boolean> {\n\t\tconst url = new URL(\n\t\t\t`./app/dashboard/repositories/${args.domain}/exists`,\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tskipAuthentication: true,\n\t\t});\n\n\t\tconst text = await res.text();\n\n\t\tif (res.ok) {\n\t\t\treturn text === \"false\"; // Endpoint returns `false` when repository exists\n\t\t} else {\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to check repository existence for domain \\`${args.domain}\\``,\n\t\t\t\t{ cause: text },\n\t\t\t);\n\t\t}\n\t}\n\n\tasync create(args: PrismicRepositoryManagerCreateArgs): Promise<void> {\n\t\tconst url = new URL(\n\t\t\t\"./authentication/newrepository?app=slicemachine\",\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\n\t\tconst body = {\n\t\t\t...DEFAULT_REPOSITORY_SETTINGS,\n\t\t\tdomain: args.domain,\n\t\t\t// These properties are optional in the API but needed for tracking\n\t\t\tframework: args.framework,\n\t\t\tstarterId: args.starterId,\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.SliceMachine, // Custom User Agent is required\n\t\t});\n\t\tconst text = await res.text();\n\n\t\t// Endpoint returns repository name on success, that should be within the validation range\n\t\t// Even when there is an error, we get a 200 success and so we have to check the name thanks to that\n\t\tif (\n\t\t\t!res.ok ||\n\t\t\ttext.length < REPOSITORY_NAME_VALIDATION.Min ||\n\t\t\ttext.length > REPOSITORY_NAME_VALIDATION.Max\n\t\t) {\n\t\t\tthrow new Error(`Failed to create repository \\`${args.domain}\\``, {\n\t\t\t\tcause: text,\n\t\t\t});\n\t\t}\n\t}\n\n\t// TODO: Delete this endpoint? It doesn't seem to be used (I might be wrong). - Angelo\n\tasync delete(args: PrismicRepositoryManagerDeleteArgs): Promise<void> {\n\t\tconst cookies = await this.user.getAuthenticationCookies();\n\n\t\tconst url = new URL(\n\t\t\t`./app/settings/delete?_=${cookies[\"X_XSRF\"]}`, // TODO: Maybe we want to throw early if the token is no available, or get the token another way\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\t\t// Update hostname to include repository domain\n\t\turl.hostname = `${args.domain}.${url.hostname}`;\n\n\t\tconst body = {\n\t\t\tconfirm: args.domain,\n\t\t\tpassword: args.password,\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.LegacyZero, // Custom User Agent is required\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new Error(`Failed to delete repository \\`${args.domain}\\``, {\n\t\t\t\tcause: res,\n\t\t\t});\n\t\t}\n\t}\n\n\tasync pushDocuments(\n\t\targs: PrismicRepositoryManagerPushDocumentsArgs,\n\t): Promise<void> {\n\t\tconst url = new URL(\"./starter/documents\", API_ENDPOINTS.PrismicWroom);\n\t\t// Update hostname to include repository domain\n\t\turl.hostname = `${args.domain}.${url.hostname}`;\n\n\t\tconst body = {\n\t\t\tsignature: args.signature,\n\t\t\tdocuments: JSON.stringify(args.documents),\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.LegacyZero, // Custom User Agent is required\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tlet reason: string | null = null;\n\t\t\ttry {\n\t\t\t\treason = await res.text();\n\t\t\t} catch {\n\t\t\t\t// Noop\n\t\t\t}\n\n\t\t\t// Ideally the API should throw a 409 or something like that...\n\t\t\tif (reason === \"Repository should not contain documents\") {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to push documents to repository \\`${args.domain}\\`, repository is not empty`,\n\t\t\t\t\t{\n\t\t\t\t\t\tcause: reason,\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to push documents to repository \\`${args.domain}\\`, ${res.status} ${res.statusText}`,\n\t\t\t\t{\n\t\t\t\t\tcause: reason,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t}\n\n\tasync pushChanges(\n\t\targs: TransactionalMergeArgs,\n\t): Promise<TransactionalMergeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tif (!(await this.user.checkIsLoggedIn())) {\n\t\t\tthrow new UnauthenticatedError();\n\t\t}\n\n\t\ttry {\n\t\t\t// Update the AWS ACL before uploading screenshots as it might have expired\n\t\t\tawait this.screenshots.initS3ACL();\n\n\t\t\tconst allChanges: AllChangeTypes[] = await Promise.all(\n\t\t\t\targs.changes.map(async (change) => {\n\t\t\t\t\tif (change.type === \"Slice\") {\n\t\t\t\t\t\tswitch (change.status) {\n\t\t\t\t\t\t\tcase \"NEW\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.slices.readSlice({\n\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst modelWithScreenshots =\n\t\t\t\t\t\t\t\t\tawait this.slices.updateSliceModelScreenshotsInPlace({\n\t\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: modelWithScreenshots,\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_INSERT,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"MODIFIED\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.slices.readSlice({\n\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst modelWithScreenshots =\n\t\t\t\t\t\t\t\t\tawait this.slices.updateSliceModelScreenshotsInPlace({\n\t\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: modelWithScreenshots,\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_UPDATE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"DELETED\":\n\t\t\t\t\t\t\t\tawait this.screenshots.deleteScreenshotFolder({\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: { id: change.id },\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_DELETE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tswitch (change.status) {\n\t\t\t\t\t\t\tcase \"NEW\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.customTypes.readCustomType({\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_INSERT,\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: model,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"MODIFIED\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.customTypes.readCustomType({\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_UPDATE,\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: model,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"DELETED\":\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: { id: change.id },\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_DELETE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\t// Compute the POST body\n\t\t\tconst requestBody: BulkBody = {\n\t\t\t\tconfirmDeleteDocuments: args.confirmDeleteDocuments,\n\t\t\t\tchanges: allChanges,\n\t\t\t};\n\n\t\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\t\t// TODO: move to customtypes client\n\t\t\tconst response = await this._fetch({\n\t\t\t\turl: new URL(\"./bulk\", API_ENDPOINTS.PrismicModels),\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: requestBody,\n\t\t\t\trepository: repositoryName,\n\t\t\t});\n\n\t\t\tswitch (response.status) {\n\t\t\t\tcase 202:\n\t\t\t\t\treturn this._decodeLimitOrThrow(\n\t\t\t\t\t\tawait response.json(),\n\t\t\t\t\t\tresponse.status,\n\t\t\t\t\t\tLimitType.SOFT,\n\t\t\t\t\t);\n\t\t\t\tcase 204:\n\t\t\t\t\treturn null;\n\t\t\t\tcase 401:\n\t\t\t\t\tthrow new UnauthenticatedError();\n\t\t\t\tcase 403:\n\t\t\t\t\treturn this._decodeLimitOrThrow(\n\t\t\t\t\t\tawait response.json(),\n\t\t\t\t\t\tresponse.status,\n\t\t\t\t\t\tLimitType.HARD,\n\t\t\t\t\t);\n\t\t\t\tcase 400:\n\t\t\t\t\tconst text = await response.text();\n\t\t\t\t\tthrow new Error(text);\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unexpected status code ${response.status}`, {\n\t\t\t\t\t\tcause: await response.text(),\n\t\t\t\t\t});\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tconsole.error(\"An error happened while pushing your changes\");\n\t\t\tconsole.error(err);\n\n\t\t\tthrow err;\n\t\t}\n\t}\n\n\tasync fetchEnvironments(): Promise<Environment[]> {\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\n\t\tconst url = new URL(\"./environments\", API_ENDPOINTS.SliceMachine);\n\t\tconst res = await this._fetch({ url, repository: repositoryName });\n\n\t\tif (res.ok) {\n\t\t\tconst json = await res.json();\n\n\t\t\tconst { value: environments, error } = decode(t.array(Environment), json);\n\n\t\t\tif (error) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to decode environments: ${error.errors.join(\", \")}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn environments;\n\t\t}\n\n\t\tswitch (res.status) {\n\t\t\tcase 401:\n\t\t\t\tthrow new UnauthenticatedError();\n\t\t\tcase 403:\n\t\t\t\tthrow new UnauthorizedError();\n\t\t\tdefault:\n\t\t\t\tthrow new Error(\"Failed to fetch environments.\");\n\t\t}\n\t}\n\n\tprivate _decodeLimitOrThrow(\n\t\tpotentialLimit: unknown,\n\t\tstatusCode: number,\n\t\tlimitType: LimitType,\n\t): Limit | null {\n\t\treturn fold<t.Errors, RawLimit, Limit | null>(\n\t\t\t() => {\n\t\t\t\tconst error: ClientError = {\n\t\t\t\t\tstatus: statusCode,\n\t\t\t\t\tmessage: `Unable to parse raw limit from ${JSON.stringify(\n\t\t\t\t\t\tpotentialLimit,\n\t\t\t\t\t)}`,\n\t\t\t\t};\n\t\t\t\tthrow error;\n\t\t\t},\n\t\t\t(rawLimit: RawLimit) => {\n\t\t\t\tconst limit = { ...rawLimit, type: limitType };\n\n\t\t\t\treturn limit;\n\t\t\t},\n\t\t)(RawLimit.decode(potentialLimit));\n\t}\n\n\tprivate async _fetch(args: {\n\t\turl: URL;\n\t\tmethod?: \"GET\" | \"POST\";\n\t\tbody?: unknown;\n\t\tuserAgent?: PrismicRepositoryUserAgents;\n\t\trepository?: string;\n\t\tskipAuthentication?: boolean;\n\t}): Promise<Response> {\n\t\tlet cookies;\n\t\ttry {\n\t\t\tcookies = await this.user.getAuthenticationCookies();\n\t\t} catch (e) {\n\t\t\tif (!args.skipAuthentication) {\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t}\n\n\t\tconst extraHeaders: Record<string, string> = {};\n\n\t\tif (args.body) {\n\t\t\textraHeaders[\"Content-Type\"] = \"application/json\";\n\t\t}\n\n\t\tif (args.repository) {\n\t\t\textraHeaders.repository = args.repository;\n\t\t}\n\n\t\treturn await fetch(args.url.toString(), {\n\t\t\tmethod: args.method,\n\t\t\tbody: args.body ? JSON.stringify(args.body) : undefined,\n\t\t\theaders: {\n\t\t\t\t// Some endpoints rely on the authorization header...\n\n\t\t\t\t...(cookies !== undefined\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tAuthorization: `Bearer ${cookies[\"prismic-auth\"]}`,\n\t\t\t\t\t\t\tCookie: serializeCookies(cookies),\n\t\t\t\t\t }\n\t\t\t\t\t: {}),\n\t\t\t\t\"User-Agent\": args.userAgent || SLICE_MACHINE_USER_AGENT,\n\t\t\t\t...extraHeaders,\n\t\t\t},\n\t\t});\n\t}\n}\n"],"names":["BaseManager","API_ENDPOINTS","decode","t","PrismicRepository","PrismicRepositoryRole","PrismicRepositoryUserAgent","REPOSITORY_NAME_VALIDATION","assertPluginsInitialized","UnauthenticatedError","ChangeTypes","LimitType","Environment","UnauthorizedError","fold","RawLimit","fetch","serializeCookies","SLICE_MACHINE_USER_AGENT"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,MAAM,8BAA8B;AAAA,EACnC,MAAM;AAAA,EACN,UAAU;AAAA,EACV,MAAM;;AAwBD,MAAO,iCAAiCA,YAAAA,YAAW;AAAA;AAAA;AAAA,EAIxD,MAAM,UAAO;AACZ,UAAM,MAAM,IAAI,IAAI,kBAAkBC,4BAAc,WAAW;AAC/D,UAAM,MAAM,MAAM,KAAK,OAAO,EAAE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IAC/B;AAED,WAAO,MAAMC,MAAAA,QAAM,KAAK,IAAI,YAAY;AAAA,MACvC,QAAQ,KAAK;AAAA,MACb,MAAM,KAAK,OAAO,KAAK,UAAU,KAAK,IAAI,IAAI;AAAA,MAC9C,SAAS;AAAA;AAAA,QAGR,GAAI,YAAY,SACb;AAAA,UACA,eAAe,UAAU,QAAQ,cAAc;AAAA,UAC/C,QAAQC,kCAAiB,OAAO;AAAA,QAAA,IAEhC;QACH,cAAc,KAAK,aAAaC,yBAAA;AAAA,QAChC,GAAG;AAAA,MACH;AAAA,IAAA,CACD;AAAA,EACF;AACA;;"}
|
|
1
|
+
{"version":3,"file":"PrismicRepositoryManager.cjs","sources":["../../../../src/managers/prismicRepository/PrismicRepositoryManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport fetch, { Response } from \"../../lib/fetch\";\nimport { fold } from \"fp-ts/Either\";\n\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decode } from \"../../lib/decode\";\nimport { serializeCookies } from \"../../lib/serializeCookies\";\n\nimport { SLICE_MACHINE_USER_AGENT } from \"../../constants/SLICE_MACHINE_USER_AGENT\";\nimport { API_ENDPOINTS } from \"../../constants/API_ENDPOINTS\";\nimport { REPOSITORY_NAME_VALIDATION } from \"../../constants/REPOSITORY_NAME_VALIDATION\";\n\nimport {\n\tUnauthenticatedError,\n\tUnauthorizedError,\n\tUnexpectedDataError,\n} from \"../../errors\";\n\nimport { BaseManager } from \"../BaseManager\";\n\nimport {\n\tAllChangeTypes,\n\tBulkBody,\n\tChangeTypes,\n\tClientError,\n\tLimit,\n\tLimitType,\n\tPrismicRepository,\n\tPrismicRepositoryRole,\n\tPrismicRepositoryUserAgent,\n\tPrismicRepositoryUserAgents,\n\tRawLimit,\n\tTransactionalMergeArgs,\n\tTransactionalMergeReturnType,\n\tFrameworkWroomTelemetryID,\n\tStarterId,\n\tEnvironment,\n} from \"./types\";\nimport { sortEnvironments } from \"./sortEnvironments\";\n\nconst DEFAULT_REPOSITORY_SETTINGS = {\n\tplan: \"personal\",\n\tisAnnual: \"false\",\n\trole: \"developer\",\n};\n\ntype PrismicRepositoryManagerCheckExistsArgs = {\n\tdomain: string;\n};\n\ntype PrismicRepositoryManagerCreateArgs = {\n\tdomain: string;\n\tframework: FrameworkWroomTelemetryID;\n\tstarterId?: StarterId;\n};\n\ntype PrismicRepositoryManagerDeleteArgs = {\n\tdomain: string;\n\tpassword: string;\n};\n\ntype PrismicRepositoryManagerPushDocumentsArgs = {\n\tdomain: string;\n\tsignature: string;\n\tdocuments: Record<string, unknown>; // TODO: Type unknown if possible(?)\n};\n\nexport class PrismicRepositoryManager extends BaseManager {\n\t// TODO: Add methods for repository-specific actions. E.g. creating a\n\t// new repository.\n\n\tasync readAll(): Promise<PrismicRepository[]> {\n\t\tconst url = new URL(\"./repositories\", API_ENDPOINTS.PrismicUser);\n\t\tconst res = await this._fetch({ url });\n\n\t\tif (res.ok) {\n\t\t\tconst json = await res.json();\n\t\t\tconst { value: repositories, error } = decode(\n\t\t\t\tt.array(PrismicRepository),\n\t\t\t\tjson,\n\t\t\t);\n\n\t\t\tif (error) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to decode repositories: ${error.errors.join(\", \")}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn repositories;\n\t\t} else {\n\t\t\tconst text = await res.text();\n\t\t\tthrow new Error(`Failed to read repositories`, { cause: text });\n\t\t}\n\t}\n\n\t// Should this be in manager? It's one of the few sync method\n\t//\n\t// Reply from Angelo 2022-12-22: I think it should be in manager\n\t// because we shouldn't be exporting root-level utilities from this\n\t// package. If we want to make it more inline with the other methods,\n\t// we could simplify the API by changing its signature to the\n\t// following:\n\t//\n\t// ```ts\n\t// (repositoryName: string) => Promise<boolean>\n\t// ```\n\t//\n\t// This method would:\n\t//\n\t// 1. Fetch the list of repositories for the user using `readAll()`.\n\t// The list would be cached.\n\t// 2. Determine if the user has write access to the given repository.\n\t//\n\t// This version has the following benefits:\n\t//\n\t// - Does not expect the consumer to supply a repository object; it\n\t// only requires a repository name, which could be sourced from\n\t// anything (incl. the project's `slicemachine.config.json`).\n\t//\n\t// - Similarly, it does not expect the consumer to call `readAll()`\n\t// before calling this method.\n\t//\n\t// - Works for repositories that the user does not have access to. For\n\t// example, I could use it to check if I have access to \"qwerty\",\n\t// even if I am not added as a user. The purpose of the method is\n\t// still valid: do I have write access to a given repository?\n\thasWriteAccess(repository: PrismicRepository): boolean {\n\t\tswitch (repository.role) {\n\t\t\tcase PrismicRepositoryRole.SuperUser:\n\t\t\tcase PrismicRepositoryRole.Owner:\n\t\t\tcase PrismicRepositoryRole.Administrator:\n\t\t\t\treturn true;\n\n\t\t\tdefault:\n\t\t\t\treturn false;\n\t\t}\n\t}\n\n\tasync checkExists(\n\t\targs: PrismicRepositoryManagerCheckExistsArgs,\n\t): Promise<boolean> {\n\t\tconst url = new URL(\n\t\t\t`./app/dashboard/repositories/${args.domain}/exists`,\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tskipAuthentication: true,\n\t\t});\n\n\t\tconst text = await res.text();\n\n\t\tif (res.ok) {\n\t\t\treturn text === \"false\"; // Endpoint returns `false` when repository exists\n\t\t} else {\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to check repository existence for domain \\`${args.domain}\\``,\n\t\t\t\t{ cause: text },\n\t\t\t);\n\t\t}\n\t}\n\n\tasync create(args: PrismicRepositoryManagerCreateArgs): Promise<void> {\n\t\tconst url = new URL(\n\t\t\t\"./authentication/newrepository?app=slicemachine\",\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\n\t\tconst body = {\n\t\t\t...DEFAULT_REPOSITORY_SETTINGS,\n\t\t\tdomain: args.domain,\n\t\t\t// These properties are optional in the API but needed for tracking\n\t\t\tframework: args.framework,\n\t\t\tstarterId: args.starterId,\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.SliceMachine, // Custom User Agent is required\n\t\t});\n\t\tconst text = await res.text();\n\n\t\t// Endpoint returns repository name on success, that should be within the validation range\n\t\t// Even when there is an error, we get a 200 success and so we have to check the name thanks to that\n\t\tif (\n\t\t\t!res.ok ||\n\t\t\ttext.length < REPOSITORY_NAME_VALIDATION.Min ||\n\t\t\ttext.length > REPOSITORY_NAME_VALIDATION.Max\n\t\t) {\n\t\t\tthrow new Error(`Failed to create repository \\`${args.domain}\\``, {\n\t\t\t\tcause: text,\n\t\t\t});\n\t\t}\n\t}\n\n\t// TODO: Delete this endpoint? It doesn't seem to be used (I might be wrong). - Angelo\n\tasync delete(args: PrismicRepositoryManagerDeleteArgs): Promise<void> {\n\t\tconst cookies = await this.user.getAuthenticationCookies();\n\n\t\tconst url = new URL(\n\t\t\t`./app/settings/delete?_=${cookies[\"X_XSRF\"]}`, // TODO: Maybe we want to throw early if the token is no available, or get the token another way\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\t\t// Update hostname to include repository domain\n\t\turl.hostname = `${args.domain}.${url.hostname}`;\n\n\t\tconst body = {\n\t\t\tconfirm: args.domain,\n\t\t\tpassword: args.password,\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.LegacyZero, // Custom User Agent is required\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new Error(`Failed to delete repository \\`${args.domain}\\``, {\n\t\t\t\tcause: res,\n\t\t\t});\n\t\t}\n\t}\n\n\tasync pushDocuments(\n\t\targs: PrismicRepositoryManagerPushDocumentsArgs,\n\t): Promise<void> {\n\t\tconst url = new URL(\"./starter/documents\", API_ENDPOINTS.PrismicWroom);\n\t\t// Update hostname to include repository domain\n\t\turl.hostname = `${args.domain}.${url.hostname}`;\n\n\t\tconst body = {\n\t\t\tsignature: args.signature,\n\t\t\tdocuments: JSON.stringify(args.documents),\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.LegacyZero, // Custom User Agent is required\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tlet reason: string | null = null;\n\t\t\ttry {\n\t\t\t\treason = await res.text();\n\t\t\t} catch {\n\t\t\t\t// Noop\n\t\t\t}\n\n\t\t\t// Ideally the API should throw a 409 or something like that...\n\t\t\tif (reason === \"Repository should not contain documents\") {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to push documents to repository \\`${args.domain}\\`, repository is not empty`,\n\t\t\t\t\t{\n\t\t\t\t\t\tcause: reason,\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to push documents to repository \\`${args.domain}\\`, ${res.status} ${res.statusText}`,\n\t\t\t\t{\n\t\t\t\t\tcause: reason,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t}\n\n\tasync pushChanges(\n\t\targs: TransactionalMergeArgs,\n\t): Promise<TransactionalMergeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tif (!(await this.user.checkIsLoggedIn())) {\n\t\t\tthrow new UnauthenticatedError();\n\t\t}\n\n\t\ttry {\n\t\t\t// Update the AWS ACL before uploading screenshots as it might have expired\n\t\t\tawait this.screenshots.initS3ACL();\n\n\t\t\tconst allChanges: AllChangeTypes[] = await Promise.all(\n\t\t\t\targs.changes.map(async (change) => {\n\t\t\t\t\tif (change.type === \"Slice\") {\n\t\t\t\t\t\tswitch (change.status) {\n\t\t\t\t\t\t\tcase \"NEW\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.slices.readSlice({\n\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst modelWithScreenshots =\n\t\t\t\t\t\t\t\t\tawait this.slices.updateSliceModelScreenshotsInPlace({\n\t\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: modelWithScreenshots,\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_INSERT,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"MODIFIED\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.slices.readSlice({\n\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst modelWithScreenshots =\n\t\t\t\t\t\t\t\t\tawait this.slices.updateSliceModelScreenshotsInPlace({\n\t\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: modelWithScreenshots,\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_UPDATE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"DELETED\":\n\t\t\t\t\t\t\t\tawait this.screenshots.deleteScreenshotFolder({\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: { id: change.id },\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_DELETE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tswitch (change.status) {\n\t\t\t\t\t\t\tcase \"NEW\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.customTypes.readCustomType({\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_INSERT,\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: model,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"MODIFIED\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.customTypes.readCustomType({\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_UPDATE,\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: model,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"DELETED\":\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: { id: change.id },\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_DELETE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\t// Compute the POST body\n\t\t\tconst requestBody: BulkBody = {\n\t\t\t\tconfirmDeleteDocuments: args.confirmDeleteDocuments,\n\t\t\t\tchanges: allChanges,\n\t\t\t};\n\n\t\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\t\t// TODO: move to customtypes client\n\t\t\tconst response = await this._fetch({\n\t\t\t\turl: new URL(\"./bulk\", API_ENDPOINTS.PrismicModels),\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: requestBody,\n\t\t\t\trepository: repositoryName,\n\t\t\t});\n\n\t\t\tswitch (response.status) {\n\t\t\t\tcase 202:\n\t\t\t\t\treturn this._decodeLimitOrThrow(\n\t\t\t\t\t\tawait response.json(),\n\t\t\t\t\t\tresponse.status,\n\t\t\t\t\t\tLimitType.SOFT,\n\t\t\t\t\t);\n\t\t\t\tcase 204:\n\t\t\t\t\treturn null;\n\t\t\t\tcase 401:\n\t\t\t\t\tthrow new UnauthenticatedError();\n\t\t\t\tcase 403:\n\t\t\t\t\treturn this._decodeLimitOrThrow(\n\t\t\t\t\t\tawait response.json(),\n\t\t\t\t\t\tresponse.status,\n\t\t\t\t\t\tLimitType.HARD,\n\t\t\t\t\t);\n\t\t\t\tcase 400:\n\t\t\t\t\tconst text = await response.text();\n\t\t\t\t\tthrow new Error(text);\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unexpected status code ${response.status}`, {\n\t\t\t\t\t\tcause: await response.text(),\n\t\t\t\t\t});\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tconsole.error(\"An error happened while pushing your changes\");\n\t\t\tconsole.error(err);\n\n\t\t\tthrow err;\n\t\t}\n\t}\n\n\tasync fetchEnvironments(): Promise<Environment[]> {\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\n\t\tconst url = new URL(`./environments`, API_ENDPOINTS.SliceMachineV1);\n\t\turl.searchParams.set(\"repository\", repositoryName);\n\t\tconst res = await this._fetch({ url });\n\n\t\tif (res.ok) {\n\t\t\tconst json = await res.json();\n\n\t\t\tconst { value, error } = decode(\n\t\t\t\tt.union([\n\t\t\t\t\tt.type({\n\t\t\t\t\t\tresults: t.array(Environment),\n\t\t\t\t\t}),\n\t\t\t\t\tt.type({\n\t\t\t\t\t\terror: t.literal(\"invalid_token\"),\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\t\tjson,\n\t\t\t);\n\n\t\t\tif (error) {\n\t\t\t\tthrow new UnexpectedDataError(\n\t\t\t\t\t`Failed to decode environments: ${error.errors.join(\", \")}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (\"results\" in value) {\n\t\t\t\treturn sortEnvironments(value.results);\n\t\t\t}\n\t\t}\n\n\t\tswitch (res.status) {\n\t\t\tcase 400:\n\t\t\tcase 401:\n\t\t\t\tthrow new UnauthenticatedError();\n\t\t\tcase 403:\n\t\t\t\tthrow new UnauthorizedError();\n\t\t\tdefault:\n\t\t\t\tthrow new Error(\"Failed to fetch environments.\");\n\t\t}\n\t}\n\n\tprivate _decodeLimitOrThrow(\n\t\tpotentialLimit: unknown,\n\t\tstatusCode: number,\n\t\tlimitType: LimitType,\n\t): Limit | null {\n\t\treturn fold<t.Errors, RawLimit, Limit | null>(\n\t\t\t() => {\n\t\t\t\tconst error: ClientError = {\n\t\t\t\t\tstatus: statusCode,\n\t\t\t\t\tmessage: `Unable to parse raw limit from ${JSON.stringify(\n\t\t\t\t\t\tpotentialLimit,\n\t\t\t\t\t)}`,\n\t\t\t\t};\n\t\t\t\tthrow error;\n\t\t\t},\n\t\t\t(rawLimit: RawLimit) => {\n\t\t\t\tconst limit = { ...rawLimit, type: limitType };\n\n\t\t\t\treturn limit;\n\t\t\t},\n\t\t)(RawLimit.decode(potentialLimit));\n\t}\n\n\tprivate async _fetch(args: {\n\t\turl: URL;\n\t\tmethod?: \"GET\" | \"POST\";\n\t\tbody?: unknown;\n\t\tuserAgent?: PrismicRepositoryUserAgents;\n\t\trepository?: string;\n\t\tskipAuthentication?: boolean;\n\t}): Promise<Response> {\n\t\tlet cookies;\n\t\ttry {\n\t\t\tcookies = await this.user.getAuthenticationCookies();\n\t\t} catch (e) {\n\t\t\tif (!args.skipAuthentication) {\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t}\n\n\t\tconst extraHeaders: Record<string, string> = {};\n\n\t\tif (args.body) {\n\t\t\textraHeaders[\"Content-Type\"] = \"application/json\";\n\t\t}\n\n\t\tif (args.repository) {\n\t\t\textraHeaders.repository = args.repository;\n\t\t}\n\n\t\treturn await fetch(args.url.toString(), {\n\t\t\tmethod: args.method,\n\t\t\tbody: args.body ? JSON.stringify(args.body) : undefined,\n\t\t\theaders: {\n\t\t\t\t// Some endpoints rely on the authorization header...\n\n\t\t\t\t...(cookies !== undefined\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tAuthorization: `Bearer ${cookies[\"prismic-auth\"]}`,\n\t\t\t\t\t\t\tCookie: serializeCookies(cookies),\n\t\t\t\t\t }\n\t\t\t\t\t: {}),\n\t\t\t\t\"User-Agent\": args.userAgent || SLICE_MACHINE_USER_AGENT,\n\t\t\t\t...extraHeaders,\n\t\t\t},\n\t\t});\n\t}\n}\n"],"names":["BaseManager","API_ENDPOINTS","decode","t","PrismicRepository","PrismicRepositoryRole","PrismicRepositoryUserAgent","REPOSITORY_NAME_VALIDATION","assertPluginsInitialized","UnauthenticatedError","ChangeTypes","LimitType","Environment","UnexpectedDataError","sortEnvironments","UnauthorizedError","fold","RawLimit","fetch","serializeCookies","SLICE_MACHINE_USER_AGENT"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,MAAM,8BAA8B;AAAA,EACnC,MAAM;AAAA,EACN,UAAU;A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MAAA;AAGV,YAAA;AAAA,IACP,GACA,CAAC,aAAsB;AACtB,YAAM,QAAQ,EAAE,GAAG,UAAU,MAAM,UAAS;AAErC,aAAA;AAAA,IACP,CAAA,EACAC,MAAS,SAAA,OAAO,cAAc,CAAC;AAAA,EAClC;AAAA,EAEQ,MAAM,OAAO,MAOpB;AACI,QAAA;AACA,QAAA;AACO,gBAAA,MAAM,KAAK,KAAK;aAClB;AACJ,UAAA,CAAC,KAAK,oBAAoB;AACvB,cAAA;AAAA,MACN;AAAA,IACD;AAED,UAAM,eAAuC,CAAA;AAE7C,QAAI,KAAK,MAAM;AACd,mBAAa,cAAc,IAAI;AAAA,IAC/B;AAED,QAAI,KAAK,YAAY;AACpB,mBAAa,aAAa,KAAK;AAAA,IAC/B;AAED,WAAO,MAAMC,MAAAA,QAAM,KAAK,IAAI,YAAY;AAAA,MACvC,QAAQ,KAAK;AAAA,MACb,MAAM,KAAK,OAAO,KAAK,UAAU,KAAK,IAAI,IAAI;AAAA,MAC9C,SAAS;AAAA;AAAA,QAGR,GAAI,YAAY,SACb;AAAA,UACA,eAAe,UAAU,QAAQ,cAAc;AAAA,UAC/C,QAAQC,kCAAiB,OAAO;AAAA,QAAA,IAEhC;QACH,cAAc,KAAK,aAAaC,yBAAA;AAAA,QAChC,GAAG;AAAA,MACH;AAAA,IAAA,CACD;AAAA,EACF;AACA;;"}
|
|
@@ -7,9 +7,10 @@ import { serializeCookies } from "../../lib/serializeCookies.js";
|
|
|
7
7
|
import { SLICE_MACHINE_USER_AGENT } from "../../constants/SLICE_MACHINE_USER_AGENT.js";
|
|
8
8
|
import { API_ENDPOINTS } from "../../constants/API_ENDPOINTS.js";
|
|
9
9
|
import { REPOSITORY_NAME_VALIDATION } from "../../constants/REPOSITORY_NAME_VALIDATION.js";
|
|
10
|
-
import { UnauthenticatedError, UnauthorizedError } from "../../errors.js";
|
|
10
|
+
import { UnauthenticatedError, UnexpectedDataError, UnauthorizedError } from "../../errors.js";
|
|
11
11
|
import { BaseManager } from "../BaseManager.js";
|
|
12
12
|
import { PrismicRepository, PrismicRepositoryRole, PrismicRepositoryUserAgent, ChangeTypes, LimitType, Environment, RawLimit } from "./types.js";
|
|
13
|
+
import { sortEnvironments } from "./sortEnvironments.js";
|
|
13
14
|
const DEFAULT_REPOSITORY_SETTINGS = {
|
|
14
15
|
plan: "personal",
|
|
15
16
|
isAnnual: "false",
|
|
@@ -295,17 +296,28 @@ class PrismicRepositoryManager extends BaseManager {
|
|
|
295
296
|
}
|
|
296
297
|
async fetchEnvironments() {
|
|
297
298
|
const repositoryName = await this.project.getRepositoryName();
|
|
298
|
-
const url = new URL(
|
|
299
|
-
|
|
299
|
+
const url = new URL(`./environments`, API_ENDPOINTS.SliceMachineV1);
|
|
300
|
+
url.searchParams.set("repository", repositoryName);
|
|
301
|
+
const res = await this._fetch({ url });
|
|
300
302
|
if (res.ok) {
|
|
301
303
|
const json = await res.json();
|
|
302
|
-
const { value
|
|
304
|
+
const { value, error } = decode(t.union([
|
|
305
|
+
t.type({
|
|
306
|
+
results: t.array(Environment)
|
|
307
|
+
}),
|
|
308
|
+
t.type({
|
|
309
|
+
error: t.literal("invalid_token")
|
|
310
|
+
})
|
|
311
|
+
]), json);
|
|
303
312
|
if (error) {
|
|
304
|
-
throw new
|
|
313
|
+
throw new UnexpectedDataError(`Failed to decode environments: ${error.errors.join(", ")}`);
|
|
314
|
+
}
|
|
315
|
+
if ("results" in value) {
|
|
316
|
+
return sortEnvironments(value.results);
|
|
305
317
|
}
|
|
306
|
-
return environments;
|
|
307
318
|
}
|
|
308
319
|
switch (res.status) {
|
|
320
|
+
case 400:
|
|
309
321
|
case 401:
|
|
310
322
|
throw new UnauthenticatedError();
|
|
311
323
|
case 403:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRepositoryManager.js","sources":["../../../../src/managers/prismicRepository/PrismicRepositoryManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport fetch, { Response } from \"../../lib/fetch\";\nimport { fold } from \"fp-ts/Either\";\n\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decode } from \"../../lib/decode\";\nimport { serializeCookies } from \"../../lib/serializeCookies\";\n\nimport { SLICE_MACHINE_USER_AGENT } from \"../../constants/SLICE_MACHINE_USER_AGENT\";\nimport { API_ENDPOINTS } from \"../../constants/API_ENDPOINTS\";\nimport { REPOSITORY_NAME_VALIDATION } from \"../../constants/REPOSITORY_NAME_VALIDATION\";\n\nimport { UnauthenticatedError, UnauthorizedError } from \"../../errors\";\n\nimport { BaseManager } from \"../BaseManager\";\n\nimport {\n\tAllChangeTypes,\n\tBulkBody,\n\tChangeTypes,\n\tClientError,\n\tLimit,\n\tLimitType,\n\tPrismicRepository,\n\tPrismicRepositoryRole,\n\tPrismicRepositoryUserAgent,\n\tPrismicRepositoryUserAgents,\n\tRawLimit,\n\tTransactionalMergeArgs,\n\tTransactionalMergeReturnType,\n\tFrameworkWroomTelemetryID,\n\tStarterId,\n\tEnvironment,\n} from \"./types\";\n\nconst DEFAULT_REPOSITORY_SETTINGS = {\n\tplan: \"personal\",\n\tisAnnual: \"false\",\n\trole: \"developer\",\n};\n\ntype PrismicRepositoryManagerCheckExistsArgs = {\n\tdomain: string;\n};\n\ntype PrismicRepositoryManagerCreateArgs = {\n\tdomain: string;\n\tframework: FrameworkWroomTelemetryID;\n\tstarterId?: StarterId;\n};\n\ntype PrismicRepositoryManagerDeleteArgs = {\n\tdomain: string;\n\tpassword: string;\n};\n\ntype PrismicRepositoryManagerPushDocumentsArgs = {\n\tdomain: string;\n\tsignature: string;\n\tdocuments: Record<string, unknown>; // TODO: Type unknown if possible(?)\n};\n\nexport class PrismicRepositoryManager extends BaseManager {\n\t// TODO: Add methods for repository-specific actions. E.g. creating a\n\t// new repository.\n\n\tasync readAll(): Promise<PrismicRepository[]> {\n\t\tconst url = new URL(\"./repositories\", API_ENDPOINTS.PrismicUser);\n\t\tconst res = await this._fetch({ url });\n\n\t\tif (res.ok) {\n\t\t\tconst json = await res.json();\n\t\t\tconst { value: repositories, error } = decode(\n\t\t\t\tt.array(PrismicRepository),\n\t\t\t\tjson,\n\t\t\t);\n\n\t\t\tif (error) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to decode repositories: ${error.errors.join(\", \")}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn repositories;\n\t\t} else {\n\t\t\tconst text = await res.text();\n\t\t\tthrow new Error(`Failed to read repositories`, { cause: text });\n\t\t}\n\t}\n\n\t// Should this be in manager? It's one of the few sync method\n\t//\n\t// Reply from Angelo 2022-12-22: I think it should be in manager\n\t// because we shouldn't be exporting root-level utilities from this\n\t// package. If we want to make it more inline with the other methods,\n\t// we could simplify the API by changing its signature to the\n\t// following:\n\t//\n\t// ```ts\n\t// (repositoryName: string) => Promise<boolean>\n\t// ```\n\t//\n\t// This method would:\n\t//\n\t// 1. Fetch the list of repositories for the user using `readAll()`.\n\t// The list would be cached.\n\t// 2. Determine if the user has write access to the given repository.\n\t//\n\t// This version has the following benefits:\n\t//\n\t// - Does not expect the consumer to supply a repository object; it\n\t// only requires a repository name, which could be sourced from\n\t// anything (incl. the project's `slicemachine.config.json`).\n\t//\n\t// - Similarly, it does not expect the consumer to call `readAll()`\n\t// before calling this method.\n\t//\n\t// - Works for repositories that the user does not have access to. For\n\t// example, I could use it to check if I have access to \"qwerty\",\n\t// even if I am not added as a user. The purpose of the method is\n\t// still valid: do I have write access to a given repository?\n\thasWriteAccess(repository: PrismicRepository): boolean {\n\t\tswitch (repository.role) {\n\t\t\tcase PrismicRepositoryRole.SuperUser:\n\t\t\tcase PrismicRepositoryRole.Owner:\n\t\t\tcase PrismicRepositoryRole.Administrator:\n\t\t\t\treturn true;\n\n\t\t\tdefault:\n\t\t\t\treturn false;\n\t\t}\n\t}\n\n\tasync checkExists(\n\t\targs: PrismicRepositoryManagerCheckExistsArgs,\n\t): Promise<boolean> {\n\t\tconst url = new URL(\n\t\t\t`./app/dashboard/repositories/${args.domain}/exists`,\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tskipAuthentication: true,\n\t\t});\n\n\t\tconst text = await res.text();\n\n\t\tif (res.ok) {\n\t\t\treturn text === \"false\"; // Endpoint returns `false` when repository exists\n\t\t} else {\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to check repository existence for domain \\`${args.domain}\\``,\n\t\t\t\t{ cause: text },\n\t\t\t);\n\t\t}\n\t}\n\n\tasync create(args: PrismicRepositoryManagerCreateArgs): Promise<void> {\n\t\tconst url = new URL(\n\t\t\t\"./authentication/newrepository?app=slicemachine\",\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\n\t\tconst body = {\n\t\t\t...DEFAULT_REPOSITORY_SETTINGS,\n\t\t\tdomain: args.domain,\n\t\t\t// These properties are optional in the API but needed for tracking\n\t\t\tframework: args.framework,\n\t\t\tstarterId: args.starterId,\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.SliceMachine, // Custom User Agent is required\n\t\t});\n\t\tconst text = await res.text();\n\n\t\t// Endpoint returns repository name on success, that should be within the validation range\n\t\t// Even when there is an error, we get a 200 success and so we have to check the name thanks to that\n\t\tif (\n\t\t\t!res.ok ||\n\t\t\ttext.length < REPOSITORY_NAME_VALIDATION.Min ||\n\t\t\ttext.length > REPOSITORY_NAME_VALIDATION.Max\n\t\t) {\n\t\t\tthrow new Error(`Failed to create repository \\`${args.domain}\\``, {\n\t\t\t\tcause: text,\n\t\t\t});\n\t\t}\n\t}\n\n\t// TODO: Delete this endpoint? It doesn't seem to be used (I might be wrong). - Angelo\n\tasync delete(args: PrismicRepositoryManagerDeleteArgs): Promise<void> {\n\t\tconst cookies = await this.user.getAuthenticationCookies();\n\n\t\tconst url = new URL(\n\t\t\t`./app/settings/delete?_=${cookies[\"X_XSRF\"]}`, // TODO: Maybe we want to throw early if the token is no available, or get the token another way\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\t\t// Update hostname to include repository domain\n\t\turl.hostname = `${args.domain}.${url.hostname}`;\n\n\t\tconst body = {\n\t\t\tconfirm: args.domain,\n\t\t\tpassword: args.password,\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.LegacyZero, // Custom User Agent is required\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new Error(`Failed to delete repository \\`${args.domain}\\``, {\n\t\t\t\tcause: res,\n\t\t\t});\n\t\t}\n\t}\n\n\tasync pushDocuments(\n\t\targs: PrismicRepositoryManagerPushDocumentsArgs,\n\t): Promise<void> {\n\t\tconst url = new URL(\"./starter/documents\", API_ENDPOINTS.PrismicWroom);\n\t\t// Update hostname to include repository domain\n\t\turl.hostname = `${args.domain}.${url.hostname}`;\n\n\t\tconst body = {\n\t\t\tsignature: args.signature,\n\t\t\tdocuments: JSON.stringify(args.documents),\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.LegacyZero, // Custom User Agent is required\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tlet reason: string | null = null;\n\t\t\ttry {\n\t\t\t\treason = await res.text();\n\t\t\t} catch {\n\t\t\t\t// Noop\n\t\t\t}\n\n\t\t\t// Ideally the API should throw a 409 or something like that...\n\t\t\tif (reason === \"Repository should not contain documents\") {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to push documents to repository \\`${args.domain}\\`, repository is not empty`,\n\t\t\t\t\t{\n\t\t\t\t\t\tcause: reason,\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to push documents to repository \\`${args.domain}\\`, ${res.status} ${res.statusText}`,\n\t\t\t\t{\n\t\t\t\t\tcause: reason,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t}\n\n\tasync pushChanges(\n\t\targs: TransactionalMergeArgs,\n\t): Promise<TransactionalMergeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tif (!(await this.user.checkIsLoggedIn())) {\n\t\t\tthrow new UnauthenticatedError();\n\t\t}\n\n\t\ttry {\n\t\t\t// Update the AWS ACL before uploading screenshots as it might have expired\n\t\t\tawait this.screenshots.initS3ACL();\n\n\t\t\tconst allChanges: AllChangeTypes[] = await Promise.all(\n\t\t\t\targs.changes.map(async (change) => {\n\t\t\t\t\tif (change.type === \"Slice\") {\n\t\t\t\t\t\tswitch (change.status) {\n\t\t\t\t\t\t\tcase \"NEW\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.slices.readSlice({\n\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst modelWithScreenshots =\n\t\t\t\t\t\t\t\t\tawait this.slices.updateSliceModelScreenshotsInPlace({\n\t\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: modelWithScreenshots,\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_INSERT,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"MODIFIED\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.slices.readSlice({\n\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst modelWithScreenshots =\n\t\t\t\t\t\t\t\t\tawait this.slices.updateSliceModelScreenshotsInPlace({\n\t\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: modelWithScreenshots,\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_UPDATE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"DELETED\":\n\t\t\t\t\t\t\t\tawait this.screenshots.deleteScreenshotFolder({\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: { id: change.id },\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_DELETE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tswitch (change.status) {\n\t\t\t\t\t\t\tcase \"NEW\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.customTypes.readCustomType({\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_INSERT,\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: model,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"MODIFIED\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.customTypes.readCustomType({\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_UPDATE,\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: model,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"DELETED\":\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: { id: change.id },\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_DELETE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\t// Compute the POST body\n\t\t\tconst requestBody: BulkBody = {\n\t\t\t\tconfirmDeleteDocuments: args.confirmDeleteDocuments,\n\t\t\t\tchanges: allChanges,\n\t\t\t};\n\n\t\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\t\t// TODO: move to customtypes client\n\t\t\tconst response = await this._fetch({\n\t\t\t\turl: new URL(\"./bulk\", API_ENDPOINTS.PrismicModels),\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: requestBody,\n\t\t\t\trepository: repositoryName,\n\t\t\t});\n\n\t\t\tswitch (response.status) {\n\t\t\t\tcase 202:\n\t\t\t\t\treturn this._decodeLimitOrThrow(\n\t\t\t\t\t\tawait response.json(),\n\t\t\t\t\t\tresponse.status,\n\t\t\t\t\t\tLimitType.SOFT,\n\t\t\t\t\t);\n\t\t\t\tcase 204:\n\t\t\t\t\treturn null;\n\t\t\t\tcase 401:\n\t\t\t\t\tthrow new UnauthenticatedError();\n\t\t\t\tcase 403:\n\t\t\t\t\treturn this._decodeLimitOrThrow(\n\t\t\t\t\t\tawait response.json(),\n\t\t\t\t\t\tresponse.status,\n\t\t\t\t\t\tLimitType.HARD,\n\t\t\t\t\t);\n\t\t\t\tcase 400:\n\t\t\t\t\tconst text = await response.text();\n\t\t\t\t\tthrow new Error(text);\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unexpected status code ${response.status}`, {\n\t\t\t\t\t\tcause: await response.text(),\n\t\t\t\t\t});\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tconsole.error(\"An error happened while pushing your changes\");\n\t\t\tconsole.error(err);\n\n\t\t\tthrow err;\n\t\t}\n\t}\n\n\tasync fetchEnvironments(): Promise<Environment[]> {\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\n\t\tconst url = new URL(\"./environments\", API_ENDPOINTS.SliceMachine);\n\t\tconst res = await this._fetch({ url, repository: repositoryName });\n\n\t\tif (res.ok) {\n\t\t\tconst json = await res.json();\n\n\t\t\tconst { value: environments, error } = decode(t.array(Environment), json);\n\n\t\t\tif (error) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to decode environments: ${error.errors.join(\", \")}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn environments;\n\t\t}\n\n\t\tswitch (res.status) {\n\t\t\tcase 401:\n\t\t\t\tthrow new UnauthenticatedError();\n\t\t\tcase 403:\n\t\t\t\tthrow new UnauthorizedError();\n\t\t\tdefault:\n\t\t\t\tthrow new Error(\"Failed to fetch environments.\");\n\t\t}\n\t}\n\n\tprivate _decodeLimitOrThrow(\n\t\tpotentialLimit: unknown,\n\t\tstatusCode: number,\n\t\tlimitType: LimitType,\n\t): Limit | null {\n\t\treturn fold<t.Errors, RawLimit, Limit | null>(\n\t\t\t() => {\n\t\t\t\tconst error: ClientError = {\n\t\t\t\t\tstatus: statusCode,\n\t\t\t\t\tmessage: `Unable to parse raw limit from ${JSON.stringify(\n\t\t\t\t\t\tpotentialLimit,\n\t\t\t\t\t)}`,\n\t\t\t\t};\n\t\t\t\tthrow error;\n\t\t\t},\n\t\t\t(rawLimit: RawLimit) => {\n\t\t\t\tconst limit = { ...rawLimit, type: limitType };\n\n\t\t\t\treturn limit;\n\t\t\t},\n\t\t)(RawLimit.decode(potentialLimit));\n\t}\n\n\tprivate async _fetch(args: {\n\t\turl: URL;\n\t\tmethod?: \"GET\" | \"POST\";\n\t\tbody?: unknown;\n\t\tuserAgent?: PrismicRepositoryUserAgents;\n\t\trepository?: string;\n\t\tskipAuthentication?: boolean;\n\t}): Promise<Response> {\n\t\tlet cookies;\n\t\ttry {\n\t\t\tcookies = await this.user.getAuthenticationCookies();\n\t\t} catch (e) {\n\t\t\tif (!args.skipAuthentication) {\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t}\n\n\t\tconst extraHeaders: Record<string, string> = {};\n\n\t\tif (args.body) {\n\t\t\textraHeaders[\"Content-Type\"] = \"application/json\";\n\t\t}\n\n\t\tif (args.repository) {\n\t\t\textraHeaders.repository = args.repository;\n\t\t}\n\n\t\treturn await fetch(args.url.toString(), {\n\t\t\tmethod: args.method,\n\t\t\tbody: args.body ? JSON.stringify(args.body) : undefined,\n\t\t\theaders: {\n\t\t\t\t// Some endpoints rely on the authorization header...\n\n\t\t\t\t...(cookies !== undefined\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tAuthorization: `Bearer ${cookies[\"prismic-auth\"]}`,\n\t\t\t\t\t\t\tCookie: serializeCookies(cookies),\n\t\t\t\t\t }\n\t\t\t\t\t: {}),\n\t\t\t\t\"User-Agent\": args.userAgent || SLICE_MACHINE_USER_AGENT,\n\t\t\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AAK,MAAM;AACd,mBAAa,cAAc,IAAI;AAAA,IAC/B;AAED,QAAI,KAAK,YAAY;AACpB,mBAAa,aAAa,KAAK;AAAA,IAC/B;AAED,WAAO,MAAM,MAAM,KAAK,IAAI,YAAY;AAAA,MACvC,QAAQ,KAAK;AAAA,MACb,MAAM,KAAK,OAAO,KAAK,UAAU,KAAK,IAAI,IAAI;AAAA,MAC9C,SAAS;AAAA;AAAA,QAGR,GAAI,YAAY,SACb;AAAA,UACA,eAAe,UAAU,QAAQ,cAAc;AAAA,UAC/C,QAAQ,iBAAiB,OAAO;AAAA,QAAA,IAEhC;QACH,cAAc,KAAK,aAAa;AAAA,QAChC,GAAG;AAAA,MACH;AAAA,IAAA,CACD;AAAA,EACF;AACA;"}
|
|
1
|
+
{"version":3,"file":"PrismicRepositoryManager.js","sources":["../../../../src/managers/prismicRepository/PrismicRepositoryManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport fetch, { Response } from \"../../lib/fetch\";\nimport { fold } from \"fp-ts/Either\";\n\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decode } from \"../../lib/decode\";\nimport { serializeCookies } from \"../../lib/serializeCookies\";\n\nimport { SLICE_MACHINE_USER_AGENT } from \"../../constants/SLICE_MACHINE_USER_AGENT\";\nimport { API_ENDPOINTS } from \"../../constants/API_ENDPOINTS\";\nimport { REPOSITORY_NAME_VALIDATION } from \"../../constants/REPOSITORY_NAME_VALIDATION\";\n\nimport {\n\tUnauthenticatedError,\n\tUnauthorizedError,\n\tUnexpectedDataError,\n} from \"../../errors\";\n\nimport { BaseManager } from \"../BaseManager\";\n\nimport {\n\tAllChangeTypes,\n\tBulkBody,\n\tChangeTypes,\n\tClientError,\n\tLimit,\n\tLimitType,\n\tPrismicRepository,\n\tPrismicRepositoryRole,\n\tPrismicRepositoryUserAgent,\n\tPrismicRepositoryUserAgents,\n\tRawLimit,\n\tTransactionalMergeArgs,\n\tTransactionalMergeReturnType,\n\tFrameworkWroomTelemetryID,\n\tStarterId,\n\tEnvironment,\n} from \"./types\";\nimport { sortEnvironments } from \"./sortEnvironments\";\n\nconst DEFAULT_REPOSITORY_SETTINGS = {\n\tplan: \"personal\",\n\tisAnnual: \"false\",\n\trole: \"developer\",\n};\n\ntype PrismicRepositoryManagerCheckExistsArgs = {\n\tdomain: string;\n};\n\ntype PrismicRepositoryManagerCreateArgs = {\n\tdomain: string;\n\tframework: FrameworkWroomTelemetryID;\n\tstarterId?: StarterId;\n};\n\ntype PrismicRepositoryManagerDeleteArgs = {\n\tdomain: string;\n\tpassword: string;\n};\n\ntype PrismicRepositoryManagerPushDocumentsArgs = {\n\tdomain: string;\n\tsignature: string;\n\tdocuments: Record<string, unknown>; // TODO: Type unknown if possible(?)\n};\n\nexport class PrismicRepositoryManager extends BaseManager {\n\t// TODO: Add methods for repository-specific actions. E.g. creating a\n\t// new repository.\n\n\tasync readAll(): Promise<PrismicRepository[]> {\n\t\tconst url = new URL(\"./repositories\", API_ENDPOINTS.PrismicUser);\n\t\tconst res = await this._fetch({ url });\n\n\t\tif (res.ok) {\n\t\t\tconst json = await res.json();\n\t\t\tconst { value: repositories, error } = decode(\n\t\t\t\tt.array(PrismicRepository),\n\t\t\t\tjson,\n\t\t\t);\n\n\t\t\tif (error) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to decode repositories: ${error.errors.join(\", \")}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn repositories;\n\t\t} else {\n\t\t\tconst text = await res.text();\n\t\t\tthrow new Error(`Failed to read repositories`, { cause: text });\n\t\t}\n\t}\n\n\t// Should this be in manager? It's one of the few sync method\n\t//\n\t// Reply from Angelo 2022-12-22: I think it should be in manager\n\t// because we shouldn't be exporting root-level utilities from this\n\t// package. If we want to make it more inline with the other methods,\n\t// we could simplify the API by changing its signature to the\n\t// following:\n\t//\n\t// ```ts\n\t// (repositoryName: string) => Promise<boolean>\n\t// ```\n\t//\n\t// This method would:\n\t//\n\t// 1. Fetch the list of repositories for the user using `readAll()`.\n\t// The list would be cached.\n\t// 2. Determine if the user has write access to the given repository.\n\t//\n\t// This version has the following benefits:\n\t//\n\t// - Does not expect the consumer to supply a repository object; it\n\t// only requires a repository name, which could be sourced from\n\t// anything (incl. the project's `slicemachine.config.json`).\n\t//\n\t// - Similarly, it does not expect the consumer to call `readAll()`\n\t// before calling this method.\n\t//\n\t// - Works for repositories that the user does not have access to. For\n\t// example, I could use it to check if I have access to \"qwerty\",\n\t// even if I am not added as a user. The purpose of the method is\n\t// still valid: do I have write access to a given repository?\n\thasWriteAccess(repository: PrismicRepository): boolean {\n\t\tswitch (repository.role) {\n\t\t\tcase PrismicRepositoryRole.SuperUser:\n\t\t\tcase PrismicRepositoryRole.Owner:\n\t\t\tcase PrismicRepositoryRole.Administrator:\n\t\t\t\treturn true;\n\n\t\t\tdefault:\n\t\t\t\treturn false;\n\t\t}\n\t}\n\n\tasync checkExists(\n\t\targs: PrismicRepositoryManagerCheckExistsArgs,\n\t): Promise<boolean> {\n\t\tconst url = new URL(\n\t\t\t`./app/dashboard/repositories/${args.domain}/exists`,\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tskipAuthentication: true,\n\t\t});\n\n\t\tconst text = await res.text();\n\n\t\tif (res.ok) {\n\t\t\treturn text === \"false\"; // Endpoint returns `false` when repository exists\n\t\t} else {\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to check repository existence for domain \\`${args.domain}\\``,\n\t\t\t\t{ cause: text },\n\t\t\t);\n\t\t}\n\t}\n\n\tasync create(args: PrismicRepositoryManagerCreateArgs): Promise<void> {\n\t\tconst url = new URL(\n\t\t\t\"./authentication/newrepository?app=slicemachine\",\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\n\t\tconst body = {\n\t\t\t...DEFAULT_REPOSITORY_SETTINGS,\n\t\t\tdomain: args.domain,\n\t\t\t// These properties are optional in the API but needed for tracking\n\t\t\tframework: args.framework,\n\t\t\tstarterId: args.starterId,\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.SliceMachine, // Custom User Agent is required\n\t\t});\n\t\tconst text = await res.text();\n\n\t\t// Endpoint returns repository name on success, that should be within the validation range\n\t\t// Even when there is an error, we get a 200 success and so we have to check the name thanks to that\n\t\tif (\n\t\t\t!res.ok ||\n\t\t\ttext.length < REPOSITORY_NAME_VALIDATION.Min ||\n\t\t\ttext.length > REPOSITORY_NAME_VALIDATION.Max\n\t\t) {\n\t\t\tthrow new Error(`Failed to create repository \\`${args.domain}\\``, {\n\t\t\t\tcause: text,\n\t\t\t});\n\t\t}\n\t}\n\n\t// TODO: Delete this endpoint? It doesn't seem to be used (I might be wrong). - Angelo\n\tasync delete(args: PrismicRepositoryManagerDeleteArgs): Promise<void> {\n\t\tconst cookies = await this.user.getAuthenticationCookies();\n\n\t\tconst url = new URL(\n\t\t\t`./app/settings/delete?_=${cookies[\"X_XSRF\"]}`, // TODO: Maybe we want to throw early if the token is no available, or get the token another way\n\t\t\tAPI_ENDPOINTS.PrismicWroom,\n\t\t);\n\t\t// Update hostname to include repository domain\n\t\turl.hostname = `${args.domain}.${url.hostname}`;\n\n\t\tconst body = {\n\t\t\tconfirm: args.domain,\n\t\t\tpassword: args.password,\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.LegacyZero, // Custom User Agent is required\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tthrow new Error(`Failed to delete repository \\`${args.domain}\\``, {\n\t\t\t\tcause: res,\n\t\t\t});\n\t\t}\n\t}\n\n\tasync pushDocuments(\n\t\targs: PrismicRepositoryManagerPushDocumentsArgs,\n\t): Promise<void> {\n\t\tconst url = new URL(\"./starter/documents\", API_ENDPOINTS.PrismicWroom);\n\t\t// Update hostname to include repository domain\n\t\turl.hostname = `${args.domain}.${url.hostname}`;\n\n\t\tconst body = {\n\t\t\tsignature: args.signature,\n\t\t\tdocuments: JSON.stringify(args.documents),\n\t\t};\n\n\t\tconst res = await this._fetch({\n\t\t\turl,\n\t\t\tmethod: \"POST\",\n\t\t\tbody,\n\t\t\tuserAgent: PrismicRepositoryUserAgent.LegacyZero, // Custom User Agent is required\n\t\t});\n\n\t\tif (!res.ok) {\n\t\t\tlet reason: string | null = null;\n\t\t\ttry {\n\t\t\t\treason = await res.text();\n\t\t\t} catch {\n\t\t\t\t// Noop\n\t\t\t}\n\n\t\t\t// Ideally the API should throw a 409 or something like that...\n\t\t\tif (reason === \"Repository should not contain documents\") {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Failed to push documents to repository \\`${args.domain}\\`, repository is not empty`,\n\t\t\t\t\t{\n\t\t\t\t\t\tcause: reason,\n\t\t\t\t\t},\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to push documents to repository \\`${args.domain}\\`, ${res.status} ${res.statusText}`,\n\t\t\t\t{\n\t\t\t\t\tcause: reason,\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t}\n\n\tasync pushChanges(\n\t\targs: TransactionalMergeArgs,\n\t): Promise<TransactionalMergeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tif (!(await this.user.checkIsLoggedIn())) {\n\t\t\tthrow new UnauthenticatedError();\n\t\t}\n\n\t\ttry {\n\t\t\t// Update the AWS ACL before uploading screenshots as it might have expired\n\t\t\tawait this.screenshots.initS3ACL();\n\n\t\t\tconst allChanges: AllChangeTypes[] = await Promise.all(\n\t\t\t\targs.changes.map(async (change) => {\n\t\t\t\t\tif (change.type === \"Slice\") {\n\t\t\t\t\t\tswitch (change.status) {\n\t\t\t\t\t\t\tcase \"NEW\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.slices.readSlice({\n\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst modelWithScreenshots =\n\t\t\t\t\t\t\t\t\tawait this.slices.updateSliceModelScreenshotsInPlace({\n\t\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: modelWithScreenshots,\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_INSERT,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"MODIFIED\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.slices.readSlice({\n\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tconst modelWithScreenshots =\n\t\t\t\t\t\t\t\t\tawait this.slices.updateSliceModelScreenshotsInPlace({\n\t\t\t\t\t\t\t\t\t\tlibraryID: change.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: modelWithScreenshots,\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_UPDATE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"DELETED\":\n\t\t\t\t\t\t\t\tawait this.screenshots.deleteScreenshotFolder({\n\t\t\t\t\t\t\t\t\tsliceID: change.id,\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: { id: change.id },\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.SLICE_DELETE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tswitch (change.status) {\n\t\t\t\t\t\t\tcase \"NEW\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.customTypes.readCustomType({\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_INSERT,\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: model,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"MODIFIED\": {\n\t\t\t\t\t\t\t\tconst { model } = await this.customTypes.readCustomType({\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (!model) {\n\t\t\t\t\t\t\t\t\tthrow Error(`Could not find model ${change.id}`);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_UPDATE,\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: model,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcase \"DELETED\":\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\tid: change.id,\n\t\t\t\t\t\t\t\t\tpayload: { id: change.id },\n\t\t\t\t\t\t\t\t\ttype: ChangeTypes.CUSTOM_TYPE_DELETE,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t);\n\n\t\t\t// Compute the POST body\n\t\t\tconst requestBody: BulkBody = {\n\t\t\t\tconfirmDeleteDocuments: args.confirmDeleteDocuments,\n\t\t\t\tchanges: allChanges,\n\t\t\t};\n\n\t\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\t\t// TODO: move to customtypes client\n\t\t\tconst response = await this._fetch({\n\t\t\t\turl: new URL(\"./bulk\", API_ENDPOINTS.PrismicModels),\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: requestBody,\n\t\t\t\trepository: repositoryName,\n\t\t\t});\n\n\t\t\tswitch (response.status) {\n\t\t\t\tcase 202:\n\t\t\t\t\treturn this._decodeLimitOrThrow(\n\t\t\t\t\t\tawait response.json(),\n\t\t\t\t\t\tresponse.status,\n\t\t\t\t\t\tLimitType.SOFT,\n\t\t\t\t\t);\n\t\t\t\tcase 204:\n\t\t\t\t\treturn null;\n\t\t\t\tcase 401:\n\t\t\t\t\tthrow new UnauthenticatedError();\n\t\t\t\tcase 403:\n\t\t\t\t\treturn this._decodeLimitOrThrow(\n\t\t\t\t\t\tawait response.json(),\n\t\t\t\t\t\tresponse.status,\n\t\t\t\t\t\tLimitType.HARD,\n\t\t\t\t\t);\n\t\t\t\tcase 400:\n\t\t\t\t\tconst text = await response.text();\n\t\t\t\t\tthrow new Error(text);\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new Error(`Unexpected status code ${response.status}`, {\n\t\t\t\t\t\tcause: await response.text(),\n\t\t\t\t\t});\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tconsole.error(\"An error happened while pushing your changes\");\n\t\t\tconsole.error(err);\n\n\t\t\tthrow err;\n\t\t}\n\t}\n\n\tasync fetchEnvironments(): Promise<Environment[]> {\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\n\t\tconst url = new URL(`./environments`, API_ENDPOINTS.SliceMachineV1);\n\t\turl.searchParams.set(\"repository\", repositoryName);\n\t\tconst res = await this._fetch({ url });\n\n\t\tif (res.ok) {\n\t\t\tconst json = await res.json();\n\n\t\t\tconst { value, error } = decode(\n\t\t\t\tt.union([\n\t\t\t\t\tt.type({\n\t\t\t\t\t\tresults: t.array(Environment),\n\t\t\t\t\t}),\n\t\t\t\t\tt.type({\n\t\t\t\t\t\terror: t.literal(\"invalid_token\"),\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\t\tjson,\n\t\t\t);\n\n\t\t\tif (error) {\n\t\t\t\tthrow new UnexpectedDataError(\n\t\t\t\t\t`Failed to decode environments: ${error.errors.join(\", \")}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (\"results\" in value) {\n\t\t\t\treturn sortEnvironments(value.results);\n\t\t\t}\n\t\t}\n\n\t\tswitch (res.status) {\n\t\t\tcase 400:\n\t\t\tcase 401:\n\t\t\t\tthrow new UnauthenticatedError();\n\t\t\tcase 403:\n\t\t\t\tthrow new UnauthorizedError();\n\t\t\tdefault:\n\t\t\t\tthrow new Error(\"Failed to fetch environments.\");\n\t\t}\n\t}\n\n\tprivate _decodeLimitOrThrow(\n\t\tpotentialLimit: unknown,\n\t\tstatusCode: number,\n\t\tlimitType: LimitType,\n\t): Limit | null {\n\t\treturn fold<t.Errors, RawLimit, Limit | null>(\n\t\t\t() => {\n\t\t\t\tconst error: ClientError = {\n\t\t\t\t\tstatus: statusCode,\n\t\t\t\t\tmessage: `Unable to parse raw limit from ${JSON.stringify(\n\t\t\t\t\t\tpotentialLimit,\n\t\t\t\t\t)}`,\n\t\t\t\t};\n\t\t\t\tthrow error;\n\t\t\t},\n\t\t\t(rawLimit: RawLimit) => {\n\t\t\t\tconst limit = { ...rawLimit, type: limitType };\n\n\t\t\t\treturn limit;\n\t\t\t},\n\t\t)(RawLimit.decode(potentialLimit));\n\t}\n\n\tprivate async _fetch(args: {\n\t\turl: URL;\n\t\tmethod?: \"GET\" | \"POST\";\n\t\tbody?: unknown;\n\t\tuserAgent?: PrismicRepositoryUserAgents;\n\t\trepository?: string;\n\t\tskipAuthentication?: boolean;\n\t}): Promise<Response> {\n\t\tlet cookies;\n\t\ttry {\n\t\t\tcookies = await this.user.getAuthenticationCookies();\n\t\t} catch (e) {\n\t\t\tif (!args.skipAuthentication) {\n\t\t\t\tthrow e;\n\t\t\t}\n\t\t}\n\n\t\tconst extraHeaders: Record<string, string> = {};\n\n\t\tif (args.body) {\n\t\t\textraHeaders[\"Content-Type\"] = \"application/json\";\n\t\t}\n\n\t\tif (args.repository) {\n\t\t\textraHeaders.repository = args.repository;\n\t\t}\n\n\t\treturn await fetch(args.url.toString(), {\n\t\t\tmethod: args.method,\n\t\t\tbody: args.body ? JSON.stringify(args.body) : undefined,\n\t\t\theaders: {\n\t\t\t\t// Some endpoints rely on the authorization header...\n\n\t\t\t\t...(cookies !== undefined\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tAuthorization: `Bearer ${cookies[\"prismic-auth\"]}`,\n\t\t\t\t\t\t\tCookie: serializeCookies(cookies),\n\t\t\t\t\t }\n\t\t\t\t\t: {}),\n\t\t\t\t\"User-Agent\": args.userAgent || SLICE_MACHINE_USER_AGENT,\n\t\t\t\t...extraHeaders,\n\t\t\t},\n\t\t});\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AAwCA,MAAM,8BAA8B;AAAA,EACnC,MAAM;AAAA,EACN,UAAU;AAAA,EACV,MAAM;;AAwBD,MAAO,iCAAiC,YAAW;AAAA;AAAA;AAAA,EAIxD,MAAM,UAAO;AACZ,UAAM,MAAM,IAAI,IAAI,kBAAkB,cAAc,WAAW;AAC/D,UAAM,MAAM,MAAM,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Ac,IAAI;AAAA,IAC/B;AAED,QAAI,KAAK,YAAY;AACpB,mBAAa,aAAa,KAAK;AAAA,IAC/B;AAED,WAAO,MAAM,MAAM,KAAK,IAAI,YAAY;AAAA,MACvC,QAAQ,KAAK;AAAA,MACb,MAAM,KAAK,OAAO,KAAK,UAAU,KAAK,IAAI,IAAI;AAAA,MAC9C,SAAS;AAAA;AAAA,QAGR,GAAI,YAAY,SACb;AAAA,UACA,eAAe,UAAU,QAAQ,cAAc;AAAA,UAC/C,QAAQ,iBAAiB,OAAO;AAAA,QAAA,IAEhC;QACH,cAAc,KAAK,aAAa;AAAA,QAChC,GAAG;AAAA,MACH;AAAA,IAAA,CACD;AAAA,EACF;AACA;"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
function sortEnvironments(environments) {
|
|
4
|
+
return [...environments].sort((a, b) => {
|
|
5
|
+
switch (a.kind) {
|
|
6
|
+
case "prod": {
|
|
7
|
+
return -1;
|
|
8
|
+
}
|
|
9
|
+
case "stage": {
|
|
10
|
+
switch (b.kind) {
|
|
11
|
+
case "prod": {
|
|
12
|
+
return 1;
|
|
13
|
+
}
|
|
14
|
+
case "stage": {
|
|
15
|
+
return a.name.localeCompare(b.name);
|
|
16
|
+
}
|
|
17
|
+
case "dev":
|
|
18
|
+
default: {
|
|
19
|
+
return -1;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
case "dev": {
|
|
24
|
+
return 1;
|
|
25
|
+
}
|
|
26
|
+
default: {
|
|
27
|
+
if (b.kind === void 0) {
|
|
28
|
+
return a.name.localeCompare(b.name);
|
|
29
|
+
}
|
|
30
|
+
return 1;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
exports.sortEnvironments = sortEnvironments;
|
|
36
|
+
//# sourceMappingURL=sortEnvironments.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortEnvironments.cjs","sources":["../../../../src/managers/prismicRepository/sortEnvironments.ts"],"sourcesContent":["import { Environment } from \"./types\";\n\n/**\n * Sorts a list of environments using the following criteria:\n *\n * - The production environment is always first.\n * - Staging environments are always after production, before development\n * environments, and sorted alphabetically by name.\n * - The development environment is always last.\n *\n * It assumes the list of environments contains one production environment and\n * at most one dev environment.\n *\n * @param environments - The environments to sort.\n *\n * @returns The sorted environments.\n */\nexport function sortEnvironments(environments: Environment[]): Environment[] {\n\treturn [...environments].sort((a, b) => {\n\t\tswitch (a.kind) {\n\t\t\tcase \"prod\": {\n\t\t\t\treturn -1;\n\t\t\t}\n\n\t\t\tcase \"stage\": {\n\t\t\t\tswitch (b.kind) {\n\t\t\t\t\tcase \"prod\": {\n\t\t\t\t\t\treturn 1;\n\t\t\t\t\t}\n\n\t\t\t\t\tcase \"stage\": {\n\t\t\t\t\t\treturn a.name.localeCompare(b.name);\n\t\t\t\t\t}\n\n\t\t\t\t\tcase \"dev\":\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\treturn -1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tcase \"dev\": {\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tif (b.kind === undefined) {\n\t\t\t\t\treturn a.name.localeCompare(b.name);\n\t\t\t\t}\n\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\t});\n}\n"],"names":[],"mappings":";;AAiBM,SAAU,iBAAiB,cAA2B;AAC3D,SAAO,CAAC,GAAG,YAAY,EAAE,KAAK,CAAC,GAAG,MAAK;AACtC,YAAQ,EAAE,MAAM;AAAA,MACf,KAAK,QAAQ;AACL,eAAA;AAAA,MACP;AAAA,MAED,KAAK,SAAS;AACb,gBAAQ,EAAE,MAAM;AAAA,UACf,KAAK,QAAQ;AACL,mBAAA;AAAA,UACP;AAAA,UAED,KAAK,SAAS;AACb,mBAAO,EAAE,KAAK,cAAc,EAAE,IAAI;AAAA,UAClC;AAAA,UAED,KAAK;AAAA,UACL,SAAS;AACD,mBAAA;AAAA,UACP;AAAA,QACD;AAAA,MACD;AAAA,MAED,KAAK,OAAO;AACJ,eAAA;AAAA,MACP;AAAA,MAED,SAAS;AACJ,YAAA,EAAE,SAAS,QAAW;AACzB,iBAAO,EAAE,KAAK,cAAc,EAAE,IAAI;AAAA,QAClC;AAEM,eAAA;AAAA,MACP;AAAA,IACD;AAAA,EAAA,CACD;AACF;;"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Environment } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Sorts a list of environments using the following criteria:
|
|
4
|
+
*
|
|
5
|
+
* - The production environment is always first.
|
|
6
|
+
* - Staging environments are always after production, before development
|
|
7
|
+
* environments, and sorted alphabetically by name.
|
|
8
|
+
* - The development environment is always last.
|
|
9
|
+
*
|
|
10
|
+
* It assumes the list of environments contains one production environment and
|
|
11
|
+
* at most one dev environment.
|
|
12
|
+
*
|
|
13
|
+
* @param environments - The environments to sort.
|
|
14
|
+
*
|
|
15
|
+
* @returns The sorted environments.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sortEnvironments(environments: Environment[]): Environment[];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
function sortEnvironments(environments) {
|
|
2
|
+
return [...environments].sort((a, b) => {
|
|
3
|
+
switch (a.kind) {
|
|
4
|
+
case "prod": {
|
|
5
|
+
return -1;
|
|
6
|
+
}
|
|
7
|
+
case "stage": {
|
|
8
|
+
switch (b.kind) {
|
|
9
|
+
case "prod": {
|
|
10
|
+
return 1;
|
|
11
|
+
}
|
|
12
|
+
case "stage": {
|
|
13
|
+
return a.name.localeCompare(b.name);
|
|
14
|
+
}
|
|
15
|
+
case "dev":
|
|
16
|
+
default: {
|
|
17
|
+
return -1;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
case "dev": {
|
|
22
|
+
return 1;
|
|
23
|
+
}
|
|
24
|
+
default: {
|
|
25
|
+
if (b.kind === void 0) {
|
|
26
|
+
return a.name.localeCompare(b.name);
|
|
27
|
+
}
|
|
28
|
+
return 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export {
|
|
34
|
+
sortEnvironments
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=sortEnvironments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortEnvironments.js","sources":["../../../../src/managers/prismicRepository/sortEnvironments.ts"],"sourcesContent":["import { Environment } from \"./types\";\n\n/**\n * Sorts a list of environments using the following criteria:\n *\n * - The production environment is always first.\n * - Staging environments are always after production, before development\n * environments, and sorted alphabetically by name.\n * - The development environment is always last.\n *\n * It assumes the list of environments contains one production environment and\n * at most one dev environment.\n *\n * @param environments - The environments to sort.\n *\n * @returns The sorted environments.\n */\nexport function sortEnvironments(environments: Environment[]): Environment[] {\n\treturn [...environments].sort((a, b) => {\n\t\tswitch (a.kind) {\n\t\t\tcase \"prod\": {\n\t\t\t\treturn -1;\n\t\t\t}\n\n\t\t\tcase \"stage\": {\n\t\t\t\tswitch (b.kind) {\n\t\t\t\t\tcase \"prod\": {\n\t\t\t\t\t\treturn 1;\n\t\t\t\t\t}\n\n\t\t\t\t\tcase \"stage\": {\n\t\t\t\t\t\treturn a.name.localeCompare(b.name);\n\t\t\t\t\t}\n\n\t\t\t\t\tcase \"dev\":\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\treturn -1;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tcase \"dev\": {\n\t\t\t\treturn 1;\n\t\t\t}\n\n\t\t\tdefault: {\n\t\t\t\tif (b.kind === undefined) {\n\t\t\t\t\treturn a.name.localeCompare(b.name);\n\t\t\t\t}\n\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\t});\n}\n"],"names":[],"mappings":"AAiBM,SAAU,iBAAiB,cAA2B;AAC3D,SAAO,CAAC,GAAG,YAAY,EAAE,KAAK,CAAC,GAAG,MAAK;AACtC,YAAQ,EAAE,MAAM;AAAA,MACf,KAAK,QAAQ;AACL,eAAA;AAAA,MACP;AAAA,MAED,KAAK,SAAS;AACb,gBAAQ,EAAE,MAAM;AAAA,UACf,KAAK,QAAQ;AACL,mBAAA;AAAA,UACP;AAAA,UAED,KAAK,SAAS;AACb,mBAAO,EAAE,KAAK,cAAc,EAAE,IAAI;AAAA,UAClC;AAAA,UAED,KAAK;AAAA,UACL,SAAS;AACD,mBAAA;AAAA,UACP;AAAA,QACD;AAAA,MACD;AAAA,MAED,KAAK,OAAO;AACJ,eAAA;AAAA,MACP;AAAA,MAED,SAAS;AACJ,YAAA,EAAE,SAAS,QAAW;AACzB,iBAAO,EAAE,KAAK,cAAc,EAAE,IAAI;AAAA,QAClC;AAEM,eAAA;AAAA,MACP;AAAA,IACD;AAAA,EAAA,CACD;AACF;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProjectManager.cjs","sources":["../../../../src/managers/project/ProjectManager.ts"],"sourcesContent":["import * as fs from \"node:fs/promises\";\nimport { existsSync } from \"node:fs\";\nimport * as path from \"node:path\";\nimport { createRequire } from \"node:module\";\nimport { detect as niDetect } from \"@antfu/ni\";\nimport { ExecaChildProcess } from \"execa\";\nimport {\n\tHookError,\n\tCallHookReturnType,\n\tProjectEnvironmentUpdateHook,\n} from \"@slicemachine/plugin-kit\";\nimport * as t from \"io-ts\";\n\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport { decodeSliceMachineConfig } from \"../../lib/decodeSliceMachineConfig\";\nimport { format } from \"../../lib/format\";\nimport { installDependencies } from \"../../lib/installDependencies\";\nimport { locateFileUpward } from \"../../lib/locateFileUpward\";\n\nimport {\n\tPackageManager,\n\tSliceMachineConfig,\n\tOnlyHookErrors,\n} from \"../../types\";\n\nimport { SliceMachineError, InternalError, PluginError } from \"../../errors\";\n\nimport { SLICE_MACHINE_CONFIG_FILENAME } from \"../../constants/SLICE_MACHINE_CONFIG_FILENAME\";\nimport { TS_CONFIG_FILENAME } from \"../../constants/TS_CONFIG_FILENAME\";\nimport { SLICE_MACHINE_NPM_PACKAGE_NAME } from \"../../constants/SLICE_MACHINE_NPM_PACKAGE_NAME\";\n\nimport { BaseManager } from \"../BaseManager\";\n\ntype ProjectManagerGetSliceMachineConfigPathArgs = {\n\tignoreCache?: boolean;\n};\n\ntype ProjectManagerGetRootArgs = {\n\tignoreCache?: boolean;\n};\n\ntype ProjectManagerCheckIsTypeScriptArgs = {\n\trootOverride?: string;\n};\n\ntype ProjectManagerWriteSliceMachineConfigArgs = {\n\tconfig: SliceMachineConfig;\n\tpath?: string;\n};\n\ntype ProjectManagerInitProjectArgs = {\n\tlog?: (message: string) => void;\n};\n\ntype ProjectManagerDetectPackageManager = {\n\troot?: string;\n};\n\ntype ProjectManagerInstallDependenciesArgs = {\n\tdependencies: Record<string, string>;\n\tdev?: boolean;\n\tpackageManager?: PackageManager;\n\tlog?: (message: string) => void;\n};\n\ntype ProjectManagerInstallDependenciesReturnType = {\n\texecaProcess: ExecaChildProcess;\n};\n\ntype ProjectManagerReadEnvironmentReturnType = {\n\tenvironment: string | undefined;\n\terrors: (DecodeError | HookError)[];\n};\n\ntype ProjectManagerUpdateEnvironmentArgs = {\n\tenvironment: string | undefined;\n};\n\nexport class ProjectManager extends BaseManager {\n\tprivate _cachedRoot: string | undefined;\n\tprivate _cachedSliceMachineConfigPath: string | undefined;\n\tprivate _cachedSliceMachineConfig: SliceMachineConfig | undefined;\n\n\tasync getSliceMachineConfigPath(\n\t\targs?: ProjectManagerGetSliceMachineConfigPathArgs,\n\t): Promise<string> {\n\t\tif (this._cachedSliceMachineConfigPath && !args?.ignoreCache) {\n\t\t\treturn this._cachedSliceMachineConfigPath;\n\t\t}\n\n\t\ttry {\n\t\t\tthis._cachedSliceMachineConfigPath = await locateFileUpward(\n\t\t\t\tSLICE_MACHINE_CONFIG_FILENAME,\n\t\t\t\t{ startDir: this.cwd },\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tthrow new Error(\n\t\t\t\t`Could not find a ${SLICE_MACHINE_CONFIG_FILENAME} file. Please create a config file at the root of your project.`,\n\t\t\t);\n\t\t}\n\n\t\treturn this._cachedSliceMachineConfigPath;\n\t}\n\n\tasync getRoot(args?: ProjectManagerGetRootArgs): Promise<string> {\n\t\tif (this._cachedRoot && !args?.ignoreCache) {\n\t\t\treturn this._cachedRoot;\n\t\t}\n\n\t\tconst sliceMachineConfigFilePath = await this.getSliceMachineConfigPath({\n\t\t\tignoreCache: args?.ignoreCache,\n\t\t});\n\n\t\tthis._cachedRoot = path.dirname(sliceMachineConfigFilePath);\n\n\t\treturn this._cachedRoot;\n\t}\n\n\tasync suggestRoot(): Promise<string> {\n\t\tconst suggestedRootPackageJSON = await locateFileUpward(\"package.json\", {\n\t\t\tstartDir: this.cwd,\n\t\t});\n\n\t\treturn path.dirname(suggestedRootPackageJSON);\n\t}\n\n\tasync suggestSliceMachineConfigPath(): Promise<string> {\n\t\tconst suggestedRoot = await this.suggestRoot();\n\n\t\treturn path.resolve(suggestedRoot, SLICE_MACHINE_CONFIG_FILENAME);\n\t}\n\n\tasync checkIsTypeScript(\n\t\targs?: ProjectManagerCheckIsTypeScriptArgs,\n\t): Promise<boolean> {\n\t\tconst root = args?.rootOverride || (await this.getRoot());\n\t\tconst rootTSConfigPath = path.resolve(root, TS_CONFIG_FILENAME);\n\n\t\t// We just care if the file exists, we don't need access to it\n\t\treturn existsSync(rootTSConfigPath);\n\t}\n\n\tasync getSliceMachineConfig(): Promise<SliceMachineConfig> {\n\t\tif (this._cachedSliceMachineConfig) {\n\t\t\treturn this._cachedSliceMachineConfig;\n\t\t} else {\n\t\t\treturn await this.loadSliceMachineConfig();\n\t\t}\n\t}\n\n\tasync writeSliceMachineConfig(\n\t\targs: ProjectManagerWriteSliceMachineConfigArgs,\n\t): Promise<void> {\n\t\tconst configFilePath =\n\t\t\targs.path || (await this.getSliceMachineConfigPath());\n\n\t\tconst config = await format(\n\t\t\tJSON.stringify(args.config, null, 2),\n\t\t\tconfigFilePath,\n\t\t);\n\n\t\tawait fs.writeFile(configFilePath, config, \"utf-8\");\n\t\tdelete this._cachedSliceMachineConfig; // Clear config cache\n\t}\n\n\tasync loadSliceMachineConfig(): Promise<SliceMachineConfig> {\n\t\t// TODO: Reload plugins with a fresh plugin runner. Plugins may\n\t\t// have been added or removed.\n\t\tconst configFilePath = await this.getSliceMachineConfigPath();\n\n\t\tlet rawConfig: unknown | undefined;\n\t\ttry {\n\t\t\tconst contents = await fs.readFile(configFilePath, \"utf8\");\n\t\t\trawConfig = JSON.parse(contents);\n\t\t} catch (error) {\n\t\t\tif (error instanceof SyntaxError) {\n\t\t\t\tthrow new SliceMachineError(\n\t\t\t\t\t`Could not parse config file at ${configFilePath}.\\n\\nError Message: ${error.message}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Noop, more specific error is thrown after\n\t\t}\n\n\t\tif (!rawConfig) {\n\t\t\t// TODO: Write a more friendly and useful message.\n\t\t\tthrow new Error(\"No config found.\");\n\t\t}\n\n\t\tconst { value: sliceMachineConfig, error } =\n\t\t\tdecodeSliceMachineConfig(rawConfig);\n\n\t\tif (error) {\n\t\t\t// TODO: Write a more friendly and useful message.\n\t\t\tthrow new Error(`Invalid config. ${error.errors.join(\", \")}`, {\n\t\t\t\tcause: { rawConfig },\n\t\t\t});\n\t\t}\n\n\t\t// Allow cached config reading using `SliceMachineManager.prototype.getProjectConfig()`.\n\t\tthis._cachedSliceMachineConfig = sliceMachineConfig;\n\n\t\treturn sliceMachineConfig;\n\t}\n\n\tasync locateSliceMachineUIDir(): Promise<string> {\n\t\tconst projectRoot = await this.getRoot();\n\n\t\tconst require = createRequire(path.join(projectRoot, \"index.js\"));\n\t\tconst sliceMachinePackageJSONPath = require.resolve(\n\t\t\t`${SLICE_MACHINE_NPM_PACKAGE_NAME}/package.json`,\n\t\t);\n\n\t\treturn path.dirname(sliceMachinePackageJSONPath);\n\t}\n\n\t/**\n\t * Returns the project's repository name (i.e. the production environment). It\n\t * ignores the currently selected environment.\n\t *\n\t * Use this method to retrieve the production environment domain.\n\t *\n\t * @returns The project's repository name.\n\t */\n\tasync getRepositoryName(): Promise<string> {\n\t\tconst sliceMachineConfig = await this.getSliceMachineConfig();\n\n\t\treturn sliceMachineConfig.repositoryName;\n\t}\n\n\t/**\n\t * Returns the currently selected environment domain if set. If an environment\n\t * is not set, it returns the project's repository name (the production\n\t * environment).\n\t *\n\t * Use this method to retrieve the repository name to be sent with Prismic API\n\t * requests.\n\t *\n\t * @returns The resolved repository name.\n\t */\n\tasync getResolvedRepositoryName(): Promise<string> {\n\t\tconst repositoryName = await this.getRepositoryName();\n\n\t\tconst supportsEnvironments = this.project.checkSupportsEnvironments();\n\t\tif (!supportsEnvironments) {\n\t\t\treturn repositoryName;\n\t\t}\n\n\t\tconst { environment } = await this.project.readEnvironment();\n\n\t\treturn environment ?? repositoryName;\n\t}\n\n\tasync getAdapterName(): Promise<string> {\n\t\tconst sliceMachineConfig = await this.getSliceMachineConfig();\n\t\tconst adapterName =\n\t\t\ttypeof sliceMachineConfig.adapter === \"string\"\n\t\t\t\t? sliceMachineConfig.adapter\n\t\t\t\t: sliceMachineConfig.adapter.resolve;\n\n\t\treturn adapterName;\n\t}\n\n\tasync locateAdapterDir(): Promise<string> {\n\t\tconst projectRoot = await this.getRoot();\n\t\tconst adapterName = await this.getAdapterName();\n\t\tconst require = createRequire(path.join(projectRoot, \"index.js\"));\n\t\tconst adapterPackageJSONPath = require.resolve(\n\t\t\t`${adapterName}/package.json`,\n\t\t);\n\n\t\treturn path.dirname(adapterPackageJSONPath);\n\t}\n\n\tasync initProject(args?: ProjectManagerInitProjectArgs): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst log = args?.log || console.log.bind(this);\n\n\t\tconst { errors } = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:init\",\n\t\t\t{\n\t\t\t\tlog,\n\t\t\t\tinstallDependencies: async (args) => {\n\t\t\t\t\tconst { execaProcess } = await this.installDependencies({\n\t\t\t\t\t\tdependencies: args.dependencies,\n\t\t\t\t\t\tdev: args.dev,\n\t\t\t\t\t\tlog,\n\t\t\t\t\t});\n\n\t\t\t\t\tawait execaProcess;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (errors.length > 0) {\n\t\t\t// TODO: Provide better error message.\n\t\t\tthrow new SliceMachineError(\n\t\t\t\t`Failed to initialize project: ${errors.join(\", \")}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tasync detectPackageManager(\n\t\targs?: ProjectManagerDetectPackageManager,\n\t): Promise<PackageManager> {\n\t\tconst projectRoot = args?.root || (await this.getRoot());\n\n\t\tconst packageManager = await niDetect({\n\t\t\tautoInstall: true,\n\t\t\tcwd: projectRoot,\n\t\t});\n\n\t\treturn packageManager || \"npm\";\n\t}\n\n\tasync installDependencies(\n\t\targs: ProjectManagerInstallDependenciesArgs,\n\t): Promise<ProjectManagerInstallDependenciesReturnType> {\n\t\tconst packageManager =\n\t\t\targs.packageManager || (await this.detectPackageManager());\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst log = args.log || console.log.bind(this);\n\n\t\tconst wrappedLogger = (data: Buffer | string | null) => {\n\t\t\tif (data instanceof Buffer) {\n\t\t\t\tlog(data.toString());\n\t\t\t} else if (typeof data === \"string\") {\n\t\t\t\tlog(data);\n\t\t\t}\n\t\t};\n\n\t\ttry {\n\t\t\tconst { execaProcess } = await installDependencies({\n\t\t\t\tpackageManager,\n\t\t\t\tdependencies: args.dependencies,\n\t\t\t\tdev: args.dev,\n\t\t\t});\n\n\t\t\t// Don't clutter console with logs when process is non TTY (CI, etc.)\n\t\t\tif (process.stdout.isTTY || process.env.NODE_ENV === \"test\") {\n\t\t\t\texecaProcess.stdout?.on(\"data\", wrappedLogger);\n\t\t\t}\n\t\t\texecaProcess.stderr?.on(\"data\", wrappedLogger);\n\n\t\t\treturn {\n\t\t\t\texecaProcess,\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tif (\n\t\t\t\terror instanceof Error &&\n\t\t\t\t\"shortMessage\" in error &&\n\t\t\t\t\"stderr\" in error\n\t\t\t) {\n\t\t\t\tthrow new InternalError(\"Package installation failed\", {\n\t\t\t\t\tcause: error,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tcheckSupportsEnvironments(): boolean {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\treturn (\n\t\t\tthis.sliceMachinePluginRunner.hooksForType(\"project:environment:read\")\n\t\t\t\t.length > 0 &&\n\t\t\tthis.sliceMachinePluginRunner.hooksForType(\"project:environment:update\")\n\t\t\t\t.length > 0\n\t\t);\n\t}\n\n\tasync readEnvironment(): Promise<ProjectManagerReadEnvironmentReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tawait this._assertAdapterSupportsEnvironments();\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:environment:read\",\n\t\t\tundefined,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tenvironment: t.union([t.undefined, t.string]),\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\n\t\t// An undefined value is equivalent to the production environment.\n\t\tconst environmentDomain =\n\t\t\tdata[0]?.environment === repositoryName\n\t\t\t\t? undefined\n\t\t\t\t: data[0]?.environment;\n\n\t\treturn {\n\t\t\tenvironment: environmentDomain,\n\t\t\terrors,\n\t\t};\n\t}\n\n\tasync updateEnvironment(\n\t\targs: ProjectManagerUpdateEnvironmentArgs,\n\t): Promise<OnlyHookErrors<CallHookReturnType<ProjectEnvironmentUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tawait this._assertAdapterSupportsEnvironments();\n\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\tconst environment =\n\t\t\targs.environment === repositoryName ? undefined : args.environment;\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:environment:update\",\n\t\t\t{ environment },\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tprivate async _assertAdapterSupportsEnvironments(): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst supportsEnvironments = this.checkSupportsEnvironments();\n\n\t\tif (!supportsEnvironments) {\n\t\t\tconst adapterName = await this.project.getAdapterName();\n\n\t\t\tthrow new PluginError(\n\t\t\t\t`${adapterName} does not support environments. Use an adapter that implements the \\`project:environment:read\\` and \\`project:environment:update\\` hooks to use environments.`,\n\t\t\t);\n\t\t}\n\t}\n}\n"],"names":["BaseManager","locateFileUpward","SLICE_MACHINE_CONFIG_FILENAME","path","TS_CONFIG_FILENAME","existsSync","format","fs","error","SliceMachineError","decodeSliceMachineConfig","require","createRequire","SLICE_MACHINE_NPM_PACKAGE_NAME","assertPluginsInitialized","errors","args","niDetect","installDependencies","InternalError","decodeHookResult","t","PluginError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgFM,MAAO,uBAAuBA,YAAAA,YAAW;AAAA,EAAzC;AAAA;AACG;AACA;AACA;AAAA;AAAA,EAER,MAAM,0BACL,MAAkD;AAElD,QAAI,KAAK,iCAAiC,EAAC,6BAAM,cAAa;AAC7D,aAAO,KAAK;AAAA,IACZ;AAEG,QAAA;AACE,WAAA,gCAAgC,MAAMC,kCAC1CC,8BAAAA,+BACA,EAAE,UAAU,KAAK,KAAK;AAAA,aAEf;AACF,YA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AAGrD,UAAA,sBACL,UAAK,CAAC,MAAN,mBAAS,iBAAgB,iBACtB,UACA,UAAK,CAAC,MAAN,mBAAS;AAEN,WAAA;AAAA,MACN,aAAa;AAAA,MACb,QAAAN;AAAA,IAAA;AAAA,EAEF;AAAA,EAEA,MAAM,kBACL,MAAyC;AAEzCD,sDAAyB,KAAK,wBAAwB;AAEtD,UAAM,KAAK;AAEX,UAAM,iBAAiB,MAAM,KAAK,QAAQ,kBAAiB;AAC3D,UAAM,cACL,KAAK,gBAAgB,iBAAiB,SAAY,KAAK;AAElD,UAAA,aAAa,MAAM,KAAK,yBAAyB,SACtD,8BACA,EAAE,aAAa;AAGT,WAAA;AAAA,MACN,QAAQ,WAAW;AAAA,IAAA;AAAA,EAErB;AAAA,EAEQ,MAAM,qCAAkC;AAC/CA,sDAAyB,KAAK,wBAAwB;AAEhD,UAAA,uBAAuB,KAAK;AAElC,QAAI,CAAC,sBAAsB;AAC1B,YAAM,cAAc,MAAM,KAAK,QAAQ,eAAc;AAE/C,YAAA,IAAIQ,OAAAA,YACT,GAAG,0KAA0K;AAAA,IAE9K;AAAA,EACF;AACA;;"}
|
|
1
|
+
{"version":3,"file":"ProjectManager.cjs","sources":["../../../../src/managers/project/ProjectManager.ts"],"sourcesContent":["import * as fs from \"node:fs/promises\";\nimport { existsSync } from \"node:fs\";\nimport * as path from \"node:path\";\nimport { createRequire } from \"node:module\";\nimport { detect as niDetect } from \"@antfu/ni\";\nimport { ExecaChildProcess } from \"execa\";\nimport {\n\tHookError,\n\tCallHookReturnType,\n\tProjectEnvironmentUpdateHook,\n} from \"@slicemachine/plugin-kit\";\nimport * as t from \"io-ts\";\n\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport { decodeSliceMachineConfig } from \"../../lib/decodeSliceMachineConfig\";\nimport { format } from \"../../lib/format\";\nimport { installDependencies } from \"../../lib/installDependencies\";\nimport { locateFileUpward } from \"../../lib/locateFileUpward\";\n\nimport {\n\tPackageManager,\n\tSliceMachineConfig,\n\tOnlyHookErrors,\n} from \"../../types\";\n\nimport { SliceMachineError, InternalError, PluginError } from \"../../errors\";\n\nimport { SLICE_MACHINE_CONFIG_FILENAME } from \"../../constants/SLICE_MACHINE_CONFIG_FILENAME\";\nimport { TS_CONFIG_FILENAME } from \"../../constants/TS_CONFIG_FILENAME\";\nimport { SLICE_MACHINE_NPM_PACKAGE_NAME } from \"../../constants/SLICE_MACHINE_NPM_PACKAGE_NAME\";\n\nimport { BaseManager } from \"../BaseManager\";\n\ntype ProjectManagerGetSliceMachineConfigPathArgs = {\n\tignoreCache?: boolean;\n};\n\ntype ProjectManagerGetRootArgs = {\n\tignoreCache?: boolean;\n};\n\ntype ProjectManagerCheckIsTypeScriptArgs = {\n\trootOverride?: string;\n};\n\ntype ProjectManagerWriteSliceMachineConfigArgs = {\n\tconfig: SliceMachineConfig;\n\tpath?: string;\n};\n\ntype ProjectManagerInitProjectArgs = {\n\tlog?: (message: string) => void;\n};\n\ntype ProjectManagerDetectPackageManager = {\n\troot?: string;\n};\n\ntype ProjectManagerInstallDependenciesArgs = {\n\tdependencies: Record<string, string>;\n\tdev?: boolean;\n\tpackageManager?: PackageManager;\n\tlog?: (message: string) => void;\n};\n\ntype ProjectManagerInstallDependenciesReturnType = {\n\texecaProcess: ExecaChildProcess;\n};\n\ntype ProjectManagerReadEnvironmentReturnType = {\n\tenvironment: string | undefined;\n\terrors: (DecodeError | HookError)[];\n};\n\ntype ProjectManagerUpdateEnvironmentArgs = {\n\tenvironment: string | undefined;\n};\n\nexport class ProjectManager extends BaseManager {\n\tprivate _cachedRoot: string | undefined;\n\tprivate _cachedSliceMachineConfigPath: string | undefined;\n\tprivate _cachedSliceMachineConfig: SliceMachineConfig | undefined;\n\n\tasync getSliceMachineConfigPath(\n\t\targs?: ProjectManagerGetSliceMachineConfigPathArgs,\n\t): Promise<string> {\n\t\tif (this._cachedSliceMachineConfigPath && !args?.ignoreCache) {\n\t\t\treturn this._cachedSliceMachineConfigPath;\n\t\t}\n\n\t\ttry {\n\t\t\tthis._cachedSliceMachineConfigPath = await locateFileUpward(\n\t\t\t\tSLICE_MACHINE_CONFIG_FILENAME,\n\t\t\t\t{ startDir: this.cwd },\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tthrow new Error(\n\t\t\t\t`Could not find a ${SLICE_MACHINE_CONFIG_FILENAME} file. Please create a config file at the root of your project.`,\n\t\t\t);\n\t\t}\n\n\t\treturn this._cachedSliceMachineConfigPath;\n\t}\n\n\tasync getRoot(args?: ProjectManagerGetRootArgs): Promise<string> {\n\t\tif (this._cachedRoot && !args?.ignoreCache) {\n\t\t\treturn this._cachedRoot;\n\t\t}\n\n\t\tconst sliceMachineConfigFilePath = await this.getSliceMachineConfigPath({\n\t\t\tignoreCache: args?.ignoreCache,\n\t\t});\n\n\t\tthis._cachedRoot = path.dirname(sliceMachineConfigFilePath);\n\n\t\treturn this._cachedRoot;\n\t}\n\n\tasync suggestRoot(): Promise<string> {\n\t\tconst suggestedRootPackageJSON = await locateFileUpward(\"package.json\", {\n\t\t\tstartDir: this.cwd,\n\t\t});\n\n\t\treturn path.dirname(suggestedRootPackageJSON);\n\t}\n\n\tasync suggestSliceMachineConfigPath(): Promise<string> {\n\t\tconst suggestedRoot = await this.suggestRoot();\n\n\t\treturn path.resolve(suggestedRoot, SLICE_MACHINE_CONFIG_FILENAME);\n\t}\n\n\tasync checkIsTypeScript(\n\t\targs?: ProjectManagerCheckIsTypeScriptArgs,\n\t): Promise<boolean> {\n\t\tconst root = args?.rootOverride || (await this.getRoot());\n\t\tconst rootTSConfigPath = path.resolve(root, TS_CONFIG_FILENAME);\n\n\t\t// We just care if the file exists, we don't need access to it\n\t\treturn existsSync(rootTSConfigPath);\n\t}\n\n\tasync getSliceMachineConfig(): Promise<SliceMachineConfig> {\n\t\tif (this._cachedSliceMachineConfig) {\n\t\t\treturn this._cachedSliceMachineConfig;\n\t\t} else {\n\t\t\treturn await this.loadSliceMachineConfig();\n\t\t}\n\t}\n\n\tasync writeSliceMachineConfig(\n\t\targs: ProjectManagerWriteSliceMachineConfigArgs,\n\t): Promise<void> {\n\t\tconst configFilePath =\n\t\t\targs.path || (await this.getSliceMachineConfigPath());\n\n\t\tconst config = await format(\n\t\t\tJSON.stringify(args.config, null, 2),\n\t\t\tconfigFilePath,\n\t\t);\n\n\t\tawait fs.writeFile(configFilePath, config, \"utf-8\");\n\t\tdelete this._cachedSliceMachineConfig; // Clear config cache\n\t}\n\n\tasync loadSliceMachineConfig(): Promise<SliceMachineConfig> {\n\t\t// TODO: Reload plugins with a fresh plugin runner. Plugins may\n\t\t// have been added or removed.\n\t\tconst configFilePath = await this.getSliceMachineConfigPath();\n\n\t\tlet rawConfig: unknown | undefined;\n\t\ttry {\n\t\t\tconst contents = await fs.readFile(configFilePath, \"utf8\");\n\t\t\trawConfig = JSON.parse(contents);\n\t\t} catch (error) {\n\t\t\tif (error instanceof SyntaxError) {\n\t\t\t\tthrow new SliceMachineError(\n\t\t\t\t\t`Could not parse config file at ${configFilePath}.\\n\\nError Message: ${error.message}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Noop, more specific error is thrown after\n\t\t}\n\n\t\tif (!rawConfig) {\n\t\t\t// TODO: Write a more friendly and useful message.\n\t\t\tthrow new Error(\"No config found.\");\n\t\t}\n\n\t\tconst { value: sliceMachineConfig, error } =\n\t\t\tdecodeSliceMachineConfig(rawConfig);\n\n\t\tif (error) {\n\t\t\t// TODO: Write a more friendly and useful message.\n\t\t\tthrow new Error(`Invalid config. ${error.errors.join(\", \")}`, {\n\t\t\t\tcause: { rawConfig },\n\t\t\t});\n\t\t}\n\n\t\t// Allow cached config reading using `SliceMachineManager.prototype.getProjectConfig()`.\n\t\tthis._cachedSliceMachineConfig = sliceMachineConfig;\n\n\t\treturn sliceMachineConfig;\n\t}\n\n\tasync locateSliceMachineUIDir(): Promise<string> {\n\t\tconst projectRoot = await this.getRoot();\n\n\t\tconst require = createRequire(path.join(projectRoot, \"index.js\"));\n\t\tconst sliceMachinePackageJSONPath = require.resolve(\n\t\t\t`${SLICE_MACHINE_NPM_PACKAGE_NAME}/package.json`,\n\t\t);\n\n\t\treturn path.dirname(sliceMachinePackageJSONPath);\n\t}\n\n\t/**\n\t * Returns the project's repository name (i.e. the production environment). It\n\t * ignores the currently selected environment.\n\t *\n\t * Use this method to retrieve the production environment domain.\n\t *\n\t * @returns The project's repository name.\n\t */\n\tasync getRepositoryName(): Promise<string> {\n\t\tconst sliceMachineConfig = await this.getSliceMachineConfig();\n\n\t\treturn sliceMachineConfig.repositoryName;\n\t}\n\n\t/**\n\t * Returns the currently selected environment domain if set. If an environment\n\t * is not set, it returns the project's repository name (the production\n\t * environment).\n\t *\n\t * Use this method to retrieve the repository name to be sent with Prismic API\n\t * requests.\n\t *\n\t * @returns The resolved repository name.\n\t */\n\tasync getResolvedRepositoryName(): Promise<string> {\n\t\tconst repositoryName = await this.getRepositoryName();\n\n\t\tconst supportsEnvironments = this.project.checkSupportsEnvironments();\n\t\tif (!supportsEnvironments) {\n\t\t\treturn repositoryName;\n\t\t}\n\n\t\tconst { environment } = await this.project.readEnvironment();\n\n\t\treturn environment ?? repositoryName;\n\t}\n\n\tasync getAdapterName(): Promise<string> {\n\t\tconst sliceMachineConfig = await this.getSliceMachineConfig();\n\t\tconst adapterName =\n\t\t\ttypeof sliceMachineConfig.adapter === \"string\"\n\t\t\t\t? sliceMachineConfig.adapter\n\t\t\t\t: sliceMachineConfig.adapter.resolve;\n\n\t\treturn adapterName;\n\t}\n\n\tasync locateAdapterDir(): Promise<string> {\n\t\tconst projectRoot = await this.getRoot();\n\t\tconst adapterName = await this.getAdapterName();\n\t\tconst require = createRequire(path.join(projectRoot, \"index.js\"));\n\t\tconst adapterPackageJSONPath = require.resolve(\n\t\t\t`${adapterName}/package.json`,\n\t\t);\n\n\t\treturn path.dirname(adapterPackageJSONPath);\n\t}\n\n\tasync initProject(args?: ProjectManagerInitProjectArgs): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst log = args?.log || console.log.bind(this);\n\n\t\tconst { errors } = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:init\",\n\t\t\t{\n\t\t\t\tlog,\n\t\t\t\tinstallDependencies: async (args) => {\n\t\t\t\t\tconst { execaProcess } = await this.installDependencies({\n\t\t\t\t\t\tdependencies: args.dependencies,\n\t\t\t\t\t\tdev: args.dev,\n\t\t\t\t\t\tlog,\n\t\t\t\t\t});\n\n\t\t\t\t\tawait execaProcess;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (errors.length > 0) {\n\t\t\t// TODO: Provide better error message.\n\t\t\tthrow new SliceMachineError(\n\t\t\t\t`Failed to initialize project: ${errors.join(\", \")}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tasync detectPackageManager(\n\t\targs?: ProjectManagerDetectPackageManager,\n\t): Promise<PackageManager> {\n\t\tconst projectRoot = args?.root || (await this.getRoot());\n\n\t\tconst packageManager = await niDetect({\n\t\t\tautoInstall: true,\n\t\t\tcwd: projectRoot,\n\t\t});\n\n\t\treturn packageManager || \"npm\";\n\t}\n\n\tasync installDependencies(\n\t\targs: ProjectManagerInstallDependenciesArgs,\n\t): Promise<ProjectManagerInstallDependenciesReturnType> {\n\t\tconst packageManager =\n\t\t\targs.packageManager || (await this.detectPackageManager());\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst log = args.log || console.log.bind(this);\n\n\t\tconst wrappedLogger = (data: Buffer | string | null) => {\n\t\t\tif (data instanceof Buffer) {\n\t\t\t\tlog(data.toString());\n\t\t\t} else if (typeof data === \"string\") {\n\t\t\t\tlog(data);\n\t\t\t}\n\t\t};\n\n\t\ttry {\n\t\t\tconst { execaProcess } = await installDependencies({\n\t\t\t\tpackageManager,\n\t\t\t\tdependencies: args.dependencies,\n\t\t\t\tdev: args.dev,\n\t\t\t});\n\n\t\t\t// Don't clutter console with logs when process is non TTY (CI, etc.)\n\t\t\tif (process.stdout.isTTY || process.env.NODE_ENV === \"test\") {\n\t\t\t\texecaProcess.stdout?.on(\"data\", wrappedLogger);\n\t\t\t}\n\t\t\texecaProcess.stderr?.on(\"data\", wrappedLogger);\n\n\t\t\treturn {\n\t\t\t\texecaProcess,\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tif (\n\t\t\t\terror instanceof Error &&\n\t\t\t\t\"shortMessage\" in error &&\n\t\t\t\t\"stderr\" in error\n\t\t\t) {\n\t\t\t\tthrow new InternalError(\"Package installation failed\", {\n\t\t\t\t\tcause: error,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tcheckSupportsEnvironments(): boolean {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\treturn (\n\t\t\tthis.sliceMachinePluginRunner.hooksForType(\"project:environment:read\")\n\t\t\t\t.length > 0 &&\n\t\t\tthis.sliceMachinePluginRunner.hooksForType(\"project:environment:update\")\n\t\t\t\t.length > 0\n\t\t);\n\t}\n\n\tasync readEnvironment(): Promise<ProjectManagerReadEnvironmentReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tawait this._assertAdapterSupportsEnvironments();\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:environment:read\",\n\t\t\tundefined,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tenvironment: t.union([t.undefined, t.string]),\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\t// An undefined value is equivalent to the production environment.\n\t\t// We cast to undefined.\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\tconst environmentDomain =\n\t\t\tdata[0]?.environment === repositoryName\n\t\t\t\t? undefined\n\t\t\t\t: data[0]?.environment;\n\n\t\treturn {\n\t\t\tenvironment: environmentDomain,\n\t\t\terrors,\n\t\t};\n\t}\n\n\tasync updateEnvironment(\n\t\targs: ProjectManagerUpdateEnvironmentArgs,\n\t): Promise<OnlyHookErrors<CallHookReturnType<ProjectEnvironmentUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tawait this._assertAdapterSupportsEnvironments();\n\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\tconst environment =\n\t\t\targs.environment === repositoryName ? undefined : args.environment;\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:environment:update\",\n\t\t\t{ environment },\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tprivate async _assertAdapterSupportsEnvironments(): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst supportsEnvironments = this.checkSupportsEnvironments();\n\n\t\tif (!supportsEnvironments) {\n\t\t\tconst adapterName = await this.project.getAdapterName();\n\n\t\t\tthrow new PluginError(\n\t\t\t\t`${adapterName} does not support environments. Use an adapter that implements the \\`project:environment:read\\` and \\`project:environment:update\\` hooks to use environments.`,\n\t\t\t);\n\t\t}\n\t}\n}\n"],"names":["BaseManager","locateFileUpward","SLICE_MACHINE_CONFIG_FILENAME","path","TS_CONFIG_FILENAME","existsSync","format","fs","error","SliceMachineError","decodeSliceMachineConfig","require","createRequire","SLICE_MACHINE_NPM_PACKAGE_NAME","assertPluginsInitialized","errors","args","niDetect","installDependencies","InternalError","decodeHookResult","t","PluginError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgFM,MAAO,uBAAuBA,YAAAA,YAAW;AAAA,EAAzC;AAAA;AACG;AACA;AACA;AAAA;AAAA,EAER,MAAM,0BACL,MAAkD;AAElD,QAAI,KAAK,iCAAiC,EAAC,6BAAM,cAAa;AAC7D,aAAO,KAAK;AAAA,IACZ;AAEG,QAAA;AACE,WAAA,gCAAgC,M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,IAAA,CAC5C,GACD,UAAU;AAKX,UAAM,iBAAiB,MAAM,KAAK,QAAQ,kBAAiB;AACrD,UAAA,sBACL,UAAK,CAAC,MAAN,mBAAS,iBAAgB,iBACtB,UACA,UAAK,CAAC,MAAN,mBAAS;AAEN,WAAA;AAAA,MACN,aAAa;AAAA,MACb,QAAAN;AAAA,IAAA;AAAA,EAEF;AAAA,EAEA,MAAM,kBACL,MAAyC;AAEzCD,sDAAyB,KAAK,wBAAwB;AAEtD,UAAM,KAAK;AAEX,UAAM,iBAAiB,MAAM,KAAK,QAAQ,kBAAiB;AAC3D,UAAM,cACL,KAAK,gBAAgB,iBAAiB,SAAY,KAAK;AAElD,UAAA,aAAa,MAAM,KAAK,yBAAyB,SACtD,8BACA,EAAE,aAAa;AAGT,WAAA;AAAA,MACN,QAAQ,WAAW;AAAA,IAAA;AAAA,EAErB;AAAA,EAEQ,MAAM,qCAAkC;AAC/CA,sDAAyB,KAAK,wBAAwB;AAEhD,UAAA,uBAAuB,KAAK;AAElC,QAAI,CAAC,sBAAsB;AAC1B,YAAM,cAAc,MAAM,KAAK,QAAQ,eAAc;AAE/C,YAAA,IAAIQ,OAAAA,YACT,GAAG,0KAA0K;AAAA,IAE9K;AAAA,EACF;AACA;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ProjectManager.js","sources":["../../../../src/managers/project/ProjectManager.ts"],"sourcesContent":["import * as fs from \"node:fs/promises\";\nimport { existsSync } from \"node:fs\";\nimport * as path from \"node:path\";\nimport { createRequire } from \"node:module\";\nimport { detect as niDetect } from \"@antfu/ni\";\nimport { ExecaChildProcess } from \"execa\";\nimport {\n\tHookError,\n\tCallHookReturnType,\n\tProjectEnvironmentUpdateHook,\n} from \"@slicemachine/plugin-kit\";\nimport * as t from \"io-ts\";\n\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport { decodeSliceMachineConfig } from \"../../lib/decodeSliceMachineConfig\";\nimport { format } from \"../../lib/format\";\nimport { installDependencies } from \"../../lib/installDependencies\";\nimport { locateFileUpward } from \"../../lib/locateFileUpward\";\n\nimport {\n\tPackageManager,\n\tSliceMachineConfig,\n\tOnlyHookErrors,\n} from \"../../types\";\n\nimport { SliceMachineError, InternalError, PluginError } from \"../../errors\";\n\nimport { SLICE_MACHINE_CONFIG_FILENAME } from \"../../constants/SLICE_MACHINE_CONFIG_FILENAME\";\nimport { TS_CONFIG_FILENAME } from \"../../constants/TS_CONFIG_FILENAME\";\nimport { SLICE_MACHINE_NPM_PACKAGE_NAME } from \"../../constants/SLICE_MACHINE_NPM_PACKAGE_NAME\";\n\nimport { BaseManager } from \"../BaseManager\";\n\ntype ProjectManagerGetSliceMachineConfigPathArgs = {\n\tignoreCache?: boolean;\n};\n\ntype ProjectManagerGetRootArgs = {\n\tignoreCache?: boolean;\n};\n\ntype ProjectManagerCheckIsTypeScriptArgs = {\n\trootOverride?: string;\n};\n\ntype ProjectManagerWriteSliceMachineConfigArgs = {\n\tconfig: SliceMachineConfig;\n\tpath?: string;\n};\n\ntype ProjectManagerInitProjectArgs = {\n\tlog?: (message: string) => void;\n};\n\ntype ProjectManagerDetectPackageManager = {\n\troot?: string;\n};\n\ntype ProjectManagerInstallDependenciesArgs = {\n\tdependencies: Record<string, string>;\n\tdev?: boolean;\n\tpackageManager?: PackageManager;\n\tlog?: (message: string) => void;\n};\n\ntype ProjectManagerInstallDependenciesReturnType = {\n\texecaProcess: ExecaChildProcess;\n};\n\ntype ProjectManagerReadEnvironmentReturnType = {\n\tenvironment: string | undefined;\n\terrors: (DecodeError | HookError)[];\n};\n\ntype ProjectManagerUpdateEnvironmentArgs = {\n\tenvironment: string | undefined;\n};\n\nexport class ProjectManager extends BaseManager {\n\tprivate _cachedRoot: string | undefined;\n\tprivate _cachedSliceMachineConfigPath: string | undefined;\n\tprivate _cachedSliceMachineConfig: SliceMachineConfig | undefined;\n\n\tasync getSliceMachineConfigPath(\n\t\targs?: ProjectManagerGetSliceMachineConfigPathArgs,\n\t): Promise<string> {\n\t\tif (this._cachedSliceMachineConfigPath && !args?.ignoreCache) {\n\t\t\treturn this._cachedSliceMachineConfigPath;\n\t\t}\n\n\t\ttry {\n\t\t\tthis._cachedSliceMachineConfigPath = await locateFileUpward(\n\t\t\t\tSLICE_MACHINE_CONFIG_FILENAME,\n\t\t\t\t{ startDir: this.cwd },\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tthrow new Error(\n\t\t\t\t`Could not find a ${SLICE_MACHINE_CONFIG_FILENAME} file. Please create a config file at the root of your project.`,\n\t\t\t);\n\t\t}\n\n\t\treturn this._cachedSliceMachineConfigPath;\n\t}\n\n\tasync getRoot(args?: ProjectManagerGetRootArgs): Promise<string> {\n\t\tif (this._cachedRoot && !args?.ignoreCache) {\n\t\t\treturn this._cachedRoot;\n\t\t}\n\n\t\tconst sliceMachineConfigFilePath = await this.getSliceMachineConfigPath({\n\t\t\tignoreCache: args?.ignoreCache,\n\t\t});\n\n\t\tthis._cachedRoot = path.dirname(sliceMachineConfigFilePath);\n\n\t\treturn this._cachedRoot;\n\t}\n\n\tasync suggestRoot(): Promise<string> {\n\t\tconst suggestedRootPackageJSON = await locateFileUpward(\"package.json\", {\n\t\t\tstartDir: this.cwd,\n\t\t});\n\n\t\treturn path.dirname(suggestedRootPackageJSON);\n\t}\n\n\tasync suggestSliceMachineConfigPath(): Promise<string> {\n\t\tconst suggestedRoot = await this.suggestRoot();\n\n\t\treturn path.resolve(suggestedRoot, SLICE_MACHINE_CONFIG_FILENAME);\n\t}\n\n\tasync checkIsTypeScript(\n\t\targs?: ProjectManagerCheckIsTypeScriptArgs,\n\t): Promise<boolean> {\n\t\tconst root = args?.rootOverride || (await this.getRoot());\n\t\tconst rootTSConfigPath = path.resolve(root, TS_CONFIG_FILENAME);\n\n\t\t// We just care if the file exists, we don't need access to it\n\t\treturn existsSync(rootTSConfigPath);\n\t}\n\n\tasync getSliceMachineConfig(): Promise<SliceMachineConfig> {\n\t\tif (this._cachedSliceMachineConfig) {\n\t\t\treturn this._cachedSliceMachineConfig;\n\t\t} else {\n\t\t\treturn await this.loadSliceMachineConfig();\n\t\t}\n\t}\n\n\tasync writeSliceMachineConfig(\n\t\targs: ProjectManagerWriteSliceMachineConfigArgs,\n\t): Promise<void> {\n\t\tconst configFilePath =\n\t\t\targs.path || (await this.getSliceMachineConfigPath());\n\n\t\tconst config = await format(\n\t\t\tJSON.stringify(args.config, null, 2),\n\t\t\tconfigFilePath,\n\t\t);\n\n\t\tawait fs.writeFile(configFilePath, config, \"utf-8\");\n\t\tdelete this._cachedSliceMachineConfig; // Clear config cache\n\t}\n\n\tasync loadSliceMachineConfig(): Promise<SliceMachineConfig> {\n\t\t// TODO: Reload plugins with a fresh plugin runner. Plugins may\n\t\t// have been added or removed.\n\t\tconst configFilePath = await this.getSliceMachineConfigPath();\n\n\t\tlet rawConfig: unknown | undefined;\n\t\ttry {\n\t\t\tconst contents = await fs.readFile(configFilePath, \"utf8\");\n\t\t\trawConfig = JSON.parse(contents);\n\t\t} catch (error) {\n\t\t\tif (error instanceof SyntaxError) {\n\t\t\t\tthrow new SliceMachineError(\n\t\t\t\t\t`Could not parse config file at ${configFilePath}.\\n\\nError Message: ${error.message}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Noop, more specific error is thrown after\n\t\t}\n\n\t\tif (!rawConfig) {\n\t\t\t// TODO: Write a more friendly and useful message.\n\t\t\tthrow new Error(\"No config found.\");\n\t\t}\n\n\t\tconst { value: sliceMachineConfig, error } =\n\t\t\tdecodeSliceMachineConfig(rawConfig);\n\n\t\tif (error) {\n\t\t\t// TODO: Write a more friendly and useful message.\n\t\t\tthrow new Error(`Invalid config. ${error.errors.join(\", \")}`, {\n\t\t\t\tcause: { rawConfig },\n\t\t\t});\n\t\t}\n\n\t\t// Allow cached config reading using `SliceMachineManager.prototype.getProjectConfig()`.\n\t\tthis._cachedSliceMachineConfig = sliceMachineConfig;\n\n\t\treturn sliceMachineConfig;\n\t}\n\n\tasync locateSliceMachineUIDir(): Promise<string> {\n\t\tconst projectRoot = await this.getRoot();\n\n\t\tconst require = createRequire(path.join(projectRoot, \"index.js\"));\n\t\tconst sliceMachinePackageJSONPath = require.resolve(\n\t\t\t`${SLICE_MACHINE_NPM_PACKAGE_NAME}/package.json`,\n\t\t);\n\n\t\treturn path.dirname(sliceMachinePackageJSONPath);\n\t}\n\n\t/**\n\t * Returns the project's repository name (i.e. the production environment). It\n\t * ignores the currently selected environment.\n\t *\n\t * Use this method to retrieve the production environment domain.\n\t *\n\t * @returns The project's repository name.\n\t */\n\tasync getRepositoryName(): Promise<string> {\n\t\tconst sliceMachineConfig = await this.getSliceMachineConfig();\n\n\t\treturn sliceMachineConfig.repositoryName;\n\t}\n\n\t/**\n\t * Returns the currently selected environment domain if set. If an environment\n\t * is not set, it returns the project's repository name (the production\n\t * environment).\n\t *\n\t * Use this method to retrieve the repository name to be sent with Prismic API\n\t * requests.\n\t *\n\t * @returns The resolved repository name.\n\t */\n\tasync getResolvedRepositoryName(): Promise<string> {\n\t\tconst repositoryName = await this.getRepositoryName();\n\n\t\tconst supportsEnvironments = this.project.checkSupportsEnvironments();\n\t\tif (!supportsEnvironments) {\n\t\t\treturn repositoryName;\n\t\t}\n\n\t\tconst { environment } = await this.project.readEnvironment();\n\n\t\treturn environment ?? repositoryName;\n\t}\n\n\tasync getAdapterName(): Promise<string> {\n\t\tconst sliceMachineConfig = await this.getSliceMachineConfig();\n\t\tconst adapterName =\n\t\t\ttypeof sliceMachineConfig.adapter === \"string\"\n\t\t\t\t? sliceMachineConfig.adapter\n\t\t\t\t: sliceMachineConfig.adapter.resolve;\n\n\t\treturn adapterName;\n\t}\n\n\tasync locateAdapterDir(): Promise<string> {\n\t\tconst projectRoot = await this.getRoot();\n\t\tconst adapterName = await this.getAdapterName();\n\t\tconst require = createRequire(path.join(projectRoot, \"index.js\"));\n\t\tconst adapterPackageJSONPath = require.resolve(\n\t\t\t`${adapterName}/package.json`,\n\t\t);\n\n\t\treturn path.dirname(adapterPackageJSONPath);\n\t}\n\n\tasync initProject(args?: ProjectManagerInitProjectArgs): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst log = args?.log || console.log.bind(this);\n\n\t\tconst { errors } = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:init\",\n\t\t\t{\n\t\t\t\tlog,\n\t\t\t\tinstallDependencies: async (args) => {\n\t\t\t\t\tconst { execaProcess } = await this.installDependencies({\n\t\t\t\t\t\tdependencies: args.dependencies,\n\t\t\t\t\t\tdev: args.dev,\n\t\t\t\t\t\tlog,\n\t\t\t\t\t});\n\n\t\t\t\t\tawait execaProcess;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (errors.length > 0) {\n\t\t\t// TODO: Provide better error message.\n\t\t\tthrow new SliceMachineError(\n\t\t\t\t`Failed to initialize project: ${errors.join(\", \")}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tasync detectPackageManager(\n\t\targs?: ProjectManagerDetectPackageManager,\n\t): Promise<PackageManager> {\n\t\tconst projectRoot = args?.root || (await this.getRoot());\n\n\t\tconst packageManager = await niDetect({\n\t\t\tautoInstall: true,\n\t\t\tcwd: projectRoot,\n\t\t});\n\n\t\treturn packageManager || \"npm\";\n\t}\n\n\tasync installDependencies(\n\t\targs: ProjectManagerInstallDependenciesArgs,\n\t): Promise<ProjectManagerInstallDependenciesReturnType> {\n\t\tconst packageManager =\n\t\t\targs.packageManager || (await this.detectPackageManager());\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst log = args.log || console.log.bind(this);\n\n\t\tconst wrappedLogger = (data: Buffer | string | null) => {\n\t\t\tif (data instanceof Buffer) {\n\t\t\t\tlog(data.toString());\n\t\t\t} else if (typeof data === \"string\") {\n\t\t\t\tlog(data);\n\t\t\t}\n\t\t};\n\n\t\ttry {\n\t\t\tconst { execaProcess } = await installDependencies({\n\t\t\t\tpackageManager,\n\t\t\t\tdependencies: args.dependencies,\n\t\t\t\tdev: args.dev,\n\t\t\t});\n\n\t\t\t// Don't clutter console with logs when process is non TTY (CI, etc.)\n\t\t\tif (process.stdout.isTTY || process.env.NODE_ENV === \"test\") {\n\t\t\t\texecaProcess.stdout?.on(\"data\", wrappedLogger);\n\t\t\t}\n\t\t\texecaProcess.stderr?.on(\"data\", wrappedLogger);\n\n\t\t\treturn {\n\t\t\t\texecaProcess,\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tif (\n\t\t\t\terror instanceof Error &&\n\t\t\t\t\"shortMessage\" in error &&\n\t\t\t\t\"stderr\" in error\n\t\t\t) {\n\t\t\t\tthrow new InternalError(\"Package installation failed\", {\n\t\t\t\t\tcause: error,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tcheckSupportsEnvironments(): boolean {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\treturn (\n\t\t\tthis.sliceMachinePluginRunner.hooksForType(\"project:environment:read\")\n\t\t\t\t.length > 0 &&\n\t\t\tthis.sliceMachinePluginRunner.hooksForType(\"project:environment:update\")\n\t\t\t\t.length > 0\n\t\t);\n\t}\n\n\tasync readEnvironment(): Promise<ProjectManagerReadEnvironmentReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tawait this._assertAdapterSupportsEnvironments();\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:environment:read\",\n\t\t\tundefined,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tenvironment: t.union([t.undefined, t.string]),\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\n\t\t// An undefined value is equivalent to the production environment.\n\t\tconst environmentDomain =\n\t\t\tdata[0]?.environment === repositoryName\n\t\t\t\t? undefined\n\t\t\t\t: data[0]?.environment;\n\n\t\treturn {\n\t\t\tenvironment: environmentDomain,\n\t\t\terrors,\n\t\t};\n\t}\n\n\tasync updateEnvironment(\n\t\targs: ProjectManagerUpdateEnvironmentArgs,\n\t): Promise<OnlyHookErrors<CallHookReturnType<ProjectEnvironmentUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tawait this._assertAdapterSupportsEnvironments();\n\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\tconst environment =\n\t\t\targs.environment === repositoryName ? undefined : args.environment;\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:environment:update\",\n\t\t\t{ environment },\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tprivate async _assertAdapterSupportsEnvironments(): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst supportsEnvironments = this.checkSupportsEnvironments();\n\n\t\tif (!supportsEnvironments) {\n\t\t\tconst adapterName = await this.project.getAdapterName();\n\n\t\t\tthrow new PluginError(\n\t\t\t\t`${adapterName} does not support environments. Use an adapter that implements the \\`project:environment:read\\` and \\`project:environment:update\\` hooks to use environments.`,\n\t\t\t);\n\t\t}\n\t}\n}\n"],"names":["error","require","args","niDetect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAgFM,MAAO,uBAAuB,YAAW;AAAA,EAAzC;AAAA;AACG;AACA;AACA;AAAA;AAAA,EAER,MAAM,0BACL,MAAkD;AAElD,QAAI,KAAK,iCAAiC,EAAC,6BAAM,cAAa;AAC7D,aAAO,KAAK;AAAA,IACZ;AAEG,QAAA;AACE,WAAA,gCAAgC,MAAM,iBAC1C,+BACA,EAAE,UAAU,KAAK,KAAK;AAAA,aAEf;AACF,YAAA,IAAI,MACT,oBAAoB,8FAA8F;AAAA,IAEnH;AAED,WAAO,KAAK;AAAA,EACb;AAAA,EAEA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;AAEX,UAAM,iBAAiB,MAAM,KAAK,QAAQ,kBAAiB;AAC3D,UAAM,cACL,KAAK,gBAAgB,iBAAiB,SAAY,KAAK;AAElD,UAAA,aAAa,MAAM,KAAK,yBAAyB,SACtD,8BACA,EAAE,aAAa;AAGT,WAAA;AAAA,MACN,QAAQ,WAAW;AAAA,IAAA;AAAA,EAErB;AAAA,EAEQ,MAAM,qCAAkC;AAC/C,6BAAyB,KAAK,wBAAwB;AAEhD,UAAA,uBAAuB,KAAK;AAElC,QAAI,CAAC,sBAAsB;AAC1B,YAAM,cAAc,MAAM,KAAK,QAAQ,eAAc;AAE/C,YAAA,IAAI,YACT,GAAG,0KAA0K;AAAA,IAE9K;AAAA,EACF;AACA;"}
|
|
1
|
+
{"version":3,"file":"ProjectManager.js","sources":["../../../../src/managers/project/ProjectManager.ts"],"sourcesContent":["import * as fs from \"node:fs/promises\";\nimport { existsSync } from \"node:fs\";\nimport * as path from \"node:path\";\nimport { createRequire } from \"node:module\";\nimport { detect as niDetect } from \"@antfu/ni\";\nimport { ExecaChildProcess } from \"execa\";\nimport {\n\tHookError,\n\tCallHookReturnType,\n\tProjectEnvironmentUpdateHook,\n} from \"@slicemachine/plugin-kit\";\nimport * as t from \"io-ts\";\n\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport { decodeSliceMachineConfig } from \"../../lib/decodeSliceMachineConfig\";\nimport { format } from \"../../lib/format\";\nimport { installDependencies } from \"../../lib/installDependencies\";\nimport { locateFileUpward } from \"../../lib/locateFileUpward\";\n\nimport {\n\tPackageManager,\n\tSliceMachineConfig,\n\tOnlyHookErrors,\n} from \"../../types\";\n\nimport { SliceMachineError, InternalError, PluginError } from \"../../errors\";\n\nimport { SLICE_MACHINE_CONFIG_FILENAME } from \"../../constants/SLICE_MACHINE_CONFIG_FILENAME\";\nimport { TS_CONFIG_FILENAME } from \"../../constants/TS_CONFIG_FILENAME\";\nimport { SLICE_MACHINE_NPM_PACKAGE_NAME } from \"../../constants/SLICE_MACHINE_NPM_PACKAGE_NAME\";\n\nimport { BaseManager } from \"../BaseManager\";\n\ntype ProjectManagerGetSliceMachineConfigPathArgs = {\n\tignoreCache?: boolean;\n};\n\ntype ProjectManagerGetRootArgs = {\n\tignoreCache?: boolean;\n};\n\ntype ProjectManagerCheckIsTypeScriptArgs = {\n\trootOverride?: string;\n};\n\ntype ProjectManagerWriteSliceMachineConfigArgs = {\n\tconfig: SliceMachineConfig;\n\tpath?: string;\n};\n\ntype ProjectManagerInitProjectArgs = {\n\tlog?: (message: string) => void;\n};\n\ntype ProjectManagerDetectPackageManager = {\n\troot?: string;\n};\n\ntype ProjectManagerInstallDependenciesArgs = {\n\tdependencies: Record<string, string>;\n\tdev?: boolean;\n\tpackageManager?: PackageManager;\n\tlog?: (message: string) => void;\n};\n\ntype ProjectManagerInstallDependenciesReturnType = {\n\texecaProcess: ExecaChildProcess;\n};\n\ntype ProjectManagerReadEnvironmentReturnType = {\n\tenvironment: string | undefined;\n\terrors: (DecodeError | HookError)[];\n};\n\ntype ProjectManagerUpdateEnvironmentArgs = {\n\tenvironment: string | undefined;\n};\n\nexport class ProjectManager extends BaseManager {\n\tprivate _cachedRoot: string | undefined;\n\tprivate _cachedSliceMachineConfigPath: string | undefined;\n\tprivate _cachedSliceMachineConfig: SliceMachineConfig | undefined;\n\n\tasync getSliceMachineConfigPath(\n\t\targs?: ProjectManagerGetSliceMachineConfigPathArgs,\n\t): Promise<string> {\n\t\tif (this._cachedSliceMachineConfigPath && !args?.ignoreCache) {\n\t\t\treturn this._cachedSliceMachineConfigPath;\n\t\t}\n\n\t\ttry {\n\t\t\tthis._cachedSliceMachineConfigPath = await locateFileUpward(\n\t\t\t\tSLICE_MACHINE_CONFIG_FILENAME,\n\t\t\t\t{ startDir: this.cwd },\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tthrow new Error(\n\t\t\t\t`Could not find a ${SLICE_MACHINE_CONFIG_FILENAME} file. Please create a config file at the root of your project.`,\n\t\t\t);\n\t\t}\n\n\t\treturn this._cachedSliceMachineConfigPath;\n\t}\n\n\tasync getRoot(args?: ProjectManagerGetRootArgs): Promise<string> {\n\t\tif (this._cachedRoot && !args?.ignoreCache) {\n\t\t\treturn this._cachedRoot;\n\t\t}\n\n\t\tconst sliceMachineConfigFilePath = await this.getSliceMachineConfigPath({\n\t\t\tignoreCache: args?.ignoreCache,\n\t\t});\n\n\t\tthis._cachedRoot = path.dirname(sliceMachineConfigFilePath);\n\n\t\treturn this._cachedRoot;\n\t}\n\n\tasync suggestRoot(): Promise<string> {\n\t\tconst suggestedRootPackageJSON = await locateFileUpward(\"package.json\", {\n\t\t\tstartDir: this.cwd,\n\t\t});\n\n\t\treturn path.dirname(suggestedRootPackageJSON);\n\t}\n\n\tasync suggestSliceMachineConfigPath(): Promise<string> {\n\t\tconst suggestedRoot = await this.suggestRoot();\n\n\t\treturn path.resolve(suggestedRoot, SLICE_MACHINE_CONFIG_FILENAME);\n\t}\n\n\tasync checkIsTypeScript(\n\t\targs?: ProjectManagerCheckIsTypeScriptArgs,\n\t): Promise<boolean> {\n\t\tconst root = args?.rootOverride || (await this.getRoot());\n\t\tconst rootTSConfigPath = path.resolve(root, TS_CONFIG_FILENAME);\n\n\t\t// We just care if the file exists, we don't need access to it\n\t\treturn existsSync(rootTSConfigPath);\n\t}\n\n\tasync getSliceMachineConfig(): Promise<SliceMachineConfig> {\n\t\tif (this._cachedSliceMachineConfig) {\n\t\t\treturn this._cachedSliceMachineConfig;\n\t\t} else {\n\t\t\treturn await this.loadSliceMachineConfig();\n\t\t}\n\t}\n\n\tasync writeSliceMachineConfig(\n\t\targs: ProjectManagerWriteSliceMachineConfigArgs,\n\t): Promise<void> {\n\t\tconst configFilePath =\n\t\t\targs.path || (await this.getSliceMachineConfigPath());\n\n\t\tconst config = await format(\n\t\t\tJSON.stringify(args.config, null, 2),\n\t\t\tconfigFilePath,\n\t\t);\n\n\t\tawait fs.writeFile(configFilePath, config, \"utf-8\");\n\t\tdelete this._cachedSliceMachineConfig; // Clear config cache\n\t}\n\n\tasync loadSliceMachineConfig(): Promise<SliceMachineConfig> {\n\t\t// TODO: Reload plugins with a fresh plugin runner. Plugins may\n\t\t// have been added or removed.\n\t\tconst configFilePath = await this.getSliceMachineConfigPath();\n\n\t\tlet rawConfig: unknown | undefined;\n\t\ttry {\n\t\t\tconst contents = await fs.readFile(configFilePath, \"utf8\");\n\t\t\trawConfig = JSON.parse(contents);\n\t\t} catch (error) {\n\t\t\tif (error instanceof SyntaxError) {\n\t\t\t\tthrow new SliceMachineError(\n\t\t\t\t\t`Could not parse config file at ${configFilePath}.\\n\\nError Message: ${error.message}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\t// Noop, more specific error is thrown after\n\t\t}\n\n\t\tif (!rawConfig) {\n\t\t\t// TODO: Write a more friendly and useful message.\n\t\t\tthrow new Error(\"No config found.\");\n\t\t}\n\n\t\tconst { value: sliceMachineConfig, error } =\n\t\t\tdecodeSliceMachineConfig(rawConfig);\n\n\t\tif (error) {\n\t\t\t// TODO: Write a more friendly and useful message.\n\t\t\tthrow new Error(`Invalid config. ${error.errors.join(\", \")}`, {\n\t\t\t\tcause: { rawConfig },\n\t\t\t});\n\t\t}\n\n\t\t// Allow cached config reading using `SliceMachineManager.prototype.getProjectConfig()`.\n\t\tthis._cachedSliceMachineConfig = sliceMachineConfig;\n\n\t\treturn sliceMachineConfig;\n\t}\n\n\tasync locateSliceMachineUIDir(): Promise<string> {\n\t\tconst projectRoot = await this.getRoot();\n\n\t\tconst require = createRequire(path.join(projectRoot, \"index.js\"));\n\t\tconst sliceMachinePackageJSONPath = require.resolve(\n\t\t\t`${SLICE_MACHINE_NPM_PACKAGE_NAME}/package.json`,\n\t\t);\n\n\t\treturn path.dirname(sliceMachinePackageJSONPath);\n\t}\n\n\t/**\n\t * Returns the project's repository name (i.e. the production environment). It\n\t * ignores the currently selected environment.\n\t *\n\t * Use this method to retrieve the production environment domain.\n\t *\n\t * @returns The project's repository name.\n\t */\n\tasync getRepositoryName(): Promise<string> {\n\t\tconst sliceMachineConfig = await this.getSliceMachineConfig();\n\n\t\treturn sliceMachineConfig.repositoryName;\n\t}\n\n\t/**\n\t * Returns the currently selected environment domain if set. If an environment\n\t * is not set, it returns the project's repository name (the production\n\t * environment).\n\t *\n\t * Use this method to retrieve the repository name to be sent with Prismic API\n\t * requests.\n\t *\n\t * @returns The resolved repository name.\n\t */\n\tasync getResolvedRepositoryName(): Promise<string> {\n\t\tconst repositoryName = await this.getRepositoryName();\n\n\t\tconst supportsEnvironments = this.project.checkSupportsEnvironments();\n\t\tif (!supportsEnvironments) {\n\t\t\treturn repositoryName;\n\t\t}\n\n\t\tconst { environment } = await this.project.readEnvironment();\n\n\t\treturn environment ?? repositoryName;\n\t}\n\n\tasync getAdapterName(): Promise<string> {\n\t\tconst sliceMachineConfig = await this.getSliceMachineConfig();\n\t\tconst adapterName =\n\t\t\ttypeof sliceMachineConfig.adapter === \"string\"\n\t\t\t\t? sliceMachineConfig.adapter\n\t\t\t\t: sliceMachineConfig.adapter.resolve;\n\n\t\treturn adapterName;\n\t}\n\n\tasync locateAdapterDir(): Promise<string> {\n\t\tconst projectRoot = await this.getRoot();\n\t\tconst adapterName = await this.getAdapterName();\n\t\tconst require = createRequire(path.join(projectRoot, \"index.js\"));\n\t\tconst adapterPackageJSONPath = require.resolve(\n\t\t\t`${adapterName}/package.json`,\n\t\t);\n\n\t\treturn path.dirname(adapterPackageJSONPath);\n\t}\n\n\tasync initProject(args?: ProjectManagerInitProjectArgs): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst log = args?.log || console.log.bind(this);\n\n\t\tconst { errors } = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:init\",\n\t\t\t{\n\t\t\t\tlog,\n\t\t\t\tinstallDependencies: async (args) => {\n\t\t\t\t\tconst { execaProcess } = await this.installDependencies({\n\t\t\t\t\t\tdependencies: args.dependencies,\n\t\t\t\t\t\tdev: args.dev,\n\t\t\t\t\t\tlog,\n\t\t\t\t\t});\n\n\t\t\t\t\tawait execaProcess;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (errors.length > 0) {\n\t\t\t// TODO: Provide better error message.\n\t\t\tthrow new SliceMachineError(\n\t\t\t\t`Failed to initialize project: ${errors.join(\", \")}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tasync detectPackageManager(\n\t\targs?: ProjectManagerDetectPackageManager,\n\t): Promise<PackageManager> {\n\t\tconst projectRoot = args?.root || (await this.getRoot());\n\n\t\tconst packageManager = await niDetect({\n\t\t\tautoInstall: true,\n\t\t\tcwd: projectRoot,\n\t\t});\n\n\t\treturn packageManager || \"npm\";\n\t}\n\n\tasync installDependencies(\n\t\targs: ProjectManagerInstallDependenciesArgs,\n\t): Promise<ProjectManagerInstallDependenciesReturnType> {\n\t\tconst packageManager =\n\t\t\targs.packageManager || (await this.detectPackageManager());\n\n\t\t// eslint-disable-next-line no-console\n\t\tconst log = args.log || console.log.bind(this);\n\n\t\tconst wrappedLogger = (data: Buffer | string | null) => {\n\t\t\tif (data instanceof Buffer) {\n\t\t\t\tlog(data.toString());\n\t\t\t} else if (typeof data === \"string\") {\n\t\t\t\tlog(data);\n\t\t\t}\n\t\t};\n\n\t\ttry {\n\t\t\tconst { execaProcess } = await installDependencies({\n\t\t\t\tpackageManager,\n\t\t\t\tdependencies: args.dependencies,\n\t\t\t\tdev: args.dev,\n\t\t\t});\n\n\t\t\t// Don't clutter console with logs when process is non TTY (CI, etc.)\n\t\t\tif (process.stdout.isTTY || process.env.NODE_ENV === \"test\") {\n\t\t\t\texecaProcess.stdout?.on(\"data\", wrappedLogger);\n\t\t\t}\n\t\t\texecaProcess.stderr?.on(\"data\", wrappedLogger);\n\n\t\t\treturn {\n\t\t\t\texecaProcess,\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tif (\n\t\t\t\terror instanceof Error &&\n\t\t\t\t\"shortMessage\" in error &&\n\t\t\t\t\"stderr\" in error\n\t\t\t) {\n\t\t\t\tthrow new InternalError(\"Package installation failed\", {\n\t\t\t\t\tcause: error,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tcheckSupportsEnvironments(): boolean {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\treturn (\n\t\t\tthis.sliceMachinePluginRunner.hooksForType(\"project:environment:read\")\n\t\t\t\t.length > 0 &&\n\t\t\tthis.sliceMachinePluginRunner.hooksForType(\"project:environment:update\")\n\t\t\t\t.length > 0\n\t\t);\n\t}\n\n\tasync readEnvironment(): Promise<ProjectManagerReadEnvironmentReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tawait this._assertAdapterSupportsEnvironments();\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:environment:read\",\n\t\t\tundefined,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tenvironment: t.union([t.undefined, t.string]),\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\t// An undefined value is equivalent to the production environment.\n\t\t// We cast to undefined.\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\tconst environmentDomain =\n\t\t\tdata[0]?.environment === repositoryName\n\t\t\t\t? undefined\n\t\t\t\t: data[0]?.environment;\n\n\t\treturn {\n\t\t\tenvironment: environmentDomain,\n\t\t\terrors,\n\t\t};\n\t}\n\n\tasync updateEnvironment(\n\t\targs: ProjectManagerUpdateEnvironmentArgs,\n\t): Promise<OnlyHookErrors<CallHookReturnType<ProjectEnvironmentUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tawait this._assertAdapterSupportsEnvironments();\n\n\t\tconst repositoryName = await this.project.getRepositoryName();\n\t\tconst environment =\n\t\t\targs.environment === repositoryName ? undefined : args.environment;\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"project:environment:update\",\n\t\t\t{ environment },\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tprivate async _assertAdapterSupportsEnvironments(): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst supportsEnvironments = this.checkSupportsEnvironments();\n\n\t\tif (!supportsEnvironments) {\n\t\t\tconst adapterName = await this.project.getAdapterName();\n\n\t\t\tthrow new PluginError(\n\t\t\t\t`${adapterName} does not support environments. Use an adapter that implements the \\`project:environment:read\\` and \\`project:environment:update\\` hooks to use environments.`,\n\t\t\t);\n\t\t}\n\t}\n}\n"],"names":["error","require","args","niDetect"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAgFM,MAAO,uBAAuB,YAAW;AAAA,EAAzC;AAAA;AACG;AACA;AACA;AAAA;AAAA,EAER,MAAM,0BACL,MAAkD;AAElD,QAAI,KAAK,iCAAiC,EAAC,6BAAM,cAAa;AAC7D,aAAO,KAAK;AAAA,IACZ;AAEG,QAAA;AACE,WAAA,gCAAgC,MAAM,iBAC1C,+BACA,EAAE,UAAU,KAAK,KAAK;AAAA,aAEf;AACF,YAAA,IAAI,MACT,oBAAoB,8FAA8F;AAAA,IAEnH;AAED,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,MAAM,QAAQ,MAAgC;AAC7C,QAAI,KAAK,eAAe,EAAC,6BAAM,cAAa;AAC3C,aAAO,KAAK;AAAA,IACZ;AAEK,UAAA,6BAA6B,MAAM,KAAK,0BAA0B;AAAA,MACvE,aAAa,6BAAM;AAAA,IAAA,CACnB;AAEI,SAAA,cAAc,KAAK,QAAQ,0BAA0B;AAE1D,WAAO,KAAK;AAAA,EACb;AAAA,EAEA,MAAM,cAAW;AACV,UAAA,2BAA2B,MAAM,iBAAiB,gBAAgB;AAAA,MACvE,UA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sB;AAC1B,YAAM,cAAc,MAAM,KAAK,QAAQ,eAAc;AAE/C,YAAA,IAAI,YACT,GAAG,0KAA0K;AAAA,IAE9K;AAAA,EACF;AACA;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slicemachine/manager",
|
|
3
|
-
"version": "0.15.4-dev-environments.
|
|
3
|
+
"version": "0.15.4-dev-environments.2",
|
|
4
4
|
"description": "Manage all aspects of a Slice Machine project.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"@prismicio/custom-types-client": "1.2.0-alpha.0",
|
|
69
69
|
"@prismicio/mocks": "2.0.0",
|
|
70
70
|
"@prismicio/types-internal": "^2.2.0",
|
|
71
|
-
"@slicemachine/plugin-kit": "^0.4.25-dev-environments.
|
|
71
|
+
"@slicemachine/plugin-kit": "^0.4.25-dev-environments.2",
|
|
72
72
|
"@wooorm/starry-night": "^1.6.0",
|
|
73
73
|
"analytics-node": "^6.2.0",
|
|
74
74
|
"cookie": "^0.5.0",
|
|
@@ -134,5 +134,5 @@
|
|
|
134
134
|
"engines": {
|
|
135
135
|
"node": ">=14.15.0"
|
|
136
136
|
},
|
|
137
|
-
"gitHead": "
|
|
137
|
+
"gitHead": "850b2b6cfb27e55c232302f6ba4f0fbc2f17d56f"
|
|
138
138
|
}
|
package/src/client/index.ts
CHANGED
|
@@ -9,7 +9,7 @@ export type APIEndpoints = {
|
|
|
9
9
|
AwsAclProvider: string;
|
|
10
10
|
PrismicOembed: string;
|
|
11
11
|
PrismicUnsplash: string;
|
|
12
|
-
|
|
12
|
+
SliceMachineV1: string;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
export const API_ENDPOINTS: APIEndpoints = (() => {
|
|
@@ -29,7 +29,7 @@ export const API_ENDPOINTS: APIEndpoints = (() => {
|
|
|
29
29
|
PrismicUnsplash: removeTrailingSlash(
|
|
30
30
|
process.env.unsplash_endpoint ?? "https://unsplash.wroom.io",
|
|
31
31
|
),
|
|
32
|
-
|
|
32
|
+
SliceMachineV1: addTrailingSlash(process.env.slice_machine_v1_endpoint),
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
const missingAPIEndpoints = Object.keys(apiEndpoints).filter((key) => {
|
|
@@ -59,8 +59,8 @@ export const API_ENDPOINTS: APIEndpoints = (() => {
|
|
|
59
59
|
"https://2iamcvnxf4.execute-api.us-east-1.amazonaws.com/stage/",
|
|
60
60
|
PrismicOembed: "https://oembed.wroom.io",
|
|
61
61
|
PrismicUnsplash: "https://unsplash.wroom.io",
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
SliceMachineV1:
|
|
63
|
+
"https://mc5qopc07a.execute-api.us-east-1.amazonaws.com/v1/",
|
|
64
64
|
};
|
|
65
65
|
}
|
|
66
66
|
|
|
@@ -75,8 +75,8 @@ export const API_ENDPOINTS: APIEndpoints = (() => {
|
|
|
75
75
|
"https://0yyeb2g040.execute-api.us-east-1.amazonaws.com/prod/",
|
|
76
76
|
PrismicOembed: "https://oembed.prismic.io",
|
|
77
77
|
PrismicUnsplash: "https://unsplash.prismic.io",
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
SliceMachineV1:
|
|
79
|
+
"https://21vvgrh0s6.execute-api.us-east-1.amazonaws.com/v1/",
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
82
|
}
|
package/src/errors.ts
CHANGED
|
@@ -79,3 +79,7 @@ export const isUnexpectedDataError = (
|
|
|
79
79
|
export const isInternalError = (error: unknown): error is InternalError => {
|
|
80
80
|
return isSliceMachineError(error) && error.name === InternalError.name;
|
|
81
81
|
};
|
|
82
|
+
|
|
83
|
+
export const isPluginError = (error: unknown): error is PluginError => {
|
|
84
|
+
return isSliceMachineError(error) && error.name === PluginError.name;
|
|
85
|
+
};
|
|
@@ -10,7 +10,11 @@ import { SLICE_MACHINE_USER_AGENT } from "../../constants/SLICE_MACHINE_USER_AGE
|
|
|
10
10
|
import { API_ENDPOINTS } from "../../constants/API_ENDPOINTS";
|
|
11
11
|
import { REPOSITORY_NAME_VALIDATION } from "../../constants/REPOSITORY_NAME_VALIDATION";
|
|
12
12
|
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
UnauthenticatedError,
|
|
15
|
+
UnauthorizedError,
|
|
16
|
+
UnexpectedDataError,
|
|
17
|
+
} from "../../errors";
|
|
14
18
|
|
|
15
19
|
import { BaseManager } from "../BaseManager";
|
|
16
20
|
|
|
@@ -32,6 +36,7 @@ import {
|
|
|
32
36
|
StarterId,
|
|
33
37
|
Environment,
|
|
34
38
|
} from "./types";
|
|
39
|
+
import { sortEnvironments } from "./sortEnvironments";
|
|
35
40
|
|
|
36
41
|
const DEFAULT_REPOSITORY_SETTINGS = {
|
|
37
42
|
plan: "personal",
|
|
@@ -431,24 +436,38 @@ export class PrismicRepositoryManager extends BaseManager {
|
|
|
431
436
|
async fetchEnvironments(): Promise<Environment[]> {
|
|
432
437
|
const repositoryName = await this.project.getRepositoryName();
|
|
433
438
|
|
|
434
|
-
const url = new URL(
|
|
435
|
-
|
|
439
|
+
const url = new URL(`./environments`, API_ENDPOINTS.SliceMachineV1);
|
|
440
|
+
url.searchParams.set("repository", repositoryName);
|
|
441
|
+
const res = await this._fetch({ url });
|
|
436
442
|
|
|
437
443
|
if (res.ok) {
|
|
438
444
|
const json = await res.json();
|
|
439
445
|
|
|
440
|
-
const { value
|
|
446
|
+
const { value, error } = decode(
|
|
447
|
+
t.union([
|
|
448
|
+
t.type({
|
|
449
|
+
results: t.array(Environment),
|
|
450
|
+
}),
|
|
451
|
+
t.type({
|
|
452
|
+
error: t.literal("invalid_token"),
|
|
453
|
+
}),
|
|
454
|
+
]),
|
|
455
|
+
json,
|
|
456
|
+
);
|
|
441
457
|
|
|
442
458
|
if (error) {
|
|
443
|
-
throw new
|
|
459
|
+
throw new UnexpectedDataError(
|
|
444
460
|
`Failed to decode environments: ${error.errors.join(", ")}`,
|
|
445
461
|
);
|
|
446
462
|
}
|
|
447
463
|
|
|
448
|
-
|
|
464
|
+
if ("results" in value) {
|
|
465
|
+
return sortEnvironments(value.results);
|
|
466
|
+
}
|
|
449
467
|
}
|
|
450
468
|
|
|
451
469
|
switch (res.status) {
|
|
470
|
+
case 400:
|
|
452
471
|
case 401:
|
|
453
472
|
throw new UnauthenticatedError();
|
|
454
473
|
case 403:
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Environment } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sorts a list of environments using the following criteria:
|
|
5
|
+
*
|
|
6
|
+
* - The production environment is always first.
|
|
7
|
+
* - Staging environments are always after production, before development
|
|
8
|
+
* environments, and sorted alphabetically by name.
|
|
9
|
+
* - The development environment is always last.
|
|
10
|
+
*
|
|
11
|
+
* It assumes the list of environments contains one production environment and
|
|
12
|
+
* at most one dev environment.
|
|
13
|
+
*
|
|
14
|
+
* @param environments - The environments to sort.
|
|
15
|
+
*
|
|
16
|
+
* @returns The sorted environments.
|
|
17
|
+
*/
|
|
18
|
+
export function sortEnvironments(environments: Environment[]): Environment[] {
|
|
19
|
+
return [...environments].sort((a, b) => {
|
|
20
|
+
switch (a.kind) {
|
|
21
|
+
case "prod": {
|
|
22
|
+
return -1;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
case "stage": {
|
|
26
|
+
switch (b.kind) {
|
|
27
|
+
case "prod": {
|
|
28
|
+
return 1;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
case "stage": {
|
|
32
|
+
return a.name.localeCompare(b.name);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
case "dev":
|
|
36
|
+
default: {
|
|
37
|
+
return -1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
case "dev": {
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
default: {
|
|
47
|
+
if (b.kind === undefined) {
|
|
48
|
+
return a.name.localeCompare(b.name);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -392,9 +392,9 @@ export class ProjectManager extends BaseManager {
|
|
|
392
392
|
hookResult,
|
|
393
393
|
);
|
|
394
394
|
|
|
395
|
-
const repositoryName = await this.project.getRepositoryName();
|
|
396
|
-
|
|
397
395
|
// An undefined value is equivalent to the production environment.
|
|
396
|
+
// We cast to undefined.
|
|
397
|
+
const repositoryName = await this.project.getRepositoryName();
|
|
398
398
|
const environmentDomain =
|
|
399
399
|
data[0]?.environment === repositoryName
|
|
400
400
|
? undefined
|