-
Notifications
You must be signed in to change notification settings - Fork 23
/
setup.js
174 lines (149 loc) · 5.47 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// "use strict";
require("dotenv").config();
// import chalk from "chalk";
// import { createRequire } from "module";
// const require = createRequire(import.meta.url);
const contentful = require("contentful-management");
const spaceImport = require("contentful-import");
const exportFile = require("./space-export/space.json");
const inquirer = require("inquirer");
// const chalk = require("chalk");
const path = require("path");
// const chalk = require("chalk");
// const path = require("path");
const { writeFileSync } = require("fs");
(async()=>{
const chalkImport = await import("chalk");
const chalk = chalkImport.default;
console.log(`
To set up this project you need to provide your Space ID
and the belonging API access tokens.
You can find all the needed information in your Contentful space under:
${chalk.yellow(
`app.contentful.com ${chalk.red("->")} Space Settings ${chalk.red(
"->"
)} API keys`
)}
The ${chalk.green("Content Management API Token")}
will be used to import and write data to your space.
The ${chalk.green("Content Delivery API Token")}
will be used to retrieve published content items.
The ${chalk.green("Content Preview API Token")}
will be used to retrieve published and unpublished content items.
Ready? Let's do it! 🎉
`);
const questions = [
{
name: "spaceId",
message: "Your Space ID",
validate: (input) =>
/^[a-z0-9]{12}$/.test(input) ||
"Space ID must be 12 lowercase characters",
},
{
name: "accessToken",
message: "Your Content Delivery API access token",
},
{
name: "previewToken",
message: "Your Content Preview API access token",
},
{
name: "managementToken",
message: "Your Content Management API access token",
},
{
name: "environment",
message: "Your Space Environment, e.g master",
},
];
inquirer
.prompt(questions)
.then((answers) => {
console.log("Writing config file...");
const spaceId = answers.spaceId ? answers.spaceId : "";
const accessToken = answers.accessToken ? answers.accessToken : "";
const previewToken = answers.previewToken ? answers.previewToken : "";
const previewSecret = answers.previewSecret ? answers.previewSecret : "";
// const env = "master";
const env = answers.environment ? answers.environment : "master";
const managementToken = answers.managementToken
? answers.managementToken
: ""
? answers.managementToken
: "";
const fileContents =
[
`NEXT_PUBLIC_SPACE_ID='${spaceId}'`,
`NEXT_PUBLIC_DELIVERY_TOKEN='${accessToken}'`,
`NEXT_PUBLIC_PREVIEW_TOKEN='${previewToken}'`,
`CMA_TOKEN='${managementToken}'`,
`NEXT_PUBLIC_PREVIEW_SECRET='testing'`,
`NEXT_PUBLIC_ENVIRONMENT='${env}'`,
].join("\n") + "\n";
writeFileSync(".env", fileContents, "utf8");
return { spaceId, managementToken, accessToken, env };
})
.then((managementConfig) => {
const spaceId = managementConfig.spaceId ? managementConfig.spaceId : "";
const managementToken = managementConfig.managementToken
? managementConfig.managementToken
: "";
const accessToken = managementConfig.accessToken
? managementConfig.accessToken
: "";
const envId = managementConfig.env ? managementConfig.env : "master";
// console.log(managementConfig);
if (spaceId && managementToken) {
const client = contentful.createClient({
// This is the access token for this space. Normally you get the token in the Contentful web app
accessToken: managementToken,
});
(async () => {
try {
const contentfulSpace = await client.getSpace(spaceId);
const environment = await contentfulSpace.getEnvironment(envId);
const entries = await environment.getEntries();
// delete entries
console.log(chalk.red(`Deleting! entries`));
for (const entry of entries.items) {
if (entry.isPublished()) {
console.log(`Unpublishing entry "${entry.sys.id}"`);
await entry.unpublish();
}
await entry.delete();
}
// delete content types
const contentTypes = await environment.getContentTypes();
let totalContentTypes = contentTypes.total;
console.log(
chalk.red(`Deleting! ${totalContentTypes} content types`)
);
console.log(contentTypes);
for (const contentType of contentTypes.items) {
if (contentType.isPublished()) {
console.log(`Unpublishing content type "${contentType.sys.id}"`);
await contentType.unpublish();
}
await contentType.delete();
}
// import to space
await spaceImport({
spaceId: spaceId,
managementToken: managementToken,
environmentId: envId,
content: exportFile,
});
} catch (error) {
console.log(chalk.red(`An error occured!`), error);
}
// end
})();
} else {
console.log(
`Missing Management token! '
)} Please Try Again ${chalk.red("ERROR")} .`
);
}
});
})()