-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathindex.ts
More file actions
85 lines (71 loc) · 2.58 KB
/
Copy pathindex.ts
File metadata and controls
85 lines (71 loc) · 2.58 KB
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
/* eslint-disable no-console */
import { statSync, existsSync } from 'fs';
import { join } from 'path';
import { green, red } from 'chalk';
import fetch from 'node-fetch';
import { ArgvFactory } from '@opensumi/ide-utils/lib/argv';
const PRODUCTION_NAME = process.env.PRODUCTION_NAME || 'OpenSumi';
const CLIENT_ID = process.env.CLIENT_ID;
const SUMI_SERVER_HOST = process.env.SUMI_SERVER_HOST || 'http://0.0.0.0:8000';
const OPENER_ROUTE = process.env.OPENER_ROUTE || 'open';
enum OpenType {
url = 'url',
file = 'file',
}
function openPathOrUrl(pathOrUrl: string): void {
if (!CLIENT_ID) {
// eslint-disable-next-line no-console
console.error(red(`${PRODUCTION_NAME} Client id is undefined!`));
process.exit(0);
}
let type: OpenType = OpenType.file;
let fullPathOrUrl = pathOrUrl;
if (isHttpProtocol(pathOrUrl)) {
type = OpenType.url;
} else if (isRelativePath(pathOrUrl)) {
fullPathOrUrl = join(process.cwd(), pathOrUrl);
}
if (type === OpenType.file) {
if (!existsSync(fullPathOrUrl)) {
// eslint-disable-next-line no-console
console.error(red(`The file path ${fullPathOrUrl} is not exist!`));
process.exit(0);
}
if (statSync(fullPathOrUrl).isDirectory()) {
console.error(red('Directory is unsupported'));
process.exit(0);
}
}
const query = `?type=${type}&${type}=${encodeURIComponent(fullPathOrUrl)}&clientId=${CLIENT_ID}`;
fetch(`${SUMI_SERVER_HOST}/${OPENER_ROUTE}${query}`).catch((err) => {
// eslint-disable-next-line no-console
console.error(red(`Open ${type} ${fullPathOrUrl} error: \n ${err.message}`));
process.exit(1);
});
}
const argv = new ArgvFactory(process.argv)
.usage(
`
Help: Open files or website from a shell.
By default, opens each file using the ${PRODUCTION_NAME} for that file.
If the file is in the form of a URL, will be opened the website use internal browser.
Examples:
1. ${green('open https://www.hostname.com')} Will open the website use internal browser.
2. ${green('open ./package.json')} Will open the file use ${PRODUCTION_NAME}.
3. ${green('open /path/to/file')} Will open the file use ${PRODUCTION_NAME}.
`,
)
.help().argv;
if (argv._[0] !== undefined) {
openPathOrUrl(argv._[0].toString());
} else {
// eslint-disable-next-line no-console
console.error(red('The path or url is not defined.'));
process.exit(0);
}
function isRelativePath(path: string): boolean {
return path.startsWith('./') || !path.startsWith('/');
}
function isHttpProtocol(url: string): boolean {
return !!url && (url.startsWith('http://') || url.startsWith('https://'));
}