forked from mixn/carbon-now-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
Β·174 lines (154 loc) Β· 3.83 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
// Packages
const meow = require('meow');
const chalk = require('chalk');
const opn = require('opn');
const queryString = require('query-string');
const terminalImage = require('terminal-image');
const Listr = require('listr');
// Source
const processContent = require('./src/process-content.js');
const getLanguage = require('./src/get-language.js');
const headlessVisit = require('./src/headless-visit.js');
const interactiveMode = require('./src/interactive-mode.js');
// Helpers
let settings = require('./src/helpers/default-settings');
const cli = meow(`
${chalk.bold('Usage')}
$ carbon-now <file>
${chalk.bold('Options')}
-s, --start Starting line of <file>
-e, --end Ending line of <file>
-i, --interactive Interactive mode
-l, --location Screenshot save location, default: cwd
-o, --open Open in browser instead of saving
${chalk.bold('Examples')}
See: https://github.com/mixn/carbon-now-cli#examples
`,
{
flags: {
start: {
type: 'string',
alias: 's',
default: 1
},
end: {
type: 'string',
alias: 'e',
default: 1000
},
open: {
type: 'boolean',
alias: 'o',
default: false
},
location: {
type: 'string',
alias: 'l',
default: process.cwd()
},
interactive: {
type: 'boolean',
alias: 'i',
default: false
}
}
});
const [file] = cli.input;
const {start, end, open, location, interactive} = cli.flags;
let url = 'https://carbon.now.sh/';
// Deny everything if not at least one argument (file) specified
if (!file) {
console.error(`
${chalk.red('Error: Please provide at least a file.')}
$ carbon-now <file>
`);
process.exit(1);
}
// Run main CLI programm
(async () => {
// If --interactive, enter interactive mode and adopt settings
if (interactive) {
settings = {
...settings,
...(await interactiveMode())
};
}
// Prepare tasks
const tasks = new Listr([
// Task 1: Process and encode file
{
title: `Processing ${file}`,
task: async ctx => {
try {
const processedContent = await processContent(file, start, end);
ctx.encodedContent = encodeURIComponent(processedContent);
} catch (error) {
return Promise.reject(error);
}
}
},
// Task 2: Merge all settings (interactive, given, default, detected)
{
title: 'Preparing connection',
task: ({encodedContent}) => {
settings = {
...settings,
code: encodedContent,
l: getLanguage(file)
};
url = `${url}?${queryString.stringify(settings)}`;
}
},
// Task 3: Open only browser if --open
{
title: 'Opening in browser',
skip: () => !open,
task: () => {
opn(url);
}
},
// Task 4: Download image to --location if not --open
{
title: 'Fetching beautiful image',
skip: () => open,
task: () => headlessVisit(url, location, settings.type)
}
]);
// Run tasks
// I like the control-flow-iness of .then() and .catch() here
// and prefer it to async/await in this case⦠go ahead, JUDGE ME
tasks
.run()
.then(async () => {
const downloadedFile = `${location}/carbon.${settings.type}`;
console.log(`
${chalk.green('Done!')}`
);
if (open) {
console.log(`
Browser opened β finish your image there! π`
);
} else {
console.log(`
The file can be found here: ${downloadedFile} π`
);
if (process.env.TERM_PROGRAM.match('iTerm')) {
console.log(`
iTerm2 should display the image below. π
${await terminalImage.file(downloadedFile)}`
);
}
}
process.exit();
})
.catch(() => {
console.error(`
${chalk.red('Error: Sending code to https://carbon.now.sh went wrong.')}
This is mostly due to:
β Insensical input like \`--start 10 --end 2\`
β Carbon being down or taking too long to respond
β Your internet connection not working or being too slow`);
process.exit(1);
});
})();