diff --git a/__tests__/lib/ls.spec.ts b/__tests__/lib/ls.spec.ts new file mode 100644 index 0000000..7f26559 --- /dev/null +++ b/__tests__/lib/ls.spec.ts @@ -0,0 +1,17 @@ +import * as fu from '../../src'; + +describe('api.ls', () => { + test('ls dir', () => { + const files = fu.ls('src'); + expect(files.includes('index.ts')).toBe(true); + expect(files.includes('lib/.gitkeep')).toBe(true); + expect(files.includes('lib')).toBe(false); + }); + + test('ls include dirs', () => { + const files = fu.ls('src', { onlyFiles: false }); + expect(files.includes('index.ts')).toBe(true); + expect(files.includes('lib/.gitkeep')).toBe(true); + expect(files.includes('lib')).toBe(true); + }); +}); diff --git a/src/index.ts b/src/index.ts index acf4a30..99aeaaf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import cp_r from './lib/cp_r'; +import ls from './lib/ls'; import mkdir_p from './lib/mkdir_p'; import mv from './lib/mv'; import pwd from './lib/pwd'; @@ -10,6 +11,7 @@ declare var wx: any; const Fileutils = { cp_r, + ls, mkdir_p, mv, pwd, diff --git a/src/lib/ls.ts b/src/lib/ls.ts new file mode 100644 index 0000000..bbe1a8b --- /dev/null +++ b/src/lib/ls.ts @@ -0,0 +1,10 @@ +import fs from 'fs'; +import fg from 'fast-glob'; +import type { Options } from 'fast-glob'; + +const ls = (src: string, options?: Options) => { + if (!fs.existsSync(src)) throw new Error(`Path ${src} does not exist`); + return fg.sync(['**/*'], { cwd: src, dot: true, onlyFiles: true, ...options }); +}; + +export default ls;