|
| 1 | +import { isAbsolute, normalize } from 'node:path'; |
1 | 2 | import { checkbox, select, input, confirm } from '@inquirer/prompts';
|
2 |
| -import { |
| 3 | +import type { |
3 | 4 | ProjectType,
|
4 | 5 | Language,
|
5 | 6 | Technology,
|
6 | 7 | Builder,
|
7 | 8 | Runtime,
|
8 | 9 | CodeStyleSetupOptions,
|
9 | 10 | } from '@code-style/code-style/config-types';
|
10 |
| -import { Promisable } from 'type-fest'; |
| 11 | +import type { Promisable } from 'type-fest'; |
11 | 12 |
|
12 | 13 | import { build } from './build.js';
|
13 |
| -import { includes_js } from './utils.js'; |
| 14 | +import { includes_js, path_exists } from './utils.js'; |
14 | 15 | import { save_rc } from './rc-file.js';
|
15 | 16 |
|
16 | 17 | export async function interactive_setup(
|
@@ -260,19 +261,23 @@ export async function interactive_setup(
|
260 | 261 | input({
|
261 | 262 | message: 'Input directory',
|
262 | 263 | default: defaults.input_dir ?? 'src/',
|
| 264 | + validate: (path) => |
| 265 | + validate_path(normalize_path(path), { relative: true }), |
263 | 266 | }),
|
264 | 267 | defaults.input_dir,
|
265 | 268 | use_defaults,
|
266 |
| - ); |
| 269 | + ).then(normalize_path); |
267 | 270 | output_dir = await question_default(
|
268 | 271 | () =>
|
269 | 272 | input({
|
270 | 273 | message: 'Output directory',
|
271 | 274 | default: defaults.output_dir ?? 'dist/',
|
| 275 | + validate: (path) => |
| 276 | + validate_path(normalize_path(path), { relative: true }), |
272 | 277 | }),
|
273 | 278 | defaults.output_dir,
|
274 | 279 | use_defaults,
|
275 |
| - ); |
| 280 | + ).then(normalize_path); |
276 | 281 | }
|
277 | 282 |
|
278 | 283 | const overwrite = await question_default(
|
@@ -322,3 +327,40 @@ export async function question_default<T>(
|
322 | 327 | ): Promise<T> {
|
323 | 328 | return use_default ? default_answer ?? question() : question();
|
324 | 329 | }
|
| 330 | + |
| 331 | +export async function validate_path( |
| 332 | + path: string, |
| 333 | + { |
| 334 | + exists = false, |
| 335 | + absolute = false, |
| 336 | + relative = false, |
| 337 | + }: { |
| 338 | + exists?: boolean; |
| 339 | + absolute?: boolean; |
| 340 | + relative?: boolean; |
| 341 | + } = {}, |
| 342 | +): Promise<string | boolean> { |
| 343 | + if (path.length < 1) { |
| 344 | + return `Must not be empty.`; |
| 345 | + } |
| 346 | + |
| 347 | + if (absolute && !isAbsolute(path)) { |
| 348 | + return `Must be an absolute path.`; |
| 349 | + } |
| 350 | + |
| 351 | + if (relative && isAbsolute(path)) { |
| 352 | + return `Must be a relative path.`; |
| 353 | + } |
| 354 | + |
| 355 | + if (exists && !(await path_exists(path))) { |
| 356 | + return `Path does not exist.`; |
| 357 | + } |
| 358 | + |
| 359 | + return true; |
| 360 | +} |
| 361 | + |
| 362 | +export function normalize_path(path: string): string { |
| 363 | + return normalize(path) |
| 364 | + .trim() |
| 365 | + .replace(/[/\\]$/u, ''); |
| 366 | +} |
0 commit comments