|
| 1 | +import { CmdLetBase } from '../../commands/cmdlet.js'; |
| 2 | +import { Input } from '../../io/input.js'; |
| 3 | +import { Output } from '../../io/output.js'; |
| 4 | +import { Token } from '../../io/token.js'; |
| 5 | +import { InputBuffer } from '../../io/zmodem/input.js'; |
| 6 | +import { OutputBuffer } from '../../io/zmodem/output.js'; |
| 7 | +import { Sz } from '../../io/zmodem/sz.js'; |
| 8 | +import { Zmodem } from '../../io/zmodem/zmodem.js'; |
| 9 | +import { Files } from '../../misc/files.js'; |
| 10 | +import { Version } from '../../misc/version.js'; |
| 11 | +import { Var } from '../../vars/var.js'; |
| 12 | + |
| 13 | +export class SzCmdLet extends CmdLetBase { |
| 14 | + name = 'sz'; |
| 15 | + category = 'files'; |
| 16 | + help = 'send a file using Z-Modem'; |
| 17 | + |
| 18 | + private static readonly USAGE: string = `Usage: sz |
| 19 | +
|
| 20 | +cat file - send file |
| 21 | + file the file to send`; |
| 22 | + |
| 23 | + public override runSync(_tokens: Token[]): Var { |
| 24 | + throw new Error("can't run in synchronous mode"); |
| 25 | + } |
| 26 | + |
| 27 | + public override async run(tokens: Token[]): Promise<Var> { |
| 28 | + const vars = this.transform(tokens, [this.parseLiteral]); |
| 29 | + if (vars === null) return this.usage(); |
| 30 | + |
| 31 | + const [filePath] = vars as [string]; |
| 32 | + Output.writeln(`Sending file: ${Output.green(filePath)}`); |
| 33 | + |
| 34 | + const debugFileName = Files.getRandomFileName('debug'); |
| 35 | + let debug = (_msg: string) => {}; |
| 36 | + |
| 37 | + if (Output.getDebugging()) { |
| 38 | + Output.debug(`writing debug to: ${debugFileName}`); |
| 39 | + const debugFile = new File(debugFileName, 'w'); |
| 40 | + debug = (msg: string) => { |
| 41 | + debugFile.write(`${msg}\n`); |
| 42 | + debugFile.flush(); |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + debug('Starting transmission'); |
| 47 | + |
| 48 | + Output.writeln(`Transmission will start in 2 seconds....`); |
| 49 | + Output.writeln(); |
| 50 | + |
| 51 | + const input = new InputBuffer(debug); |
| 52 | + const output = new OutputBuffer(debug); |
| 53 | + |
| 54 | + Input.setInterceptRaw(input); |
| 55 | + try { |
| 56 | + await Sz.sleep(2000); |
| 57 | + const zmodem = new Zmodem(input, output, debug); |
| 58 | + await zmodem.send(filePath); |
| 59 | + } catch (error) { |
| 60 | + if (error instanceof Error) { |
| 61 | + debug(`Error: ${error.message}`); |
| 62 | + debug(`Stack: ${error.stack}`); |
| 63 | + } else { |
| 64 | + debug(`Error: Unknown error`); |
| 65 | + } |
| 66 | + } finally { |
| 67 | + Input.setInterceptRaw(null); |
| 68 | + this.checkDebugFile(debugFileName); |
| 69 | + } |
| 70 | + return Var.ZERO; |
| 71 | + } |
| 72 | + |
| 73 | + private checkDebugFile(debugFileName: string) { |
| 74 | + Output.debug('ZModem output...'); |
| 75 | + try { |
| 76 | + const debugFile = new File(debugFileName, 'r'); |
| 77 | + for ( |
| 78 | + let line = debugFile.readLine(); |
| 79 | + line.length != 0; |
| 80 | + line = debugFile.readLine() |
| 81 | + ) { |
| 82 | + Output.debug(`\t${Output.yellow(line.trimEnd())}`); |
| 83 | + } |
| 84 | + } finally { |
| 85 | + Output.debug('ZModem output complete'); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public usage(): Var { |
| 90 | + Output.writeln(SzCmdLet.USAGE); |
| 91 | + return Var.ZERO; |
| 92 | + } |
| 93 | + |
| 94 | + public override isSupported(): boolean { |
| 95 | + switch (Process.platform) { |
| 96 | + case 'linux': |
| 97 | + if (Version.VERSION >= Version.BINARY_MODE_MIN_VERSION) { |
| 98 | + return true; |
| 99 | + } else { |
| 100 | + return false; |
| 101 | + } |
| 102 | + case 'windows': |
| 103 | + case 'barebone': |
| 104 | + case 'darwin': |
| 105 | + case 'freebsd': |
| 106 | + case 'qnx': |
| 107 | + default: |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments