my-gadgetry 1.0.12 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1,9 @@
1
- export declare const getAllFiles: (dir: string, _files?: string[]) => string[];
1
+ export interface Options {
2
+ include: string[];
3
+ exclude: string[];
4
+ }
5
+ export declare const OPTIONS: {
6
+ include: never[];
7
+ exclude: never[];
8
+ };
9
+ export declare const getAllFiles: (dir: string, _files?: string[], { include, exclude }?: Options) => string[];
package/dist/_core/fs.js CHANGED
@@ -1,12 +1,26 @@
1
1
  import { statSync, readdirSync } from 'fs';
2
- export const getAllFiles = (dir, _files = []) => {
2
+ import { minimatch } from 'minimatch';
3
+ export const OPTIONS = {
4
+ include: [],
5
+ exclude: [],
6
+ };
7
+ const isMatch = (path, patterns) => {
8
+ return patterns.some(pattern => minimatch(path, pattern));
9
+ };
10
+ export const getAllFiles = (dir, _files = [], { include, exclude } = OPTIONS) => {
3
11
  const files = readdirSync(dir);
4
12
  for (const file of files) {
5
13
  const name = dir + '/' + file;
14
+ if (isMatch(name, exclude)) {
15
+ continue;
16
+ }
6
17
  if (statSync(name).isDirectory()) {
7
- getAllFiles(name, _files);
18
+ getAllFiles(name, _files, { include, exclude });
8
19
  }
9
20
  else {
21
+ if (include.length > 0 && !isMatch(name, include)) {
22
+ continue;
23
+ }
10
24
  _files.push(name);
11
25
  }
12
26
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-gadgetry",
3
3
  "type": "module",
4
- "version": "1.0.12",
4
+ "version": "1.1.2",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "repository": {
@@ -29,20 +29,17 @@
29
29
  "tsc": "tsc -p tsconfig.publish.json && tsc-alias -p tsconfig.publish.json",
30
30
  "generate": "tsx --tsconfig ./tsconfig.zx.json ./tools/generate-alias.zx.ts",
31
31
  "ci:update-version": "tsx --tsconfig ./tsconfig.zx.json ./tools/update-version.zx.ts",
32
-
33
32
  "//--- test ---//": "",
34
33
  "test": "run-s test:*",
35
34
  "test:lint": "npm run lint",
36
35
  "test:prettier": "npm run prettier:check",
37
36
  "test:jest": "npm run jest",
38
-
39
37
  "//--- build ---//": "",
40
38
  "build": "run-s build:*",
41
39
  "build:test": "npm run test",
42
40
  "build:clean": "npm run clean",
43
41
  "build:tsc": "npm run tsc",
44
42
  "build:generate": "npm run generate",
45
-
46
43
  "//--- publish ---//": "",
47
44
  "publish:build": "run-s build:{clean,tsc,generate}"
48
45
  },
@@ -51,6 +48,7 @@
51
48
  "@actions/exec": "^1.1.1",
52
49
  "@actions/github": "^6.0.0",
53
50
  "fs-extra": "^11.2.0",
51
+ "minimatch": "^9.0.4",
54
52
  "zx": "^8.1.0"
55
53
  },
56
54
  "devDependencies": {
@@ -72,12 +70,12 @@
72
70
  "husky": "^9.0.11",
73
71
  "jest": "^29.7.0",
74
72
  "lint-staged": "^15.2.2",
75
- "npm-run-all2": "^6.1.2",
73
+ "npm-run-all2": "^6.2.0",
76
74
  "prettier": "^3.2.5",
77
75
  "ts-jest": "^29.1.2",
78
76
  "ts-node": "^10.9.2",
79
77
  "tsc-alias": "^1.8.10",
80
- "tsx": "^4.10.2",
78
+ "tsx": "^4.10.4",
81
79
  "typescript": "^5.4.5",
82
80
  "typescript-eslint": "^7.9.0"
83
81
  },
@@ -0,0 +1,73 @@
1
+ import { resolve, join } from 'path'
2
+ import { getAllFiles } from '../fs.js'
3
+
4
+ describe('getAllFiles', () => {
5
+ const path = resolve(__dirname, '../../..', 'fs-test')
6
+ it('no exclude & include', () => {
7
+ const exclude: string[] = []
8
+ const include: string[] = []
9
+ const expects = [
10
+ join(path, 'foo/piyo/index.ts'),
11
+ join(path, 'foo/piyo.ts'),
12
+ join(path, 'foo.ts'),
13
+ join(path, 'hoge/foo/piyo/index.ts'),
14
+ join(path, 'hoge/foo/piyo.ts'),
15
+ join(path, 'hoge/foo.ts'),
16
+ join(path, 'hoge.ts'),
17
+ ]
18
+ expect(JSON.stringify(getAllFiles(path, [], { include, exclude }))).toBe(JSON.stringify(expects))
19
+ })
20
+ describe('set exclude', () => {
21
+ it('filename', () => {
22
+ const exclude: string[] = ['**/index.ts']
23
+ const include: string[] = []
24
+ const expects = [
25
+ join(path, 'foo/piyo.ts'),
26
+ join(path, 'foo.ts'),
27
+ join(path, 'hoge/foo/piyo.ts'),
28
+ join(path, 'hoge/foo.ts'),
29
+ join(path, 'hoge.ts'),
30
+ ]
31
+ expect(JSON.stringify(getAllFiles(path, [], { include, exclude }))).toBe(JSON.stringify(expects))
32
+ })
33
+ it('dirname', () => {
34
+ const exclude: string[] = ['**/piyo/**/*']
35
+ const include: string[] = []
36
+ const expects = [
37
+ join(path, 'foo/piyo.ts'),
38
+ join(path, 'foo.ts'),
39
+ join(path, 'hoge/foo/piyo.ts'),
40
+ join(path, 'hoge/foo.ts'),
41
+ join(path, 'hoge.ts'),
42
+ ]
43
+ expect(JSON.stringify(getAllFiles(path, [], { include, exclude }))).toBe(JSON.stringify(expects))
44
+ })
45
+ })
46
+ describe('set include', () => {
47
+ it('filename', () => {
48
+ const exclude: string[] = []
49
+ const include: string[] = ['**/index.ts']
50
+ const expects = [join(path, 'foo/piyo/index.ts'), join(path, 'hoge/foo/piyo/index.ts')]
51
+ expect(JSON.stringify(getAllFiles(path, [], { include, exclude }))).toBe(JSON.stringify(expects))
52
+ })
53
+ it('dirname', () => {
54
+ const exclude: string[] = []
55
+ const include: string[] = ['**/foo/**/*']
56
+ const expects = [
57
+ join(path, 'foo/piyo/index.ts'),
58
+ join(path, 'foo/piyo.ts'),
59
+ join(path, 'hoge/foo/piyo/index.ts'),
60
+ join(path, 'hoge/foo/piyo.ts'),
61
+ ]
62
+ expect(JSON.stringify(getAllFiles(path, [], { include, exclude }))).toBe(JSON.stringify(expects))
63
+ })
64
+ })
65
+ describe('set include & exclude', () => {
66
+ it('filename & dirname', () => {
67
+ const exclude: string[] = ['**/p*.ts']
68
+ const include: string[] = ['**/foo/**/*']
69
+ const expects = [join(path, 'foo/piyo/index.ts'), join(path, 'hoge/foo/piyo/index.ts')]
70
+ expect(JSON.stringify(getAllFiles(path, [], { include, exclude }))).toBe(JSON.stringify(expects))
71
+ })
72
+ })
73
+ })
package/src/_core/fs.ts CHANGED
@@ -1,12 +1,34 @@
1
1
  import { statSync, readdirSync } from 'fs'
2
2
 
3
- export const getAllFiles = (dir: string, _files: string[] = []) => {
3
+ import { minimatch } from 'minimatch'
4
+
5
+ export interface Options {
6
+ include: string[]
7
+ exclude: string[]
8
+ }
9
+
10
+ export const OPTIONS = {
11
+ include: [],
12
+ exclude: [],
13
+ }
14
+
15
+ const isMatch = (path: string, patterns: string[]) => {
16
+ return patterns.some(pattern => minimatch(path, pattern))
17
+ }
18
+
19
+ export const getAllFiles = (dir: string, _files: string[] = [], { include, exclude }: Options = OPTIONS) => {
4
20
  const files = readdirSync(dir)
5
21
  for (const file of files) {
6
22
  const name = dir + '/' + file
23
+ if (isMatch(name, exclude)) {
24
+ continue
25
+ }
7
26
  if (statSync(name).isDirectory()) {
8
- getAllFiles(name, _files)
27
+ getAllFiles(name, _files, { include, exclude })
9
28
  } else {
29
+ if (include.length > 0 && !isMatch(name, include)) {
30
+ continue
31
+ }
10
32
  _files.push(name)
11
33
  }
12
34
  }