Skip to content

Commit

Permalink
Merge pull request #6 from jupyterlite/wasm-commands
Browse files Browse the repository at this point in the history
Use WASM commands
  • Loading branch information
ianthomas23 authored Jul 4, 2024
2 parents 9e554fb + bd25730 commit da2f773
Show file tree
Hide file tree
Showing 41 changed files with 11,958 additions and 953 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ npm t

To run single test file
```bash
npm t -- test/whatever.ts
npm t -- tests/whatever.ts
```
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ module.exports = {
},
verbose: true,
collectCoverage: true,
coverageDirectory: '.coverage'
coverageDirectory: '.coverage',
testEnvironment: "jsdom",
setupFilesAfterEnv: ["<rootDir>/tests/global_setup.ts"]
};
1,061 changes: 895 additions & 166 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jupyterlite/cockle",
"version": "0.0.3",
"version": "0.0.4",
"description": "In browser bash-like shell",
"homepage": "https://github.com/jupyterlite/cockle",
"license": "BSD-3-Clause",
Expand All @@ -24,13 +24,16 @@
"test": "jest"
},
"dependencies": {
"@jupyterlab/services": "^7.1.6"
"@jupyterlab/services": "^7.1.6",
"@jupyterlite/contents": "^0.3.0"
},
"devDependencies": {
"@playwright/test": "^1.45.1",
"@types/jest": "^29.5.12",
"@types/json-schema": "^7.0.15",
"@types/react": "^18.2.79",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.4.5"
},
Expand Down
7 changes: 0 additions & 7 deletions src/command.ts

This file was deleted.

49 changes: 18 additions & 31 deletions src/command_registry.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { Command } from "./command"
import * as AllCommands from "./commands"
import { ICommandRunner } from "./commands/command_runner"
import { BuiltinCommandRunner } from "./commands/builtin_command_runner"
import { CoreutilsCommandRunner } from "./commands/coreutils_command_runner"

export class CommandRegistry {
private constructor() {}

/**
* Create a new Command identified by its string name.
*/
create(name: string): Command | null {
const cls = this.get(name)
if (cls) {
return new (cls as any)()
} else {
return null
private constructor() {
this._commandRunners = [
new BuiltinCommandRunner(),
new CoreutilsCommandRunner(),
]

// Command name -> runner mapping
for (const runner of this._commandRunners) {
for (const name of runner.names()) {
this._map.set(name, runner)
}
}
}

get(name: string): typeof Command | null {
return this._map.get(name) ?? null
get(name: string): ICommandRunner | null {
return this._map.get(name) ?? null
}

static instance(): CommandRegistry {
Expand All @@ -33,22 +34,8 @@ export class CommandRegistry {
})
}

register(name: string, cls: typeof Command) {
if (name.endsWith("Command")) {
const shortName = name.slice(0, -7).toLowerCase()
this._map.set(shortName, cls)
}
}
private _commandRunners: ICommandRunner[];
private _map: Map<string, ICommandRunner> = new Map()

private static _instance: CommandRegistry
private _map: Map<string, typeof Command> = new Map()
}

function registerCommands(commands: {[key: string]: typeof Command}) {
const registry = CommandRegistry.instance()
for (const [key, value] of Object.entries(commands)) {
registry.register(key, value)
}
}

registerCommands(AllCommands)
28 changes: 28 additions & 0 deletions src/commands/builtin_command_runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ICommandRunner } from "./command_runner"
import { Context } from "../context"

export class BuiltinCommandRunner implements ICommandRunner {
names(): string[] {
return ["cd"]
}

async run(cmdName: string, context: Context): Promise<void> {
switch (cmdName) {
case "cd":
this._cd(context)
break
}
}

private _cd(context: Context) {
const { args } = context
if (args.length < 1) {
// Do nothing.
return;
}
const path = args[0] // Ignore other arguments?
// Need to handle path of "-". Maybe previous path is in an env var?
context.fileSystem.FS.chdir(path)
// Need to set PWD env var?
}
}
18 changes: 0 additions & 18 deletions src/commands/cat.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/commands/command_runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Context } from "../context"

export interface ICommandRunner {
names(): string[]
run(cmdName: string, context: Context): Promise<void>
}
21 changes: 21 additions & 0 deletions src/commands/coreutils_command_runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as CoreutilsModule from "../wasm/coreutils"
import { WasmCommandRunner } from "./wasm_command_runner"

export class CoreutilsCommandRunner extends WasmCommandRunner {
names(): string[] {
return [
// File commands
"cp", "echo", "env", "ln", "ls", "mkdir", "mv", "pwd", "realpath", "rm", "rmdir", "touch", "uname",
// Text commands
"cat", "cut", "head", "join", "md5sum", "nl", "sha1sum", "sha224sum", "sha256sum",
"sha384sum", "sha512sum", "sort", "tail", "tr", "wc",
// Shell commands
"basename", "date", "dirname", "echo", "env", "expr", "id", "logname", "pwd", "seq", "sleep",
"stat", "uname",
]
}

protected _getWasmModule(): any {
return CoreutilsModule.default
}
}
16 changes: 0 additions & 16 deletions src/commands/echo.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/commands/env.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/commands/index.ts

This file was deleted.

109 changes: 0 additions & 109 deletions src/commands/ls.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/commands/pwd.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/commands/rm.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/commands/touch.ts

This file was deleted.

Loading

0 comments on commit da2f773

Please sign in to comment.