-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdenoget.ts
189 lines (170 loc) · 4.2 KB
/
denoget.ts
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env deno --allow-all
const {
args,
env,
readDirSync,
mkdirSync,
writeFileSync,
exit,
stdin,
run,
} = Deno;
import * as path from 'https://deno.land/x/fs/path.ts';
import { parse } from './shebang.ts';
const enc = new TextEncoder();
const dec = new TextDecoder('utf-8');
enum Permission {
Unknown,
Read,
Write,
Net,
Env,
Run,
All,
}
function getPermissionFromFlag(flag: string): Permission {
switch (flag) {
case '--allow-read':
return Permission.Read;
case '--allow-write':
return Permission.Write;
case '--allow-net':
return Permission.Net;
case '--allow-env':
return Permission.Env;
case '--allow-run':
return Permission.Run;
case '--allow-all':
return Permission.All;
case '-A':
return Permission.All;
}
return Permission.Unknown;
}
function getFlagFromPermission(perm: Permission): string {
switch (perm) {
case Permission.Read:
return '--allow-read';
case Permission.Write:
return '--allow-write';
case Permission.Net:
return '--allow-net';
case Permission.Env:
return '--allow-env';
case Permission.Run:
return '--allow-run';
case Permission.All:
return '--allow-all';
}
return '';
}
async function readCharacter(): Promise<string> {
const byteArray = new Uint8Array(1024);
await stdin.read(byteArray);
const dec = new TextDecoder();
const line = dec.decode(byteArray);
return line[0];
}
async function grantPermission(
perm: Permission,
moduleName: string = 'Deno'
): Promise<boolean> {
let msg = `${moduleName} requests `;
switch (perm) {
case Permission.Read:
msg += 'read access to file system. ';
break;
case Permission.Write:
msg += 'write access to file system. ';
break;
case Permission.Net:
msg += 'network access. ';
break;
case Permission.Env:
msg += 'access to environment variable. ';
break;
case Permission.Run:
msg += 'access to run a subprocess. ';
break;
case Permission.All:
msg += 'all available access. ';
break;
default:
return false;
}
msg += 'Grant permanently? [yN]';
console.log(msg);
const input = await readCharacter();
if (input !== 'y' && input !== 'Y') {
return false;
}
return true;
}
function createDirIfNotExists(path: string) {
try {
readDirSync(path);
} catch (e) {
mkdirSync(path);
}
}
async function main() {
const { HOME } = env();
if (!HOME) {
throw new Error('$HOME is not defined.');
}
const DENOGET_HOME = `${HOME}/.deno/denoget`;
const DENOGET_BIN = `${DENOGET_HOME}/bin`;
const modulePath: string = args[args.length - 1];
if (!modulePath.startsWith('http')) {
throw new Error('module path is incorrect.');
}
const moduleName = path.basename(modulePath, '.ts');
const wget = run({
args: ['wget', '--quiet', '-O', '-', modulePath],
stdout: 'piped',
});
const moduleText = dec.decode(await wget.output());
const status = await wget.status();
wget.close();
if (status.code !== 0) {
throw new Error(`Failed to get remote script: ${modulePath}`);
}
console.log('Completed loading remote script.');
createDirIfNotExists(DENOGET_HOME);
createDirIfNotExists(DENOGET_BIN);
const BIN_FILE_PATH = `${DENOGET_BIN}/${moduleName}`;
const shebang = parse(moduleText.split('\n')[0]);
const grantedPermissions: Array<Permission> = [];
for (const flag of shebang.args) {
const permission = getPermissionFromFlag(flag);
if (permission === Permission.Unknown) {
continue;
}
if (!(await grantPermission(permission, moduleName))) {
continue;
}
grantedPermissions.push(permission);
}
const commands = [
'deno',
...grantedPermissions.map(getFlagFromPermission),
modulePath,
'$@',
];
writeFileSync(BIN_FILE_PATH, enc.encode(commands.join(' ')));
const makeExecutable = run({ args: ['chmod', '+x', BIN_FILE_PATH] });
await makeExecutable.status();
makeExecutable.close();
console.log(`Successfully installed ${moduleName}.`);
}
try {
main();
} catch (e) {
const err = e as Error;
if (err.message) {
console.log(err.message);
exit(1);
}
console.log(e);
exit(1);
}