@arktype/fs
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -56,5 +56,4 @@ import path from "node:path" | ||
let formatted = original | ||
if (original.startsWith("file:///")) { | ||
formatted = fileURLToPath(original) | ||
} | ||
if (original.startsWith("file:///")) formatted = fileURLToPath(original) | ||
if (relative) { | ||
@@ -66,5 +65,5 @@ formatted = path.relative( | ||
} | ||
if (separator) { | ||
if (separator) | ||
formatted = formatted.replace(new RegExp(`\\${path.sep}`, "g"), separator) | ||
} | ||
return formatted | ||
@@ -75,5 +74,4 @@ } | ||
let upStackBy = options.upStackBy ?? 0 | ||
if (!options.methodName && !options.upStackBy) { | ||
upStackBy = 3 | ||
} | ||
if (!options.methodName && !options.upStackBy) upStackBy = 3 | ||
let match: SourcePosition | undefined | ||
@@ -96,11 +94,7 @@ while (!match) { | ||
} | ||
if (options.skip?.(candidate)) { | ||
upStackBy++ | ||
} else { | ||
match = candidate | ||
} | ||
if (options.skip?.(candidate)) upStackBy++ | ||
else match = candidate | ||
} | ||
if (match.file.startsWith("file:///")) { | ||
match.file = fileURLToPath(match.file) | ||
} | ||
if (match.file.startsWith("file:///")) match.file = fileURLToPath(match.file) | ||
return match | ||
@@ -107,0 +101,0 @@ } |
32
fs.ts
@@ -22,8 +22,6 @@ import { | ||
if (existsSync(path)) { | ||
if (!statSync(path).isDirectory()) { | ||
if (!statSync(path).isDirectory()) | ||
throw new Error(`${path} exists and is not a directory.`) | ||
} | ||
} else { | ||
mkdirSync(path, { recursive: true }) | ||
} | ||
} else mkdirSync(path, { recursive: true }) | ||
return path | ||
@@ -68,8 +66,6 @@ } | ||
const isFile = lstatSync(path).isFile() | ||
if (isDir && options.ignoreDirsMatching?.test(path)) { | ||
return paths | ||
} | ||
if (isFile && options.ignoreFilesMatching?.test(path)) { | ||
return paths | ||
} | ||
if (isDir && options.ignoreDirsMatching?.test(path)) return paths | ||
if (isFile && options.ignoreFilesMatching?.test(path)) return paths | ||
const excludeCurrent = | ||
@@ -130,5 +126,3 @@ (options.excludeDirs && isDir) || | ||
*/ | ||
if ("name" in contents) { | ||
return dirToCheck | ||
} | ||
if ("name" in contents) return dirToCheck | ||
} catch { | ||
@@ -155,3 +149,3 @@ // If the file doesn't exist, go up another level | ||
.split("\n") | ||
.filter((path) => existsSync(path) && statSync(path).isFile()) | ||
.filter(path => existsSync(path) && statSync(path).isFile()) | ||
@@ -161,3 +155,3 @@ export const tsFileMatcher = /^.*\.(c|m)?tsx?$/ | ||
const inFileFilter: WalkOptions = { | ||
include: (path) => tsFileMatcher.test(path), | ||
include: path => tsFileMatcher.test(path), | ||
ignoreDirsMatching: /node_modules|out|dist|docgen/ | ||
@@ -172,3 +166,3 @@ } | ||
export const getSourceFileEntries = (dir = "."): SourceFileEntry[] => | ||
getSourceFilePaths(dir).map((path) => [path, readFile(path)]) | ||
getSourceFilePaths(dir).map(path => [path, readFile(path)]) | ||
@@ -190,3 +184,3 @@ export type ReplacementDictionary = Record< | ||
const findReplace: (matchers: Matchers) => (input: string) => string = | ||
(matchers) => (input) => | ||
matchers => input => | ||
matchers.reduce((acc, m) => acc.replaceAll(m.pattern, m.replacement), input) | ||
@@ -202,3 +196,3 @@ | ||
(files: readonly string[] = []): void => | ||
void files.forEach((file) => { | ||
void files.forEach(file => { | ||
writeFile(file, findReplace(matchers)(readFile(file))) | ||
@@ -205,0 +199,0 @@ }) |
@@ -64,3 +64,3 @@ // Copied from unmaintained package https://github.com/bevry/get-current-line to fix imports | ||
try { | ||
// @ts-expect-error | ||
/** @ts-expect-error https://github.com/winstonjs/winston/issues/401#issuecomment-61913086 */ | ||
const previous = err.__previous__ || err.__previous | ||
@@ -75,10 +75,5 @@ stack = previous && previous.stack | ||
if (stack) { | ||
if (Array.isArray(stack)) { | ||
frames = Array(stack) | ||
} else { | ||
frames = stack.toString().split("\n") | ||
} | ||
} else { | ||
frames = [] | ||
} | ||
if (Array.isArray(stack)) frames = Array(stack) | ||
else frames = stack.toString().split("\n") | ||
} else frames = [] | ||
@@ -105,5 +100,3 @@ // Parse our frames | ||
// skip empty lines | ||
if (line.length === 0) { | ||
continue | ||
} | ||
if (line.length === 0) continue | ||
@@ -110,0 +103,0 @@ // Error |
@@ -20,11 +20,9 @@ import path from "node:path"; | ||
let formatted = original; | ||
if (original.startsWith("file:///")) { | ||
if (original.startsWith("file:///")) | ||
formatted = fileURLToPath(original); | ||
} | ||
if (relative) { | ||
formatted = path.relative(typeof relative === "string" ? relative : process.cwd(), formatted); | ||
} | ||
if (separator) { | ||
if (separator) | ||
formatted = formatted.replace(new RegExp(`\\${path.sep}`, "g"), separator); | ||
} | ||
return formatted; | ||
@@ -34,5 +32,4 @@ }; | ||
let upStackBy = options.upStackBy ?? 0; | ||
if (!options.methodName && !options.upStackBy) { | ||
if (!options.methodName && !options.upStackBy) | ||
upStackBy = 3; | ||
} | ||
let match; | ||
@@ -51,12 +48,9 @@ while (!match) { | ||
}; | ||
if (options.skip?.(candidate)) { | ||
if (options.skip?.(candidate)) | ||
upStackBy++; | ||
} | ||
else { | ||
else | ||
match = candidate; | ||
} | ||
} | ||
if (match.file.startsWith("file:///")) { | ||
if (match.file.startsWith("file:///")) | ||
match.file = fileURLToPath(match.file); | ||
} | ||
return match; | ||
@@ -63,0 +57,0 @@ }; |
@@ -11,9 +11,7 @@ import { cpSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs"; | ||
if (existsSync(path)) { | ||
if (!statSync(path).isDirectory()) { | ||
if (!statSync(path).isDirectory()) | ||
throw new Error(`${path} exists and is not a directory.`); | ||
} | ||
} | ||
else { | ||
else | ||
mkdirSync(path, { recursive: true }); | ||
} | ||
return path; | ||
@@ -32,8 +30,6 @@ }; | ||
const isFile = lstatSync(path).isFile(); | ||
if (isDir && options.ignoreDirsMatching?.test(path)) { | ||
if (isDir && options.ignoreDirsMatching?.test(path)) | ||
return paths; | ||
} | ||
if (isFile && options.ignoreFilesMatching?.test(path)) { | ||
if (isFile && options.ignoreFilesMatching?.test(path)) | ||
return paths; | ||
} | ||
const excludeCurrent = (options.excludeDirs && isDir) || | ||
@@ -79,5 +75,4 @@ (options.excludeFiles && !isDir) || | ||
*/ | ||
if ("name" in contents) { | ||
if ("name" in contents) | ||
return dirToCheck; | ||
} | ||
} | ||
@@ -100,10 +95,10 @@ catch { | ||
.split("\n") | ||
.filter((path) => existsSync(path) && statSync(path).isFile()); | ||
.filter(path => existsSync(path) && statSync(path).isFile()); | ||
export const tsFileMatcher = /^.*\.(c|m)?tsx?$/; | ||
const inFileFilter = { | ||
include: (path) => tsFileMatcher.test(path), | ||
include: path => tsFileMatcher.test(path), | ||
ignoreDirsMatching: /node_modules|out|dist|docgen/ | ||
}; | ||
export const getSourceFilePaths = (dir = ".") => walkPaths(dir, inFileFilter); | ||
export const getSourceFileEntries = (dir = ".") => getSourceFilePaths(dir).map((path) => [path, readFile(path)]); | ||
export const getSourceFileEntries = (dir = ".") => getSourceFilePaths(dir).map(path => [path, readFile(path)]); | ||
/** | ||
@@ -113,3 +108,3 @@ * Given a set of {@link Matchers} and an input string, | ||
*/ | ||
const findReplace = (matchers) => (input) => matchers.reduce((acc, m) => acc.replaceAll(m.pattern, m.replacement), input); | ||
const findReplace = matchers => input => matchers.reduce((acc, m) => acc.replaceAll(m.pattern, m.replacement), input); | ||
/** | ||
@@ -120,3 +115,3 @@ * Applies a set of {@link Matchers} to a list of files and rewrites | ||
*/ | ||
const findReplaceMany = (matchers) => (files = []) => void files.forEach((file) => { | ||
const findReplaceMany = (matchers) => (files = []) => void files.forEach(file => { | ||
writeFile(file, findReplace(matchers)(readFile(file))); | ||
@@ -123,0 +118,0 @@ }); |
@@ -15,3 +15,3 @@ // Copied from unmaintained package https://github.com/bevry/get-current-line to fix imports | ||
try { | ||
// @ts-expect-error | ||
/** @ts-expect-error https://github.com/winstonjs/winston/issues/401#issuecomment-61913086 */ | ||
const previous = err.__previous__ || err.__previous; | ||
@@ -26,12 +26,9 @@ stack = previous && previous.stack; | ||
if (stack) { | ||
if (Array.isArray(stack)) { | ||
if (Array.isArray(stack)) | ||
frames = Array(stack); | ||
} | ||
else { | ||
else | ||
frames = stack.toString().split("\n"); | ||
} | ||
} | ||
else { | ||
else | ||
frames = []; | ||
} | ||
// Parse our frames | ||
@@ -52,5 +49,4 @@ return frames; | ||
// skip empty lines | ||
if (line.length === 0) { | ||
if (line.length === 0) | ||
continue; | ||
} | ||
// Error | ||
@@ -57,0 +53,0 @@ // at file:///Users/balupton/Projects/active/get-current-line/asd.js:1:13 |
{ | ||
"name": "@arktype/fs", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "David Blass", |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
91141
1008