Skip to content

Commit 356acbe

Browse files
committed
refactor: use parseArgs for flag parsing; remove wildcard redirect from frontend template
1 parent 53b2eca commit 356acbe

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

src/cli.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as clack from '@clack/prompts';
33
import { realpathSync } from 'node:fs';
44
import { basename, resolve } from 'node:path';
55
import { fileURLToPath, pathToFileURL } from 'node:url';
6+
import { parseArgs } from 'node:util';
67
import { promptAppExtensions } from './prompts/appExtensions.js';
78
import { promptDatabase } from './prompts/database.js';
89
import { promptProjectName } from './prompts/projectName.js';
@@ -20,7 +21,7 @@ export function nextStepLines(options: NextStepOptions): string[] {
2021
`cd ${options.nameOrPath}`,
2122
'cp .env.example .env',
2223
'# fill in PIPEDRIVE_CLIENT_ID and PIPEDRIVE_CLIENT_SECRET',
23-
'docker-compose up',
24+
'docker-compose up --watch',
2425
];
2526

2627
return ['', 'Next steps:', ...steps.map((s) => ` ${s}`)];
@@ -55,38 +56,42 @@ interface ParsedFlags {
5556
}
5657

5758
export function parseFlags(argv: string[]): ParsedFlags {
59+
const { values } = parseArgs({
60+
args: argv.slice(2),
61+
options: {
62+
'project-name': { type: 'string' },
63+
'database': { type: 'string' },
64+
'app-extensions': { type: 'string' },
65+
},
66+
strict: false,
67+
});
68+
5869
const result: ParsedFlags = {};
5970

60-
const nameIdx = argv.indexOf('--project-name');
61-
if (nameIdx !== -1) {
62-
const value = argv[nameIdx + 1];
63-
if (!value || value.startsWith('--')) throw new Error('--project-name requires a value.');
64-
if (!value.trim()) throw new Error('--project-name cannot be empty.');
65-
result.nameOrPath = value.trim();
71+
if (values['project-name'] !== undefined) {
72+
const name = values['project-name'] as string;
73+
if (!name.trim()) throw new Error('--project-name cannot be empty.');
74+
result.nameOrPath = name.trim();
6675
}
6776

68-
const dbIdx = argv.indexOf('--database');
69-
if (dbIdx !== -1) {
70-
const value = argv[dbIdx + 1];
71-
if (!value || value.startsWith('--')) throw new Error('--database requires a value.');
77+
if (values['database'] !== undefined) {
78+
const db = values['database'] as string;
7279
const valid: Database[] = ['postgres', 'mysql', 'sqlite'];
73-
if (!valid.includes(value as Database)) {
74-
throw new Error(`Invalid database "${value}". Choose one of: postgres, mysql, sqlite.`);
80+
if (!valid.includes(db as Database)) {
81+
throw new Error(`Invalid database "${db}". Choose one of: postgres, mysql, sqlite.`);
7582
}
76-
result.database = value as Database;
83+
result.database = db as Database;
7784
}
7885

79-
const extIdx = argv.indexOf('--app-extensions');
80-
if (extIdx !== -1) {
81-
const value = argv[extIdx + 1];
82-
if (!value || value.startsWith('--')) throw new Error('--app-extensions requires a value.');
83-
if (value === 'none') {
86+
if (values['app-extensions'] !== undefined) {
87+
const raw = values['app-extensions'] as string;
88+
if (raw === 'none') {
8489
result.appExtensions = [];
8590
} else {
86-
if (value.includes(',') && value.split(',').includes('none')) {
91+
if (raw.includes(',') && raw.split(',').includes('none')) {
8792
throw new Error('"none" cannot be combined with other extension types.');
8893
}
89-
const types = value.split(',');
94+
const types = raw.split(',');
9095
for (const type of types) {
9196
if (!isAppExtensionType(type)) {
9297
throw new Error(`Invalid app extension type "${type}". Choose from: custom-panel, custom-modal.`);

0 commit comments

Comments
 (0)