All files / demo/blong-cli/text/orchestrator/text textInput.ts

100% Statements 39/39
100% Branches 5/5
100% Functions 1/1
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 406x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 4x 2x 1x 1x 1x 1x 1x 6x 6x  
import {library} from '@feasibleone/blong';
 
/** The input both `text` handlers work from. */
export type TextParams = {
    /** Text given on the command line. */
    value?: string;
    /** A file to read the text from instead. */
    file?: string;
};
 
/** The slice of the platform this library needs (`IPlatformApi` structurally). */
type TextPlatform = {
    readFileSync(path: string, options: {encoding: BufferEncoding}): string | Buffer;
};
 
/**
 * The text a command should operate on — `--value`, or the contents of `--file`.
 *
 * A `library()` function rather than a handler: it has no endpoint of its own,
 * and both handlers need it. It is attached to the handler proxy as
 * `lib.textInput` for every handler in this group, so neither has to import it —
 * the framework's IoC rule.
 *
 * It reads the platform off `this`, exactly as a handler does. Under the `cli`
 * intent that is the Node platform, which is how a command reaches the
 * filesystem: no HTTP server and no browser sandbox are involved.
 */
export default library(
    () =>
        function textInput(this: {platform: TextPlatform}, params: TextParams): string {
            if (params.value !== undefined) return String(params.value);
            if (params.file !== undefined) {
                return String(this.platform.readFileSync(params.file, {encoding: 'utf-8'}));
            }
            // A real realm would define this in the error layer instead of throwing a
            // plain Error — see the `blong-error` skill. The demo keeps it short.
            throw new Error('provide --value=TEXT or --file=PATH');
        },
);