npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validate-access

v2.0.0

Published

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

Downloads

39

Readme

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 ⭐