Skip to content

jalal246/validate-access

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

validate-access

Utility functions, parse and validate a given directory with multiple entries.

npm install validate-access

API

parseDir

Parse a given directory without validation

// DEFAULT_DIR_FOLDERS = ["src", "lib", "dist"];

function parseDir(
  pureDir: string,
  targetedFolders: string[] | string = DEFAULT_DIR_FOLDERS,
  isEnforceSub: boolean = true
): {
  dir: string;
  subDir: string;
  filename: string;
  srcName: string;
};

Example - parseDir

Directory includes source folder and file name:

let result = parseDir("home/to/pkg/folder/src/a.js");

result = {
  dir: "home/to/pkg",
  subDir: "folder/src",
  srcName: "src",
  filename: "a.j",
};

Custom source folders:

// You can pass an array or a string
const result = parseDir("home/to/pkg/test/folder/myFile.js", "test");

result = {
  dir: "home/to/pkg",
  subDir: "test/folder",
  srcName: "test",
  filename: "myFile.js",
};

detectFileInDir

Returns a valid file name with an extension for the given directory even if the directory is missing file name extension.

// DEFAULT_EXTENSIONS= ["js", "ts", "jsx", "tsx"]

function detectFileInDir(
  dir: string,
  extensions: string | string[] = DEFAULT_EXTENSIONS,
  enableSearchForExt = true
): {
  includeValidEntry: boolean;
  ext: string;
  name: string;
};

When enableSearchForExt the function will add extensions to your directory and try to validate the output.

Example - detectFileInDir

const result = detectFileInDir("home/to/pkg/folder/myFile.js");

result = {
  includeValidEntry: true,
  name: "myFile",
  ext: "js",
};

This also works:

const result = detectFileInDir("home/to/pkg/folder/myFile");

result = {
  includeValidEntry: true,
  name: "myFile",
  ext: "js",
};

No valid file:

const result = detectFileInDir("home/to/pkg/test/folder");

result = {
  includeValidEntry: false,
  name: "",
  ext: "",
};

parseAndValidateDir

Parse and validate a given directory

function parseAndValidateDir(ParseDirInput): ParseDirOutput;
  • ParseDirInput object contains:

    • dir?: string
    • targetedFolders?: string | string[] Default: ["src", "lib", "dist"]
    • extensions?: string | string[] Default: ["js", "ts", "jsx", "tsx"]
    • isEnforceSub?: boolean
    • isEnforceSrcLookup?: boolean
  • ParseDirOutput object contains:

    • dir: string
    • subDir: string
    • srcName: string
    • includeSrcName: boolean
    • includeValidEntry: boolean
    • ext: string
    • name: string

Example - parseAndValidateDir

Assuming we have:

├─pkg
├───src
│   ├───bar.ts
│   └───foo.js
const result = parseAndValidateDir({ dir: "home/to/pkg" });

result = {
  subDir: "valid-json-entry-lib",
  srcName: "src", // Auto detected
  includeSrcName: false,
  includeValidEntry: false,
  ext: "",
  name: "",
};

If Directory has a srcName:

const result = parseAndValidateDir({ dir: "home/to/pkg/src" });

result = {
  subDir: "valid-json-entry-lib",
  srcName: "src", // Auto detected
  includeSrcName: true,
  includeValidEntry: false,
  ext: "",
  name: "",
};

With a file name provided:

const result = parseAndValidateDir({ dir: "home/to/pkg/src/foo.js" });

result = {
  subDir: "valid-json-entry-lib",
  srcName: "src", // Auto detected
  includeSrcName: true,
  includeValidEntry: true,
  name: "foo",
  ext: "js",
};

validateAccess

Validates package accessibility including package.json and entry file or multiple entries. It doesn't just check for string format, it actually goes deeper to check for valid extension and then validate existence.

function validateAccess({
  dir,
  entry = "index",
  targetedFolders = DEFAULT_DIR_FOLDERS,
  extensions = DEFAULT_EXTENSIONS,
  isValidateJson = true,
  enableFoldersLookup= true
}): ValidationOneEntry | ValidationMulti

The result object depends on input.

For one entry it returns ValidationOneEntry:

  • dir: string
  • subDir: string
  • isJsonValid: boolean | null - true if dir has package.json.
  • srcName: string - If there's src folder recognized.

with EntryInfo:

  • isEntryValid: boolean - if the given entry is exist.
  • entry: string - the input entry.
  • entryDir: string - extracted entry directory.
  • ext: string - entry extension if exist.
  • name: string - entry file name that was checked.

And for multi entries entries: [EntryInfo]

├─pkg
│
├───src
│   ├───bar.ts
│   └───foo.js
│
├───random
│   ├───foobar.ts
│
├───package.json

Example - validateAccess

const result = validateAccess({
  dir: "home/to/pkg",
  entry: "random/foobar.ts",
});

result = {
  dir: "home/to/pkg",
  subDir: "",
  entry: "random/foobar.ts",
  entryDir: "random",
  isJsonValid: true,
  srcName: "src",
  isEntryValid: true,
  name: "foo",
  ext: "js",
};

You will get the same result for entry: "random/foobar" - without extension.

Assuming the dir "home/to/pkg", all the following entries are valid:

entry: "src/foo"
entry: "src/foo.js"
entry: "foo.js"
entry: "foo"

Or you can provide dir like the following and still get true validation:

dir: "path/to/valid/package/src/foo"
dir: "path/to/valid/package/src/foo.js"

Doing multiple entries is also possible:

├─pkg
│
├───src
│   ├───bar.ts
│   └───foo.js
│
├───random
│   ├───foobar.ts
│
├───index.js
├───package.json
const filePath = resolve(source, "valid-json-entries-src");

const result = validateAccess({
  dir: "to/pkg",
  entry: ["foo", "src/bar.ts", "index.js"],
  enableFoldersLookup: false,
});

result = {
  ...essential,
  entries: [
    {
      entry: "foo",
      entryDir: "",
      name: "foo",
      ext: "js",
      isEntryValid: false,
    },
    {
      entry: "src/bar.ts",
      entryDir: "src",
      name: "bar",
      ext: "ts",
      isEntryValid: true,
    },
    {
      entry: "index",
      entryDir: "",
      name: "index",
      ext: "js",
      isEntryValid: true,
    },
  ],
};

If enableFoldersLookup is enabled validation will always be done inside subdirectories. Otherwise, it will combine entry with directory ignore diving into src/lib..etc.

Test

npm test

License

This project is licensed under the GPL-3.0 License

Related projects

Support this package by giving it a Star ⭐