1 |
|
2 |
|
3 | const chalk = require('chalk');
|
4 | const path = require('path');
|
5 | const termImg = require('term-img');
|
6 |
|
7 | const emoji = require('./emoji');
|
8 | const logoFallback = require('./logo-fallback');
|
9 |
|
10 | const blue = chalk.blueBright;
|
11 | const bold = chalk.bold;
|
12 | const cyan = chalk.cyan;
|
13 |
|
14 | const commands = [
|
15 | {
|
16 | name: 'new',
|
17 | args: '<relative-path>',
|
18 | description: 'Create new project in current directory'
|
19 | },
|
20 | {
|
21 | name: 'lib',
|
22 | args: '',
|
23 | description: 'Add a component from the library'
|
24 | },
|
25 | {
|
26 | name: 'logout',
|
27 | args: '',
|
28 | description: 'Remove local GitHub credentials'
|
29 | },
|
30 | {
|
31 | name: 'component',
|
32 | args: '<name>',
|
33 | description: 'Create new React component'
|
34 | },
|
35 | {
|
36 | name: 'page',
|
37 | args: '<name> <human-readable-name>',
|
38 | description: 'Create new static site page component'
|
39 | },
|
40 | {
|
41 | name: 'rename',
|
42 | args: '<old-name> <new-name>',
|
43 | description: 'Rename React component'
|
44 | },
|
45 | {
|
46 | name: 'stateful',
|
47 | args: '<component-name>',
|
48 | description: 'Convert React component to stateful'
|
49 | },
|
50 | {
|
51 | name: 'stateless',
|
52 | args: '<component-name>',
|
53 | description: 'Convert React component to stateless'
|
54 | }
|
55 | ];
|
56 |
|
57 | const longestCommandLength = commands.reduce(
|
58 | (accum, { args, name }) => Math.max(accum, name.length + args.length),
|
59 | 0
|
60 | );
|
61 |
|
62 | const clearedGitHubCredentials = () => {
|
63 | console.log('GitHub credentials removed');
|
64 | };
|
65 |
|
66 | const componentAlreadyExists = componentName => {
|
67 | console.log(
|
68 | `${emoji('☠️', '×')} ${componentName} ${chalk.redBright(
|
69 | 'already exists. Skipping.'
|
70 | )}`
|
71 | );
|
72 | };
|
73 |
|
74 | const componentsAdded = () => {
|
75 | console.log(`${emoji('🎉', '♥')} Components added!`);
|
76 | };
|
77 |
|
78 | const downloadingComponents = () => {
|
79 | console.log(`${emoji('⬇️')} Downloading components`);
|
80 | };
|
81 |
|
82 | const emptyLine = () => {
|
83 | console.log('');
|
84 | };
|
85 |
|
86 | const error = text => {
|
87 | emptyLine();
|
88 | console.log(`${emoji('❌', '✖︎')} ${chalk.redBright(text)}`);
|
89 | emptyLine();
|
90 | };
|
91 |
|
92 | const messageList = messages => {
|
93 | messages.forEach(message => {
|
94 | console.log(`${emoji(message.emoji)} ${message.text}`);
|
95 | });
|
96 | };
|
97 |
|
98 | const printLineCommand = ({ args, name, description }) => {
|
99 | const padding = new String(' ').repeat(
|
100 | longestCommandLength - (name.length + args.length)
|
101 | );
|
102 |
|
103 | return `${emoji('👉', '•')} ${blue(name)} ${cyan(
|
104 | args
|
105 | )} ${padding} ${description}\n`;
|
106 | };
|
107 |
|
108 | const connectingToGitHub = () => {
|
109 | console.log(`${emoji('🌎')} Connecting go GitHub...`);
|
110 | };
|
111 |
|
112 | const githubRateLimitExceeded = resetTime => {
|
113 | const resetTimeMs = resetTime * 1000;
|
114 | const remainingMinutes = Math.round(
|
115 | (resetTimeMs - new Date().getTime()) / (1000 * 60)
|
116 | );
|
117 | const sparkleRainbow5000 =
|
118 | emoji('✨') +
|
119 | chalk.redBright('5') +
|
120 | chalk.yellowBright('0') +
|
121 | chalk.greenBright('0') +
|
122 | chalk.cyan('0') +
|
123 | emoji('✨');
|
124 |
|
125 | console.log(
|
126 | `${emoji('😭', '×')} ${chalk.redBright(
|
127 | 'Uh oh! GitHub API usage limit exceeded.'
|
128 | )}
|
129 |
|
130 | ${chalk.bold('Here are some things you can do:')}
|
131 | • Run ${chalk.cyan(
|
132 | 'creuna lib'
|
133 | )} again and log in to Github. This will increase your API usage limit from 60 to ${sparkleRainbow5000} requests per hour.
|
134 | • Download components directly from ${chalk.cyan(
|
135 | 'https://github.com/Creuna-Oslo/react-components'
|
136 | )}
|
137 | • Try again in ${remainingMinutes} minutes.
|
138 |
|
139 | The 60 requests per hour limit is shared between all non-authenticated users on the same network.\n`
|
140 | );
|
141 | };
|
142 |
|
143 | const gitHubLoginError = tokenName => {
|
144 | error(`Failed to log in. Things to try:
|
145 | • Try again! You might've had a typo.
|
146 | • If you have previously logged in on this computer, try going to ${chalk.blueBright(
|
147 | 'https://github.com/settings/tokens'
|
148 | )} and deleting the token called ${chalk.cyan(tokenName)}`);
|
149 | };
|
150 |
|
151 | const gitHubReadError = error => {
|
152 | console.log(
|
153 | `${emoji('🙀', '×')} ${chalk.redBright("Oh no! Couldn't get files!")}
|
154 | You should let ${chalk.blueBright(
|
155 | 'asbjorn.hegdahl@creuna.no'
|
156 | )} know ASAP.\n${error}`
|
157 | );
|
158 | };
|
159 |
|
160 | const gitHubRequestTimeout = () => {
|
161 | console.log(
|
162 | `${emoji('😩', '×')} ${chalk.redBright(
|
163 | "Couldn't connect to GitHub. Make sure you're connected to the interwebs!"
|
164 | )}`
|
165 | );
|
166 | };
|
167 |
|
168 | const help = () => {
|
169 | termImg(path.join(__dirname, 'creuna.png'), {
|
170 | fallback: () => {
|
171 | console.log(logoFallback);
|
172 | }
|
173 | });
|
174 | emptyLine();
|
175 | console.log(`${bold('Usage:')} creuna ${blue('<command>')}\n`);
|
176 | console.log(bold('Commands:'));
|
177 | console.log(commands.map(printLineCommand).join(''));
|
178 | console.log(
|
179 | `${emoji('🌈', '♥')} All command ${cyan('<arguments>')} are optional\n`
|
180 | );
|
181 | };
|
182 |
|
183 | const missingFile = () => {
|
184 | console.log(chalk.redBright(`${emoji('⁉️', '×')} Missing file`));
|
185 | };
|
186 |
|
187 | const noComponentsToWrite = () => {
|
188 | console.log(
|
189 | `${emoji('😐', '×')} ${chalk.redBright('No components to write. Exiting')}`
|
190 | );
|
191 | };
|
192 |
|
193 | const searchingForComponents = () => {
|
194 | console.log(`${emoji('🕵')} Searching for components...`);
|
195 | };
|
196 |
|
197 | const noComponentsSelected = () => {
|
198 | console.log(chalk.redBright('No components selected. Exiting.'));
|
199 | };
|
200 |
|
201 | const selectComponents = () => {
|
202 | console.log(chalk.bold('Select components'));
|
203 | console.log(
|
204 | `Use ${blue('[Spacebar]')} to select and deselect, ${blue(
|
205 | '[Enter]'
|
206 | )} to download selected, ${blue('[ESC]')} to abort\n`
|
207 | );
|
208 | };
|
209 |
|
210 | const selectComponentsCancel = () => {
|
211 | console.log(chalk.redBright('Cancelled. Exiting.'));
|
212 | };
|
213 |
|
214 | const unrecognizedCommand = command => {
|
215 | console.log(
|
216 | `${emoji('😱', '×')} Unrecognized command "${chalk.redBright(command)}".`
|
217 | );
|
218 | };
|
219 |
|
220 | const versionConflict = (currentVersion, latestVersion) => {
|
221 | console.log(
|
222 | `${emoji('🦄', '︎︎♥')} ${chalk.greenBright(
|
223 | `You are using version ${chalk.blueBright(
|
224 | currentVersion
|
225 | )}, but the latest version is ${chalk.blueBright(latestVersion)}.`
|
226 | )}`
|
227 | );
|
228 | console.log(
|
229 | `${emoji('👩💻')} Run ${chalk.blueBright(
|
230 | 'yarn global add @creuna/cli'
|
231 | )} or ${chalk.cyan('npm i -g @creuna/cli')} to get the latest version.`
|
232 | );
|
233 | };
|
234 |
|
235 | const writingFiles = () => {
|
236 | console.log(`${emoji('💾')} Writing files`);
|
237 | };
|
238 |
|
239 | module.exports = {
|
240 | clearedGitHubCredentials,
|
241 | componentAlreadyExists,
|
242 | componentsAdded,
|
243 | connectingToGitHub,
|
244 | downloadingComponents,
|
245 | emptyLine,
|
246 | error,
|
247 | gitHubLoginError,
|
248 | githubRateLimitExceeded,
|
249 | gitHubReadError,
|
250 | gitHubRequestTimeout,
|
251 | help,
|
252 | messageList,
|
253 | missingFile,
|
254 | noComponentsSelected,
|
255 | noComponentsToWrite,
|
256 | searchingForComponents,
|
257 | selectComponents,
|
258 | selectComponentsCancel,
|
259 | unrecognizedCommand,
|
260 | versionConflict,
|
261 | writingFiles
|
262 | };
|