-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (59 loc) · 1.49 KB
/
index.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
#!/usr/bin/env node
require('./src/set-require-base-to-project-root');
const program = require('commander');
const packageConfig = require('./package.json');
const getId = require('src/get-id');
const openAsApp = require('src/open-as-chrome-app');
const openUrl = require('src/open-url');
let id;
program
.version(packageConfig.version, '-v, --version')
.description(packageConfig.description)
.option('-o, --open', 'Open in the default browser')
.option('-a, --app', 'Open with Chrome in app mode')
.arguments('<url>')
.action((url) => {
id = getId(url);
})
.parse(process.argv)
;
const open = (id) => {
const url = `https://youtube.com/embed/${ id }`;
if (!program.args.length && process.stdin.isTTY) {
program.help();
return
}
if (program.app) {
return openAsApp(url);
}
if (program.open) {
return openUrl(url);
}
console.log(url);
return;
}
const chunks = [];
process.stdin.on('data', (chunk) => {
chunks.push(chunk);
});
process.stdin.on('end', () => {
const urlString = chunks
.trim()
.split('\\\\') // Removes double back slashes
.map((substring) => substring.split('\\').join('')) // Unescapes back slashes
.join('\\') // Replaces double back slashes with single
;
id = getId(urlString);
open(id);
})
process.stdout.on('error', function( err ) {
if (err.code == "EPIPE") {
process.exit(0);
}
});
if (process.stdin.isTTY) {
open(id);
process.exit(0);
}
process.stdin.resume();
process.stdin.setEncoding('utf8');