diff --git a/commands/cat.js b/commands/cat.js old mode 100755 new mode 100644 index 60a70b9..79b4ecd --- a/commands/cat.js +++ b/commands/cat.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check if (!args[1]) { diff --git a/commands/cd.js b/commands/cd.js old mode 100755 new mode 100644 index 16a78c6..d934969 --- a/commands/cd.js +++ b/commands/cd.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check __open(args[1] || path.resolve('.'), true); diff --git a/commands/chmod.js b/commands/chmod.js old mode 100755 new mode 100644 index 4ed797a..89b6450 --- a/commands/chmod.js +++ b/commands/chmod.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check diff --git a/commands/cp.js b/commands/cp.js old mode 100755 new mode 100644 index e1aa350..0c0ff98 --- a/commands/cp.js +++ b/commands/cp.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check if (args.length != 3) { diff --git a/commands/echo.js b/commands/echo.js old mode 100755 new mode 100644 index 283664a..fcb2510 --- a/commands/echo.js +++ b/commands/echo.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check terminal.writeln(args.slice(1).join(' ')); diff --git a/commands/help.js b/commands/help.js old mode 100755 new mode 100644 index be76c02..5d5e592 --- a/commands/help.js +++ b/commands/help.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check terminal.writeln('Some unix commands available, ls /bin to see them.'); diff --git a/commands/ln.js b/commands/ln.js old mode 100755 new mode 100644 index 900e46b..8d2d281 --- a/commands/ln.js +++ b/commands/ln.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check diff --git a/commands/ls.js b/commands/ls.js old mode 100755 new mode 100644 index 3a7ff2f..5adc184 --- a/commands/ls.js +++ b/commands/ls.js @@ -1,3 +1,4 @@ +export {}; /// /// // @ts-check diff --git a/commands/mkdir.js b/commands/mkdir.js old mode 100755 new mode 100644 index 78a7721..c634782 --- a/commands/mkdir.js +++ b/commands/mkdir.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check if (!args[1]) { diff --git a/commands/mv.js b/commands/mv.js old mode 100755 new mode 100644 index 74060eb..cd4227a --- a/commands/mv.js +++ b/commands/mv.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check if (args.length != 3) { diff --git a/commands/open-editor.js b/commands/open-editor.js old mode 100755 new mode 100644 index b80368c..9a954a5 --- a/commands/open-editor.js +++ b/commands/open-editor.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check __editor_open(args[1]); diff --git a/commands/pwd.js b/commands/pwd.js old mode 100755 new mode 100644 index 993e78c..38828f7 --- a/commands/pwd.js +++ b/commands/pwd.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check terminal.writeln(path.cwd); diff --git a/commands/rm.js b/commands/rm.js old mode 100755 new mode 100644 index 44b30d5..0aaf261 --- a/commands/rm.js +++ b/commands/rm.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check if (!args[1]) { diff --git a/commands/stat.js b/commands/stat.js old mode 100755 new mode 100644 index 7cf8ced..17b1f72 --- a/commands/stat.js +++ b/commands/stat.js @@ -1,3 +1,4 @@ +export {}; const { S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFIFO, S_IFLNK, S_IFSOCK } = fs.constants; const dateFormatter = new Intl.DateTimeFormat('en-US', { diff --git a/commands/touch.js b/commands/touch.js old mode 100755 new mode 100644 index 993f666..b79fab2 --- a/commands/touch.js +++ b/commands/touch.js @@ -1,3 +1,4 @@ +export {}; /// // @ts-check if (!args[1]) { diff --git a/src/shell.ts b/src/shell.ts index 24f4387..7555b78 100644 --- a/src/shell.ts +++ b/src/shell.ts @@ -26,45 +26,37 @@ fitAddon.fit(); const AsyncFunction = async function () {}.constructor as (...args: string[]) => (...args: unknown[]) => Promise; -async function wait(n: number): Promise { - const { promise, resolve } = Promise.withResolvers(); - setTimeout(resolve, n); - return promise; +/** + * Handles removing TS-specific stuff + */ +function parse_source(source: string): string { + return source.replaceAll(/^\s*export {};\s*\n/g, ''); } -export async function exec(__cmdLine: string): Promise { - const args = __cmdLine.trim().split(' '); +export async function exec(line: string): Promise { + const args = line.trim().split(' '); if (!args[0]) { return; } - const __filename = '/bin/' + args[0] + '.js'; + const filename = '/bin/' + args[0] + '.js'; - if (!fs.existsSync(__filename)) { + if (!fs.existsSync(filename)) { terminal.writeln('Unknown command: ' + args[0]); return; } - if (!fs.statSync(__filename).hasAccess(X_OK)) { + if (!fs.statSync(filename).hasAccess(X_OK)) { terminal.writeln('Missing permission: ' + args[0]); return; } - await AsyncFunction( - '{ fs, path, utilium, terminal, __open, __editor_open, args, wait }', - fs.readFileSync(__filename, 'utf8') - )({ - fs, - path, - chalk, - utilium, - terminal, - __open, - __editor_open, - args, - wait, - } satisfies ExecutionLocals | object); + const source = parse_source(fs.readFileSync(filename, 'utf8')); + + const locals = { fs, path, chalk, utilium, terminal, __open, __editor_open, args } satisfies ExecutionLocals; + + await AsyncFunction(`{${Object.keys(locals).join(',')}}`, source)(locals); } const shell = createShell({