-
Notifications
You must be signed in to change notification settings - Fork 79
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
7 changed files
with
135 additions
and
143 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
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
export const removeEmptyFolders = (p: string) => { | ||
const files = fs.readdirSync(p); | ||
if (!files) { | ||
fs.rmdirSync(p); | ||
} | ||
for (const file of files) { | ||
const fullPath = path.join(p, file); | ||
if (fs.lstatSync(fullPath).isDirectory()) { | ||
removeEmptyFolders(fullPath); | ||
if (!fs.readdirSync(fullPath).length) { | ||
fs.rmdirSync(fullPath); | ||
} | ||
} | ||
/** Recursively removes all empty folders. */ | ||
export function removeEmptyFolders(directory: string): void { | ||
const files = fs.readdirSync(directory); | ||
if (!files) return fs.rmdirSync(directory); | ||
|
||
for (const filePath of files) { | ||
const fullPath = path.resolve(directory, filePath); | ||
|
||
const isDirectory = fs.lstatSync(fullPath).isDirectory(); | ||
if (!isDirectory) continue; | ||
|
||
removeEmptyFolders(fullPath); | ||
|
||
const isEmpty = fs.readdirSync(fullPath).length === 0; | ||
if (isEmpty) fs.rmdirSync(fullPath); | ||
} | ||
}; | ||
} |
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,61 @@ | ||
import glob from "glob"; | ||
import path from "path"; | ||
import { existsSync, lstatSync, readdirSync } from "fs-extra"; | ||
|
||
import logger from "../shared/logger"; | ||
import { Options } from "../index"; | ||
|
||
const isDirectory = (filePath: string) => lstatSync(filePath).isDirectory(); | ||
const isFile = (filePath: string) => lstatSync(filePath).isFile(); | ||
|
||
const globSearch = (pattern: string) => { | ||
const matches = glob.sync(pattern); | ||
const files = matches.filter(match => isFile(match)); | ||
|
||
if (files.length === 0) { | ||
logger.error("Could not find any files for: " + pattern, 1); | ||
} | ||
|
||
return files; | ||
}; | ||
|
||
/** Recursively get all file paths. */ | ||
export const getFilePaths = (paths: string[], options: Options) => { | ||
let { detectRoots } = options; | ||
const files: string[][] = []; | ||
|
||
while (paths.length > 0) { | ||
const filePath = paths.pop(); | ||
|
||
if (filePath == null || filePath.length === 0) continue; | ||
|
||
const isGlobPattern = glob.hasMagic(filePath); | ||
if (isGlobPattern) { | ||
files.push(globSearch(filePath)); | ||
continue; | ||
} | ||
|
||
if (existsSync(filePath)) { | ||
if (isFile(filePath)) { | ||
files.push([filePath]); | ||
} else if (isDirectory(filePath)) { | ||
if (detectRoots) { | ||
const childDirectories = readdirSync(path.resolve(filePath)) | ||
.map(x => path.join(filePath, x)) | ||
.filter(x => isDirectory(x)); | ||
|
||
paths.push(...childDirectories); | ||
detectRoots = false; | ||
} else { | ||
paths.push(path.join(filePath, "/**/*.*")); | ||
} | ||
} | ||
} else { | ||
logger.error(`Unable to resolve the path: ${filePath}`); | ||
} | ||
} | ||
|
||
return files; | ||
}; | ||
|
||
export default getFilePaths; |
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