genomic 0.0.1 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/commander.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { Opts, ParsedArgs } from 'minimist';
2
+ import { Readable, Writable } from 'stream';
3
+ import { Prompter } from './prompt';
4
+ export type CommandHandler = (argv: ParsedArgs, prompter: Prompter, options: CLIOptions) => void;
5
+ export interface CLIOptions {
6
+ noTty: boolean;
7
+ input: Readable;
8
+ output: Writable;
9
+ minimistOpts: Opts;
10
+ version: string;
11
+ }
12
+ export declare const defaultCLIOptions: CLIOptions;
13
+ export declare class CLI {
14
+ private argv;
15
+ private prompter;
16
+ private commandHandler;
17
+ private options;
18
+ constructor(commandHandler: CommandHandler, options: Partial<CLIOptions>, argv?: any);
19
+ run(): Promise<void>;
20
+ }
21
+ export default CLI;
package/commander.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.CLI = exports.defaultCLIOptions = void 0;
7
+ const deepmerge_1 = __importDefault(require("deepmerge"));
8
+ const minimist_1 = __importDefault(require("minimist"));
9
+ const prompt_1 = require("./prompt");
10
+ const utils_1 = require("./utils");
11
+ exports.defaultCLIOptions = {
12
+ version: `genomic@${(0, utils_1.getVersion)()}`,
13
+ noTty: false,
14
+ input: process.stdin,
15
+ output: process.stdout,
16
+ minimistOpts: {
17
+ alias: {
18
+ v: 'version'
19
+ }
20
+ }
21
+ };
22
+ class CLI {
23
+ argv;
24
+ prompter;
25
+ commandHandler;
26
+ options;
27
+ constructor(commandHandler, options, argv) {
28
+ const { input, output, ...optionsWithoutIO } = options;
29
+ const { input: defaultInput, output: defaultOutput, ...defaultOptionsWithoutIO } = exports.defaultCLIOptions;
30
+ const mergedOptions = (0, deepmerge_1.default)(defaultOptionsWithoutIO, optionsWithoutIO);
31
+ mergedOptions.input = input || defaultInput;
32
+ mergedOptions.output = output || defaultOutput;
33
+ this.options = mergedOptions;
34
+ this.argv = argv ? argv : (0, minimist_1.default)(process.argv.slice(2), this.options.minimistOpts);
35
+ this.prompter = new prompt_1.Prompter({
36
+ noTty: this.options.noTty,
37
+ input: this.options.input,
38
+ output: this.options.output
39
+ });
40
+ this.commandHandler = commandHandler;
41
+ }
42
+ async run() {
43
+ if (!('tty' in this.argv)) {
44
+ this.argv.tty = true;
45
+ }
46
+ if (this.argv.version) {
47
+ console.log(this.options.version);
48
+ process.exit(0);
49
+ }
50
+ const args = await this.commandHandler(this.argv, this.prompter, this.options);
51
+ // this.prompter.exit();
52
+ this.prompter.close();
53
+ return args;
54
+ }
55
+ }
56
+ exports.CLI = CLI;
57
+ exports.default = CLI;
@@ -0,0 +1,50 @@
1
+ import deepmerge from 'deepmerge';
2
+ import minimist from 'minimist';
3
+ import { Prompter } from './prompt';
4
+ import { getVersion } from './utils';
5
+ export const defaultCLIOptions = {
6
+ version: `genomic@${getVersion()}`,
7
+ noTty: false,
8
+ input: process.stdin,
9
+ output: process.stdout,
10
+ minimistOpts: {
11
+ alias: {
12
+ v: 'version'
13
+ }
14
+ }
15
+ };
16
+ export class CLI {
17
+ argv;
18
+ prompter;
19
+ commandHandler;
20
+ options;
21
+ constructor(commandHandler, options, argv) {
22
+ const { input, output, ...optionsWithoutIO } = options;
23
+ const { input: defaultInput, output: defaultOutput, ...defaultOptionsWithoutIO } = defaultCLIOptions;
24
+ const mergedOptions = deepmerge(defaultOptionsWithoutIO, optionsWithoutIO);
25
+ mergedOptions.input = input || defaultInput;
26
+ mergedOptions.output = output || defaultOutput;
27
+ this.options = mergedOptions;
28
+ this.argv = argv ? argv : minimist(process.argv.slice(2), this.options.minimistOpts);
29
+ this.prompter = new Prompter({
30
+ noTty: this.options.noTty,
31
+ input: this.options.input,
32
+ output: this.options.output
33
+ });
34
+ this.commandHandler = commandHandler;
35
+ }
36
+ async run() {
37
+ if (!('tty' in this.argv)) {
38
+ this.argv.tty = true;
39
+ }
40
+ if (this.argv.version) {
41
+ console.log(this.options.version);
42
+ process.exit(0);
43
+ }
44
+ const args = await this.commandHandler(this.argv, this.prompter, this.options);
45
+ // this.prompter.exit();
46
+ this.prompter.close();
47
+ return args;
48
+ }
49
+ }
50
+ export default CLI;
package/esm/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './commander';
2
+ export * from './prompt';
3
+ export * from './question';
4
+ export * from './resolvers';
@@ -0,0 +1,95 @@
1
+ const defaultProcessWrapper = {
2
+ exit: (code) => process.exit(code)
3
+ };
4
+ export const KEY_CODES = {
5
+ UP_ARROW: '\u001b[A',
6
+ DOWN_ARROW: '\u001b[B',
7
+ RIGHT_ARROW: '\u001b[C',
8
+ LEFT_ARROW: '\u001b[D',
9
+ ENTER: '\r',
10
+ SPACE: ' ',
11
+ CTRL_C: '\u0003',
12
+ BACKSPACE: '\x7f', // Commonly used BACKSPACE key in Unix-like systems
13
+ BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
14
+ };
15
+ /**
16
+ * Handles keyboard input for interactive prompts.
17
+ *
18
+ * **Important**: Only one TerminalKeypress instance should be actively listening
19
+ * on a given input stream at a time. If you need multiple Prompter instances,
20
+ * call `close()` on the first instance before using the second, or reuse a single
21
+ * instance for all prompts.
22
+ *
23
+ * Multiple instances sharing the same input stream (e.g., process.stdin) will
24
+ * each receive all keypresses, which can cause duplicate or unexpected behavior.
25
+ */
26
+ export class TerminalKeypress {
27
+ listeners = {};
28
+ active = true;
29
+ noTty;
30
+ input;
31
+ proc;
32
+ dataHandler = null;
33
+ constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
34
+ this.noTty = noTty;
35
+ this.input = input;
36
+ this.proc = proc;
37
+ if (this.isTTY()) {
38
+ this.input.resume();
39
+ this.input.setEncoding('utf8');
40
+ }
41
+ this.setupListeners();
42
+ }
43
+ isTTY() {
44
+ return !this.noTty;
45
+ }
46
+ setupListeners() {
47
+ this.dataHandler = (key) => {
48
+ if (!this.active)
49
+ return;
50
+ const handlers = this.listeners[key];
51
+ handlers?.forEach(handler => handler());
52
+ if (key === KEY_CODES.CTRL_C) {
53
+ this.proc.exit(0);
54
+ }
55
+ };
56
+ this.input.on('data', this.dataHandler);
57
+ }
58
+ on(key, callback) {
59
+ if (!this.listeners[key]) {
60
+ this.listeners[key] = [];
61
+ }
62
+ this.listeners[key].push(callback);
63
+ }
64
+ off(key, callback) {
65
+ if (this.listeners[key]) {
66
+ const index = this.listeners[key].indexOf(callback);
67
+ if (index !== -1) {
68
+ this.listeners[key].splice(index, 1);
69
+ }
70
+ }
71
+ }
72
+ clearHandlers() {
73
+ this.listeners = {};
74
+ }
75
+ pause() {
76
+ this.active = false;
77
+ this.clearHandlers();
78
+ }
79
+ resume() {
80
+ this.active = true;
81
+ if (this.isTTY() && typeof this.input.setRawMode === 'function') {
82
+ this.input.setRawMode(true);
83
+ }
84
+ }
85
+ destroy() {
86
+ if (typeof this.input.setRawMode === 'function') {
87
+ this.input.setRawMode(false);
88
+ }
89
+ this.input.pause();
90
+ if (this.dataHandler) {
91
+ this.input.removeListener('data', this.dataHandler);
92
+ this.dataHandler = null;
93
+ }
94
+ }
95
+ }