goerwin / directory-validator

Tool to validate directory structures
MIT License
15 stars 3 forks source link
directory directory-analyzer directory-utilities directory-validator folder folder-validator validation validator

directory-validator

Package Version

CLI Tool to validate directory structures. If you want to have control over what files/dirs a directory can have then this can be useful.

Installation

$ 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 .

The tool will evaluate the rules provided by the configuration file against the current directory and output errors if any.

Configuration File

{
  "ignoreFiles": [".gitignore"],
  "ignoreDirs": ["node_modules", ".git"],
  "commonRules": {
    "rule_indexfile": {
      "type": "file",
      "name": "index.js"
    }
  },
  "rules": [
    {
      "type": "file",
      "name": "package.json"
    },
    {
      "type": "common",
      "key": "rule_indexfile"
    },
    {
      "type": "directory",
      "name": "src",
      "isOptional": true,
      "rules": [
        {
          "type": "common",
          "key": "rule_indexfile"
        }
      ]
    }
  ]
}

In this example:

ignoreFiles:

A string or glob pattern. For example:

[
  "package.json",
  "**/*.test.js",
  ".*" // files starting with "."
]

ignoreDirs:

A string or glob pattern. For example:

[
  "node_modules",
  "src/**/tests",
  ".*" // dirs starting with "."
]

commonRules:

Define File, Directory and Common rules that can be reused in rules

{
  // key must start with "rule_"
  // Examples:
  "rule_indexfile": {
    "type": "file",
    "name": "index.js"
  },
  "rule_anotherrule": {
    "type": "directory",
    "name": "images",
    "rules": [
      {
        "type": "file",
        "name": "logo.png"
      }
    ]
  }
}

rules:

Can contain File, Directory and Common Rules

File Rule

{
  // Required
  "type": "file",

  // Required
  // can be string or RegExp
  // if RegExp then it has to start and end with /
  // if string then it can contain one
  // special case: [camelCase], [UPPERCASE], [dash-case], [snake_case], *
  // Examples:
  "name": "package.json",
  "name": "[snake_case]",
  "name": "[camelCase].js",
  "name": ".[UPPERCASE]",
  "name": ".[dash-case].jpg",
  "name": "*.png",
  "name": "/index.(js|ts)/",

  // Optional
  // default: null
  // can be string or RegExp (do not include the dot)
  // if RegExp then it has to start and end with /
  // Examples:
  "extension": "js",
  "extension": "png",
  "extension": "/(png|jpg|gif)/",

  // Optional
  // default: false
  // Whether the file can be included
  "isOptional": false
}

Directory Rule

{
  // Required
  "type": "directory",

  // Required
  // Same options as file names
  // Examples:
  "name": "src",
  "name": "important-[dash-case]",

  // Optional
  // default: false
  // Whether the directory can be included
  "isOptional": false,

  // Optional
  // default: false
  // Whether the directory can be recursive
  // Adds the ability to check directory rules recursively
  "isRecursive": false,

  // Optional
  // An array containing file and directory rules
  // If empty or omitted then we don't validate dir content
  "rules": []
}

Common Rule

{
  // Required
  "type": "common",

  // Required
  // must match a key property inside "commonRules"
  // examples:
  "key": "rule_indexfile",
  "key": "rule_test2",
  "key": "rule_whatever",

  // Optional
  // default: false
  // Whether the directory can be included
  "isOptional": false
}

Notes