Skip to content

Commit

Permalink
Improved commands locals handling
Browse files Browse the repository at this point in the history
Fixed IntelliSense for commands
  • Loading branch information
james-pre committed Oct 16, 2024
1 parent a3aa72c commit af62c7c
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 23 deletions.
1 change: 1 addition & 0 deletions commands/cat.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
if (!args[1]) {
Expand Down
1 change: 1 addition & 0 deletions commands/cd.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
__open(args[1] || path.resolve('.'), true);
1 change: 1 addition & 0 deletions commands/chmod.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check

Expand Down
1 change: 1 addition & 0 deletions commands/cp.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
if (args.length != 3) {
Expand Down
1 change: 1 addition & 0 deletions commands/echo.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
terminal.writeln(args.slice(1).join(' '));
1 change: 1 addition & 0 deletions commands/help.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
terminal.writeln('Some unix commands available, ls /bin to see them.');
1 change: 1 addition & 0 deletions commands/ln.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check

Expand Down
1 change: 1 addition & 0 deletions commands/ls.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
/// <reference lib="esnext" />
// @ts-check
Expand Down
1 change: 1 addition & 0 deletions commands/mkdir.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
if (!args[1]) {
Expand Down
1 change: 1 addition & 0 deletions commands/mv.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
if (args.length != 3) {
Expand Down
1 change: 1 addition & 0 deletions commands/open-editor.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
__editor_open(args[1]);
1 change: 1 addition & 0 deletions commands/pwd.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
terminal.writeln(path.cwd);
1 change: 1 addition & 0 deletions commands/rm.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
if (!args[1]) {
Expand Down
1 change: 1 addition & 0 deletions commands/stat.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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', {
Expand Down
1 change: 1 addition & 0 deletions commands/touch.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {};
/// <reference types="./lib.d.ts" />
// @ts-check
if (!args[1]) {
Expand Down
38 changes: 15 additions & 23 deletions src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,37 @@ fitAddon.fit();

const AsyncFunction = async function () {}.constructor as (...args: string[]) => (...args: unknown[]) => Promise<void>;

async function wait(n: number): Promise<void> {
const { promise, resolve } = Promise.withResolvers<void>();
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<void> {
const args = __cmdLine.trim().split(' ');
export async function exec(line: string): Promise<void> {
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({
Expand Down

0 comments on commit af62c7c

Please sign in to comment.