mirror of
https://gitea.com/actions/dorny-paths-filter.git
synced 2026-01-06 14:28:20 +00:00
Provide shell and escape options when formatting matching files
This commit is contained in:
@@ -1,5 +1,28 @@
|
||||
// Uses easy safe set of characters which can be left unescaped to keep it readable.
|
||||
// Every other character will be backslash-escaped
|
||||
export default function shellEscape(value: string): string {
|
||||
// Backslash escape every character except small subset of definitely safe characters
|
||||
export function escape(value: string): string {
|
||||
return value.replace(/([^a-zA-Z0-9,._+:@%/-])/gm, '\\$1')
|
||||
}
|
||||
|
||||
// Returns filename escaped for usage as shell argument.
|
||||
// Applies "human readable" approach with as few escaping applied as possible
|
||||
export function shellEscape(value: string): string {
|
||||
if (value === '') return value
|
||||
|
||||
// Only safe characters
|
||||
if (/^[a-zA-Z0-9,._+:@%/-]+$/m.test(value)) {
|
||||
return value
|
||||
}
|
||||
|
||||
if (value.includes("'")) {
|
||||
// Only safe characters, single quotes and white-spaces
|
||||
if (/^[a-zA-Z0-9,._+:@%/'\s-]+$/m.test(value)) {
|
||||
return `"${value}"`
|
||||
}
|
||||
|
||||
// Split by single quote and apply escaping recursively
|
||||
return value.split("'").map(shellEscape).join("\\'")
|
||||
}
|
||||
|
||||
// Contains some unsafe characters but no single quote
|
||||
return `'${value}'`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user