@file-services/utils
Advanced tools
Comparing version 0.4.8 to 0.4.9
@@ -38,2 +38,3 @@ "use strict"; | ||
function populateDirectorySync(directoryPath, contents) { | ||
const filePaths = []; | ||
ensureDirectorySync(directoryPath); | ||
@@ -45,2 +46,3 @@ for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
writeFileSync(nodePath, nodeValue); | ||
filePaths.push(nodePath); | ||
} | ||
@@ -51,2 +53,3 @@ else { | ||
} | ||
return filePaths; | ||
} | ||
@@ -69,2 +72,29 @@ function removeSync(entryPath) { | ||
} | ||
function findClosestFileSync(initialDirectoryPath, fileName) { | ||
let currentPath = initialDirectoryPath; | ||
let lastPath; | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName); | ||
if (fileExistsSync(filePath)) { | ||
return filePath; | ||
} | ||
lastPath = currentPath; | ||
currentPath = path.dirname(currentPath); | ||
} | ||
return null; | ||
} | ||
function findFilesInAncestorsSync(initialDirectoryPath, fileName) { | ||
const filePaths = []; | ||
let currentPath = initialDirectoryPath; | ||
let lastPath; | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName); | ||
if (fileExistsSync(filePath)) { | ||
filePaths.push(filePath); | ||
} | ||
lastPath = currentPath; | ||
currentPath = path.dirname(currentPath); | ||
} | ||
return filePaths; | ||
} | ||
return Object.assign({}, baseFs, { fileExistsSync, | ||
@@ -74,3 +104,5 @@ directoryExistsSync, | ||
populateDirectorySync, | ||
removeSync }); | ||
removeSync, | ||
findClosestFileSync, | ||
findFilesInAncestorsSync }); | ||
} | ||
@@ -113,2 +145,3 @@ exports.createSyncFileSystem = createSyncFileSystem; | ||
async function populateDirectory(directoryPath, contents) { | ||
const filePaths = []; | ||
await ensureDirectory(directoryPath); | ||
@@ -120,2 +153,3 @@ for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
await writeFile(nodePath, nodeValue); | ||
filePaths.push(nodePath); | ||
} | ||
@@ -126,2 +160,3 @@ else { | ||
} | ||
return filePaths; | ||
} | ||
@@ -142,2 +177,29 @@ async function remove(entryPath) { | ||
} | ||
async function findClosestFile(initialDirectoryPath, fileName) { | ||
let currentPath = initialDirectoryPath; | ||
let lastPath; | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName); | ||
if (await fileExists(filePath)) { | ||
return filePath; | ||
} | ||
lastPath = currentPath; | ||
currentPath = path.dirname(currentPath); | ||
} | ||
return null; | ||
} | ||
async function findFilesInAncestors(initialDirectoryPath, fileName) { | ||
const filePaths = []; | ||
let currentPath = initialDirectoryPath; | ||
let lastPath; | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName); | ||
if (await fileExists(filePath)) { | ||
filePaths.push(filePath); | ||
} | ||
lastPath = currentPath; | ||
currentPath = path.dirname(currentPath); | ||
} | ||
return filePaths; | ||
} | ||
return Object.assign({}, baseFs, { fileExists, | ||
@@ -147,5 +209,7 @@ directoryExists, | ||
populateDirectory, | ||
remove }); | ||
remove, | ||
findClosestFile, | ||
findFilesInAncestors }); | ||
} | ||
exports.createAsyncFileSystem = createAsyncFileSystem; | ||
//# sourceMappingURL=create-extended-api.js.map |
@@ -36,2 +36,3 @@ export function createSyncFileSystem(baseFs) { | ||
function populateDirectorySync(directoryPath, contents) { | ||
const filePaths = []; | ||
ensureDirectorySync(directoryPath); | ||
@@ -43,2 +44,3 @@ for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
writeFileSync(nodePath, nodeValue); | ||
filePaths.push(nodePath); | ||
} | ||
@@ -49,2 +51,3 @@ else { | ||
} | ||
return filePaths; | ||
} | ||
@@ -67,2 +70,29 @@ function removeSync(entryPath) { | ||
} | ||
function findClosestFileSync(initialDirectoryPath, fileName) { | ||
let currentPath = initialDirectoryPath; | ||
let lastPath; | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName); | ||
if (fileExistsSync(filePath)) { | ||
return filePath; | ||
} | ||
lastPath = currentPath; | ||
currentPath = path.dirname(currentPath); | ||
} | ||
return null; | ||
} | ||
function findFilesInAncestorsSync(initialDirectoryPath, fileName) { | ||
const filePaths = []; | ||
let currentPath = initialDirectoryPath; | ||
let lastPath; | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName); | ||
if (fileExistsSync(filePath)) { | ||
filePaths.push(filePath); | ||
} | ||
lastPath = currentPath; | ||
currentPath = path.dirname(currentPath); | ||
} | ||
return filePaths; | ||
} | ||
return Object.assign({}, baseFs, { fileExistsSync, | ||
@@ -72,3 +102,5 @@ directoryExistsSync, | ||
populateDirectorySync, | ||
removeSync }); | ||
removeSync, | ||
findClosestFileSync, | ||
findFilesInAncestorsSync }); | ||
} | ||
@@ -110,2 +142,3 @@ export function createAsyncFileSystem(baseFs) { | ||
async function populateDirectory(directoryPath, contents) { | ||
const filePaths = []; | ||
await ensureDirectory(directoryPath); | ||
@@ -117,2 +150,3 @@ for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
await writeFile(nodePath, nodeValue); | ||
filePaths.push(nodePath); | ||
} | ||
@@ -123,2 +157,3 @@ else { | ||
} | ||
return filePaths; | ||
} | ||
@@ -139,2 +174,29 @@ async function remove(entryPath) { | ||
} | ||
async function findClosestFile(initialDirectoryPath, fileName) { | ||
let currentPath = initialDirectoryPath; | ||
let lastPath; | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName); | ||
if (await fileExists(filePath)) { | ||
return filePath; | ||
} | ||
lastPath = currentPath; | ||
currentPath = path.dirname(currentPath); | ||
} | ||
return null; | ||
} | ||
async function findFilesInAncestors(initialDirectoryPath, fileName) { | ||
const filePaths = []; | ||
let currentPath = initialDirectoryPath; | ||
let lastPath; | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName); | ||
if (await fileExists(filePath)) { | ||
filePaths.push(filePath); | ||
} | ||
lastPath = currentPath; | ||
currentPath = path.dirname(currentPath); | ||
} | ||
return filePaths; | ||
} | ||
return Object.assign({}, baseFs, { fileExists, | ||
@@ -144,4 +206,6 @@ directoryExists, | ||
populateDirectory, | ||
remove }); | ||
remove, | ||
findClosestFile, | ||
findFilesInAncestors }); | ||
} | ||
//# sourceMappingURL=create-extended-api.js.map |
{ | ||
"name": "@file-services/utils", | ||
"description": "Common file system utility functions.", | ||
"version": "0.4.8", | ||
"version": "0.4.9", | ||
"main": "cjs/index.js", | ||
@@ -17,3 +17,3 @@ "module": "esm/index.js", | ||
"dependencies": { | ||
"@file-services/types": "^0.4.8" | ||
"@file-services/types": "^0.4.9" | ||
}, | ||
@@ -33,3 +33,3 @@ "files": [ | ||
"sideEffects": false, | ||
"gitHead": "8d71dfadccd2534c01ad1d3f332915693967e957" | ||
"gitHead": "822c37fa3606df3f2bca380d9968c97a3e18ebcf" | ||
} |
# @file-services/utils | ||
[![npm version](https://img.shields.io/npm/v/@file-services/utils.svg)](https://www.npmjs.com/package/@file-services/utils) | ||
[![package size](https://badgen.net/bundlephobia/minzip/@file-services/utils)](https://bundlephobia.com/result?p=@file-services/utils) | ||
@@ -4,0 +5,0 @@ Common file system utility functions. |
@@ -44,3 +44,4 @@ import { | ||
function populateDirectorySync(directoryPath: string, contents: IDirectoryContents): void { | ||
function populateDirectorySync(directoryPath: string, contents: IDirectoryContents): string[] { | ||
const filePaths: string[] = [] | ||
ensureDirectorySync(directoryPath) | ||
@@ -52,2 +53,3 @@ for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
writeFileSync(nodePath, nodeValue) | ||
filePaths.push(nodePath) | ||
} else { | ||
@@ -57,5 +59,6 @@ populateDirectorySync(nodePath, nodeValue) | ||
} | ||
return filePaths | ||
} | ||
function removeSync(entryPath: string) { | ||
function removeSync(entryPath: string): void { | ||
const stats = lstatSync(entryPath) | ||
@@ -75,2 +78,35 @@ if (stats.isDirectory()) { | ||
function findClosestFileSync(initialDirectoryPath: string, fileName: string): string | null { | ||
let currentPath = initialDirectoryPath | ||
let lastPath: string | undefined | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName) | ||
if (fileExistsSync(filePath)) { | ||
return filePath | ||
} | ||
lastPath = currentPath | ||
currentPath = path.dirname(currentPath) | ||
} | ||
return null | ||
} | ||
function findFilesInAncestorsSync(initialDirectoryPath: string, fileName: string): string[] { | ||
const filePaths: string[] = [] | ||
let currentPath = initialDirectoryPath | ||
let lastPath: string | undefined | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName) | ||
if (fileExistsSync(filePath)) { | ||
filePaths.push(filePath) | ||
} | ||
lastPath = currentPath | ||
currentPath = path.dirname(currentPath) | ||
} | ||
return filePaths | ||
} | ||
return { | ||
@@ -82,3 +118,5 @@ ...baseFs, | ||
populateDirectorySync, | ||
removeSync | ||
removeSync, | ||
findClosestFileSync, | ||
findFilesInAncestorsSync | ||
} | ||
@@ -122,3 +160,4 @@ } | ||
async function populateDirectory(directoryPath: string, contents: IDirectoryContents): Promise<void> { | ||
async function populateDirectory(directoryPath: string, contents: IDirectoryContents): Promise<string[]> { | ||
const filePaths: string[] = [] | ||
await ensureDirectory(directoryPath) | ||
@@ -130,2 +169,3 @@ for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
await writeFile(nodePath, nodeValue) | ||
filePaths.push(nodePath) | ||
} else { | ||
@@ -135,5 +175,6 @@ await populateDirectory(nodePath, nodeValue) | ||
} | ||
return filePaths | ||
} | ||
async function remove(entryPath: string) { | ||
async function remove(entryPath: string): Promise<void> { | ||
const stats = await lstat(entryPath) | ||
@@ -151,2 +192,35 @@ if (stats.isDirectory()) { | ||
async function findClosestFile(initialDirectoryPath: string, fileName: string): Promise<string | null> { | ||
let currentPath = initialDirectoryPath | ||
let lastPath: string | undefined | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName) | ||
if (await fileExists(filePath)) { | ||
return filePath | ||
} | ||
lastPath = currentPath | ||
currentPath = path.dirname(currentPath) | ||
} | ||
return null | ||
} | ||
async function findFilesInAncestors(initialDirectoryPath: string, fileName: string): Promise<string[]> { | ||
const filePaths: string[] = [] | ||
let currentPath = initialDirectoryPath | ||
let lastPath: string | undefined | ||
while (currentPath !== lastPath) { | ||
const filePath = path.join(currentPath, fileName) | ||
if (await fileExists(filePath)) { | ||
filePaths.push(filePath) | ||
} | ||
lastPath = currentPath | ||
currentPath = path.dirname(currentPath) | ||
} | ||
return filePaths | ||
} | ||
return { | ||
@@ -158,4 +232,6 @@ ...baseFs, | ||
populateDirectory, | ||
remove | ||
remove, | ||
findClosestFile, | ||
findFilesInAncestors | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86128
1419
33
Updated@file-services/types@^0.4.9