1 | const _ = require('underscore');
|
2 | const ora = require('ora');
|
3 | const prompt = require('prompt');
|
4 | const figlet = require('figlet');
|
5 | const notice = require('../lib/notice');
|
6 | const readPkg = require('read-pkg');
|
7 | const writePkg = require('write-pkg');
|
8 | const installProfilePackage = require('../lib/install-profile-pkg');
|
9 | const dotLinc = require('../lib/dot-linc');
|
10 | const lincProfiles = require('../lib/linc-profiles');
|
11 | const assertPkg = require('../lib/package-json').assert;
|
12 |
|
13 | prompt.colors = false;
|
14 | prompt.message = '';
|
15 | prompt.delimiter = '';
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const showProfiles = () => {
|
21 | console.log(`
|
22 | Please choose a profile:
|
23 | `);
|
24 | _.each(lincProfiles, (p, k) => {
|
25 | console.log(`${k}) ${p.name}`);
|
26 | });
|
27 | };
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const askProfile = () => new Promise((resolve, reject) => {
|
33 | showProfiles();
|
34 |
|
35 | const schema = {
|
36 | properties: {
|
37 | profile: {
|
38 | pattern: /^[A-Za-z]$/,
|
39 | description: 'Profile to use for this site:',
|
40 | message: 'Please enter a valid option',
|
41 | type: 'string',
|
42 | default: 'A',
|
43 | },
|
44 | },
|
45 | };
|
46 | prompt.start();
|
47 | prompt.get(schema, (err, result) => {
|
48 | if (err) return reject(err);
|
49 |
|
50 | return resolve(result);
|
51 | });
|
52 | });
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | const error = (err) => {
|
59 | console.log('Something went wrong:');
|
60 | console.log(err.message);
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | const linclet = (msg) => new Promise((resolve, reject) => {
|
69 | figlet(msg, (err, data) => {
|
70 | if (err) return reject();
|
71 |
|
72 | console.log(data);
|
73 | return resolve();
|
74 | });
|
75 | });
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | const askOtherProfile = () => new Promise((resolve, reject) => {
|
81 | console.log();
|
82 |
|
83 | const schema = {
|
84 | properties: {
|
85 | profile: {
|
86 | description: '\nProfile name to use for this site:',
|
87 | message: 'Please enter a valid option',
|
88 | type: 'string',
|
89 | },
|
90 | },
|
91 | };
|
92 | prompt.start();
|
93 | prompt.get(schema, (err, result) => {
|
94 | if (err) return reject(err);
|
95 |
|
96 | return resolve(result.profile);
|
97 | });
|
98 | });
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | const initialise = async (argv) => {
|
106 | const spinner = ora();
|
107 |
|
108 | if (argv.buildProfile) {
|
109 | spinner.succeed('This project is already initialised.');
|
110 | process.exit(255);
|
111 | }
|
112 |
|
113 |
|
114 | dotLinc.ensureDir();
|
115 |
|
116 | const linc = {};
|
117 |
|
118 | try {
|
119 | await linclet;
|
120 |
|
121 | |
122 |
|
123 |
|
124 | const result = await askProfile();
|
125 | const selectedProfile = result.profile.toUpperCase();
|
126 | let profile = lincProfiles[selectedProfile].pkg;
|
127 | if (!profile) {
|
128 | profile = await askOtherProfile();
|
129 | }
|
130 | linc.buildProfile = profile;
|
131 |
|
132 | |
133 |
|
134 |
|
135 | spinner.start('Installing profile package. Please wait...');
|
136 | await installProfilePackage(profile, { force: true });
|
137 | spinner.succeed('Profile package installed.');
|
138 |
|
139 | |
140 |
|
141 |
|
142 | const packageJson = await readPkg();
|
143 | packageJson.linc = linc;
|
144 | await writePkg(packageJson);
|
145 |
|
146 | spinner.succeed('Updated package.json.');
|
147 | spinner.succeed('Done.');
|
148 | } catch (e) {
|
149 | error(e);
|
150 | }
|
151 | };
|
152 |
|
153 | exports.command = ['init', 'create'];
|
154 | exports.desc = 'Initialise a new site locally';
|
155 | exports.handler = (argv) => {
|
156 | assertPkg();
|
157 |
|
158 | notice();
|
159 |
|
160 | initialise(argv)
|
161 | .then(() => {});
|
162 | };
|