Skip to content

Commit

Permalink
refactor: simplify quote regexp (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Dec 25, 2024
1 parent 95f8bfd commit 131e6c9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,12 @@ export async function importPath(
filepath: string,
origin = filepath
): Promise<void> {
const ext = path.extname(filepath)
const base = path.basename(filepath)
const dir = path.dirname(filepath)
const { ext, base, dir } = path.parse(filepath)

if (ext === '') {
const tmpFilename = fs.existsSync(`${filepath}.mjs`)
? `${base}-${randomId()}.mjs`
: `${base}.mjs`
const tmpFilename = fs.existsSync(filepath + EXT)
? base + '-' + randomId() + EXT
: base + EXT

return writeAndImport(
await fs.readFile(filepath),
Expand All @@ -205,7 +203,7 @@ export async function importPath(
if (ext === '.md') {
return writeAndImport(
transformMarkdown(await fs.readFile(filepath)),
path.join(dir, base + '.mjs'),
path.join(dir, base + EXT),
origin
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function preferLocalBin(
// }

export function quote(arg: string): string {
if (/^[a-z0-9/_.\-@:=]+$/i.test(arg) || arg === '') {
if (/^[\w/.-@:=]+$/.test(arg) || arg === '') {
return arg
}
return (
Expand All @@ -120,7 +120,7 @@ export function quote(arg: string): string {
}

export function quotePowerShell(arg: string): string {
if (/^[a-z0-9/_.\-]+$/i.test(arg) || arg === '') {
if (/^[\w/.-]+$/.test(arg) || arg === '') {
return arg
}
return `'` + arg.replace(/'/g, "''") + `'`
Expand Down

0 comments on commit 131e6c9

Please sign in to comment.