1 | const ora = require('ora');
|
2 | const prompt = require('prompt');
|
3 | const readPkg = require('read-pkg');
|
4 | const domains = require('../../lib/domains');
|
5 | const environments = require('../../lib/environments');
|
6 | const notice = require('../../lib/notice');
|
7 | const assertPkg = require('../../lib/package-json').assert;
|
8 |
|
9 | const spinner = ora();
|
10 |
|
11 | prompt.colors = false;
|
12 | prompt.message = '';
|
13 | prompt.delimiter = '';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const askDomainName = () => new Promise((resolve, reject) => {
|
19 | const schema = {
|
20 | properties: {
|
21 | domainName: {
|
22 |
|
23 | pattern: /^(\*\.)?(((?!-)[a-z0-9-]{0,62}[a-z0-9])\.)+((?!-)[a-z0-9-]{1,62}[a-z0-9])$/,
|
24 | description: 'Domain name to add:',
|
25 | message: 'Must be a valid domain name (lowercase only).',
|
26 | required: true,
|
27 | },
|
28 | },
|
29 | };
|
30 | prompt.start();
|
31 | prompt.get(schema, (err, result) => {
|
32 | if (err) return reject(err);
|
33 |
|
34 | return resolve(result);
|
35 | });
|
36 | });
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | const askEnvironment = () => new Promise((resolve, reject) => {
|
42 | console.log(`
|
43 | Please select the environment to which you want to attach the domain.
|
44 | `);
|
45 | const schema = {
|
46 | properties: {
|
47 | environment_index: {
|
48 | description: 'Environment to use:',
|
49 | pattern: /^(?!-)[a-zA-Z]$/,
|
50 | default: 'A',
|
51 | required: true,
|
52 | },
|
53 | },
|
54 | };
|
55 | prompt.start();
|
56 | prompt.get(schema, (err, result) => {
|
57 | if (err) return reject(err);
|
58 |
|
59 | return resolve(result);
|
60 | });
|
61 | });
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | const error = (err) => {
|
68 | console.log('Oops! Something went wrong:');
|
69 | console.log(err);
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | const pickEnvironment = async (envs) => {
|
77 | if (envs.environments.length < 1) return 'prod';
|
78 | if (envs.environments.length < 2) return envs.environments[0].name;
|
79 |
|
80 | await environments.showAvailableEnvironments(envs);
|
81 | const env = await askEnvironment();
|
82 | const index = env.environment_index.toUpperCase().charCodeAt(0) - 65;
|
83 | if (index > envs.environments.length - 1) {
|
84 | throw new Error('Invalid input.');
|
85 | }
|
86 | return envs.environments[index].name;
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | const addDomain = async (siteName) => {
|
94 | const pkg = await readPkg();
|
95 | const { linc } = pkg;
|
96 | if (!linc || !linc.buildProfile) {
|
97 | throw new Error('Initalisation incomplete. Did you forget to run `linc site create`?');
|
98 | }
|
99 |
|
100 | spinner.start('Retrieving environments. Please wait...');
|
101 | const envs = await environments.getAvailableEnvironments(siteName);
|
102 | spinner.stop();
|
103 |
|
104 | const envName = await pickEnvironment(envs);
|
105 |
|
106 | const { domainName } = await askDomainName();
|
107 |
|
108 | spinner.start('Adding domain. Please wait...');
|
109 | await domains.addDomain(domainName, envName, siteName);
|
110 | spinner.stop();
|
111 |
|
112 | console.log(`Domain name successfully added. Shortly, you may be receiving
|
113 | emails asking you to approve an SSL certificate (if needed).
|
114 | `);
|
115 | };
|
116 |
|
117 | exports.command = 'add';
|
118 | exports.desc = 'Add a domain name';
|
119 | exports.handler = (argv) => {
|
120 | const { siteName } = argv;
|
121 |
|
122 | if (!siteName) {
|
123 | console.log('This project does not have a site name. Please create a site first.');
|
124 | process.exit(255);
|
125 | }
|
126 |
|
127 | assertPkg();
|
128 |
|
129 | notice();
|
130 |
|
131 | addDomain(siteName)
|
132 | .then(() => {})
|
133 | .catch(err => {
|
134 | spinner.stop();
|
135 | error(err);
|
136 | });
|
137 | };
|