Skip to content

Commit

Permalink
Moved commands
Browse files Browse the repository at this point in the history
Changed to Fetch backend
Added dynamic library stuff
  • Loading branch information
james-pre committed Oct 17, 2024
1 parent 60a3dea commit 7c0ece5
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 22 deletions.
43 changes: 35 additions & 8 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node
import { build, context, type BuildOptions } from 'esbuild';
import { build, context, type BuildOptions, type PluginBuild } from 'esbuild';
import { execSync } from 'node:child_process';
import { cpSync, existsSync, mkdirSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';

const {
Expand All @@ -20,25 +21,51 @@ if (!existsSync('build')) {
mkdirSync('build');
}

execSync('npx make-index commands -o build/index.json', { stdio: 'inherit' });
cpSync('system', 'build/system', { recursive: true });

cpSync('commands', 'build/system');
const shared_config: BuildOptions = {
target: 'es2022',
keepNames: true,
bundle: true,
format: 'esm',
platform: 'browser',
};

const lib_config: BuildOptions & { entryPoints: { in: string; out: string }[] } = {
...shared_config,
entryPoints: [],
outdir: outdir + '/system/lib',
};

for (const specifier of ['@zenfs/core', 'utilium', 'chalk', '@zenfs/core/path']) {
lib_config.entryPoints.push({
in: fileURLToPath(import.meta.resolve(specifier)),
out: specifier,
});
}

const config: BuildOptions = {
...shared_config,
entryPoints: ['src/index.ts', 'src/index.html', 'src/styles.css'],
target: 'es2022',
outdir,
loader: {
'.html': 'copy',
},
sourcemap: true,
keepNames: true,
bundle: true,
format: 'esm',
platform: 'browser',
logOverride: {
'direct-eval': 'info',
},
plugins: [
{
name: 'build-libs',
setup({ onStart }: PluginBuild): void | Promise<void> {
onStart(async () => {
await build(lib_config);
execSync('npx make-index build/system -o build/index.json -q', { stdio: 'inherit' });
});
},
},
],
};

switch (mode) {
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@xterm/addon-fit": "^0.10.0",
"@xterm/addon-web-links": "^0.11.0",
"@xterm/xterm": "^5.5.0",
"@zenfs/core": "^1.0.8",
"@zenfs/core": "^1.0.10",
"@zenfs/dom": "^0.2.17",
"@zenfs/iso": "^0.3.0",
"@zenfs/zip": "^0.5.1",
Expand Down
5 changes: 4 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ await configure({
mounts: {
'/': {
backend: Overlay,
readable: Fetch.create({}),
readable: Fetch.create({
baseUrl: '/system',
index: '/index.json',
}),
writable: InMemory.create({ name: 'root-cow' }),
},
},
Expand Down
32 changes: 27 additions & 5 deletions src/shell.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/only-throw-error */
import { FitAddon } from '@xterm/addon-fit';
import { WebLinksAddon } from '@xterm/addon-web-links';
import { Terminal } from '@xterm/xterm';
Expand All @@ -24,7 +25,7 @@ fitAddon.fit();

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

const import_regex = /import (?:\* as )?(\w+) from '([^']+)';/g;
const import_regex = /import (\* as )?(\w+) from '([^']+)';/g;

/**
* Handles linking and stuff
Expand All @@ -36,13 +37,31 @@ async function parse_source(source: string): Promise<{ source: string; imports:
const imports = Object.create(null) as Record<string, unknown>;
let match: RegExpExecArray | null;
while ((match = import_regex.exec(source))) {
const [, binding, specifier] = match;
const [, isNamespace, binding, specifier] = match;

imports[binding] = await import(specifier);
const lib_path = `/lib/${specifier}.js`;

source = source.replace(match[0], '');
if (!fs.existsSync(lib_path)) {
throw 'Could not locate library: ' + specifier;
}

const lib_contents = fs.readFileSync(lib_path, 'utf-8');

const url = URL.createObjectURL(new Blob([lib_contents], { type: 'text/javascript' }));

const _module = await import(url);

Check warning on line 52 in src/shell.ts

View workflow job for this annotation

GitHub Actions / CI

Unsafe assignment of an `any` value

if (specifier == 'chalk') {
_module.default.level = 2;

Check warning on line 55 in src/shell.ts

View workflow job for this annotation

GitHub Actions / CI

Unsafe member access .default on an `any` value
}

imports[binding] = isNamespace ? _module : _module.default;

Check warning on line 58 in src/shell.ts

View workflow job for this annotation

GitHub Actions / CI

Unsafe member access .default on an `any` value

URL.revokeObjectURL(url);
}

source = source.replaceAll(import_regex, '');

return { source, imports };
}

Expand All @@ -69,6 +88,10 @@ export async function exec(line: string): Promise<void> {

const locals = { args, fs, terminal, __open, __editor_open, __mount_resolve, ...imports };

if ($('#terminal input.debug').is(':checked')) {
console.debug('EXEC:\nlocals:', locals, '\nsource:', source);
}

await AsyncFunction(`{${Object.keys(locals).join(',')}}`, source)(locals);
}

Expand All @@ -84,7 +107,6 @@ const shell = createShell({
await exec(line).catch((error: Error | string) => {
terminal.writeln('Error: ' + ((error as Error).message ?? error));
if ($('#terminal input.debug').is(':checked')) {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw error;
}
});
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions commands/ls.js → system/bin/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function colorize(text, stats) {
}
return colorize(text);
}
console.log(colorize);

const formatter = new Intl.DateTimeFormat('en-US', {
month: 'short',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 7c0ece5

Please sign in to comment.