@inquirer/demo 0.8.8 → 0.9.0

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/demos/search.mjs CHANGED
@@ -1,9 +1,30 @@
1
+ import { promises as fs } from 'node:fs';
2
+ import path from 'node:path';
1
3
  import * as url from 'node:url';
2
4
  import { search } from '@inquirer/prompts';
3
5
 
6
+ async function fileExists(filepath) {
7
+ return fs.access(filepath).then(
8
+ () => true,
9
+ () => false,
10
+ );
11
+ }
12
+
13
+ async function isDirectory(path) {
14
+ if (await fileExists(path)) {
15
+ const stats = await fs.stat(path);
16
+ return stats.isDirectory();
17
+ }
18
+
19
+ return false;
20
+ }
21
+
22
+ const root = path.dirname(path.join(url.fileURLToPath(import.meta.url), '../../..'));
23
+
4
24
  const demo = async () => {
5
25
  let answer;
6
26
 
27
+ // Demo: Search results from an API
7
28
  answer = await search({
8
29
  message: 'Select an npm package',
9
30
  source: async (input = 'inquirer', { signal }) => {
@@ -22,6 +43,42 @@ const demo = async () => {
22
43
  },
23
44
  });
24
45
  console.log('Answer:', answer);
46
+
47
+ // Demo: Using the search prompt as an autocomplete tool.
48
+ answer = await search({
49
+ message: 'Select a file',
50
+ source: async (term = '') => {
51
+ let dirPath = path.join(root, term);
52
+ while (!(await isDirectory(dirPath)) && dirPath !== root) {
53
+ dirPath = path.dirname(dirPath);
54
+ }
55
+
56
+ const files = await fs.readdir(dirPath, { withFileTypes: true });
57
+ return files
58
+ .sort((a, b) => {
59
+ if (a.isDirectory() === b.isDirectory()) {
60
+ return a.name.localeCompare(b.name);
61
+ }
62
+
63
+ // Sort dir first
64
+ return a.isDirectory() ? -1 : 1;
65
+ })
66
+ .map((file) => ({
67
+ name:
68
+ path.relative(root, path.join(dirPath, file.name)) +
69
+ (file.isDirectory() ? '/' : ''),
70
+ value: path.join(file.parentPath, file.name),
71
+ }))
72
+ .filter(({ value }) => value.includes(term));
73
+ },
74
+ validate: async (filePath) => {
75
+ if (!(await fileExists(filePath)) || (await isDirectory(filePath))) {
76
+ return 'You must select a file';
77
+ }
78
+ return true;
79
+ },
80
+ });
81
+ console.log('Answer:', answer);
25
82
  };
26
83
 
27
84
  if (import.meta.url.startsWith('file:')) {
package/demos/timeout.mjs CHANGED
@@ -1,26 +1,17 @@
1
1
  import * as url from 'node:url';
2
- import { setTimeout } from 'node:timers/promises';
3
2
  import { input } from '@inquirer/prompts';
4
3
 
5
4
  async function demo() {
6
- const ac = new AbortController();
7
- const prompt = input({
8
- message: 'Enter a value (timing out in 5 seconds)',
9
- });
10
-
11
- prompt
12
- .finally(() => {
13
- ac.abort();
14
- })
15
- // Silencing the cancellation error.
16
- .catch(() => {});
5
+ const answer = await input(
6
+ { message: 'Enter a value (timing out in 5 seconds)' },
7
+ { signal: AbortSignal.timeout(5000) },
8
+ ).catch((error) => {
9
+ if (error.name === 'AbortPromptError') {
10
+ return 'Default value';
11
+ }
17
12
 
18
- const defaultValue = setTimeout(5000, 'timeout', { signal: ac.signal }).then(() => {
19
- prompt.cancel();
20
- return 'Timed out!';
13
+ throw error;
21
14
  });
22
-
23
- const answer = await Promise.race([defaultValue, prompt]);
24
15
  console.log('Answer:', answer);
25
16
  }
26
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/demo",
3
- "version": "0.8.8",
3
+ "version": "0.9.0",
4
4
  "engines": {
5
5
  "node": ">=18"
6
6
  },
@@ -61,13 +61,13 @@
61
61
  "license": "MIT",
62
62
  "homepage": "https://github.com/SBoudrias/Inquirer.js",
63
63
  "dependencies": {
64
- "@inquirer/core": "^9.0.10",
65
- "@inquirer/prompts": "^5.3.8",
64
+ "@inquirer/core": "^9.1.0",
65
+ "@inquirer/prompts": "^5.4.0",
66
66
  "yoctocolors-cjs": "^2.1.2"
67
67
  },
68
68
  "publishConfig": {
69
69
  "access": "public"
70
70
  },
71
71
  "sideEffects": false,
72
- "gitHead": "273b16eeb96fab346854b9b92257c930d7ca7661"
72
+ "gitHead": "0c039599ef88fe9eb804fe083ee386ec906a856f"
73
73
  }