Skip to content

Commit

Permalink
Chore: Set demo to only run automatically if they're the entrypoint. …
Browse files Browse the repository at this point in the history
…Otherwise, export a demo function.
  • Loading branch information
SBoudrias committed May 21, 2023
1 parent 13f8025 commit 42c1458
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 53 deletions.
16 changes: 13 additions & 3 deletions packages/checkbox/demo.mts → packages/demo/demos/checkbox.mjs
@@ -1,6 +1,7 @@
import checkbox, { Separator } from './src/index.mjs';
import * as url from 'node:url';
import { checkbox, Separator } from '@inquirer/prompts';

(async () => {
const demo = async () => {
let answer;

answer = await checkbox({
Expand Down Expand Up @@ -52,4 +53,13 @@ import checkbox, { Separator } from './src/index.mjs';
],
});
console.log('Answer:', answer);
})();
};

if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
demo();
}
}

export default demo;
16 changes: 13 additions & 3 deletions packages/confirm/demo.mts → packages/demo/demos/confirm.mjs
@@ -1,6 +1,7 @@
import confirm from './src/index.mjs';
import * as url from 'node:url';
import { confirm } from '@inquirer/prompts';

(async () => {
const demo = async () => {
console.log(
'Answer:',
await confirm({
Expand Down Expand Up @@ -29,4 +30,13 @@ import confirm from './src/index.mjs';
'Cleared prompt answer:',
await confirm({ message: 'Confirm?' }, { clearPromptOnDone: true })
);
})();
};

if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
demo();
}
}

export default demo;
25 changes: 25 additions & 0 deletions packages/demo/demos/editor.mjs
@@ -0,0 +1,25 @@
import * as url from 'node:url';
import { editor } from '@inquirer/prompts';

const demo = async () => {
const answer = await editor({
message: 'Please write a short bio of at least 3 lines.',
validate(text) {
if (text.trim().split('\n').length < 3) {
return 'Must be at least 3 lines.';
}

return true;
},
});
console.log('Answer:', answer);
};

if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
demo();
}
}

export default demo;
16 changes: 13 additions & 3 deletions packages/expand/demo.mts → packages/demo/demos/expand.mjs
@@ -1,6 +1,7 @@
import expand from './src/index.mjs';
import * as url from 'node:url';
import { expand } from '@inquirer/prompts';

(async () => {
const demo = async () => {
let answer;

answer = await expand({
Expand Down Expand Up @@ -85,4 +86,13 @@ import expand from './src/index.mjs';
],
});
console.log('Answer:', answer);
})();
};

if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
demo();
}
}

export default demo;
22 changes: 16 additions & 6 deletions packages/input/demo.mts → packages/demo/demos/input.mjs
@@ -1,12 +1,13 @@
import * as url from 'node:url';
import chalk from 'chalk';
import input from './src/index.mjs';
import { input } from '@inquirer/prompts';

const hexRegEx = /([0-9]|[a-f])/gim;
const isHex = (value: string) =>
const isHex = (value) =>
(value.match(hexRegEx) || []).length === value.length &&
(value.length === 3 || value.length === 6);

(async () => {
const demo = async () => {
let answer;

answer = await input({
Expand All @@ -17,7 +18,7 @@ const isHex = (value: string) =>

answer = await input({
message: 'Enter an hex color?',
transformer(value = '', { isFinal }: { isFinal: boolean }) {
transformer(value = '', { isFinal }) {
const color = chalk.hex(isHex(value) ? value : 'fff');
return isFinal ? color.underline(value) : color(value);
},
Expand All @@ -27,7 +28,7 @@ const isHex = (value: string) =>

answer = await input({
message: '(Slow validation) provide a number:',
validate: (value: unknown) =>
validate: (value) =>
new Promise((resolve) => {
setTimeout(
() => resolve(!Number.isNaN(Number(value)) || 'You must provide a number'),
Expand All @@ -44,4 +45,13 @@ const isHex = (value: string) =>
}),
});
console.log('Answer:', answer);
})();
};

if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
demo();
}
}

export default demo;
28 changes: 28 additions & 0 deletions packages/demo/demos/password.mjs
@@ -0,0 +1,28 @@
import * as url from 'node:url';
import { password } from '@inquirer/prompts';

const demo = async () => {
console.log(
'Answer:',
await password({
message: 'Enter a silent password?',
})
);

console.log(
'Answer:',
await password({
message: 'Enter a masked password?',
mask: '*',
})
);
};

if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
demo();
}
}

export default demo;
16 changes: 13 additions & 3 deletions packages/rawlist/demo.mts → packages/demo/demos/rawlist.mjs
@@ -1,6 +1,7 @@
import rawlist, { Separator } from './src/index.mjs';
import * as url from 'node:url';
import { rawlist, Separator } from '@inquirer/prompts';

(async () => {
const demo = async () => {
let answer;

answer = await rawlist({
Expand Down Expand Up @@ -54,4 +55,13 @@ import rawlist, { Separator } from './src/index.mjs';
],
});
console.log('Answer:', answer);
})();
};

if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
demo();
}
}

export default demo;
16 changes: 13 additions & 3 deletions packages/select/demo.mts → packages/demo/demos/select.mjs
@@ -1,6 +1,7 @@
import select, { Separator } from './src/index.mjs';
import * as url from 'node:url';
import { select, Separator } from '@inquirer/prompts';

(async () => {
const demo = async () => {
let answer;

answer = await select({
Expand Down Expand Up @@ -56,4 +57,13 @@ import select, { Separator } from './src/index.mjs';
],
});
console.log('Answer:', answer);
})();
};

if (import.meta.url.startsWith('file:')) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
demo();
}
}

export default demo;
48 changes: 48 additions & 0 deletions packages/demo/index.mjs
@@ -0,0 +1,48 @@
#!/usr/bin/env node
/* eslint-disable no-await-in-loop */

import { select } from '@inquirer/prompts';
import checkboxDemo from './demos/checkbox.mjs';
import confirmDemo from './demos/confirm.mjs';
import editorDemo from './demos/editor.mjs';
import expandDemo from './demos/expand.mjs';
import inputDemo from './demos/input.mjs';
import passwordDemo from './demos/password.mjs';
import rawlistDemo from './demos/rawlist.mjs';
import selectDemo from './demos/select.mjs';

const demos = {
checkbox: checkboxDemo,
confirm: confirmDemo,
editor: editorDemo,
expand: expandDemo,
input: inputDemo,
password: passwordDemo,
rawlist: rawlistDemo,
select: selectDemo,
};

function askNextDemo() {
return select({
message: 'Which prompt demo do you want to run?',
choices: [
{ name: 'Input', value: 'input' },
{ name: 'Password', value: 'password' },
{ name: 'Confirm', value: 'confirm' },
{ name: 'Select', value: 'select' },
{ name: 'Checkbox', value: 'checkbox' },
{ name: 'Expand', value: 'expand' },
{ name: 'Rawlist', value: 'rawlist' },
{ name: 'Editor', value: 'editor' },
{ name: "Exit (I'm done)", value: 'exit' },
],
});
}

(async () => {
let nextDemo = await askNextDemo();
while (nextDemo !== 'exit') {
await demos[nextDemo]();
nextDemo = await askNextDemo();
}
})();
70 changes: 70 additions & 0 deletions packages/demo/package.json
@@ -0,0 +1,70 @@
{
"name": "@inquirer/demo",
"version": "0.0.0",
"engines": {
"node": ">=14.18.0"
},
"type": "module",
"description": "Inquirer demos",
"main": "index.mjs",
"bin": {
"inquirer-demo": "index.mjs"
},
"files": [
"index.mjs",
"demos/"
],
"repository": {
"type": "git",
"url": "https://github.com/SBoudrias/Inquirer.js.git"
},
"keywords": [
"answer",
"answers",
"ask",
"base",
"cli",
"command",
"command-line",
"confirm",
"enquirer",
"generate",
"generator",
"hyper",
"input",
"inquire",
"inquirer",
"interface",
"iterm",
"javascript",
"menu",
"node",
"nodejs",
"prompt",
"promptly",
"prompts",
"question",
"readline",
"scaffold",
"scaffolder",
"scaffolding",
"stdin",
"stdout",
"terminal",
"tty",
"ui",
"yeoman",
"yo",
"zsh"
],
"author": "Simon Boudrias <admin@simonboudrias.com>",
"license": "MIT",
"homepage": "https://github.com/SBoudrias/Inquirer.js",
"dependencies": {
"chalk": "^4.1.2",
"@inquirer/prompts": "^1.2.2"
},
"publishConfig": {
"access": "public"
}
}
16 changes: 0 additions & 16 deletions packages/editor/demo.mts

This file was deleted.

16 changes: 0 additions & 16 deletions packages/password/demo.mts

This file was deleted.

0 comments on commit 42c1458

Please sign in to comment.