Skip to content

Commit

Permalink
Port the ResultSet class
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 6, 2024
1 parent 9b08fcb commit 0c83456
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 72 deletions.
49 changes: 49 additions & 0 deletions lib/finder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Finds the instances of an executable in the system path.
*/
export declare class Finder {
#private;
/**
* The list of executable file extensions.
*/
readonly extensions: Set<string>;
/**
* The list of system paths.
*/
readonly paths: Set<string>;
/**
* Creates a new finder.
* @param options An object providing values to initialize this instance.
*/
constructor(options?: FinderOptions);
/**
* Value indicating whether the current platform is Windows.
*/
static get isWindows(): boolean;
/**
* Finds the instances of an executable in the system path.
* @param command The command to be resolved.
* @returns The paths of the executables found.
*/
find(command: string): AsyncGenerator<string, void>;
/**
* Gets a value indicating whether the specified file is executable.
* @param file The path of the file to be checked.
* @returns `true` if the specified file is executable, otherwise `false`.
*/
isExecutable(file: string): Promise<boolean>;
}
/**
* Defines the options of a {@link Finder} instance.
*/
export type FinderOptions = Partial<{
/**
* The list of executable file extensions.
*/
extensions: Array<string>;
/**
* The list of system paths.
*/
paths: Array<string>;
}>;
//# sourceMappingURL=finder.d.ts.map
32 changes: 32 additions & 0 deletions lib/result_set.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Finder} from "./finder.js";

/**
* Provides convenient access to the stream of search results.
*/
export class ResultSet {

/**
* Creates a new result set.
* @param command The searched command.
* @param finder The finder used to perform the search.
*/
constructor(command: string, finder: Finder);

/**
* Returns all instances of the searched command.
* @returns All search results.
*/
all(): Promise<Array<string>>;

/**
* Returns the first instance of the searched command.
* @returns The first search result.
*/
first(): Promise<string>;

/**
* Returns a stream of instances of the searched command.
* @returns A stream of the search results.
*/
stream(): AsyncGenerator<string, void>;
}
2 changes: 2 additions & 0 deletions src/finder.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Finds the instances of an executable in the system path.
export class Finder
15 changes: 0 additions & 15 deletions src/index.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/result_set.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {delimiter} from "node:path";
import {Finder} from "./finder.js";

# Provides convenient access to the stream of search results.
export class ResultSet

# Creates a new result set.
constructor: (command, finder) ->

# The searched command.
@_command = command

# The finder used to perform the search.
@_finder = finder

# Returns all instances of the searched command.
all: ->
executables = new Set
executables.add path for await path from @stream()
if executables.size then Array.from executables else
paths = Array.from(@_finder.paths).join if Finder.isWindows then ";" else delimiter
throw Error "No \"#{@_command}\" in (#{paths})."

# Returns the first instance of the searched command.
first: ->
{value} = await @stream().next()
if value then value else
paths = Array.from(@_finder.paths).join if Finder.isWindows then ";" else delimiter
throw Error "No \"#{@_command}\" in (#{paths})."

# Returns a stream of instances of the searched command.
stream: -> @_finder.find @_command
57 changes: 0 additions & 57 deletions src/result_set.ts

This file was deleted.

Empty file added test/finder_test.coffee
Empty file.
Empty file added test/result_set_test.coffee
Empty file.

0 comments on commit 0c83456

Please sign in to comment.