Skip to content

Commit

Permalink
more help docs wip and etc
Browse files Browse the repository at this point in the history
  • Loading branch information
suchipi committed Dec 21, 2024
1 parent 2e3d828 commit 086a1f1
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/api/commands/extname/extname.help.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ declare function extname(
options?: { full?: boolean }
): string;
```

See also `help(basename)`.
4 changes: 3 additions & 1 deletion src/api/commands/ls/ls.help.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# `ls` - Return directory contents

Returns the contents of a directory, as absolute paths. `.` and `..` are omitted.
Returns the contents of a directory, as an Array of absolute paths. `.` and `..` are omitted.

If `ls()` is called with no directory, the present working directory (`pwd()`) is used.

```ts
// Defined in yavascript/src/api/commands/ls
Expand Down
7 changes: 0 additions & 7 deletions src/api/commands/pwd/pwd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ export function pwd(): Path {
const initialPwd = pwd();
Object.freeze(initialPwd);
Object.freeze(initialPwd.segments);
Object.defineProperty(initialPwd, "separator", {
configurable: false,
writable: false,
enumerable: true,

value: initialPwd.separator,
});

Object.defineProperty(pwd, "initial", {
configurable: false,
Expand Down
2 changes: 2 additions & 0 deletions src/api/commands/realpath/realpath.help.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ Provides the same functionality as the unix binary of the same name.
// Defined in yavascript/src/api/commands/realpath
declare function realpath(path: string | Path): Path;
```

If you want to convert a relative path to an absolute path, but the path's target might NOT exist, use `Path.normalize`.
4 changes: 2 additions & 2 deletions src/api/commands/sleep/sleep.help.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `sleep` - Wait the specified number of milliseconds

`sleep` and `sleep.sync` block the current thread for at least the specified number of milliseconds, or maybe a tiny bit longer.
`sleep` and `sleep.sync` block the current thread for at least the specified number of milliseconds, but maybe a tiny bit longer.

`sleep.async` returns a Promise which resolves in at least the specified number of milliseconds, or maybe a tiny bit longer.
`sleep.async` returns a Promise which resolves in at least the specified number of milliseconds, but maybe a tiny bit longer.

`sleep` and `sleep.sync` block the current thread. `sleep.async` doesn't block the current thread.

Expand Down
6 changes: 3 additions & 3 deletions src/api/commands/sleep/sleep.inc.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Blocks the current thread for at least the specified number of milliseconds,
* or maybe a tiny bit longer.
* but maybe a tiny bit longer.
*
* alias for `sleep.sync`.
*/
declare var sleep: {
/**
* Blocks the current thread for at least the specified number of milliseconds,
* or maybe a tiny bit longer.
* but maybe a tiny bit longer.
*
* alias for `sleep.sync`.
*
Expand All @@ -17,7 +17,7 @@ declare var sleep: {

/**
* Blocks the current thread for at least the specified number of milliseconds,
* or maybe a tiny bit longer.
* but maybe a tiny bit longer.
*
* @param milliseconds - The number of milliseconds to block for.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/api/commands/sleep/sleep_async.help.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `sleep.async` - Wait for a while without blocking the thread

`sleep.async` returns a Promise which resolves in at least the specified number of milliseconds, or maybe a tiny bit longer.
`sleep.async` returns a Promise which resolves in at least the specified number of milliseconds, but maybe a tiny bit longer.

`sleep.async` doesn't block the current thread, so other JavaScript code (registered event handlers, async functions, timers, etc) can run while `sleep.async`'s return Promise is waiting to resolve. If this is not the behavior you want, use `sleep.sync` instead.

Expand Down
2 changes: 1 addition & 1 deletion src/api/commands/sleep/sleep_sync.help.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `sleep.sync` - Block the current thread for a while

`sleep.sync` blocks the current thread for at least the specified number of milliseconds, or maybe a tiny bit longer.
`sleep.sync` blocks the current thread for at least the specified number of milliseconds, but maybe a tiny bit longer.

No other JavaScript code can run while `sleep.sync` is running. If this is not the behavior you want, use `sleep.async` instead.

Expand Down
1 change: 0 additions & 1 deletion src/api/parse-script-args/parse-script-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export function parseScriptArgs(
}

case Path: {
// TODO: Path hint should return Path object in flags object instead of string
hintsForClef[key] = clefParse.Path;
pathKeys.add(key);
break;
Expand Down
28 changes: 28 additions & 0 deletions src/api/path/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ const windowsDefaultPathExt =
".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC";

class Path {
// [x] helpdocs done
static OS_SEGMENT_SEPARATOR = os.platform === "win32" ? "\\" : "/";
// [x] helpdocs done
static OS_ENV_VAR_SEPARATOR = os.platform === "win32" ? ";" : ":";
// [x] helpdocs done
static OS_PROGRAM_EXTENSIONS = new Set(
os.platform === "win32"
? (env.PATHEXT || windowsDefaultPathExt).split(";")
: []
);

// [x] helpdocs done
static splitToSegments(inputParts: Array<string> | string): Array<string> {
assert.type(
inputParts,
Expand All @@ -60,6 +64,7 @@ class Path {
);
}

// [x] helpdocs done
static detectSeparator<Fallback extends string | null = string>(
input: Array<string> | string,
// @ts-ignore might be instantiated with a different subtype
Expand All @@ -84,6 +89,7 @@ class Path {
return fallback;
}

// [ ] helpdocs done
static normalize(
...inputs: Array<string | Path | Array<string | Path>>
): Path {
Expand All @@ -101,6 +107,7 @@ class Path {
return new Path(...inputs).normalize();
}

// [ ] helpdocs done
static isAbsolute(path: string | Path): boolean {
assert.type(path, types.or(types.string, types.Path));

Expand All @@ -111,9 +118,12 @@ class Path {
}
}

// [x] helpdocs done
segments: Array<string>;
// [x] helpdocs done
separator: string;

// [-] helpdocs done (sorta)
constructor(...inputs: Array<string | Path | Array<string | Path>>) {
assert.type(
inputs,
Expand All @@ -135,6 +145,7 @@ class Path {
this.separator = Path.detectSeparator(parts);
}

// [x] helpdocs done
static fromRaw(
segments: Array<string>,
separator: string = Path.OS_SEGMENT_SEPARATOR
Expand All @@ -148,6 +159,7 @@ class Path {
return path;
}

// [ ] helpdocs done
normalize(): Path {
// we clone this cause we're gonna mutate it
const segments = [...this.segments];
Expand Down Expand Up @@ -185,6 +197,7 @@ class Path {
return Path.fromRaw(newSegments, this.separator);
}

// [ ] helpdocs done
concat(...others: Array<string | Path | Array<string | Path>>): Path {
assert.type(
others,
Expand All @@ -201,6 +214,7 @@ class Path {
return Path.fromRaw(this.segments.concat(otherSegments), this.separator);
}

// [ ] helpdocs done
isAbsolute(): boolean {
const firstPart = this.segments[0];

Expand All @@ -214,6 +228,7 @@ class Path {
return false;
}

// [ ] helpdocs done
clone(): this {
// @ts-ignore could be instantiated with different subtype
return (this.constructor as typeof Path).fromRaw(
Expand All @@ -222,6 +237,7 @@ class Path {
);
}

// [ ] helpdocs done
relativeTo(
dir: Path | string,
options: { noLeadingDot?: boolean } = {}
Expand Down Expand Up @@ -250,6 +266,7 @@ class Path {
}
}

// [ ] helpdocs done
toString(): string {
let result = this.segments.join(this.separator);
if (result == "") {
Expand All @@ -260,22 +277,27 @@ class Path {
}
}

// [ ] helpdocs done
toJSON(): string {
return this.toString();
}

// [ ] helpdocs done
basename(): string {
return this.segments.at(-1) || "";
}

// [ ] helpdocs done
extname(options: { full?: boolean } = {}): string {
return extname(this, options);
}

// [ ] helpdocs done
dirname(): Path {
return this.replaceLast([]);
}

// [ ] helpdocs done
startsWith(value: string | Path | Array<string | Path>): boolean {
value = new Path(value);

Expand All @@ -284,6 +306,7 @@ class Path {
);
}

// [ ] helpdocs done
endsWith(value: string | Path | Array<string | Path>): boolean {
value = new Path(value);

Expand All @@ -295,6 +318,7 @@ class Path {
);
}

// [ ] helpdocs done
indexOf(
value: string | Path | Array<string | Path>,
fromIndex: number = 0
Expand All @@ -315,13 +339,15 @@ class Path {
return -1;
}

// [ ] helpdocs done
includes(
value: string | Path | Array<string | Path>,
fromIndex: number = 0
): boolean {
return this.indexOf(value, fromIndex) !== -1;
}

// [ ] helpdocs done
replace(
value: string | Path | Array<string | Path>,
replacement: string | Path | Array<string | Path>
Expand All @@ -343,6 +369,7 @@ class Path {
}
}

// [ ] helpdocs done
replaceAll(
value: string | Path | Array<string | Path>,
replacement: string | Path | Array<string | Path>
Expand All @@ -367,6 +394,7 @@ class Path {
return currentPath;
}

// [ ] helpdocs done
replaceLast(replacement: string | Path | Array<string | Path>): Path {
replacement = new Path(replacement);

Expand Down
1 change: 1 addition & 0 deletions todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
- [ ] repl sometimes needs you to press Ctrl+C more times than it should
- [ ] fix chmod so it has add/remove semantics and respects existing perms
- [ ] yavascript globals aren't set up in workers
- [ ] add `.on("exit")` support to node-compat `process` shim

0 comments on commit 086a1f1

Please sign in to comment.