@slicemachine/init 1.1.10-alpha.3 → 1.1.10
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/.caches/eslint +1 -0
- package/build/index.js +284 -392
- package/build/index.js.map +1 -1
- package/package.json +4 -11
- package/src/index.ts +36 -32
- package/src/steps/choose-or-create-a-repository.ts +43 -42
- package/src/steps/configure-project.ts +3 -5
- package/src/steps/display-final-message.ts +23 -5
- package/src/steps/loginOrBypass.ts +18 -14
- package/src/steps/sendStarterData.ts +18 -30
- package/src/steps/starters/custom-types.ts +29 -30
- package/src/steps/starters/documents.ts +30 -55
- package/src/steps/starters/prompts.ts +1 -1
- package/src/steps/starters/s3.ts +51 -182
- package/src/steps/starters/slices.ts +55 -42
- package/src/utils/auth/helpers.ts +5 -1
- package/src/utils/auth/index.ts +30 -30
- package/src/utils/client.ts +67 -0
- package/src/utils/create-repo.ts +9 -15
- package/src/utils/fs.ts +18 -0
- package/src/utils/index.ts +16 -0
- package/src/utils/validateRepositoryName.ts +44 -0
- package/src/steps/starters/communication.ts +0 -171
- package/src/steps/starters/endpoints.ts +0 -20
- package/src/utils/communication.ts +0 -72
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ApplicationMode } from "@slicemachine/client";
|
|
2
2
|
import Prismic from "@slicemachine/core/build/prismic";
|
|
3
3
|
import Tracker from "./utils/tracker";
|
|
4
4
|
import {
|
|
@@ -12,17 +12,28 @@ import {
|
|
|
12
12
|
installLib,
|
|
13
13
|
sendStarterData,
|
|
14
14
|
} from "./steps";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
findArgument,
|
|
17
|
+
findFlag,
|
|
18
|
+
logs,
|
|
19
|
+
getApplicationMode,
|
|
20
|
+
InitClient,
|
|
21
|
+
} from "./utils";
|
|
16
22
|
|
|
17
23
|
async function init() {
|
|
18
24
|
const cwd = findArgument(process.argv, "cwd") || process.cwd();
|
|
19
|
-
const
|
|
25
|
+
const mode: ApplicationMode =
|
|
26
|
+
getApplicationMode(findArgument(process.argv, "mode")) ||
|
|
27
|
+
ApplicationMode.PROD;
|
|
20
28
|
const lib: string | undefined = findArgument(process.argv, "library");
|
|
21
29
|
const branch: string | undefined = findArgument(process.argv, "branch");
|
|
22
|
-
const isTrackingAvailable =
|
|
30
|
+
const isTrackingAvailable: boolean =
|
|
23
31
|
findArgument(process.argv, "tracking") !== "false";
|
|
24
|
-
const
|
|
25
|
-
|
|
32
|
+
const preSelectedRepository: string | undefined = findArgument(
|
|
33
|
+
process.argv,
|
|
34
|
+
"repository"
|
|
35
|
+
);
|
|
36
|
+
const pushDocuments = !findFlag(process.argv, "no-docs");
|
|
26
37
|
|
|
27
38
|
Tracker.get().initialize(
|
|
28
39
|
process.env.PUBLIC_SM_INIT_SEGMENT_KEY ||
|
|
@@ -30,7 +41,14 @@ async function init() {
|
|
|
30
41
|
isTrackingAvailable
|
|
31
42
|
);
|
|
32
43
|
|
|
33
|
-
void Tracker.get().trackInitStart(
|
|
44
|
+
void Tracker.get().trackInitStart(preSelectedRepository);
|
|
45
|
+
|
|
46
|
+
// initializing the client with what we have for now.
|
|
47
|
+
const client = new InitClient(
|
|
48
|
+
mode,
|
|
49
|
+
null,
|
|
50
|
+
Prismic.PrismicSharedConfigManager.getAuth()
|
|
51
|
+
);
|
|
34
52
|
|
|
35
53
|
console.log(
|
|
36
54
|
logs.purple(
|
|
@@ -42,48 +60,34 @@ async function init() {
|
|
|
42
60
|
validatePkg(cwd);
|
|
43
61
|
|
|
44
62
|
// login
|
|
45
|
-
const user = await loginOrBypass(
|
|
46
|
-
if (!user) throw new Error("The user should be logged in!");
|
|
47
|
-
|
|
48
|
-
// If we get the info from the profile we want to identify all the previous events sent or continue in anonymous mode
|
|
49
|
-
if (user.profile) {
|
|
50
|
-
Tracker.get().identifyUser(user.profile.shortId, user.profile.intercomHash);
|
|
51
|
-
}
|
|
63
|
+
const user = await loginOrBypass(client);
|
|
52
64
|
|
|
65
|
+
Tracker.get().identifyUser(user.shortId, user.intercomHash);
|
|
53
66
|
void Tracker.get().trackInitIdentify();
|
|
54
67
|
|
|
55
|
-
// retrieve tokens for api calls
|
|
56
|
-
const config = Prismic.PrismicSharedConfigManager.get();
|
|
57
|
-
|
|
58
68
|
// detect the framework used by the project
|
|
59
69
|
const frameworkResult = await detectFramework(cwd);
|
|
60
70
|
|
|
61
71
|
// select the repository used with the project.
|
|
62
|
-
const
|
|
72
|
+
const repository = await chooseOrCreateARepository(
|
|
73
|
+
client,
|
|
63
74
|
cwd,
|
|
64
75
|
frameworkResult.value,
|
|
65
|
-
|
|
66
|
-
config.base,
|
|
67
|
-
maybeRepositorySubdomain
|
|
76
|
+
preSelectedRepository
|
|
68
77
|
);
|
|
69
78
|
|
|
70
|
-
Tracker.get().setRepository(
|
|
79
|
+
Tracker.get().setRepository(repository);
|
|
80
|
+
client.updateRepository(repository);
|
|
71
81
|
|
|
72
82
|
const sliceLibPath = lib ? await installLib(cwd, lib, branch) : undefined;
|
|
73
83
|
|
|
74
|
-
const wasStarter = await sendStarterData(
|
|
75
|
-
repositoryDomainName,
|
|
76
|
-
config.base,
|
|
77
|
-
config.cookies,
|
|
78
|
-
sendDocs,
|
|
79
|
-
cwd
|
|
80
|
-
); // will be false if no sm.json is found
|
|
84
|
+
const wasStarter = await sendStarterData(client, cwd, pushDocuments); // will be false if no sm.json is found
|
|
81
85
|
|
|
82
86
|
// configure the SM.json file and the json package file of the project..
|
|
83
87
|
await configureProject(
|
|
88
|
+
client,
|
|
84
89
|
cwd,
|
|
85
|
-
|
|
86
|
-
repositoryDomainName,
|
|
90
|
+
repository,
|
|
87
91
|
frameworkResult,
|
|
88
92
|
sliceLibPath,
|
|
89
93
|
isTrackingAvailable
|
|
@@ -93,7 +97,7 @@ async function init() {
|
|
|
93
97
|
await installRequiredDependencies(cwd, frameworkResult.value, wasStarter);
|
|
94
98
|
|
|
95
99
|
// Ask the user to run slice-machine.
|
|
96
|
-
displayFinalMessage(cwd);
|
|
100
|
+
displayFinalMessage(cwd, wasStarter, repository, client.apisEndpoints.Wroom);
|
|
97
101
|
}
|
|
98
102
|
|
|
99
103
|
init()
|
|
@@ -1,26 +1,24 @@
|
|
|
1
1
|
import * as inquirer from "inquirer";
|
|
2
2
|
import Separator from "inquirer/lib/objects/separator";
|
|
3
|
-
import {
|
|
4
|
-
import * as Prismic from "@slicemachine/core/build/prismic";
|
|
3
|
+
import { Models } from "@slicemachine/core";
|
|
5
4
|
import * as NodeUtils from "@slicemachine/core/build/node-utils";
|
|
6
5
|
import { createRepository } from "../utils/create-repo";
|
|
7
|
-
import {
|
|
6
|
+
import { validateRepositoryName } from "../utils/validateRepositoryName";
|
|
7
|
+
import { InitClient, logs } from "../utils";
|
|
8
8
|
|
|
9
9
|
export const CREATE_REPO = "$_CREATE_REPO"; // not a valid domain name
|
|
10
|
-
const DEFAULT_BASE = CONSTS.DEFAULT_BASE;
|
|
11
10
|
|
|
12
|
-
export function prettyRepoName(address: URL,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
)}`;
|
|
11
|
+
export function prettyRepoName(address: URL, domain: string): string {
|
|
12
|
+
return `${logs.cyan.dim(`${address.protocol}//`)}${logs.cyan(
|
|
13
|
+
domain
|
|
14
|
+
)}${logs.cyan.dim(`.${address.hostname}`)}`;
|
|
17
15
|
}
|
|
18
16
|
|
|
19
17
|
export async function promptForRepoDomain(
|
|
20
|
-
|
|
18
|
+
client: InitClient,
|
|
21
19
|
defaultValue?: string
|
|
22
20
|
): Promise<string> {
|
|
23
|
-
const address = new URL(
|
|
21
|
+
const address = new URL(client.apisEndpoints.Wroom);
|
|
24
22
|
|
|
25
23
|
logs.writeInfo(
|
|
26
24
|
"The name acts as a domain/endpoint for your content repo and should be completely unique."
|
|
@@ -34,16 +32,9 @@ export async function promptForRepoDomain(
|
|
|
34
32
|
type: "input",
|
|
35
33
|
required: true,
|
|
36
34
|
default: defaultValue,
|
|
37
|
-
transformer: (value: string) =>
|
|
38
|
-
prettyRepoName(address, value || defaultValue),
|
|
39
|
-
|
|
40
|
-
const result = await Prismic.Communication.validateRepositoryName(
|
|
41
|
-
name,
|
|
42
|
-
base,
|
|
43
|
-
false
|
|
44
|
-
);
|
|
45
|
-
return result === name || result;
|
|
46
|
-
},
|
|
35
|
+
transformer: (value: string | undefined) =>
|
|
36
|
+
prettyRepoName(address, value || defaultValue || "repository"),
|
|
37
|
+
validate: (name: string) => validateRepositoryName(client, name),
|
|
47
38
|
},
|
|
48
39
|
])
|
|
49
40
|
.then((res) => res.repoDomain);
|
|
@@ -122,47 +113,57 @@ export function sortReposForPrompt(
|
|
|
122
113
|
}
|
|
123
114
|
|
|
124
115
|
export async function chooseOrCreateARepository(
|
|
116
|
+
client: InitClient,
|
|
125
117
|
cwd: string,
|
|
126
118
|
framework: Models.Frameworks,
|
|
127
|
-
|
|
128
|
-
base = DEFAULT_BASE,
|
|
129
|
-
domain?: string
|
|
119
|
+
preSelectedRepository?: string
|
|
130
120
|
): Promise<string> {
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
121
|
+
const repositories: Models.Repository[] = await client.listRepositories();
|
|
122
|
+
|
|
123
|
+
const isPreSelectedValid =
|
|
124
|
+
preSelectedRepository &&
|
|
125
|
+
repositories.find(
|
|
126
|
+
(repository) => repository.domain === preSelectedRepository
|
|
127
|
+
);
|
|
128
|
+
if (isPreSelectedValid) return preSelectedRepository;
|
|
129
|
+
|
|
130
|
+
// No repository to display, ask for a new repository name to create it.
|
|
131
|
+
if (repositories.length === 0) {
|
|
132
|
+
const domainName = await promptForRepoDomain(client, preSelectedRepository);
|
|
133
|
+
await createRepository(client, domainName, framework);
|
|
134
|
+
return domainName;
|
|
140
135
|
}
|
|
141
136
|
|
|
142
|
-
|
|
143
|
-
|
|
137
|
+
// prepare the list of repositories to display
|
|
138
|
+
const choices = sortReposForPrompt(
|
|
139
|
+
repositories,
|
|
140
|
+
client.apisEndpoints.Wroom,
|
|
141
|
+
cwd
|
|
142
|
+
);
|
|
144
143
|
const numberOfDisabledRepos = choices.filter((repo) => {
|
|
145
144
|
if (repo instanceof Separator) return false;
|
|
146
145
|
return repo.disabled;
|
|
147
146
|
}).length;
|
|
148
147
|
|
|
149
|
-
|
|
148
|
+
// display the list of repositories and wait for the user to choose one.
|
|
149
|
+
const promptResult = await inquirer.prompt<{ chosenRepository: string }>([
|
|
150
150
|
{
|
|
151
151
|
type: "list",
|
|
152
|
-
name: "
|
|
152
|
+
name: "chosenRepository",
|
|
153
153
|
default: 0,
|
|
154
154
|
required: true,
|
|
155
155
|
message: "Connect a Prismic Repository or create a new one",
|
|
156
156
|
choices,
|
|
157
157
|
pageSize: numberOfDisabledRepos + 2 <= 7 ? 7 : numberOfDisabledRepos + 2,
|
|
158
|
-
// loop: false
|
|
159
158
|
},
|
|
160
159
|
]);
|
|
161
160
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
161
|
+
// If the user has chosen to create a new repository.
|
|
162
|
+
if (promptResult.chosenRepository === CREATE_REPO) {
|
|
163
|
+
const domainName = await promptForRepoDomain(client, preSelectedRepository);
|
|
164
|
+
await createRepository(client, domainName, framework);
|
|
165
|
+
return domainName;
|
|
165
166
|
}
|
|
166
167
|
|
|
167
|
-
return
|
|
168
|
+
return promptResult.chosenRepository;
|
|
168
169
|
}
|
|
@@ -3,16 +3,14 @@ import type { Models } from "@slicemachine/core";
|
|
|
3
3
|
import * as Prismic from "@slicemachine/core/build/prismic";
|
|
4
4
|
import * as NodeUtils from "@slicemachine/core/build/node-utils";
|
|
5
5
|
import { FrameworkResult } from "./detect-framework";
|
|
6
|
-
import { logs } from "../utils";
|
|
6
|
+
import { InitClient, logs } from "../utils";
|
|
7
7
|
import Tracker from "../utils/tracker";
|
|
8
8
|
|
|
9
|
-
type Base = Prismic.Endpoints.Base;
|
|
10
|
-
|
|
11
9
|
const defaultSliceMachineVersion = "0.0.41";
|
|
12
10
|
|
|
13
11
|
export async function configureProject(
|
|
12
|
+
client: InitClient,
|
|
14
13
|
cwd: string,
|
|
15
|
-
base: Base,
|
|
16
14
|
repositoryDomainName: string,
|
|
17
15
|
framework: FrameworkResult,
|
|
18
16
|
sliceLibPath: string[] = [],
|
|
@@ -45,7 +43,7 @@ export async function configureProject(
|
|
|
45
43
|
? manifest.content
|
|
46
44
|
: { _latest: sliceMachineVersionInstalled }),
|
|
47
45
|
apiEndpoint: Prismic.Endpoints.buildRepositoryEndpoint(
|
|
48
|
-
|
|
46
|
+
client.apisEndpoints.Wroom,
|
|
49
47
|
repositoryDomainName
|
|
50
48
|
),
|
|
51
49
|
libraries: [...libs, ...sliceLibPath], // odd case here for staters
|
|
@@ -2,12 +2,30 @@ import { CONSTS } from "@slicemachine/core";
|
|
|
2
2
|
import * as NodeUtils from "@slicemachine/core/build/node-utils";
|
|
3
3
|
import { logs } from "../utils";
|
|
4
4
|
|
|
5
|
-
export function displayFinalMessage(
|
|
5
|
+
export function displayFinalMessage(
|
|
6
|
+
cwd: string,
|
|
7
|
+
wasStarter: boolean,
|
|
8
|
+
reponame: string,
|
|
9
|
+
base: string
|
|
10
|
+
): void {
|
|
6
11
|
const yarnLock = NodeUtils.Files.exists(NodeUtils.YarnLockPath(cwd));
|
|
7
12
|
const command = `${yarnLock ? "yarn" : "npm"} run ${CONSTS.SCRIPT_NAME}`;
|
|
8
|
-
|
|
9
13
|
console.log();
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
|
|
15
|
+
if (wasStarter) {
|
|
16
|
+
const repoUrl = new URL(base);
|
|
17
|
+
repoUrl.hostname = `${reponame}.${repoUrl.hostname}`;
|
|
18
|
+
const urlAsString = repoUrl.toString();
|
|
19
|
+
|
|
20
|
+
const message = `${logs.white(
|
|
21
|
+
"■"
|
|
22
|
+
)} Start editing your content in Prismic ${logs.purple(urlAsString)}`;
|
|
23
|
+
console.log(message);
|
|
24
|
+
} else {
|
|
25
|
+
console.log(
|
|
26
|
+
`${logs.white("■")} Run ${logs.purple(
|
|
27
|
+
command
|
|
28
|
+
)} to launch Slice Machine and create your first Custom Type`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
13
31
|
}
|
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import { Models } from "@slicemachine/core";
|
|
2
|
-
import {
|
|
3
|
-
import { logs, Auth } from "../utils";
|
|
2
|
+
import { PrismicSharedConfigManager } from "@slicemachine/core/build/prismic";
|
|
3
|
+
import { logs, Auth, InitClient } from "../utils";
|
|
4
|
+
|
|
5
|
+
export async function loginOrBypass(
|
|
6
|
+
client: InitClient
|
|
7
|
+
): Promise<Models.UserProfile> {
|
|
8
|
+
const user: Models.UserProfile | null = await client
|
|
9
|
+
.profile()
|
|
10
|
+
.catch(() => null);
|
|
4
11
|
|
|
5
|
-
export async function loginOrBypass(base: string): Promise<{
|
|
6
|
-
info: Models.UserInfo;
|
|
7
|
-
profile: Models.UserProfile | null;
|
|
8
|
-
} | null> {
|
|
9
|
-
const user = await validateSessionAndGetProfile(base).catch((err) =>
|
|
10
|
-
console.log(err)
|
|
11
|
-
);
|
|
12
12
|
if (user) {
|
|
13
|
-
const email = user.
|
|
13
|
+
const email = user.email;
|
|
14
14
|
logs.writeCheck(`Logged in as ${logs.bold(email)}`);
|
|
15
15
|
return user;
|
|
16
|
-
} else {
|
|
17
|
-
await Auth.login(base);
|
|
18
|
-
const user = await validateSessionAndGetProfile(base);
|
|
19
|
-
return user;
|
|
20
16
|
}
|
|
17
|
+
|
|
18
|
+
await Auth.login(client);
|
|
19
|
+
|
|
20
|
+
// update token used to make calls.
|
|
21
|
+
client.updateAuthenticationToken(PrismicSharedConfigManager.getAuth());
|
|
22
|
+
|
|
23
|
+
const userAfterLogin: Models.UserProfile = await client.profile();
|
|
24
|
+
return userAfterLogin;
|
|
21
25
|
}
|
|
@@ -1,43 +1,31 @@
|
|
|
1
|
-
import { parsePrismicAuthToken } from "@slicemachine/core/build/utils/cookie";
|
|
2
1
|
import { retrieveManifest, Files } from "@slicemachine/core/build/node-utils";
|
|
3
2
|
import path from "path";
|
|
4
|
-
import { sendSlicesFromStarter } from "./starters/slices";
|
|
5
|
-
import { sendCustomTypesFromStarter } from "./starters/custom-types";
|
|
6
|
-
import { sendDocumentsFromStarter } from "./starters/documents";
|
|
7
3
|
import fs from "fs";
|
|
4
|
+
import { sendSlices } from "./starters/slices";
|
|
5
|
+
import { sendCustomTypes } from "./starters/custom-types";
|
|
6
|
+
import { sendDocuments } from "./starters/documents";
|
|
7
|
+
import { InitClient } from "../utils";
|
|
8
8
|
|
|
9
9
|
export async function sendStarterData(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
cwd
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
const pathToDocuments = path.join(cwd, "documents");
|
|
18
|
-
const hasDocuments = Files.exists(pathToDocuments);
|
|
10
|
+
client: InitClient,
|
|
11
|
+
cwd: string,
|
|
12
|
+
pushDocuments = true
|
|
13
|
+
) {
|
|
14
|
+
const manifest = retrieveManifest(cwd);
|
|
15
|
+
const documentsPath = path.join(cwd, "documents");
|
|
16
|
+
const hasDocuments = Files.exists(documentsPath);
|
|
19
17
|
|
|
20
|
-
if (
|
|
18
|
+
if (manifest.exists === false || hasDocuments === false)
|
|
21
19
|
return Promise.resolve(false);
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
if (manifest.content) await sendSlices(client, cwd, manifest.content);
|
|
22
|
+
await sendCustomTypes(client, cwd);
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
repository,
|
|
29
|
-
authTokenFromCookie,
|
|
30
|
-
smJson.content.libraries,
|
|
31
|
-
cwd
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
await sendCustomTypesFromStarter(repository, authTokenFromCookie, base, cwd);
|
|
36
|
-
|
|
37
|
-
if (sendDocs === false) {
|
|
38
|
-
fs.rmSync(pathToDocuments, { recursive: true, force: true });
|
|
24
|
+
// If the user choose not to push documents, we still delete the documents folder.
|
|
25
|
+
if (!pushDocuments) {
|
|
26
|
+
fs.rmSync(documentsPath, { recursive: true, force: true });
|
|
39
27
|
return Promise.resolve(true);
|
|
40
28
|
}
|
|
41
29
|
|
|
42
|
-
return
|
|
30
|
+
return sendDocuments(client, cwd);
|
|
43
31
|
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import { CustomType } from "@prismicio/types-internal/lib/customtypes";
|
|
2
2
|
import { Files, CustomTypesPaths } from "@slicemachine/core/build/node-utils";
|
|
3
3
|
import { isLeft } from "fp-ts/lib/Either";
|
|
4
|
-
import {
|
|
5
|
-
getRemoteCustomTypeIds,
|
|
6
|
-
sendManyCustomTypesToPrismic,
|
|
7
|
-
} from "./communication";
|
|
8
4
|
import { promptToPushCustomTypes } from "./prompts";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
5
|
+
import { InitClient, logs } from "../../utils";
|
|
6
|
+
import { ClientError } from "@slicemachine/client";
|
|
11
7
|
|
|
12
|
-
export function
|
|
8
|
+
export function readLocalCustomTypes(cwd: string): Array<CustomType> {
|
|
13
9
|
const customTypePaths = CustomTypesPaths(cwd);
|
|
14
10
|
const dir = customTypePaths.value();
|
|
15
11
|
|
|
@@ -40,23 +36,15 @@ export function readCustomTypes(cwd: string): Array<CustomType> {
|
|
|
40
36
|
return files;
|
|
41
37
|
}
|
|
42
38
|
|
|
43
|
-
export async function
|
|
44
|
-
|
|
45
|
-
authorization: string,
|
|
46
|
-
base: string,
|
|
47
|
-
cwd: string
|
|
48
|
-
) {
|
|
49
|
-
const customTypeApiEndpoint = getEndpointsFromBase(base).Models;
|
|
39
|
+
export async function sendCustomTypes(client: InitClient, cwd: string) {
|
|
40
|
+
const localCustomTypes = readLocalCustomTypes(cwd);
|
|
50
41
|
|
|
51
|
-
|
|
42
|
+
// nothing to push
|
|
43
|
+
if (localCustomTypes.length === 0) return Promise.resolve(false);
|
|
52
44
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
customTypeApiEndpoint,
|
|
57
|
-
repository,
|
|
58
|
-
authorization
|
|
59
|
-
);
|
|
45
|
+
const remoteCustomTypeIds = await client
|
|
46
|
+
.getCustomTypes()
|
|
47
|
+
.then((customTypes) => customTypes.map((customType) => customType.id));
|
|
60
48
|
|
|
61
49
|
if (remoteCustomTypeIds.length) {
|
|
62
50
|
const shouldPush = await promptToPushCustomTypes();
|
|
@@ -68,15 +56,26 @@ export async function sendCustomTypesFromStarter(
|
|
|
68
56
|
);
|
|
69
57
|
spinner.start();
|
|
70
58
|
|
|
71
|
-
await
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
59
|
+
await Promise.all(
|
|
60
|
+
localCustomTypes.map(async (customType) => {
|
|
61
|
+
const promise = remoteCustomTypeIds.includes(customType.id)
|
|
62
|
+
? client.updateCustomType(customType)
|
|
63
|
+
: client.insertCustomType(customType);
|
|
64
|
+
|
|
65
|
+
return promise.catch((error: ClientError) => {
|
|
66
|
+
logs.writeError(
|
|
67
|
+
`Sending custom type ${customType.id} - ${error.message}`
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// throwing the error again to stop the Promise.all
|
|
71
|
+
throw error;
|
|
72
|
+
});
|
|
73
|
+
})
|
|
74
|
+
).catch(() => {
|
|
75
|
+
// the error about the custom type that failed to be pushed should be in the terminal already.
|
|
76
|
+
process.exit(1);
|
|
77
|
+
});
|
|
78
78
|
|
|
79
79
|
spinner.succeed();
|
|
80
|
-
|
|
81
80
|
return Promise.resolve(true);
|
|
82
81
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import fs from "fs";
|
|
3
|
-
import
|
|
3
|
+
import type { AxiosError } from "axios";
|
|
4
4
|
|
|
5
5
|
import * as t from "io-ts";
|
|
6
6
|
import { getOrElseW } from "fp-ts/Either";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { InitClient, logs, lsdir, lsfiles } from "../../utils";
|
|
8
|
+
import { PrismicSharedConfigManager } from "@slicemachine/core/build/prismic/SharedConfig";
|
|
9
9
|
|
|
10
10
|
const SignatureFileReader = t.type({
|
|
11
11
|
signature: t.string,
|
|
@@ -23,74 +23,50 @@ export async function readSignatureFile(cwd: string): Promise<SignatureFile> {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
async function
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
.filter((name) => fs.statSync(path.join(dir, name)).isDirectory())
|
|
30
|
-
.map((subdirectory) => path.join(dir, subdirectory));
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async function lsfiles(dir: string): Promise<Array<string>> {
|
|
35
|
-
return fs.promises.readdir(dir).then((dirs) => {
|
|
36
|
-
return dirs
|
|
37
|
-
.filter((name) => fs.statSync(path.join(dir, name)).isFile())
|
|
38
|
-
.map((file) => path.join(dir, file));
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export async function readDocuments(cwd: string) {
|
|
26
|
+
export async function readDocuments(
|
|
27
|
+
cwd: string
|
|
28
|
+
): Promise<Record<string, unknown>> {
|
|
43
29
|
const documentDir = path.join(cwd, "documents");
|
|
44
30
|
const dirs = await lsdir(documentDir);
|
|
45
31
|
|
|
46
32
|
const files = (await Promise.all(dirs.map((dir) => lsfiles(dir)))).flat();
|
|
47
33
|
|
|
48
34
|
const documentObj = files.reduce<Record<string, unknown>>((acc, file) => {
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
35
|
+
const filename: string = path.parse(file).name;
|
|
36
|
+
const fileContent: string = fs.readFileSync(file, "utf-8");
|
|
37
|
+
const json = JSON.parse(fileContent) as Record<string, unknown>;
|
|
38
|
+
|
|
39
|
+
return { ...acc, [filename]: json };
|
|
53
40
|
}, {});
|
|
54
41
|
|
|
55
|
-
return
|
|
42
|
+
return documentObj;
|
|
56
43
|
}
|
|
57
44
|
|
|
58
|
-
export
|
|
59
|
-
|
|
60
|
-
cookies: string,
|
|
61
|
-
base: string,
|
|
45
|
+
export async function sendDocuments(
|
|
46
|
+
client: InitClient,
|
|
62
47
|
cwd: string
|
|
63
|
-
): Promise<boolean>
|
|
48
|
+
): Promise<boolean> {
|
|
64
49
|
const pathToDocuments = path.join(cwd, "documents");
|
|
65
50
|
const pathToSignatureFile = path.join(pathToDocuments, "index.json");
|
|
66
51
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
52
|
+
// Without the signature, we can't push documents to Prismic.
|
|
53
|
+
if (!fs.existsSync(pathToSignatureFile)) return Promise.resolve(false);
|
|
70
54
|
|
|
71
55
|
const signatureObj = await readSignatureFile(cwd);
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
const payload = {
|
|
75
|
-
signature: signatureObj.signature,
|
|
76
|
-
documents: documentsStr,
|
|
77
|
-
};
|
|
56
|
+
const documents: Record<string, unknown> = await readDocuments(cwd);
|
|
78
57
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
prismicUrl.pathname = "starter/documents";
|
|
82
|
-
const endpointURL = prismicUrl.toString();
|
|
58
|
+
// No documents to push
|
|
59
|
+
if (Object.keys(documents).length === 0) return Promise.resolve(false);
|
|
83
60
|
|
|
84
61
|
const spinner = logs.spinner("Pushing existing documents to your repository");
|
|
85
62
|
spinner.start();
|
|
86
63
|
|
|
87
|
-
return
|
|
88
|
-
.
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
})
|
|
64
|
+
return client
|
|
65
|
+
.pushDocuments(
|
|
66
|
+
signatureObj.signature,
|
|
67
|
+
documents,
|
|
68
|
+
PrismicSharedConfigManager.get().cookies
|
|
69
|
+
)
|
|
94
70
|
.then(() => {
|
|
95
71
|
spinner.succeed();
|
|
96
72
|
fs.rmSync(pathToDocuments, { recursive: true, force: true });
|
|
@@ -103,12 +79,11 @@ export const sendDocumentsFromStarter = async (
|
|
|
103
79
|
"The selected repository is not empty, documents cannot be uploaded. Please choose an empty repository or delete the documents contained in your repository."
|
|
104
80
|
);
|
|
105
81
|
} else {
|
|
106
|
-
|
|
107
|
-
"
|
|
108
|
-
e
|
|
82
|
+
logs.writeError(
|
|
83
|
+
"Sending documents failed, please try again. If the problem persists, contact us."
|
|
109
84
|
);
|
|
85
|
+
logs.writeError(`Full error: ${e.code || 500} - ${e.message}`);
|
|
110
86
|
}
|
|
111
|
-
|
|
112
87
|
process.exit(1);
|
|
113
88
|
});
|
|
114
|
-
}
|
|
89
|
+
}
|
|
@@ -22,7 +22,7 @@ export async function promptToPushCustomTypes(): Promise<boolean> {
|
|
|
22
22
|
name: "pushCustomTypes",
|
|
23
23
|
default: false,
|
|
24
24
|
message:
|
|
25
|
-
"Your repository already contains Custom Types. Do you want to continue pushing your local
|
|
25
|
+
"Your repository already contains Custom Types. Do you want to continue pushing your local Custom Types?",
|
|
26
26
|
},
|
|
27
27
|
])
|
|
28
28
|
.then((res) => res.pushCustomTypes);
|