Utility functions, parse and validate a given directory with multiple entries.
npm install validate-accessParse 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;
};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",
};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.
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: "",
};Parse and validate a given directory
function parseAndValidateDir(ParseDirInput): ParseDirOutput;-
ParseDirInputobject contains:dir?: stringtargetedFolders?: string | string[]Default:["src", "lib", "dist"]extensions?: string | string[]Default:["js", "ts", "jsx", "tsx"]isEnforceSub?: booleanisEnforceSrcLookup?: boolean
-
ParseDirOutputobject contains:dir: stringsubDir: stringsrcName: stringincludeSrcName: booleanincludeValidEntry: booleanext: stringname: string
Assuming we have:
├─pkg
├───src
│ ├───bar.ts
│ └───foo.jsconst 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",
};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 | ValidationMultiThe result object depends on input.
For one entry it returns ValidationOneEntry:
dir: stringsubDir: stringisJsonValid: 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.jsonconst 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.
npm testThis project is licensed under the GPL-3.0 License
-
builderz - Zero Configuration JS bundler.
-
packageSorter - Sorting packages for monorepos production.
-
corename - Extracts package name.
-
move-position - Moves element index in an array.
-
get-info - Utility functions for projects production.
-
textics & textics-stream - Counts lines, words, chars and spaces for a given string.
Support this package by giving it a Star ⭐