Skip to content

Commit d656b9e

Browse files
committed
new 2 version
1 parent b4090ed commit d656b9e

File tree

18 files changed

+826
-181
lines changed

18 files changed

+826
-181
lines changed

.directoryvalidator.json

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
".gitignore",
44
".directoryvalidator.json",
55
"package-lock.json",
6-
".DS_Store"
6+
".DS_Store",
7+
"**/.DS_Store"
78
],
8-
"ignoreDirs": ["node_modules", "dist", ".git", ".github"],
9+
"ignoreDirs": ["node_modules", "dist", ".git", ".github", "**/temp42"],
910
"commonRules": {
1011
"rule_indexfile": {
1112
"type": "file",
@@ -15,33 +16,17 @@
1516
"rules": [
1617
{
1718
"type": "file",
18-
"name": "package.json"
19-
},
20-
{
21-
"type": "file",
22-
"name": "LICENSE"
23-
},
24-
{
25-
"type": "file",
26-
"name": "tsconfig.json"
27-
},
28-
{
29-
"type": "file",
30-
"name": "jest.*.js"
31-
},
32-
{
33-
"type": "file",
34-
"name": "*.md"
35-
},
36-
{
37-
"type": "file",
38-
"name": ".prettier*"
19+
"name": "testfile.json"
3920
},
4021
{
4122
"type": "directory",
42-
"name": "src",
43-
"isOptional": true,
44-
"rules": []
23+
"name": "temp2",
24+
"rules": [
25+
{
26+
"type": "directory",
27+
"name": "temp3"
28+
}
29+
]
4530
}
4631
]
4732
}

biome.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.8.2/schema.json",
3+
"organizeImports": {
4+
"enabled": true
5+
},
6+
"formatter": {
7+
"enabled": true,
8+
"indentStyle": "space"
9+
},
10+
"linter": {
11+
"enabled": true,
12+
"rules": {
13+
"recommended": true,
14+
"complexity": {
15+
"noForEach": "off"
16+
},
17+
"correctness": {
18+
"noUnusedImports": "error",
19+
"noUnusedVariables": "error"
20+
}
21+
}
22+
},
23+
"javascript": {
24+
"formatter": {
25+
"quoteStyle": "single"
26+
}
27+
}
28+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
"directory-validator": "dist/index.js"
88
},
99
"scripts": {
10+
"dev": "npx tsx src/index.ts",
1011
"build": "npm run clean && tsc && cp src/resources/* dist/resources",
1112
"clean": "rm -rf dist",
1213
"prettier": "prettier -c .",
13-
"test": "npm run prettier && jest"
14+
"test": "jest"
1415
},
1516
"author": "Erwin Gaitan O <erwingaitano@gmail.com>",
1617
"license": "MIT",

src/_tests/program/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as path from 'path';
1+
import path from 'node:path';
22
import * as errors from '../../errors';
33
import * as program from '../../program';
44

src/_tests/validator/directory.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as types from '../../types';
1+
import type * as types from '../../types';
22
import * as validator from '../../validator';
33

44
export function run() {
@@ -61,7 +61,7 @@ export function run() {
6161
];
6262

6363
expect(() => validator.run(files, configObject)).toThrowError(
64-
`${JSON.stringify(configObject[0])}, deep: 1, rule did not passed`
64+
`${JSON.stringify(configObject[0])}, deep: 1, rule did not passed`,
6565
);
6666
});
6767

@@ -158,7 +158,7 @@ export function run() {
158158
];
159159

160160
expect(() => validator.run(files, configObject)).toThrowError(
161-
'}, deep: 2, rule did not passed'
161+
'}, deep: 2, rule did not passed',
162162
);
163163
});
164164

@@ -172,7 +172,7 @@ export function run() {
172172
];
173173

174174
expect(() => validator.run(files, configObject)).toThrowError(
175-
`${JSON.stringify(configObject[0])}, deep: 1, rule did not passed`
175+
`${JSON.stringify(configObject[0])}, deep: 1, rule did not passed`,
176176
);
177177
});
178178

@@ -233,7 +233,7 @@ export function run() {
233233
});
234234

235235
describe('EmptyDirs:', () => {
236-
it('should validate empty dir has no rules', () => {
236+
it('should validate empty dir with no rules', () => {
237237
const files: string[] = [];
238238
const emptyDirs = ['src'];
239239

@@ -245,7 +245,29 @@ export function run() {
245245
];
246246

247247
expect(() =>
248-
validator.run(files, configObject, emptyDirs)
248+
validator.run(files, configObject, emptyDirs),
249+
).not.toThrow();
250+
});
251+
252+
it('should validate nested empty dir with no rules', () => {
253+
const files: string[] = [];
254+
const emptyDirs = ['src/lol'];
255+
const configObject: types.Rules = [
256+
{
257+
name: 'src',
258+
type: 'directory',
259+
rules: [
260+
{
261+
name: 'lol',
262+
type: 'directory',
263+
rules: [],
264+
},
265+
],
266+
},
267+
];
268+
269+
expect(() =>
270+
validator.run(files, configObject, emptyDirs),
249271
).not.toThrow();
250272
});
251273

@@ -262,9 +284,9 @@ export function run() {
262284
];
263285

264286
expect(() =>
265-
validator.run(files, configObject, emptyDirs)
287+
validator.run(files, configObject, emptyDirs),
266288
).toThrowError(
267-
`${JSON.stringify(configObject[0])}, deep: 1, rule did not passed`
289+
`${JSON.stringify(configObject[0])}, deep: 1, rule did not passed`,
268290
);
269291
});
270292

@@ -280,7 +302,7 @@ export function run() {
280302
];
281303

282304
expect(() =>
283-
validator.run(files, configObject, emptyDirs)
305+
validator.run(files, configObject, emptyDirs),
284306
).toThrowError('src, was not validated');
285307
});
286308
});

src/_tests/validator/fileDirectory.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function run() {
132132
];
133133

134134
expect(() => validator.run(files, configObject)).toThrowError(
135-
`${JSON.stringify(configObject[2])}, deep: 1, rule did not passed`
135+
`${JSON.stringify(configObject[2])}, deep: 1, rule did not passed`,
136136
);
137137
});
138138

@@ -155,7 +155,7 @@ export function run() {
155155
];
156156

157157
expect(() => validator.run(files, configObject)).toThrowError(
158-
`${files[3]}, was not validated`
158+
`${files[3]}, was not validated`,
159159
);
160160
});
161161

@@ -211,7 +211,7 @@ export function run() {
211211
];
212212

213213
expect(() => validator.run(files, configObject)).toThrowError(
214-
`${files[4]}, was not validated`
214+
`${files[4]}, was not validated`,
215215
);
216216
});
217217

@@ -244,8 +244,8 @@ export function run() {
244244

245245
expect(() => validator.run(files, configObject)).toThrowError(
246246
`${JSON.stringify(
247-
(configObject[0] as types.DirectoryRule).rules![2]
248-
)}, deep: 2, rule did not passed`
247+
(configObject[0] as types.DirectoryRule).rules![2],
248+
)}, deep: 2, rule did not passed`,
249249
);
250250
});
251251

@@ -273,8 +273,8 @@ export function run() {
273273

274274
expect(() => validator.run(files, configObject)).toThrowError(
275275
`${JSON.stringify(
276-
(configObject[0] as types.DirectoryRule).rules![0]
277-
)}, deep: 2, rule did not passed`
276+
(configObject[0] as types.DirectoryRule).rules![0],
277+
)}, deep: 2, rule did not passed`,
278278
);
279279
});
280280

@@ -305,8 +305,8 @@ export function run() {
305305

306306
expect(() => validator.run(files, configObject)).toThrowError(
307307
`${JSON.stringify(
308-
(configObject[0] as types.DirectoryRule).rules![0]
309-
)}, deep: 2, rule did not passed`
308+
(configObject[0] as types.DirectoryRule).rules![0],
309+
)}, deep: 2, rule did not passed`,
310310
);
311311
});
312312
});
@@ -357,7 +357,7 @@ export function run() {
357357
];
358358

359359
expect(() => validator.run(files, configObject)).toThrowError(
360-
'srrcNice/index.js, was not validated'
360+
'srrcNice/index.js, was not validated',
361361
);
362362
});
363363
});
@@ -393,7 +393,7 @@ export function run() {
393393
];
394394

395395
expect(() => validator.run(files, configObject)).toThrowError(
396-
'SRC/index.js, was not validated'
396+
'SRC/index.js, was not validated',
397397
);
398398
});
399399
});

src/helpers/file.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
type File = {
5+
type: 'file';
6+
fullPath: string;
7+
path: string;
8+
};
9+
10+
type Directory = {
11+
type: 'dir';
12+
fullPath: string;
13+
path: string;
14+
isEmpty: boolean;
15+
};
16+
17+
export function getFilesAndDirectories(
18+
dirPath: string,
19+
options: {
20+
recursive: boolean;
21+
_initialPath?: string;
22+
ignoreFilesAndDirectories?: string[];
23+
} = { recursive: true },
24+
): (File | Directory)[] {
25+
const items = fs.readdirSync(dirPath);
26+
options._initialPath = options._initialPath || dirPath;
27+
const lPath = path.relative(options._initialPath, dirPath);
28+
29+
if (options.ignoreFilesAndDirectories?.includes(lPath)) return [];
30+
31+
const result: (File | Directory)[] = [
32+
{
33+
type: 'dir',
34+
fullPath: dirPath,
35+
path: lPath,
36+
isEmpty: items.length === 0,
37+
},
38+
];
39+
40+
for (const item of items) {
41+
const fullPath = path.join(dirPath, item);
42+
const itemStat = fs.statSync(fullPath);
43+
const localPath = path.relative(options._initialPath, fullPath);
44+
45+
if (itemStat.isDirectory()) {
46+
if (!options.recursive)
47+
result.push({ type: 'dir', fullPath, path: localPath, isEmpty: false });
48+
else result.push(...getFilesAndDirectories(fullPath, options));
49+
} else {
50+
if (options.ignoreFilesAndDirectories?.includes(localPath)) continue;
51+
result.push({ type: 'file', fullPath, path: localPath });
52+
}
53+
}
54+
55+
return result;
56+
}

0 commit comments

Comments
 (0)