-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
import { fdir } from 'fdir'; | ||
|
||
export async function prepareFilePaths({ | ||
inputPaths, | ||
outputDirectoryPath, | ||
extensions, | ||
}) { | ||
const files = new Set(); | ||
const directories = new Set(); | ||
const inputPathsSet = new Set(inputPaths); | ||
|
||
await Promise.all( | ||
[...inputPathsSet].map(async currentPath => { | ||
try { | ||
const stat = await fs.promises.stat(currentPath); | ||
|
||
if (stat.isDirectory()) { | ||
directories.add(currentPath); | ||
} else if (stat.isFile() && checkFileType(currentPath, extensions)) { | ||
files.add(getRelativePath(currentPath)); | ||
} | ||
} catch {} | ||
}), | ||
); | ||
|
||
const crawler = new fdir() // eslint-disable-line new-cap | ||
.withFullPaths() | ||
.filter(currentPath => checkFileType(currentPath, extensions)); | ||
|
||
const crawledPaths = await Promise.all( | ||
[...directories].map(currentPath => crawler.crawl(currentPath).withPromise()), | ||
); | ||
|
||
for (const crawledPath of crawledPaths.flat()) { | ||
files.add(crawledPath); | ||
} | ||
|
||
const result = [...files].map(filePath => { | ||
let outputPath = filePath; | ||
|
||
if (outputDirectoryPath) { | ||
for (const directory of directories) { | ||
if (filePath.startsWith(directory)) { | ||
outputPath = path.join(outputDirectoryPath, filePath.slice(directory.length)); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return { | ||
input: getRelativePath(filePath), | ||
output: outputPath, | ||
}; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
function getRelativePath(filePath) { | ||
const replacePath = `${process.cwd()}${path.sep}`; | ||
|
||
return filePath.startsWith(replacePath) | ||
? filePath.slice(replacePath.length) | ||
: filePath; | ||
} | ||
|
||
function checkFileType(filePath, extensions) { | ||
const extension = path.extname(filePath).toLowerCase().slice(1); | ||
return extensions.includes(extension); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.