-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoutputPathCompiler.mjs
34 lines (31 loc) · 1.12 KB
/
outputPathCompiler.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// @ts-check
import path from 'node:path';
/**
* @param {string} input
* @param {string} output
* @returns {(file: string) => string}
*/
export const createOutputPathCompiler = (input, output) => {
return output.includes(path.sep)
? /\*\*\/.*\*.*/.test(output) // "**/" and "*" used in pattern
? (file) => {
// retrieve what ** was matched as
const matched = new RegExp(
input
.replace(/\*/g, '\\*')
.replace('\\*\\*', '(?<dir>.*)')
.replace('\\*', '.*'),
).exec(file);
const dir = matched ? matched[1] : undefined;
return (
dir === undefined
? output.replace('**/', '')
: output.replace('**', dir)
).replace('*', path.parse(file).name);
}
: (file) => output.replace('*', path.parse(file).name) // no "**"" placeholder, flattening to the same directory
: (file) => {
const parsed = path.parse(file);
return path.join(parsed.dir, output.replace('*', parsed.name)); // no path separator -> output in the same directory as input
};
};