-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcldsh.js
More file actions
executable file
·71 lines (61 loc) · 1.83 KB
/
Copy pathcldsh.js
File metadata and controls
executable file
·71 lines (61 loc) · 1.83 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
#!/usr/bin/env node
import readline from 'node:readline';
import process from 'node:process';
import { loadEnvAndConfig } from './lib/env.js';
import * as printer from './lib/printer.js';
import { parseLine } from './lib/parse.js';
import * as cld from './lib/cld.js';
// Commands
import helpCmd from './commands/help.js';
import lsCmd from './commands/ls.js';
import cdCmd from './commands/cd.js';
import mkdirCmd from './commands/mkdir.js';
import uploadCmd from './commands/upload.js';
import rmCmd from './commands/rm.js';
import pwdCmd from './commands/pwd.js';
import treeCmd from './commands/tree.js';
import urlCmd from './commands/url.js';
loadEnvAndConfig();
let cwd = '';
let prevCwd = '';
const setCwd = (next) => { prevCwd = cwd; cwd = cld.norm(next); };
const getPrompt = () => (cwd ? `$~/${cwd} ` : `$~ `);
const ctx = {
get cwd() { return cwd; },
get prevCwd() { return prevCwd; },
setCwd,
utils: cld,
printer,
};
const commands = {
help: helpCmd,
ls: lsCmd,
cd: cdCmd,
mkdir: mkdirCmd,
upload: uploadCmd,
rm: rmCmd,
pwd: pwdCmd,
tree: treeCmd,
url: urlCmd,
};
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
printer.println(printer.dim('Cloudinary Shell — type "help" for commands.'));
function ask() { rl.question(getPrompt(), onLine); }
async function onLine(line) {
const { cmd, args, flags } = parseLine(line);
try {
if (!cmd) return ask();
if (cmd === 'exit' || cmd === 'quit') { rl.close(); process.exit(0); return; }
const run = commands[cmd];
if (!run) {
printer.println(printer.warn(`Unknown command: ${cmd}`));
printer.println(printer.dim('Type "help" to see available commands.'));
} else {
await run(ctx, args, flags);
}
} catch (e) {
printer.println(printer.error(e?.message || String(e)));
}
ask();
}
ask();