Skip to content

Commit

Permalink
feat: add ls cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
afeiship committed Aug 9, 2023
1 parent 05d7cdd commit 5243ac9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions __tests__/lib/ls.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,6 +11,7 @@ declare var wx: any;

const Fileutils = {
cp_r,
ls,
mkdir_p,
mv,
pwd,
Expand Down
10 changes: 10 additions & 0 deletions src/lib/ls.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 5243ac9

Please sign in to comment.