Skip to content

Commit 13fc9c8

Browse files
authored
Introduce the CLI mode
1 parent b81fc90 commit 13fc9c8

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,26 @@ import customFileSystem from './some-destination';
106106

107107
const mfr: MFR = new MFR( [], customFileSystem );
108108
```
109+
## CLI
110+
111+
MFR supports also CLI version. To run mfr in the CLI mode install it first using `npm install -g mfr`.
112+
Then you will be able to run MFR via the `mfr` command:
113+
114+
```
115+
Usage: -m <mode> -s <source> -d <destination>
116+
117+
Options:
118+
--help Show help [boolean]
119+
--version Show version number [boolean]
120+
-m, --mode Application mode [string] [required]
121+
-s, --source Source path [string] [required]
122+
-d, --destination Destiatnion path [string] [required]
123+
-f, --formats Handled formats [array]
124+
-p, --pattern Pattern [string]
125+
-a, --appendix Appendix [string]
126+
--pre, --prefix Prefix pattern [string]
127+
--pos, --postfix Prefix patern [string]
128+
```
109129

110130
---
111131

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,28 @@
1414
"lint:fix": "npm run lint -- --fix",
1515
"test": "nyc ava"
1616
},
17+
"bin": {
18+
"mfr": "./dist/bin/index.js"
19+
},
1720
"repository": {
1821
"type": "git",
1922
"url": "git@github.com:elszczepano/mfr.git"
2023
},
2124
"author": "Dominik Szczepaniak",
2225
"license": "MIT",
2326
"dependencies": {
24-
"uuid": "8.3.2"
27+
"chalk": "4.1.0",
28+
"uuid": "8.3.2",
29+
"yargs": "16.2.0"
2530
},
2631
"devDependencies": {
2732
"@cksource-cs/eslint-config-cs-module": "^2.1.2",
33+
"@types/chalk": "2.2.0",
2834
"@types/node": "12.12.6",
2935
"@types/rimraf": "3.0.0",
3036
"@types/sinon": "9.0.10",
3137
"@types/uuid": "8.3.0",
38+
"@types/yargs": "16.0.0",
3239
"ava": "3.15.0",
3340
"eslint": "6.6.0",
3441
"eslint-import-resolver-node": "0.3.4",

src/bin/index.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
/*
3+
* Copyright © 2021 Dominik Szczepaniak, MIT License.
4+
*/
5+
6+
import chalk from 'chalk';
7+
import yargs from 'yargs';
8+
9+
import MFR from '../Engine';
10+
11+
interface IOptions extends Record<string, unknown> {
12+
mode: string;
13+
source: string;
14+
destination: string;
15+
formats?: string[];
16+
pattern?: string;
17+
appendix?: 'prefix' | 'postfix' | 'all' | 'none';
18+
prefix?: string;
19+
postfix?: string;
20+
}
21+
22+
// @ts-ignore - issue with typings
23+
const options: IOptions = yargs
24+
.usage( 'Usage: -m <mode> -s <source> -d <destination>' )
25+
.option( 'm', { alias: 'mode', describe: 'Application mode', type: 'string', demandOption: true } )
26+
.option( 's', { alias: 'source', describe: 'Source path', type: 'string', demandOption: true } )
27+
.option( 'd', { alias: 'destination', describe: 'Destiatnion path', type: 'string', demandOption: true } )
28+
.option( 'f', { alias: 'formats', describe: 'Handled formats', type: 'array' } )
29+
.option( 'p', { alias: 'pattern', describe: 'Pattern', type: 'string' } )
30+
.option( 'a', { alias: 'appendix', describe: 'Appendix', type: 'string' } )
31+
.option( 'pre', { alias: 'prefix', describe: 'Prefix pattern', type: 'string' } )
32+
.option( 'pos', { alias: 'postfix', describe: 'Prefix patern', type: 'string' } )
33+
.argv;
34+
35+
( async function() {
36+
try {
37+
const mfr: MFR = new MFR();
38+
39+
const { mode, source, destination, formats, pattern, appendix, prefix, postfix } = options;
40+
41+
await mfr.run( {
42+
mode,
43+
sourcePath: source,
44+
destinationPath: destination,
45+
formats,
46+
pattern,
47+
appendix,
48+
prefixPattern: prefix,
49+
postfixPattern: postfix
50+
} );
51+
52+
chalk.green.bold( 'Done!' );
53+
} catch ( error ) {
54+
chalk.red.bold( 'Rename operation failed. An unexpected error occurred!', error );
55+
}
56+
}() );

0 commit comments

Comments
 (0)