From 76dc3bcc20f5f34b79199a5fc8143088453cc87a Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Sat, 21 Dec 2024 22:55:22 +0100 Subject: [PATCH] fix: kpy to properly expand directories (like globby did) --- package.json | 4 +++- src/bin/kpy.ts | 4 ++-- src/fs/fs2.ts | 15 +++++++++++++++ src/fs/kpy.ts | 21 ++++++++++++++++++--- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index a4e6de8..231a116 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,9 @@ "secrets-gen-key-debug": "tsn ./src/bin/secrets-gen-key.ts", "secrets-encrypt-debug": "tsn ./src/bin/secrets-encrypt.ts", "secrets-decrypt-debug": "tsn ./src/bin/secrets-decrypt.ts", - "kpy-debug": "tsn ./src/bin/kpy.ts node_modules dist", + "kpy-debug": "tsn ./src/bin/kpy.ts --verbose scripts tmp/scripts", + "kpy-debug2": "tsn ./src/bin/kpy.ts --verbose scripts bench non-ex non-ex/** colors* tmp/scripts", + "kpy-debug3": "tsn ./src/bin/kpy.ts --verbose src colors csv stream non-ex non-ex/** tmp/src", "json2env-debug": "tsn ./src/bin/json2env.ts ./src/test/someFile.json" }, "dependencies": { diff --git a/src/bin/kpy.ts b/src/bin/kpy.ts index 2f5e949..d3e8314 100644 --- a/src/bin/kpy.ts +++ b/src/bin/kpy.ts @@ -11,11 +11,11 @@ runScript(() => { } = yargs.demandCommand(2).options({ silent: { type: 'boolean', - descr: 'Suppress all text output', // todo: desc! + desc: 'Suppress all text output', }, verbose: { type: 'boolean', - descr: 'Report progress on every file', + desc: 'Report progress on every file', }, overwrite: { type: 'boolean', diff --git a/src/fs/fs2.ts b/src/fs/fs2.ts index a08b3d4..cffa8c6 100644 --- a/src/fs/fs2.ts +++ b/src/fs/fs2.ts @@ -299,6 +299,21 @@ class FS2 { await this.removePathAsync(src) } + /** + * Returns true if the path is a directory. + * Otherwise returns false. + * Doesn't throw, returns false instead. + */ + isDirectory(filePath: string): boolean { + return ( + fs2 + .stat(filePath, { + throwIfNoEntry: false, + }) + ?.isDirectory() || false + ) + } + // Re-export the whole fs/fsp, for the edge cases where they are needed fs = fs fsp = fsp diff --git a/src/fs/kpy.ts b/src/fs/kpy.ts index 9d015f7..65d7424 100644 --- a/src/fs/kpy.ts +++ b/src/fs/kpy.ts @@ -1,5 +1,5 @@ import path from 'node:path' -import { _since, UnixTimestampMillis } from '@naturalcycles/js-lib' +import { _since, localTime, UnixTimestampMillis } from '@naturalcycles/js-lib' import { boldWhite, dimGrey, grey, yellow } from '../colors/colors' import { fastGlob, fs2 } from '../index' @@ -43,7 +43,7 @@ export interface KpyOptions { } export async function kpy(opt: KpyOptions): Promise { - const started = Date.now() as UnixTimestampMillis + const started = localTime.nowUnixMillis() kpyPrepare(opt) @@ -82,7 +82,7 @@ export async function kpy(opt: KpyOptions): Promise { } export function kpySync(opt: KpyOptions): void { - const started = Date.now() as UnixTimestampMillis + const started = localTime.nowUnixMillis() kpyPrepare(opt) @@ -130,6 +130,21 @@ function kpyPrepare(opt: KpyOptions): void { } fs2.ensureDir(opt.outputDir) + + // Expand directories (ex-globby feature), experimental! + const extraPatterns: string[] = [] + + for (const pattern of opt.inputPatterns) { + if (pattern.includes('*')) continue + if (fs2.isDirectory(path.resolve(opt.baseDir, pattern))) { + extraPatterns.push(`${pattern}/**`) + } + } + + if (opt.verbose) { + console.log({ extraPatterns }) + } + opt.inputPatterns.push(...extraPatterns) } function kpyLogFilenames(opt: KpyOptions, filenames: string[]): void {