Skip to content

Commit

Permalink
feat: Set default value
Browse files Browse the repository at this point in the history
This patch sets default value on the prompts to allow use of the
resolved values

Signed-off-by: AlexNg <[email protected]>
  • Loading branch information
caffeine-addictt committed Jun 23, 2024
1 parent 4e6f0fd commit 4aa6d0e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
23 changes: 17 additions & 6 deletions dist/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const readline_1 = __importDefault(require("readline"));
const io_util_1 = require("./io-util");
const resolver_1 = __importDefault(require("./resolver"));
/**
* For interacting with stdin/stdout
*/
Expand All @@ -28,15 +29,25 @@ const question = (query, validator = [], trimWhitespace = true) => new Promise((
}));
/** Ask for project information */
const fetchInfo = async (cleanup) => {
const name = await question('Name? (This will go on the LICENSE)\n=> ');
const email = await question('Email?\n=> ', [
const resolved = await (0, resolver_1.default)();
// Ask user
const name = await question(`Name? (This will go on the LICENSE)${resolved.name ? ` [Resolved: ${resolved.name}]` : ''}\n=> `)
.then((val) => val.trim())
.then((val) => (val.length ? val : resolved.name ?? ''));
const email = await question(`Email?${resolved.email ? ` [Resolved: ${resolved.email}]` : ''}\n=> `, [
{
validate: (s) => /.+@.+\..+/.test(s),
validate: (s) => !!resolved.email || /.+@.+\..+/.test(s),
onError: () => console.log('Invalid email!'),
},
]);
const username = await question('Username? (https://github.com/<username>)\n=> ');
const repository = await question(`Repository? (https://github.com/${username}/<repo>)\n=> `);
])
.then((val) => val.trim())
.then((val) => (val.length ? val : resolved.email ?? ''));
const username = await question(`Username? (https://github.com/<username>)${resolved.org ? ` [Resolved: ${resolved.org}]` : ''}\n=> `)
.then((val) => val.trim())
.then((val) => (val.length ? val : resolved.org ?? ''));
const repository = await question(`Repository? (https://github.com/${username}/<repo>)${resolved.repo ? ` [Resolved: ${resolved.repo}]` : ''}\n=> `)
.then((val) => val.trim())
.then((val) => (val.length ? val : resolved.repo ?? ''));
const proj_name = await question('Project name?\n=> ');
const proj_short_desc = await question('Short description?\n=> ');
const proj_long_desc = await question('Long description?\n=> ');
Expand Down
43 changes: 32 additions & 11 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import readline from 'readline';

import type { ProjectInfo } from './types';
import { replaceInFile, withTempDir } from './io-util';
import resolveGitInfo from './resolver';

/**
* For interacting with stdin/stdout
Expand Down Expand Up @@ -41,19 +42,39 @@ const question = (
const fetchInfo = async (
cleanup: () => void | unknown,
): Promise<ProjectInfo> => {
const name = await question('Name? (This will go on the LICENSE)\n=> ');
const email = await question('Email?\n=> ', [
{
validate: (s: string) => /.+@.+\..+/.test(s),
onError: () => console.log('Invalid email!'),
},
]);
const resolved = await resolveGitInfo();

// Ask user
const name = await question(
`Name? (This will go on the LICENSE)${resolved.name ? ` [Resolved: ${resolved.name}]` : ''}\n=> `,
)
.then((val) => val.trim())
.then((val) => (val.length ? val : resolved.name ?? ''));

const email = await question(
`Email?${resolved.email ? ` [Resolved: ${resolved.email}]` : ''}\n=> `,
[
{
validate: (s: string) => !!resolved.email || /.+@.+\..+/.test(s),
onError: () => console.log('Invalid email!'),
},
],
)
.then((val) => val.trim())
.then((val) => (val.length ? val : resolved.email ?? ''));

const username = await question(
'Username? (https://github.com/<username>)\n=> ',
);
`Username? (https://github.com/<username>)${resolved.org ? ` [Resolved: ${resolved.org}]` : ''}\n=> `,
)
.then((val) => val.trim())
.then((val) => (val.length ? val : resolved.org ?? ''));

const repository = await question(
`Repository? (https://github.com/${username}/<repo>)\n=> `,
);
`Repository? (https://github.com/${username}/<repo>)${resolved.repo ? ` [Resolved: ${resolved.repo}]` : ''}\n=> `,
)
.then((val) => val.trim())
.then((val) => (val.length ? val : resolved.repo ?? ''));

const proj_name = await question('Project name?\n=> ');
const proj_short_desc = await question('Short description?\n=> ');
const proj_long_desc = await question('Long description?\n=> ');
Expand Down

0 comments on commit 4aa6d0e

Please sign in to comment.