-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli.js
executable file
Β·175 lines (145 loc) Β· 3.54 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
175
#!/usr/bin/env node
// @ts-check
"use strict";
/**
* share-cli
* Quickly share files from command line to your local network
* Author: Mario Nebl <https://github.com/marionebl>
* License: MIT
*/
const crypto = require("crypto");
const chalk = require("chalk");
const meow = require("meow");
const Ink = require("ink");
const React = require("react");
const html = require("htm").bind(React.createElement);
const Provider = require("unstated").Provider;
const Subscribe = require("unstated").Subscribe;
const getFile = require("./lib/get-file").getFile;
const serveFile = require("./lib/serve-file").serveFile;
const UI = require("./lib/ui").UI;
const State = require("./lib/state").State;
const cli = meow(
`
Usage
$ share [file]
Options
-n, --name Forced download name of the file
Examples
$ share shared.png
http://192.168.1.1:1337/unequal-wish
$ cat shared.png | share --name=shared.png
http://192.168.1.1:1337/important-downtown
`,
{
// @ts-ignore
alias: {
n: "name"
}
}
);
const stdin = process.stdin;
/**
* Execute share-cli main procedure
*
* @param {string} filePath - non-flag arguments
* @param {Object<string, any>} args - flag arguments
* @return {Promise<void | Error>} - shareable address
*/
async function main(filePath, args) {
// Sanitize input
if (stdin.isTTY && typeof filePath === "undefined") {
const error = new Error("Either stdin or [file] have to be given");
// @ts-ignore
error.cli = true;
return error;
}
const state = new State();
const tree = html`
<${Provider} inject=${[state]}>
<${Subscribe} to=${[State]}>
${({state}) => html`<${UI} state=${state}/>`}
</${Subscribe}>
</${Provider}>
`;
const app = Ink.render(tree);
const isStdin = stdin.isTTY !== true && typeof filePath === "undefined";
state.setFilePhase('started', 'Preparing file');
// Get a file object
const fileResult = await getFile({
fileName: args.name,
filePath,
isStdin,
password:
args.password ||
encodeURIComponent(crypto.randomBytes(24).toString("hex"))
});
if (fileResult instanceof Error) {
state.setFilePhase('errored', 'Preparing file failed');
return fileResult;
}
state.setFilePhase('done', 'Prepared file');
const connectionResult = await serveFile(fileResult, {
tunnel: args.tunnel !== false,
state
});
if (connectionResult instanceof Error) {
return connectionResult;
}
state.subscribe(() => {
if (state.state.shutdown === true) {
process.nextTick(() => {
app.unmount();
process.exit(0);
});
return;
}
// TODO: Remodel using phases?
if (state.state.wait === true && !state.state.waiting) {
state.setWaiting(true);
const closeTime = Date.now() + 60 * 1000;
const timer = setInterval(() => {
state.trigger();
if (closeTime - Date.now() <= 0) {
process.exit(0);
}
}, 1000);
process.stdin.setRawMode(true);
const onData = data => {
switch (data.toString()) {
case '\u0003':
case '\u0004':
state.shutdown();
return;
case '\u0012':
clearInterval(timer);
process.stdin.setRawMode(false);
process.stdin.off('data', onData);
state.keep();
return;
}
}
process.stdin.on('data', onData);
}
});
return;
}
main(cli.input[0], cli.flags)
.then(result => {
if (result instanceof Error) {
// @ts-ignore
console.error(chalk.red(result.message));
}
})
.catch(error => {
if (error.cli) {
if (error.message) {
// @ts-ignore
console.error(chalk.red(error.message));
}
cli.showHelp(1);
}
setTimeout(() => {
throw error;
});
});