Skip to content

Commit

Permalink
fix: kpy to properly expand directories (like globby did)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Dec 21, 2024
1 parent 882897c commit 76dc3bc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/kpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions src/fs/fs2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions src/fs/kpy.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -43,7 +43,7 @@ export interface KpyOptions {
}

export async function kpy(opt: KpyOptions): Promise<void> {
const started = Date.now() as UnixTimestampMillis
const started = localTime.nowUnixMillis()

kpyPrepare(opt)

Expand Down Expand Up @@ -82,7 +82,7 @@ export async function kpy(opt: KpyOptions): Promise<void> {
}

export function kpySync(opt: KpyOptions): void {
const started = Date.now() as UnixTimestampMillis
const started = localTime.nowUnixMillis()

kpyPrepare(opt)

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 76dc3bc

Please sign in to comment.