Détail du package

locter

tada5hi443.2kMIT2.1.6

A library to locate a file/module by criteria and load it!

file, loader, transpiling, compiler

readme

Locter 🔥

npm version CI codecov Known Vulnerabilities Conventional Commits

Locter is a library to locate and load a file/modules 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 { locateMany } from 'locter';

(async () => {
    let files = await locateMany(
        'files/example.{js,.ts}'
    );

    console.log(files);
    /*
    [
        { path: 'files', name: 'example', extension: '.js'},
        { path: 'files', name: 'example', extension: '.ts'}
    ]
     */

    files = await locateMany(
        'files/*.{js,ts}'
    );

    console.log(files);
    /*
    [
        { path: 'files', name: 'example', extension: '.js'},
        { path: 'files', name: 'example', extension: '.ts'},
        { path: 'files', name: 'example-long', extension: '.ts'},
    ]
     */
})

A synchronous variant is also available: locateManySync

Single

Locating a single file will return information about the first file matching the pattern.

import { locate } from 'locter';

(async () => {
    let file = await locate(
        'files/example.{js,.ts}'
    );

    console.log(file);
    /*
    { path: 'files', name: 'example', extension: '.js'}
     */
})

A synchronous variant is also available: locateSync

Loader

The load method can be used to load a file/module in an asynchronous fashion. Either a string or the output of the locate/locateSync method can be passed as argument.

import { load, locate } from 'locter';

(async () => {
    const file = await locate(
        'files/example.{js,.ts}'
    );

    let content = await load(file);
    console.log(content);
    // ...

    content = await load('...');
    console.log(content);
    // ...
})

There is also a synchronous method called loadSync to load files.

import { loadSync, locateSync } from 'locter';

(async () => {
    const file = await locateSync(
        'files/example.{js,.ts}'
    );

    let content = await loadSync(file);
    console.log(content);
    // ...

    content = await loadSync('...');
    console.log(content);
    // ...
})

Two loaders are predefined from scratch and already registered:

  • ConfLoader: This loader allows to load .conf files.
  • JSONLoader: This loader allows to load .json files.
  • YAMLLoader: This loader allows to load .yml files.
  • ModuleLoader: This loader allows to load modules with .js, .mjs, .mts, .cjs, .cts, .ts file extensions independent of the environment (cjs or esm).

To register loader for other file types, the function registerLoader can be used.

import { registerLoader } from 'locter';

registerLoader(['.ext'], {
    execute(input: string) {

    },
    executeSync(input: string) {

    }
})

License

Made with 💚

Published under MIT License.

changelog

2.1.6 (2025-01-24)

Bug Fixes

  • deps: bump the minorandpatch group with 13 updates (#753) (7dd1b67)

2.1.5 (2024-11-06)

Bug Fixes

2.1.4 (2024-10-11)

Bug Fixes

  • use native import for load fn (68b1af9)

2.1.3 (2024-10-08)

Bug Fixes

2.1.2 (2024-09-26)

Bug Fixes

2.1.1 (2024-08-23)

Bug Fixes

  • add missing .yaml extension for loader manager (0e56ab4)
  • deps: bump jiti from 1.21.0 to 1.21.6 (#544) (7f24ea9)
  • deps: bump yaml from 2.4.1 to 2.4.5 (#543) (790c080)
  • deps: bump yaml from 2.4.5 to 2.5.0 (#580) (cd29c41)

2.1.0 (2024-04-02)

Bug Fixes

Features

  • add locator onlyFiles & onlyDirectories option (#469) (147e824)

2.0.2 (2024-01-18)

Bug Fixes

  • nested default exports with using dynamic import (9cf3e36)

2.0.1 (2024-01-18)

Bug Fixes

  • use require for async loading in jest environment (878ab64)

2.0.0 (2024-01-18)

Features

  • add typescript error check utility (0735473)
  • refactor module loader (a8ab89d)

BREAKING CHANGES

  • get module export api changed

1.3.0 (2024-01-09)

Features

1.2.3 (2023-11-25)

Bug Fixes

1.2.2 (2023-09-02)

Bug Fixes

1.2.1 (2023-07-24)

Bug Fixes

  • deps: bump glob to v10.x (af38163)

1.2.0 (2023-07-20)

Features

  • optimize remove file name extension (fb6edfb)

1.1.3 (2023-07-18)

Bug Fixes

1.1.2 (2023-05-29)

Bug Fixes

  • append file extension for esm unsupported url scheme error (fa308b8)

1.1.1 (2023-05-29)

Bug Fixes

  • don't use fallback for module loading on syntax/reference-error (a6c9009)

1.1.0 (2023-03-27)

Features

  • add built-in conf loader (cc50423)

1.0.11 (2023-03-26)

Bug Fixes

1.0.10 (2023-03-17)

Bug Fixes

1.0.9 (2023-03-10)

Bug Fixes

  • reading module export item (adf4115)
  • set module export value type to any (7154898)

1.0.8 (2023-03-10)

Bug Fixes

  • deps: bump jiti from 1.17.1 to 1.17.2 (#211) (6efd49a)
  • enhanced module loading (b7c99c6)

1.0.7 (2023-03-05)

Bug Fixes

  • enhance exception handling (3b741b1)
  • optimized file path building (cea0141)

1.0.6 (2023-02-28)

Bug Fixes

  • deps: bump glob to v9 + simplified commitlint config (1cf0baa)

1.0.5 (2023-02-17)

Bug Fixes

1.0.4 (2023-02-17)

Bug Fixes

  • deps: bump jiti from 1.17.0 to 1.17.1 (#188) (fc7ca49)

1.0.3 (2023-02-14)

Bug Fixes

  • append file extension for loading by file-info (122e3f9)

1.0.2 (2023-02-14)

Bug Fixes

  • deps: bump jiti from 1.16.2 to 1.17.0 (#175) (1df7fc4)

1.0.1 (2023-02-07)

Bug Fixes

  • change return type of load and loadSync (1613372)

1.0.0 (2023-02-07)

Features

  • refactored code base and public api (a864805)

BREAKING CHANGES

  • public api changed

0.8.2 (2023-01-31)

Bug Fixes

  • compatibility issues with ts-node (da3db7f)

0.8.1 (2023-01-31)

Bug Fixes

  • fallback to require for jest env, due segmentation fault (c514882)

0.8.0 (2023-01-30)

Bug Fixes

  • added missing loader exports (7bcc6fb)
  • deps: bump dependencies (cf93b11)

Features

  • enable just in time compilation/transpiling for esm & ts files (732bb4b)

0.7.1 (2023-01-17)

Bug Fixes

  • deps: bump glob from 8.0.3 to 8.1.0 (403b309)

0.7.0 (2023-01-14)

Features

  • stricter file loader + enhanced cjs/esm build (85b7e70)

0.6.2 (2023-01-07)

Bug Fixes

  • deps: bump json5 from 1.0.1 to 1.0.2 (118696d)

0.6.1 (2022-12-19)

Bug Fixes

  • transpile dynamic imports for cjs to require (5a62235)

0.6.0 (2022-12-01)

Features

  • accept multiple inputs for loading json (7a81c7a)
  • better error handling for script loading (8f18261)

0.5.3 (2022-11-30)

Bug Fixes

  • guarantee that option is not already set on load error (b1736dd)

0.5.2 (2022-11-29)

Bug Fixes

  • sync script loading with fallback for appending extension (b988e0f)

0.5.1 (2022-11-29)

Bug Fixes

  • enhance async script loader (f6091fa)

0.5.0 (2022-11-29)

Features

  • enhance loader api + fix esm build (253ea59)

0.4.0 (2022-11-28)

Features

  • added esm modules support (e201769)

0.3.2 (2022-10-11)

Bug Fixes

0.3.1 (2022-10-03)

Bug Fixes

  • add missing util function export (7ee7de0)

0.3.0 (2022-10-03)

Features

  • add file-name extension helper (ce1095f)