UNPKG

6.66 kBJavaScriptView Raw
1/* eslint-env node */
2/* eslint-disable no-console */
3const chalk = require('chalk');
4const path = require('path');
5const termImg = require('term-img');
6
7const emoji = require('./emoji');
8const logoFallback = require('./logo-fallback');
9
10const blue = chalk.blueBright;
11const bold = chalk.bold;
12const cyan = chalk.cyan;
13
14const 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 mockup 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
57const longestCommandLength = commands.reduce(
58 (accum, { args, name }) => Math.max(accum, name.length + args.length),
59 0
60);
61
62const clearedGitHubCredentials = () => {
63 console.log('GitHub credentials removed');
64};
65
66const componentAlreadyExists = componentName => {
67 console.log(
68 `${emoji('☠️', '×')} ${componentName} ${chalk.redBright(
69 'already exists. Skipping.'
70 )}`
71 );
72};
73
74const componentsAdded = () => {
75 console.log(`${emoji('🎉', '♥')} Components added!`);
76};
77
78const downloadingComponents = () => {
79 console.log(`${emoji('⬇️')} Downloading components`);
80};
81
82const emptyLine = () => {
83 console.log('');
84};
85
86const error = text => {
87 emptyLine();
88 console.log(`${emoji('❌', '✖︎')} ${chalk.redBright(text)}`);
89 emptyLine();
90};
91
92const messageList = messages => {
93 messages.forEach(message => {
94 console.log(`${emoji(message.emoji)} ${message.text}`);
95 });
96};
97
98const 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
108const connectingToGitHub = () => {
109 console.log(`${emoji('🌎')} Connecting go GitHub...`);
110};
111
112const 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
139The 60 requests per hour limit is shared between all non-authenticated users on the same network.\n`
140 );
141};
142
143const 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
151const gitHubReadError = error => {
152 console.log(
153 `${emoji('🙀', '×')} ${chalk.redBright("Oh no! Couldn't get files!")}
154You should let ${chalk.blueBright(
155 'asbjorn.hegdahl@creuna.no'
156 )} know ASAP.\n${error}`
157 );
158};
159
160const 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
168const 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
183const missingFile = () => {
184 console.log(chalk.redBright(`${emoji('⁉️', '×')} Missing file`));
185};
186
187const noComponentsToWrite = () => {
188 console.log(
189 `${emoji('😐', '×')} ${chalk.redBright('No components to write. Exiting')}`
190 );
191};
192
193const searchingForComponents = () => {
194 console.log(`${emoji('🕵')} Searching for components...`);
195};
196
197const noComponentsSelected = () => {
198 console.log(chalk.redBright('No components selected. Exiting.'));
199};
200
201const 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
210const selectComponentsCancel = () => {
211 console.log(chalk.redBright('Cancelled. Exiting.'));
212};
213
214const unrecognizedCommand = command => {
215 console.log(
216 `${emoji('😱', '×')} Unrecognized command "${chalk.redBright(command)}".`
217 );
218};
219
220const version = versionNumber => {
221 console.log(versionNumber);
222};
223
224const versionConflict = (currentVersion, latestVersion) => {
225 console.log(
226 `${emoji('🦄', '︎︎♥')} ${chalk.greenBright(
227 `You are using version ${chalk.blueBright(
228 currentVersion
229 )}, but the latest version is ${chalk.blueBright(latestVersion)}.`
230 )}`
231 );
232 console.log(
233 `${emoji('👩‍💻')} Run ${chalk.blueBright(
234 'yarn global add @creuna/cli'
235 )} or ${chalk.cyan('npm i -g @creuna/cli')} to get the latest version.`
236 );
237};
238
239const writingFiles = () => {
240 console.log(`${emoji('💾')} Writing files`);
241};
242
243module.exports = {
244 clearedGitHubCredentials,
245 componentAlreadyExists,
246 componentsAdded,
247 connectingToGitHub,
248 downloadingComponents,
249 emptyLine,
250 error,
251 gitHubLoginError,
252 githubRateLimitExceeded,
253 gitHubReadError,
254 gitHubRequestTimeout,
255 help,
256 messageList,
257 missingFile,
258 noComponentsSelected,
259 noComponentsToWrite,
260 searchingForComponents,
261 selectComponents,
262 selectComponentsCancel,
263 unrecognizedCommand,
264 version,
265 versionConflict,
266 writingFiles
267};