Skip to content

Commit

Permalink
refactor!: typescript supported
Browse files Browse the repository at this point in the history
  • Loading branch information
lnquy065 committed Mar 29, 2023
1 parent 9a6059f commit 63d7f8d
Show file tree
Hide file tree
Showing 19 changed files with 357 additions and 326 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
/demo
yarn.lock
package-lock.json
package-lock.json
example/ts-demo/node_modules
.idea
69 changes: 0 additions & 69 deletions dist/cjs/example.js

This file was deleted.

50 changes: 0 additions & 50 deletions dist/cjs/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions dist/cjs/package.json

This file was deleted.

12 changes: 12 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import inquirer from "inquirer";
import PromptConstructor = inquirer.prompts.PromptConstructor;
declare const InterruptedPrompt: {
EVENT_INTERRUPTED: string;
from: (promptBase: PromptConstructor) => PromptConstructor;
/**
* @deprecated
*/
replaceAllDefaults: (inquirer: any) => void;
fromAll: (inquirer: any) => void;
};
export default InterruptedPrompt;
52 changes: 52 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import observe from "inquirer/lib/utils/events.js";
const rejectIfInterrupted = (event, hotkey, reject) => {
let isReject = false;
if (hotkey) {
const hotkeys = hotkey.split('+');
if (hotkeys.length === 2) {
const [specKey, key] = hotkeys;
isReject = event.key[specKey] && event.key.name === key;
}
else {
if (event.key.name === hotkey)
isReject = true;
}
}
if (isReject) {
reject(InterruptedPrompt.EVENT_INTERRUPTED);
}
};
/**
* Create an interrupted prompt from any Prompt from inquirer or its plugins
*/
const from = (promptBase) => {
class InterruptedPrompt extends promptBase {
run() {
const interruptedKeyName = this['opt'].interruptedKeyName || this['opt'].interruptedKeyname || 'escape';
return new Promise((resolve, reject) => {
const events = observe(this['rl']);
events.keypress.pipe().forEach(e => rejectIfInterrupted(e, interruptedKeyName, reject));
super.run().then(resolve, reject);
});
}
}
return InterruptedPrompt;
};
/**
* Override all default inquirer prompts to interrupted prompts
*/
const replaceAllDefaults = (inquirer) => {
Object.keys(inquirer.prompt.prompts).forEach((key) => {
inquirer.prompt.prompts[key] = InterruptedPrompt.from(inquirer.prompt.prompts[key]);
});
};
const InterruptedPrompt = {
EVENT_INTERRUPTED: 'EVENT_INTERRUPTED',
from,
/**
* @deprecated
*/
replaceAllDefaults,
fromAll: replaceAllDefaults
};
export default InterruptedPrompt;
50 changes: 0 additions & 50 deletions dist/mjs/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions dist/mjs/package.json

This file was deleted.

88 changes: 88 additions & 0 deletions example/ts-demo/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/ts-demo/dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions example/ts-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "ts-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"compilerOptions": {
"moduleResolution": "node"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "tsc && node ./dist/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^18.15.11",
"@types/inquirer": "^9.0.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.2",
"typescript": "^5.0.2"
},
"dependencies": {
"inquirer": "^9.1.5",
"inquirer-file-tree-selection-prompt": "^2.0.5"
}
}
Loading

0 comments on commit 63d7f8d

Please sign in to comment.