Skip to content

DisQada/PathFinder

Repository files navigation

PathFinder

Badges

github npm

version monthly downloads

test Generate and Deploy documentation semantic-release

Table of Contents

About

This tool was made to be able to find file paths using only their names, or getting all the paths of files with "hello" word in their names. The file paths you want to get later must be stored first, this will usually be done at the beginning of the program.

Learn

Check the Getting started guide for first time usage.

License

Copyright © 2023 DisQada

This tool is licensed under the Apache License, Version 2.0.
See the LICENSE file for more information.

Getting started

Store paths

The first thing to do when using this tool is to store all the paths that will be required later on in some other file

To store the wanted file paths, use the function storeFolderPaths() and specify the array of directories to store their file paths.

Store all the paths (once) at the beginning of the discord bot (or application)

Let's say that this is out workspace

<root>
  ├── package.json
  ├── src
  │     ├── a.js
  │     └── b.js
  └── data
        └── a.json

We use this code to store the paths of all files in the src and data folder

import { storeFolderPaths } from '@disqada/pathfinder'
storeFolderPaths(['src', 'data'])

Now we'll have the following paths stored successfully

The file paths are stored in order

.../src/a.js
.../src/b.js
.../data/a.json

Find paths

In another file in the middle of some function, you want to require a module of one of the files stored earlier, you remember it's name but you don't remember it's location and you don't want to modify the path in the require function every time you move one of the files

Here is where this tools comes to help, if you only know the name of the file then you can easily fetch it's full path using the function findPath() or findPaths()

Both functions work the same, it's just that filePath() returns the first match while filePaths() returns them all in an array

Let's say that we want to require the file src/b.js, we use the following code for that

import { findPath } from '@disqada/pathfinder'

const filePath = findPath({ name: 'b' })
const { ... } = (await import(filePath.fullPath)).default

With that we successfully required the module and used it just like normal but without the need to know it's full path

If you have multiple files with the same name, then check Same file names guide

Same file names

Let's say that this is out workspace

<root>
  ├── package.json
  └── src
        ├── a.js
        └── a.json

Now let's say we've written some code and we want to require the file src/a.json, we can use the same code, but there is a problem, the function is returning the path of src/a.js instead of src/a.json because it was stored before and the function will return the first match

To work around this, we have couple of choices

Renaming files

Renaming one of the files to another name will solve this problem, but it's not always something we can do

Receiving all matches

Instead of getting one file path, we can get an array of all the file paths of that name by changing the function from findPath() to findPaths()

import { findPaths } from '@disqada/pathfinder'

const filePaths = findPaths({ name: 'a' })
const { ... } = (await import(filePaths[1].fullPath)).default

This will solve the problem, for now, what if we then added another file named a.js somewhere in our project or the order of the files changed (files order in the workspace or the order of storing)?

Filtering by different properties

More than filtering property can be used at the same time

Extension difference

The best way to work around the problem of file name conflict is by adding more details to the FilterOptions object parameter

Both files have the same file name and path except for only the extension, so we can specify the extension property in the FilterOptions parameter to get our desired file

import { findPath } from '@disqada/pathfinder'

const filePath = findPath({ extension: 'json' })
const { ... } = (await import(filePath.fullPath)).default

Folder difference

If our workspace was instead like this, where both the files' name and extension are identical, but they're in different folder

<root>
  ├── package.json
  ├── src
  │     └── a.js
  └── data
        └── a.js

Then the way we'll get the correct path is as follows

import { findPath } from '@disqada/pathfinder'

const filePath = findPath({ folder: 'data' })
const { ... } = (await import(filePath.fullPath)).default