recursa 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/dist/index.js +56 -0
- package/package.json +15 -0
- package/tsconfig.json +27 -0
- package/types/index.d.ts +10 -0
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Autoware
|
|
2
|
+
|
|
3
|
+
Recursively load files from a folder for Javascript/Typescript (NodeJS)
|
|
4
|
+
|
|
5
|
+
- Has typescript types
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
- CommonJS
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const Autoware = require("autoware");
|
|
13
|
+
const { toOject } = require("autoware");
|
|
14
|
+
|
|
15
|
+
let map = Autoware("commands"); // "commands" transforms into process.cwd() + "/commands"
|
|
16
|
+
|
|
17
|
+
// To make an object of key-pairs:
|
|
18
|
+
let object = toObject(map);
|
|
19
|
+
let keys = Object.keys(object); // array of paths
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
- Module
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import Autoware, { toObject } from "autoware";
|
|
26
|
+
|
|
27
|
+
let map = Autoware("commands"); // "commands" transforms into process.cwd() + "/commands"
|
|
28
|
+
|
|
29
|
+
// To make an object of key-pairs:
|
|
30
|
+
let object = toObject(map);
|
|
31
|
+
let keys = Object.keys(object); // array of paths
|
|
32
|
+
```
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFilesRecursively = getFilesRecursively;
|
|
4
|
+
exports.default = recursa;
|
|
5
|
+
exports.toObject = toObject;
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
function parsePath(folderpath) {
|
|
8
|
+
let parsedPath = folderpath;
|
|
9
|
+
if (folderpath.startsWith("./")) {
|
|
10
|
+
let foldername = folderpath.replace("./", "");
|
|
11
|
+
parsedPath = `${process.cwd()}${foldername}`;
|
|
12
|
+
}
|
|
13
|
+
else if (folderpath.startsWith("/")) {
|
|
14
|
+
let foldername = folderpath.replace(/^(\/)/g, "");
|
|
15
|
+
parsedPath = `${process.cwd()}${foldername}`;
|
|
16
|
+
}
|
|
17
|
+
else if (!folderpath.includes("/") ||
|
|
18
|
+
!folderpath.includes("\\\\") ||
|
|
19
|
+
!folderpath.includes("\\")) {
|
|
20
|
+
parsedPath = `${process.cwd()}/${folderpath}`;
|
|
21
|
+
}
|
|
22
|
+
return parsedPath;
|
|
23
|
+
}
|
|
24
|
+
function getFilesRecursively(folder) {
|
|
25
|
+
let list = new Map();
|
|
26
|
+
let dirsAndFiles = (0, fs_1.readdirSync)(folder, { withFileTypes: true });
|
|
27
|
+
for (let f of dirsAndFiles) {
|
|
28
|
+
if (f.isDirectory()) {
|
|
29
|
+
let filesOfFolder = getFilesRecursively(`${folder}/${f.name}`);
|
|
30
|
+
filesOfFolder.forEach((v, k) => {
|
|
31
|
+
list.set(k, v);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else if (f.isFile()) {
|
|
35
|
+
let filepath = `${folder}/${f.name}`;
|
|
36
|
+
list.set(filepath, (0, fs_1.readFileSync)(filepath, "utf-8"));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return list;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
*
|
|
43
|
+
* @param folderpath Path to root folder, can be absolute or relative.
|
|
44
|
+
* @returns A map where key is path to file and value is the content of the file.
|
|
45
|
+
*/
|
|
46
|
+
function recursa(folderpath) {
|
|
47
|
+
folderpath = parsePath(folderpath);
|
|
48
|
+
return getFilesRecursively(folderpath);
|
|
49
|
+
}
|
|
50
|
+
function toObject(map) {
|
|
51
|
+
let obj = {};
|
|
52
|
+
for (let [k, v] of map) {
|
|
53
|
+
obj[k] = v;
|
|
54
|
+
}
|
|
55
|
+
return obj;
|
|
56
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "recursa",
|
|
3
|
+
"dependencies": {},
|
|
4
|
+
"devDependencies": {
|
|
5
|
+
"@types/bun": "^1.3.8"
|
|
6
|
+
},
|
|
7
|
+
"types": "./types/index.d.ts",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"module": "dist/index.js",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"typescript": "^5"
|
|
13
|
+
},
|
|
14
|
+
"version": "0.1.0"
|
|
15
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Environment setup & latest features
|
|
4
|
+
"lib": ["ESNext"],
|
|
5
|
+
"target": "esnext",
|
|
6
|
+
"module": "commonjs",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
|
|
9
|
+
// Bundler mode
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
|
|
12
|
+
// Best practices
|
|
13
|
+
"strict": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"noFallthroughCasesInSwitch": true,
|
|
16
|
+
"noUncheckedIndexedAccess": true,
|
|
17
|
+
"noImplicitOverride": true,
|
|
18
|
+
|
|
19
|
+
// Some stricter flags (disabled by default)
|
|
20
|
+
"noUnusedLocals": false,
|
|
21
|
+
"noUnusedParameters": false,
|
|
22
|
+
"noPropertyAccessFromIndexSignature": false,
|
|
23
|
+
"outDir": "dist",
|
|
24
|
+
"declaration": true,
|
|
25
|
+
"declarationDir": "types"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function getFilesRecursively(folder: string): Map<string, string>;
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param folderpath Path to root folder, can be absolute or relative.
|
|
5
|
+
* @returns A map where key is path to file and value is the content of the file.
|
|
6
|
+
*/
|
|
7
|
+
export default function recursa(folderpath: string): Map<string, string>;
|
|
8
|
+
export declare function toObject(map: Map<string, string>): {
|
|
9
|
+
[key: string]: string;
|
|
10
|
+
};
|