Locter 🔥
Locter is a library to locate and load a file regarding specific criteria.
Table of Contents
Installation
npm install locter --save
Usage
The following examples are based on some shared assumptions:
- A folder named
files
exists in the root directory. - The folder
files
contains the following files:
- example.js
- example.json
- example.ts
- example-long.ts
Locator
Multiple
Locating multiple files will return information about all files matching the pattern.
import { locateFiles } from 'locter';
(async () => {
let files = await locateFiles(
'files/example.{js,.ts}',
{
path: process.cwd()
}
);
console.log(files);
files = await locateFiles(
'files/*.{js,ts}',
{
path: process.cwd()
}
);
console.log(files);
})
A synchronous variant is also available: locateFilesSync
Single
Locating a single file will return information about the first file matching the pattern.
import { locateFile } from 'locter';
(async () => {
let file = await locateFile(
'files/example.{js,.ts}',
{
path: process.cwd()
}
);
console.log(file);
})
A synchronous variant is also available: locateFileSync
Loader
The method will load the located file.
import { loadFile, locateFile } from 'locter';
(async () => {
const file = await locateFile(
'files/example.{js,.ts}',
{
path: process.cwd()
}
);
const content = loadFile(file);
console.log(content);
})
A synchronous variant is also available: loadFileSync
License
Made with 💚
Published under MIT License.