Skip to content

Commit

Permalink
feat: prompt for db url
Browse files Browse the repository at this point in the history
- prompt user for mongo url to automatically create environment variables for
- validate the url by attempting to connect to the url

Closes #18
  • Loading branch information
RyanClementsHax committed Feb 21, 2024
1 parent c5e2bf7 commit 738df18
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 6 deletions.
135 changes: 131 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"figlet": "^1.7.0",
"fs-extra": "^11.2.0",
"gradient-string": "^2.0.2",
"mongodb": "^6.3.0",
"node-emoji": "^2.1.3",
"nypm": "^0.3.6",
"portscanner": "^2.2.0",
Expand Down
4 changes: 2 additions & 2 deletions src/actions/start/start.action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prompt } from 'enquirer';
import Enquirer from 'enquirer';
import { isValidVariableName } from '../../utils/globalValidators.helper';
import {
printWithBadge,
Expand Down Expand Up @@ -73,7 +73,7 @@ export default class StartAction extends Action {
}

private async getWorkspaceInfo(): Promise<StartCommandOptions> {
return prompt([
return Enquirer.prompt([
{
type: 'input',
name: 'workspace',
Expand Down
30 changes: 30 additions & 0 deletions src/commands/init.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import path from 'path';
import { installDependencies } from 'nypm';
import { fileURLToPath } from 'url';
import { execa } from 'execa';
import Enquirer from 'enquirer';
import { MongoClient } from 'mongodb';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

Expand All @@ -12,8 +14,13 @@ export default new Command('init')
.action(async () => {
const templatePath = path.resolve(__dirname, '../templates/mantis-todo');
const workspacePath = path.join(process.cwd(), 'mantis-todo');
// Get db url
const dbUrl = await promptForDbUrl();
// Copy over files
await fs.copy(templatePath, workspacePath, { overwrite: true });
const envPath = path.join(workspacePath, 'apps/server/.env.local');
await fs.ensureFile(envPath);
await fs.appendFile(envPath, `\nMONGODB_URI='${dbUrl}'`);
// Install dependencies
await installDependencies({
cwd: workspacePath,
Expand All @@ -30,3 +37,26 @@ export default new Command('init')
{ cwd: workspacePath, stdio: 'inherit' },
);
});

const promptForDbUrl = async (): Promise<string> => {
interface PromptResult {
dbUrl: string;
}
const { dbUrl } = await Enquirer.prompt<PromptResult>([
{
type: 'input',
name: 'dbUrl' satisfies keyof PromptResult,
message: 'Enter the url for the mongo db to connect to:',
validate: async (value) => {
if (value.trim().length <= 0) return 'Db url must not be empty';
try {
await MongoClient.connect(value);
return true;
} catch (error) {
return `Db url seems to be invalid: ${error.message}`;
}
},
},
]);
return dbUrl;
};

0 comments on commit 738df18

Please sign in to comment.