-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from Flow-Works/thinliquid/qol
🐛 bugfix: fix fs command bugs
- Loading branch information
Showing
8 changed files
with
195 additions
and
53 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,138 @@ | ||
import { table } from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'; | ||
|
||
const modeToString = (mode) => { | ||
if (mode.mode) mode = mode.mode; // handle fs.stat | ||
|
||
let result = new Buffer('drwxrwxrwx', 'ascii'); | ||
|
||
if (!(mode & 0o40000)) result[0] = 45; | ||
|
||
for (let i = 1, bit = 0o400; bit; i++, bit >>= 1) { | ||
if (!(mode & bit)) result[i] = 45; // ASCII for '-' | ||
} | ||
|
||
return result.toString(); | ||
}; | ||
|
||
export const metadata = { | ||
cmd: 'ls', | ||
description: 'List information about the FILEs (the current directory by default).' | ||
cmd: 'ls', | ||
description: | ||
'List information about the FILEs (the current directory by default).', | ||
}; | ||
|
||
export const exec = (fs, term, usr, dir) => { | ||
return fs.readdirSync(dir.path); | ||
}; | ||
export const exec = (fs, term, usr, dir, args) => { | ||
let realPath = fs.realpathSync(dir.path); | ||
|
||
let path; | ||
if (!args[1]) { path = realPath; }; | ||
|
||
if (path !== realPath && args[1].startsWith('/')) { | ||
path = args[1]; | ||
} else if (path !== realPath) { | ||
if (realPath == '/') { path = '/' + args[1]; } | ||
else { path = realPath + '/' + args[1]; } | ||
}; | ||
|
||
const config = { | ||
singleLine: true, | ||
columns: [ | ||
{ alignment: 'left' }, | ||
{ alignment: 'left' }, | ||
{ alignment: 'left' }, | ||
{ alignment: 'left' }, | ||
{ alignment: 'right' }, | ||
{ alignment: 'left' }, | ||
{ alignment: 'left' }, | ||
], | ||
}; | ||
|
||
const data = []; | ||
|
||
if (JSON.stringify(fs.readdirSync(path)) == '[]') return ''; | ||
|
||
fs.readdirSync(path).forEach((name) => { | ||
const stat = fs.statSync(path + '/' + name); | ||
const date = new Intl.DateTimeFormat('en-US', { | ||
dateStyle: 'medium', | ||
timeStyle: 'short', | ||
hour12: false, | ||
}) | ||
.format(stat.mtime) | ||
.replace(/\,/g, '') | ||
.replace(new Date().getFullYear() + ' ', ''); | ||
|
||
if (stat.isFile()) { | ||
let icon; | ||
|
||
switch (name.split('.').pop()) { | ||
default: | ||
icon = ' '; | ||
break; | ||
case 'key': | ||
icon = ' '; | ||
break; | ||
case 'crt': | ||
icon = ' '; | ||
break; | ||
case 'txt': | ||
case 'text': | ||
icon = ' '; | ||
break; | ||
case 'jpg': | ||
case 'jpeg': | ||
case 'png': | ||
case 'gif': | ||
case 'webp': | ||
icon = ' '; | ||
break; | ||
|
||
case 'cjs': | ||
case 'mjs': | ||
case 'js': | ||
icon = ' '; | ||
break; | ||
case 'ts': | ||
icon = ' '; | ||
break; | ||
case 'jsonc': | ||
case 'json': | ||
icon = ' '; | ||
break; | ||
case 'md': | ||
icon = ' '; | ||
break; | ||
case 'css': | ||
icon = ' '; | ||
break; | ||
case 'py': | ||
icon = ' '; | ||
break; | ||
case 'html': | ||
icon = ' '; | ||
break; | ||
} | ||
|
||
data.push([ | ||
modeToString(stat.mode), | ||
stat.nlink, | ||
usr.username, | ||
'user', | ||
stat.size, | ||
date, | ||
icon + name, | ||
]); | ||
} else if (stat.isDirectory()) { | ||
data.push([ | ||
modeToString(stat.mode), | ||
stat.nlink, | ||
usr.username, | ||
'user', | ||
stat.size, | ||
date, | ||
' ' + name, | ||
]); | ||
} | ||
}); | ||
|
||
return table(data, config).split('\n'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ import sh from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm'; | |
export class CommandsAddon { | ||
_disposables = []; | ||
|
||
promptMSG = () => `${c.blue('')}${c.bgBlue(` flow@${this.user.username}`)}${c.bgCyan(c.blue('') + ` ${this.dir.path}`)}${c.cyan('')} `; | ||
promptMSG = async () => `${c.blue('')}${c.bgBlue(` flow@${this.user.username}`)}${c.bgCyan(c.blue('') + ` ${this.dir.path}`)}${c.cyan('')} `; | ||
|
||
constructor(options, usr, dir) { | ||
this.commandsMap = options.commands; | ||
|
@@ -32,14 +32,15 @@ export class CommandsAddon { | |
if (e) console.error(e); | ||
|
||
this.fs = require('fs'); | ||
this.promptMSG = async () => `${c.blue('')}${c.bgBlue(` flow@${this.user.username}`)}${c.bgCyan(c.blue('') + ` ${this.fs.realpathSync(this.dir.path)}`)}${c.cyan('')} `; | ||
}); | ||
} | ||
|
||
activate(term) { | ||
this.terminal = term; | ||
|
||
this.terminal.prompt = () => { | ||
this.terminal.write('\r' + this.promptMSG()); | ||
this.terminal.prompt = async () => { | ||
this.terminal.write('\r' + await this.promptMSG()); | ||
}; | ||
|
||
this.terminal.writeln('Welcome to FluSH!'); | ||
|
@@ -72,18 +73,18 @@ export class CommandsAddon { | |
if (this.current.length == 0) return; | ||
term.writeln(''); | ||
const args = sh.parse(this.current); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
import('./commands/' + args[0] + '.js').then((command) => { | ||
const cmd = eval(`command.exec(this.fs, this.terminal, this.user, this.dir, args)`); | ||
|
||
import('./commands/' + args[0] + '.js').then(async (command) => { | ||
const cmd = await command.exec(this.fs, this.terminal, this.user, this.dir, args); | ||
if (cmd && !Array.isArray(cmd)) { | ||
term.writeln(cmd); | ||
} else if (Array.isArray(cmd)) { | ||
cmd.forEach(ln => term.writeln(ln)); | ||
}; | ||
term.prompt(); | ||
}).catch((e) => { | ||
term.writeln(c.red(e.name + ': ' + e.message)); | ||
console.error(e); | ||
term.writeln(c.red(e.message)); | ||
term.prompt(); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters