Skip to content

Commit c317750

Browse files
committed
🐛 normalize & validate paths
1 parent 4f342ad commit c317750

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

packages/create-configs/src/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export async function build({
2626
languages,
2727
runtime,
2828
builder,
29-
input_dir = 'src/',
30-
output_dir = 'dist/',
29+
input_dir = 'src',
30+
output_dir = 'dist',
3131
technologies = [],
3232
library = false,
3333
lenient = false,

packages/create-configs/src/interactive.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
import { isAbsolute, normalize } from 'node:path';
12
import { checkbox, select, input, confirm } from '@inquirer/prompts';
2-
import {
3+
import type {
34
ProjectType,
45
Language,
56
Technology,
67
Builder,
78
Runtime,
89
CodeStyleSetupOptions,
910
} from '@code-style/code-style/config-types';
10-
import { Promisable } from 'type-fest';
11+
import type { Promisable } from 'type-fest';
1112

1213
import { build } from './build.js';
13-
import { includes_js } from './utils.js';
14+
import { includes_js, path_exists } from './utils.js';
1415
import { save_rc } from './rc-file.js';
1516

1617
export async function interactive_setup(
@@ -260,19 +261,23 @@ export async function interactive_setup(
260261
input({
261262
message: 'Input directory',
262263
default: defaults.input_dir ?? 'src/',
264+
validate: (path) =>
265+
validate_path(normalize_path(path), { relative: true }),
263266
}),
264267
defaults.input_dir,
265268
use_defaults,
266-
);
269+
).then(normalize_path);
267270
output_dir = await question_default(
268271
() =>
269272
input({
270273
message: 'Output directory',
271274
default: defaults.output_dir ?? 'dist/',
275+
validate: (path) =>
276+
validate_path(normalize_path(path), { relative: true }),
272277
}),
273278
defaults.output_dir,
274279
use_defaults,
275-
);
280+
).then(normalize_path);
276281
}
277282

278283
const overwrite = await question_default(
@@ -322,3 +327,40 @@ export async function question_default<T>(
322327
): Promise<T> {
323328
return use_default ? default_answer ?? question() : question();
324329
}
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+
}

packages/create-configs/src/steps/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function create_vscode_config({
7373

7474
'files.exclude': {
7575
...(output_dir != null
76-
? { [`**/${output_dir.replace(/\/$/u, '')}/`]: true }
76+
? { [`**/${output_dir}/`]: true }
7777
: {}),
7878
...(technologies.includes('nextjs')
7979
? { '.next/': true }

packages/create-configs/src/steps/git.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ type Options = Pick<
111111
/** @private */
112112
export function _generate_gitignore(options: Options): string {
113113
return [
114-
gitignore_sets.compiled([options.output_dir]),
114+
gitignore_sets.compiled([`/${options.output_dir}/`]),
115115
gitignore_sets.dependencies(options),
116116
gitignore_sets.logs(options),
117117
gitignore_sets.reports(options),

packages/create-configs/src/steps/language-configuration.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ export function _generate_base_ts_config({
3434
const config: TSConfig = {
3535
extends: [],
3636
compilerOptions: {
37-
baseUrl: input_dir,
38-
outDir: output_dir,
37+
baseUrl: `${input_dir}/`,
38+
outDir: `${output_dir}/`,
3939
},
4040
include: ['./'],
41-
exclude: [output_dir, 'coverage/'],
41+
exclude: [`${output_dir}/`, 'coverage/'],
4242
};
4343

4444
switch (project_type) {
@@ -112,7 +112,7 @@ export function _generate_build_ts_config({
112112
compilerOptions: {
113113
baseUrl: './',
114114
},
115-
include: [input_dir],
115+
include: [`${input_dir}/`],
116116
exclude: ['**/*.spec.ts', '**/*.test.ts'],
117117
};
118118

packages/create-configs/src/steps/scripts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function _generate_build_script({
148148
case 'swc':
149149
deps.d.depend('@swc/core');
150150
scripts['build:js'] =
151-
`${deps.d.depend('@swc/cli', { cmd: 'swc' })} ./${input_dir} --ignore '${test_file_glob}' --out-dir ${output_dir}`;
151+
`${deps.d.depend('@swc/cli', { cmd: 'swc' })} ./${input_dir}/ --ignore '${test_file_glob}' --out-dir ${output_dir}`;
152152
break;
153153
case 'tsc':
154154
scripts['build:js'] =

0 commit comments

Comments
 (0)