-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.js
72 lines (65 loc) · 2.1 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
const spaceImport = require('contentful-import');
const inquirer = require('inquirer');
const chalk = require('chalk');
const path = require('path');
const { writeFileSync } = require('fs');
const envfile = require('envfile');
const exportFile = require('./contentful-config.json');
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 ship published production-ready content in your Gatsby app.
The ${chalk.green('Content Preview API Token')}
will be used to show not published data in your development environment.
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: 'deliveryToken',
message: 'Your Content Delivery API access token',
},
{
name: 'managementToken',
message: 'Your Content Management API access token',
},
];
inquirer
.prompt(questions)
.then(({ spaceId, deliveryToken, managementToken }) => {
console.log('Writing config file...');
const configFilePath = path.resolve(__dirname, '..', '.env');
const envData = envfile.stringifySync({
SPACE_ID: spaceId,
ACCESS_TOKEN: deliveryToken,
});
writeFileSync(configFilePath, envData);
console.log(`Config file ${chalk.yellow(configFilePath)} written`);
return { spaceId, managementToken };
})
.then(({ spaceId, managementToken }) =>
spaceImport({ spaceId, managementToken, content: exportFile }),
)
.then(() => {
console.log(
`All set! You can now run ${chalk.yellow(
'yarn develop',
)} to see it in action.`,
);
})
.catch(error => console.error(error));