UNPKG

3.66 kBJavaScriptView Raw
1const ora = require('ora');
2const prompt = require('prompt');
3const readPkg = require('read-pkg');
4const domains = require('../../lib/domains');
5const environments = require('../../lib/environments');
6const notice = require('../../lib/notice');
7const assertPkg = require('../../lib/package-json').assert;
8
9const spinner = ora();
10
11prompt.colors = false;
12prompt.message = '';
13prompt.delimiter = '';
14
15/**
16 * Ask user for domain name
17 */
18const askDomainName = () => new Promise((resolve, reject) => {
19 const schema = {
20 properties: {
21 domainName: {
22 // This is the pattern AWS uses for domain names
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 * Ask user which environment to use
40 */
41const askEnvironment = () => new Promise((resolve, reject) => {
42 console.log(`
43Please 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 * Error message
65 * @param err
66 */
67const error = (err) => {
68 console.log('Oops! Something went wrong:');
69 console.log(err);
70};
71
72/**
73 * Pick environment from a list
74 * @param envs
75 */
76const 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 * Add a domain
91 * @param siteName
92 */
93const addDomain = async (siteName) => { // eslint-disable-line consistent-return
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
113emails asking you to approve an SSL certificate (if needed).
114`);
115};
116
117exports.command = 'add';
118exports.desc = 'Add a domain name';
119exports.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};