-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.js
139 lines (126 loc) · 4.28 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
require("dotenv").config();
const contentful = require("contentful-management");
const spaceImport = require("contentful-import");
const exportFile = require("./contentful/exports.json");
const inquirer = require("inquirer");
// const chalk = require("chalk");
const path = require("path");
const { writeFileSync } = require("fs");
(async () => {
// const inquirer = await import("inquirer");
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: "previewSecret",
message: "A secret word for Content Preview!",
},
{
name: "managementToken",
message: "Your Content Management API access token",
},
];
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 managementToken = answers.managementToken
? answers.managementToken
: ""
? answers.managementToken
: "";
const configFiles = [`config.js`].map((file) =>
path.join(__dirname, "components", file)
);
const fileContents =
[
`// All environment variables will be sourced`,
`// Do NOT commit this file to source control`,
`module.exports = {`,
`space_id: '${spaceId}',`,
`delivery_token: '${accessToken}',`,
`preview_token: '${previewToken}',`,
`previewSecret: '${previewSecret}',`,
`environment: 'master'`,
`}`,
].join("\n") + "\n";
configFiles.forEach((file) => {
writeFileSync(file, fileContents, "utf8");
console.log(`Config file ${chalk.yellow(file)} written`);
});
return { spaceId, managementToken, accessToken };
})
.then((managementConfig) => {
const spaceId = managementConfig.spaceId ? managementConfig.spaceId : "";
const managementToken = managementConfig.managementToken
? managementConfig.managementToken
: "";
const accessToken = managementConfig.accessToken
? managementConfig.accessToken
: "";
// console.log(managementConfig);
if (spaceId && managementToken) {
spaceImport({
spaceId: spaceId,
managementToken: managementToken,
content: exportFile,
})
.then(() => {
console.log(
`All set! Make sure to give your API key access to your new demo environment. You can now run ${chalk.yellow(
"npm run dev"
)} and bring up the app in a browser ${chalk.yellow(
"http://localhost:9009"
)} .`
);
})
.catch((err) => {
console.log(
`An Error Occured!${chalk.yellow(err)}
.`
);
});
} else {
console.log(
`Missing Management token! '
)} Please Try Again ${chalk.red("ERROR")} .`
);
}
});
})();