From a9cf2a1d210444bc37988b02b4a0dfbf0db9a4b9 Mon Sep 17 00:00:00 2001 From: b1ek Date: Fri, 17 Mar 2023 16:24:18 +1000 Subject: [PATCH] export file command --- react/resume/package.json | 1 + .../src/emulator/commands/export_file.js | 43 +++++++++++++++++++ react/resume/src/emulator/commands/index.js | 1 + 3 files changed, 45 insertions(+) create mode 100644 react/resume/src/emulator/commands/export_file.js diff --git a/react/resume/package.json b/react/resume/package.json index f37bf4a..9367b3f 100644 --- a/react/resume/package.json +++ b/react/resume/package.json @@ -21,6 +21,7 @@ "dependencies": { "@parcel/fs": "^2.8.3", "copy-to-clipboard": "^3.3.3", + "file-saver": "^2.0.5", "memfs": "^3.4.13", "react": "^18.2.0", "react-dom": "^18.2.0", diff --git a/react/resume/src/emulator/commands/export_file.js b/react/resume/src/emulator/commands/export_file.js new file mode 100644 index 0000000..2216896 --- /dev/null +++ b/react/resume/src/emulator/commands/export_file.js @@ -0,0 +1,43 @@ +import { Terminal } from 'xterm'; +import { saveAs } from 'file-saver'; +const fs = require('../fs'); + +/** + * + * @param { string[] } argv + * @param { Terminal } terminal + */ +module.exports = (argv, terminal) => { + if (argv.indexOf('--help') != -1 || argv.length == 1) { + terminal.write( +`Usage: ${argv[0]} [FILES] [-z] +Export file from filesystem to your device. + -z Compress in zip format (not supported)`); + return; + } + + let files = [...argv]; + files.shift(); + + if (argv.indexOf('-z') != -1) { + terminal.write(`${argv[0]}: cannot zip '${files.join(' ').replace('\'', "\\'")}': not supported\n`); + return; + } + + try { + var isFileSaverSupported = !!new Blob; + } catch (e) {} + + if (!isFileSaverSupported) { + terminal.write(`${argv[0]}: Your browser does not support this feature.`); + return; + } + + if (!fs.existsSync(argv[1])) { + terminal.write(`${argv[0]}: cannot open ${argv[1]}: no such file or directory\n`); + return; + } + saveAs(new Blob([fs.readFileSync(argv[1])]), argv[1]); + + +} \ No newline at end of file diff --git a/react/resume/src/emulator/commands/index.js b/react/resume/src/emulator/commands/index.js index 315b2e3..9ff1979 100644 --- a/react/resume/src/emulator/commands/index.js +++ b/react/resume/src/emulator/commands/index.js @@ -7,6 +7,7 @@ let cmds = { 'skills': require('./skills'), 'mkdir': require('./mkdir'), 'wget': require('./wget'), + 'export_file': require('./export_file'), // alias l='ls -l' 'l': (a,t) => {require('./ls')([...a, '-l'], t)},