-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset-up.mjs
150 lines (134 loc) · 4.32 KB
/
set-up.mjs
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
import inquirer from 'inquirer';
import chalk from 'chalk';
import fs from 'fs';
import pkg from 'contentful-management';
const { createClient } = pkg;
import contentfulImport from 'contentful-import';
const ORG_ID = '1jtZ8D4corGeQgsNmtaFM4'; // Studio Staging Org
// const ORG_ID = '5ZLP1tojPlbZtiMhmNFKzG'; // Colorful Composition Org
(async () => {
const { cmaToken, spaceName, orgId } = await inquirer.prompt([
{
name: 'cmaToken',
message: 'What is your Contentful CMA token?',
type: 'password',
},
{
name: 'spaceName',
message: 'What would you like to call your space?',
},
{
name: 'orgId',
default: ORG_ID,
message: 'What is your Organization ID?',
},
]);
let client;
let space;
try {
client = await createClient({
accessToken: cmaToken,
});
} catch (e) {
console.log(chalk.red('Your cma token may be invalid.'));
return;
}
console.log(chalk.green(`Creating your space: "${spaceName}"...`));
console.log('\n');
try {
space = await client.createSpace(
{
name: spaceName,
},
orgId
);
} catch (e) {
console.log(chalk.red('Your cma token may be invalid.'));
return;
}
try {
await client.rawRequest({
method: 'PUT',
url: `https://api.contentful.com/spaces/${space.sys.id}/enablements`,
data: {
crossSpaceLinks: { enabled: false },
spaceTemplates: { enabled: false },
studioExperiences: { enabled: true },
},
headers: {
'X-Contentful-Enable-Alpha-Feature': 'enablements',
'X-Contentful-Version': 1,
},
});
} catch (e) {
return {
state: 'error',
error: 'Failed to enable experiences',
stacktrace: e,
};
}
console.log(chalk.green('------------------------------'));
console.log(chalk.green('| |'));
console.log(chalk.green('| Creating Access Tokens |'));
console.log(chalk.green('| |'));
console.log(chalk.green('------------------------------'));
console.log('\n');
const tokens = await space.createApiKey({
name: 'Studio Starter',
});
const deliveryToken = tokens.accessToken;
const { accessToken: previewToken } = await space.getPreviewApiKey(
tokens.preview_api_key.sys.id
);
console.log(chalk.green('------------------------------'));
console.log(chalk.green('| |'));
console.log(chalk.green('| Creating a .env file |'));
console.log(chalk.green('| |'));
console.log(chalk.green('------------------------------'));
console.log('\n');
await fs.writeFileSync(
'.env.local',
`NEXT_PUBLIC_SPACE_ID=${space.sys.id}
NEXT_PUBLIC_ACCESS_TOKEN=${deliveryToken}
NEXT_PUBLIC_PREVIEW_TOKEN=${previewToken}
NEXT_PUBLIC_STUDIO_TYPE_ID=landingPage`
);
console.log(chalk.blue('------------------------------'));
console.log(chalk.blue('| |'));
console.log(chalk.blue('| Importing content into |'));
console.log(chalk.blue('| your space. This could |'));
console.log(chalk.blue('| take a few minutes |'));
console.log(chalk.blue('| |'));
console.log(chalk.blue('------------------------------'));
await contentfulImport({
contentFile: './content-export.json',
spaceId: space.sys.id,
managementToken: cmaToken,
});
console.log(chalk.green('------------------------------'));
console.log(chalk.green('| |'));
console.log(chalk.green('| Setting preview urls. |'));
console.log(chalk.green('| |'));
console.log(chalk.green('------------------------------'));
console.log('\n');
await client.rawRequest({
method: 'POST',
url: `https://api.contentful.com/spaces/${space.sys.id}/preview_environments`,
data: {
configurations: [
{
contentType: 'landingPage',
enabled: true,
example: false,
url: `http://localhost:3000/{locale}/{entry.fields.slug}?mode=preview`,
},
],
name: 'Localhost Preview',
description: 'Preview content as a standard web page',
},
});
console.table({
Frontend: `http://localhost:3000/en-US`,
'Contentful App': `https://app.contentful.com/spaces/${space.sys.id}`,
});
})();