From d035aead78eeff567e2d5381dc7cb6454dd59fe0 Mon Sep 17 00:00:00 2001 From: Erwin G Date: Sat, 26 Feb 2022 12:41:55 -0500 Subject: [PATCH] refactoring and better naming for some functions --- .directoryvalidator.json | 2 +- .gitignore | 4 +-- .prettierignore | 2 ++ .prettierrc.json | 13 ++++++++++ README.md | 13 ++++++---- package-lock.json | 25 ++++++++++++++++--- package.json | 16 ++++++------ .../program/examples/project2/conf2.json | 4 +-- .../program/examples/project3/conf2.json | 4 +-- .../program/examples/project3/conf4.json | 4 +-- src/index.ts | 5 ++-- src/program.ts | 2 +- .../defaultConfig.json | 0 src/{supportFiles => resources}/schema.json | 0 todo.md | 11 ++++---- tsconfig.json | 4 +-- 16 files changed, 71 insertions(+), 38 deletions(-) create mode 100644 .prettierignore create mode 100644 .prettierrc.json rename src/{supportFiles => resources}/defaultConfig.json (100%) rename src/{supportFiles => resources}/schema.json (100%) diff --git a/.directoryvalidator.json b/.directoryvalidator.json index bc79040..2784d9d 100644 --- a/.directoryvalidator.json +++ b/.directoryvalidator.json @@ -5,7 +5,7 @@ "package-lock.json", ".DS_Store" ], - "ignoreDirs": ["node_modules", "lib", ".git", ".github"], + "ignoreDirs": ["node_modules", "dist", ".git", ".github"], "commonRules": { "rule_indexfile": { "type": "file", diff --git a/.gitignore b/.gitignore index a109eee..8d67a86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ .DS_Store node_modules -.nyc_output -coverage -lib +dist diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..d7948ca --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +src/_tests +dist diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..6790521 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,13 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "useTabs": false, + "semi": true, + "singleQuote": true, + "jsxSingleQuote": false, + "embeddedLanguageFormatting": "off", + "arrowParens": "always", + "printWidth": 80, + "endOfLine": "lf", + "bracketSpacing": true +} diff --git a/README.md b/README.md index 7cebca6..8b85072 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,13 @@ $ npm install directory-validator ## Usage Generate a configuration file `.directoryvalidator.json` to start with: + ``` $ directory-validator --init ``` Run the validator on the current directory: + ``` $ directory-validator . ``` @@ -67,6 +69,7 @@ The tool will evaluate the rules provided by the configuration file against the ``` In this example: + - We ignore the file `.gitignore` and both `.node_modules` and `.git` directories from being analized - We want to have one file name `package.json` and one file named `index.js` - We want one directory `src` to have one file named `index.js`. Since it's optional, @@ -81,7 +84,7 @@ A string or glob pattern. For example: [ "package.json", "**/*.test.js", - ".*" // files starting with "." + ".*", // files starting with "." ] ``` @@ -93,7 +96,7 @@ A string or glob pattern. For example: [ "node_modules", "src/**/tests", - ".*" // dirs starting with "." + ".*", // dirs starting with "." ] ``` @@ -214,7 +217,7 @@ Can contain File, Directory and Common Rules ## Notes -* When you run `$ directory-validator ./` it will look for a `.directoryvalidator.json` file in the current directory, if it doesn't find one, it will try to look for one in the upper directory and so on until the home directory is reached. If no file is found then no rules are applied. +- When you run `$ directory-validator ./` it will look for a `.directoryvalidator.json` file in the current directory, if it doesn't find one, it will try to look for one in the upper directory and so on until the home directory is reached. If no file is found then no rules are applied. -* Rules are inclusive, meaning that if multiple rules match the same files/dirs, they pass. - * For example, the rules `{ "name": "index.js", "type": "file" }` and `{ "name": "[camelCase].js", "type": "file" }`, will match a file `index.js` so they both pass. +- Rules are inclusive, meaning that if multiple rules match the same files/dirs, they pass. + - For example, the rules `{ "name": "index.js", "type": "file" }` and `{ "name": "[camelCase].js", "type": "file" }`, will match a file `index.js` so they both pass. diff --git a/package-lock.json b/package-lock.json index d2f7f33..915a94e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "directory-validator", - "version": "1.5.0", + "version": "1.5.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "directory-validator", - "version": "1.5.0", + "version": "1.5.1", "license": "MIT", "dependencies": { "ajv": "^8.0.2", @@ -17,7 +17,7 @@ "lodash": "^4.17.21" }, "bin": { - "directory-validator": "lib/index.js" + "directory-validator": "dist/index.js" }, "devDependencies": { "@types/glob": "^7.1.3", @@ -25,6 +25,7 @@ "@types/lodash": "^4.14.168", "@types/node": "^14.14.37", "jest": "^26.6.3", + "prettier": "^2.5.1", "ts-jest": "^26.5.4", "typescript": "^4.5.5" } @@ -4517,6 +4518,18 @@ "node": ">= 0.8.0" } }, + "node_modules/prettier": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.5.1.tgz", + "integrity": "sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + } + }, "node_modules/pretty-format": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-26.6.2.tgz", @@ -9767,6 +9780,12 @@ "integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=", "dev": true }, + "prettier": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.5.1.tgz", + "integrity": "sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==", + "dev": true + }, "pretty-format": { "version": "26.6.2", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-26.6.2.tgz", diff --git a/package.json b/package.json index 303102e..257e7a0 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,16 @@ { "name": "directory-validator", - "version": "1.5.1", + "version": "1.5.2", "description": "CLI Tool to validate directory structures.", - "main": "lib/index.js", + "main": "dist/index.js", "bin": { - "directory-validator": "lib/index.js" + "directory-validator": "dist/index.js" }, "scripts": { - "build": "npm run clean && tsc && cp src/supportFiles/* lib/supportFiles", - "clean": "rm -rf lib", - "test": "jest" + "build": "npm run clean && tsc && cp src/resources/* dist/resources", + "clean": "rm -rf dist", + "prettier": "prettier -c .", + "test": "npm run prettier && jest" }, "author": "Erwin Gaitan O ", "license": "MIT", @@ -19,6 +20,7 @@ "@types/lodash": "^4.14.168", "@types/node": "^14.14.37", "jest": "^26.6.3", + "prettier": "^2.5.1", "ts-jest": "^26.5.4", "typescript": "^4.5.5" }, @@ -31,7 +33,7 @@ "lodash": "^4.17.21" }, "files": [ - "lib", + "dist", "LICENSE", "README.md" ], diff --git a/src/_tests/program/examples/project2/conf2.json b/src/_tests/program/examples/project2/conf2.json index 1a4b65f..dfcdb72 100644 --- a/src/_tests/program/examples/project2/conf2.json +++ b/src/_tests/program/examples/project2/conf2.json @@ -1,7 +1,5 @@ { - "ignoreFiles": [ - "file1.jpg" - ], + "ignoreFiles": ["file1.jpg"], "rules": [ { "type": "file", diff --git a/src/_tests/program/examples/project3/conf2.json b/src/_tests/program/examples/project3/conf2.json index 62fb4cd..f84f6d2 100644 --- a/src/_tests/program/examples/project3/conf2.json +++ b/src/_tests/program/examples/project3/conf2.json @@ -1,7 +1,5 @@ { - "ignoreDirs": [ - "dir1" - ], + "ignoreDirs": ["dir1"], "rules": [ { "type": "file", diff --git a/src/_tests/program/examples/project3/conf4.json b/src/_tests/program/examples/project3/conf4.json index caeaef7..2fdc511 100644 --- a/src/_tests/program/examples/project3/conf4.json +++ b/src/_tests/program/examples/project3/conf4.json @@ -17,9 +17,7 @@ "rule_dir": { "type": "directory", "name": "dir1", - "rules": [ - { "type": "file", "name": "lul" } - ] + "rules": [{ "type": "file", "name": "lul" }] } } } diff --git a/src/index.ts b/src/index.ts index 0bac6f1..bbf39c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,7 +30,7 @@ function getDefaultConfigFilePath(dirPath: string) { export function writeDefaultConfigFile(parentPath: string) { fs.copyFileSync( - path.join(__dirname, './supportFiles/defaultConfig.json'), + path.join(__dirname, './resources/defaultConfig.json'), parentPath ); } @@ -59,7 +59,7 @@ const selectedOptions = commanderProgram.opts(); if (selectedOptions.init) { fs.copyFileSync( - path.join(__dirname, './supportFiles/defaultConfig.json'), + path.join(__dirname, './resources/defaultConfig.json'), path.join(process.cwd(), initConfigFilename) ); console.log('\n\t', initConfigFilename.red, 'created', '\n'); @@ -82,6 +82,7 @@ if (selectedOptions.init) { if (selectedOptions.print && results.asciiTree) { console.log( + '\n', results.asciiTree .replace(/\/fileIgnored/g, '[File Ignored]'.dim) .replace(/\/directoryIgnored/g, '[Directory Ignored]'.dim) diff --git a/src/program.ts b/src/program.ts index ebf27be..a5161c1 100644 --- a/src/program.ts +++ b/src/program.ts @@ -8,7 +8,7 @@ import { } from 'goerwin-helpers/node/file'; import * as _ from 'lodash'; import * as errors from './errors'; -import schema from './supportFiles/schema.json'; +import schema from './resources/schema.json'; import * as types from './types'; import * as validator from './validator'; diff --git a/src/supportFiles/defaultConfig.json b/src/resources/defaultConfig.json similarity index 100% rename from src/supportFiles/defaultConfig.json rename to src/resources/defaultConfig.json diff --git a/src/supportFiles/schema.json b/src/resources/schema.json similarity index 100% rename from src/supportFiles/schema.json rename to src/resources/schema.json diff --git a/todo.md b/todo.md index d3eb211..6a078da 100644 --- a/todo.md +++ b/todo.md @@ -1,16 +1,17 @@ - # TODO + - more error info (path) in validatorRuleError - better ubication of a rule when displaying errors (rules[3].rules[1]) like ajv when validating json schemas - Examples - Provide some folder examples: - * examples/basic - * examples/recursive-directories - * examples/multimatching-rules + - examples/basic + - examples/recursive-directories + - examples/multimatching-rules - allow rules to be exclusive # DONE + - common dir rules can have other common rules inside - common rules can be optional - option to print the directory structure printed @@ -26,7 +27,7 @@ - allow RegExp in names/extensions - create bin file - what if you want 2 dirs in a directory and the rest can be whatever? - - { name: * } should validate all the files/dirs recursively + - { name: \* } should validate all the files/dirs recursively - better errors in console - rename it to directory-validator - read config from passed file diff --git a/tsconfig.json b/tsconfig.json index 16e4ec0..84caaea 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "exclude": ["src/**/_tests/*", "src/**/*/*.test.ts"], + "exclude": ["src/**/_tests/*", "src/**/*/*.test.ts", "dist"], "compilerOptions": { /* Basic Options */ "target": "es2016" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */, @@ -13,7 +13,7 @@ "declaration": true /* Generates corresponding '.d.ts' file. */, // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./lib" /* Redirect output structure to the directory. */, + "outDir": "./dist" /* Redirect output structure to the directory. */, "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, // "removeComments": true, /* Do not emit comments to output. */ // "noEmit": true, /* Do not emit outputs. */