yargs-file-commands 0.0.1 → 0.0.3
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 +18 -19
- package/dist/lib/fileCommands.d.ts +1 -1
- package/dist/lib/fileCommands.js +7 -7
- package/dist/lib/scanDirectory.d.ts +1 -0
- package/dist/lib/scanDirectory.js +6 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# Yargs File Commands
|
|
2
2
|
|
|
3
3
|
[![NPM Package][npm]][npm-url]
|
|
4
|
-
[![Build Size][build-size]][build-size-url]
|
|
5
4
|
[![NPM Downloads][npm-downloads]][npmtrends-url]
|
|
6
5
|
|
|
7
6
|
This Yargs helper function lets you define all your commands as individual files and their file names and directory structure defines via implication your nested command structure.
|
|
@@ -19,21 +18,15 @@ npm install yargs-file-commands
|
|
|
19
18
|
## Example
|
|
20
19
|
|
|
21
20
|
```ts
|
|
22
|
-
import { createRequire } from 'module';
|
|
23
|
-
import path from 'path';
|
|
24
|
-
import yargs from 'yargs';
|
|
25
|
-
import { fileCommands } from 'yargs-file-commands';
|
|
26
|
-
|
|
27
|
-
const require = createRequire(import.meta.url);
|
|
28
|
-
|
|
29
21
|
export const main = async () => {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
22
|
+
const commandsDir = path.join(distDir, 'commands');
|
|
23
|
+
|
|
24
|
+
return yargs(hideBin(process.argv))
|
|
25
|
+
.scriptName(packageInfo.name!)
|
|
26
|
+
.version(packageInfo.version!)
|
|
27
|
+
.command(
|
|
28
|
+
await fileCommands({ commandDirs: [commandsDir], logLevel: 'debug' })
|
|
29
|
+
)
|
|
37
30
|
.help().argv;
|
|
38
31
|
};
|
|
39
32
|
```
|
|
@@ -90,7 +83,7 @@ studio start
|
|
|
90
83
|
|
|
91
84
|
The "fileCommands" method takes the following options:
|
|
92
85
|
|
|
93
|
-
**
|
|
86
|
+
**commandDirs**
|
|
94
87
|
|
|
95
88
|
- An array of directories where the routes are located relative to the build root folder.
|
|
96
89
|
- Required
|
|
@@ -100,11 +93,10 @@ The "fileCommands" method takes the following options:
|
|
|
100
93
|
- An array of file extensions for the route files. Files without matching extensions are ignored
|
|
101
94
|
- Default: `[".js", ".ts"]`
|
|
102
95
|
|
|
103
|
-
**
|
|
104
|
-
**
|
|
96
|
+
**ignorePatterns**
|
|
105
97
|
|
|
106
98
|
- An array of regexs which if matched against a filename or directory, lead it to being ignored/skipped over.
|
|
107
|
-
- Default: `[ /^[
|
|
99
|
+
- Default: `[ /^[.|_].*/, /\.(?:test|spec)\.[jt]s$/, /__(?:test|spec)__/, /\.d\.ts$/ ]`
|
|
108
100
|
|
|
109
101
|
**logLevel**
|
|
110
102
|
|
|
@@ -140,3 +132,10 @@ npm run publish
|
|
|
140
132
|
# run example cli
|
|
141
133
|
npx example-cli
|
|
142
134
|
```
|
|
135
|
+
|
|
136
|
+
Underneath the hood, we are using [NX](https://nx.dev) to manage the monorepo and shared scripts.
|
|
137
|
+
|
|
138
|
+
[npm]: https://img.shields.io/npm/v/yargs-file-commands
|
|
139
|
+
[npm-url]: https://www.npmjs.com/package/yargs-file-commands
|
|
140
|
+
[npm-downloads]: https://img.shields.io/npm/dw/yargs-file-commands
|
|
141
|
+
[npmtrends-url]: https://www.npmtrends.com/yargs-file-commands
|
package/dist/lib/fileCommands.js
CHANGED
|
@@ -5,9 +5,9 @@ import { segmentPath } from './segmentPath.js';
|
|
|
5
5
|
export const DefaultFileCommandsOptions = {
|
|
6
6
|
extensions: ['.js', '.ts'],
|
|
7
7
|
ignorePatterns: [
|
|
8
|
-
/^[
|
|
9
|
-
/\.(test|spec)\.[jt]s$/,
|
|
10
|
-
/__(test|spec)__/,
|
|
8
|
+
/^[.|_].*/,
|
|
9
|
+
/\.(?:test|spec)\.[jt]s$/,
|
|
10
|
+
/__(?:test|spec)__/,
|
|
11
11
|
/\.d\.ts$/
|
|
12
12
|
],
|
|
13
13
|
logLevel: 'info'
|
|
@@ -15,13 +15,13 @@ export const DefaultFileCommandsOptions = {
|
|
|
15
15
|
export const fileCommands = async (options) => {
|
|
16
16
|
const fullOptions = { ...DefaultFileCommandsOptions, ...options };
|
|
17
17
|
const commands = [];
|
|
18
|
-
await Promise.all(options.
|
|
19
|
-
const filePaths = await scanDirectory(
|
|
18
|
+
await Promise.all(options.commandDirs.map(async (commandDir) => {
|
|
19
|
+
const filePaths = await scanDirectory(commandDir, fullOptions);
|
|
20
20
|
const rootDirCommands = await Promise.all(filePaths.map(async (filePath) => {
|
|
21
|
-
const segments = segmentPath(filePath,
|
|
21
|
+
const segments = segmentPath(filePath, commandDir);
|
|
22
22
|
return {
|
|
23
23
|
fullPath: filePath,
|
|
24
|
-
segments: segmentPath(filePath,
|
|
24
|
+
segments: segmentPath(filePath, commandDir),
|
|
25
25
|
commandModule: await importCommandFromFile(filePath, segments[segments.length - 1])
|
|
26
26
|
};
|
|
27
27
|
}));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { readdir, stat } from 'fs/promises';
|
|
2
|
-
import { join } from 'path';
|
|
2
|
+
import path, { join } from 'path';
|
|
3
3
|
export const scanDirectory = async (dirPath, options = {}) => {
|
|
4
|
-
const { ignorePatterns = [] } = options;
|
|
4
|
+
const { ignorePatterns = [], extensions = [] } = options;
|
|
5
5
|
// Check if path should be ignored
|
|
6
6
|
const shouldIgnore = ignorePatterns.some((pattern) => pattern.test(dirPath));
|
|
7
7
|
if (shouldIgnore) {
|
|
@@ -20,6 +20,10 @@ export const scanDirectory = async (dirPath, options = {}) => {
|
|
|
20
20
|
if (stats.isDirectory()) {
|
|
21
21
|
return scanDirectory(fullPath, options);
|
|
22
22
|
}
|
|
23
|
+
const extension = path.extname(fullPath);
|
|
24
|
+
if (!extensions.includes(extension)) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
23
27
|
return [fullPath];
|
|
24
28
|
});
|
|
25
29
|
const nestedFiles = await Promise.all(nestedFilesPromises);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yargs-file-commands",
|
|
3
3
|
"description": "A yargs helper function that lets you define your commands structure via directory and file naming conventions.",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|