Skip to content

Commit

Permalink
new 2 version
Browse files Browse the repository at this point in the history
  • Loading branch information
goerwin committed Jul 17, 2024
1 parent b4090ed commit d656b9e
Show file tree
Hide file tree
Showing 18 changed files with 826 additions and 181 deletions.
37 changes: 11 additions & 26 deletions .directoryvalidator.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
".gitignore",
".directoryvalidator.json",
"package-lock.json",
".DS_Store"
".DS_Store",
"**/.DS_Store"
],
"ignoreDirs": ["node_modules", "dist", ".git", ".github"],
"ignoreDirs": ["node_modules", "dist", ".git", ".github", "**/temp42"],
"commonRules": {
"rule_indexfile": {
"type": "file",
Expand All @@ -15,33 +16,17 @@
"rules": [
{
"type": "file",
"name": "package.json"
},
{
"type": "file",
"name": "LICENSE"
},
{
"type": "file",
"name": "tsconfig.json"
},
{
"type": "file",
"name": "jest.*.js"
},
{
"type": "file",
"name": "*.md"
},
{
"type": "file",
"name": ".prettier*"
"name": "testfile.json"
},
{
"type": "directory",
"name": "src",
"isOptional": true,
"rules": []
"name": "temp2",
"rules": [
{
"type": "directory",
"name": "temp3"
}
]
}
]
}
28 changes: 28 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.2/schema.json",
"organizeImports": {
"enabled": true
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"complexity": {
"noForEach": "off"
},
"correctness": {
"noUnusedImports": "error",
"noUnusedVariables": "error"
}
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
"directory-validator": "dist/index.js"
},
"scripts": {
"dev": "npx tsx src/index.ts",
"build": "npm run clean && tsc && cp src/resources/* dist/resources",
"clean": "rm -rf dist",
"prettier": "prettier -c .",
"test": "npm run prettier && jest"
"test": "jest"
},
"author": "Erwin Gaitan O <[email protected]>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/_tests/program/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from 'path';
import path from 'node:path';
import * as errors from '../../errors';
import * as program from '../../program';

Expand Down
40 changes: 31 additions & 9 deletions src/_tests/validator/directory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as types from '../../types';
import type * as types from '../../types';
import * as validator from '../../validator';

export function run() {
Expand Down Expand Up @@ -61,7 +61,7 @@ export function run() {
];

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

Expand Down Expand Up @@ -158,7 +158,7 @@ export function run() {
];

expect(() => validator.run(files, configObject)).toThrowError(
'}, deep: 2, rule did not passed'
'}, deep: 2, rule did not passed',
);
});

Expand All @@ -172,7 +172,7 @@ export function run() {
];

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

Expand Down Expand Up @@ -233,7 +233,7 @@ export function run() {
});

describe('EmptyDirs:', () => {
it('should validate empty dir has no rules', () => {
it('should validate empty dir with no rules', () => {
const files: string[] = [];
const emptyDirs = ['src'];

Expand All @@ -245,7 +245,29 @@ export function run() {
];

expect(() =>
validator.run(files, configObject, emptyDirs)
validator.run(files, configObject, emptyDirs),
).not.toThrow();
});

it('should validate nested empty dir with no rules', () => {
const files: string[] = [];
const emptyDirs = ['src/lol'];
const configObject: types.Rules = [
{
name: 'src',
type: 'directory',
rules: [
{
name: 'lol',
type: 'directory',
rules: [],
},
],
},
];

expect(() =>
validator.run(files, configObject, emptyDirs),
).not.toThrow();
});

Expand All @@ -262,9 +284,9 @@ export function run() {
];

expect(() =>
validator.run(files, configObject, emptyDirs)
validator.run(files, configObject, emptyDirs),
).toThrowError(
`${JSON.stringify(configObject[0])}, deep: 1, rule did not passed`
`${JSON.stringify(configObject[0])}, deep: 1, rule did not passed`,
);
});

Expand All @@ -280,7 +302,7 @@ export function run() {
];

expect(() =>
validator.run(files, configObject, emptyDirs)
validator.run(files, configObject, emptyDirs),
).toThrowError('src, was not validated');
});
});
Expand Down
22 changes: 11 additions & 11 deletions src/_tests/validator/fileDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function run() {
];

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

Expand All @@ -155,7 +155,7 @@ export function run() {
];

expect(() => validator.run(files, configObject)).toThrowError(
`${files[3]}, was not validated`
`${files[3]}, was not validated`,
);
});

Expand Down Expand Up @@ -211,7 +211,7 @@ export function run() {
];

expect(() => validator.run(files, configObject)).toThrowError(
`${files[4]}, was not validated`
`${files[4]}, was not validated`,
);
});

Expand Down Expand Up @@ -244,8 +244,8 @@ export function run() {

expect(() => validator.run(files, configObject)).toThrowError(
`${JSON.stringify(
(configObject[0] as types.DirectoryRule).rules![2]
)}, deep: 2, rule did not passed`
(configObject[0] as types.DirectoryRule).rules![2],
)}, deep: 2, rule did not passed`,
);
});

Expand Down Expand Up @@ -273,8 +273,8 @@ export function run() {

expect(() => validator.run(files, configObject)).toThrowError(
`${JSON.stringify(
(configObject[0] as types.DirectoryRule).rules![0]
)}, deep: 2, rule did not passed`
(configObject[0] as types.DirectoryRule).rules![0],
)}, deep: 2, rule did not passed`,
);
});

Expand Down Expand Up @@ -305,8 +305,8 @@ export function run() {

expect(() => validator.run(files, configObject)).toThrowError(
`${JSON.stringify(
(configObject[0] as types.DirectoryRule).rules![0]
)}, deep: 2, rule did not passed`
(configObject[0] as types.DirectoryRule).rules![0],
)}, deep: 2, rule did not passed`,
);
});
});
Expand Down Expand Up @@ -357,7 +357,7 @@ export function run() {
];

expect(() => validator.run(files, configObject)).toThrowError(
'srrcNice/index.js, was not validated'
'srrcNice/index.js, was not validated',
);
});
});
Expand Down Expand Up @@ -393,7 +393,7 @@ export function run() {
];

expect(() => validator.run(files, configObject)).toThrowError(
'SRC/index.js, was not validated'
'SRC/index.js, was not validated',
);
});
});
Expand Down
56 changes: 56 additions & 0 deletions src/helpers/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fs from 'node:fs';
import path from 'node:path';

type File = {
type: 'file';
fullPath: string;
path: string;
};

type Directory = {
type: 'dir';
fullPath: string;
path: string;
isEmpty: boolean;
};

export function getFilesAndDirectories(
dirPath: string,
options: {
recursive: boolean;
_initialPath?: string;
ignoreFilesAndDirectories?: string[];
} = { recursive: true },
): (File | Directory)[] {
const items = fs.readdirSync(dirPath);
options._initialPath = options._initialPath || dirPath;
const lPath = path.relative(options._initialPath, dirPath);

if (options.ignoreFilesAndDirectories?.includes(lPath)) return [];

const result: (File | Directory)[] = [
{
type: 'dir',
fullPath: dirPath,
path: lPath,
isEmpty: items.length === 0,
},
];

for (const item of items) {
const fullPath = path.join(dirPath, item);
const itemStat = fs.statSync(fullPath);
const localPath = path.relative(options._initialPath, fullPath);

if (itemStat.isDirectory()) {
if (!options.recursive)
result.push({ type: 'dir', fullPath, path: localPath, isEmpty: false });
else result.push(...getFilesAndDirectories(fullPath, options));
} else {
if (options.ignoreFilesAndDirectories?.includes(localPath)) continue;
result.push({ type: 'file', fullPath, path: localPath });
}
}

return result;
}
Loading

0 comments on commit d656b9e

Please sign in to comment.