Skip to content

Commit ba392ca

Browse files
authored
Merge pull request #47 from codingtools/feature/add-output-functionality-to-file#21
Feature/add output functionality to file#21, closes #21
2 parents 728b3b2 + 9833290 commit ba392ca

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ USAGE
141141
$ cdt hash [STRING]
142142
143143
OPTIONS
144-
-f, --file=file file to be hashed
145-
-h, --help show CLI help
146-
-s, --string=string string to be hashed
147-
-t, --type=type type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]
144+
-f, --file=file file to be hashed
145+
-h, --help show CLI help
146+
-o, --outputFile=outputFile output file path
147+
-s, --string=string string to be hashed
148+
-t, --type=type type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]
148149
```
149150

150151
_See code: [src/commands/hash.ts](https://github.com/codingtools/cdt/blob/v0.1.5/src/commands/hash.ts)_
@@ -175,9 +176,12 @@ USAGE
175176
$ cdt minify [FILE]
176177
177178
OPTIONS
178-
-f, --file=file file to be minified
179-
-h, --help show CLI help
180-
-t, --type=type type of file to be minified, it will try to find type with extension supported: JS, HTML/HTM, CSS
179+
-f, --file=file file to be minified
180+
-h, --help show CLI help
181+
-o, --outputFile=outputFile output file path
182+
183+
-t, --type=type type of file to be minified, it will try to find type with extension supported: JS,
184+
HTML/HTM, CSS
181185
```
182186

183187
_See code: [src/commands/minify.ts](https://github.com/codingtools/cdt/blob/v0.1.5/src/commands/minify.ts)_

src/commands/crypto.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import {Command, flags} from '@oclif/command'
22
import * as CryptoJS from 'crypto-js'
33

44
import Logger from '../utilities/logger'
5-
6-
import Hash from './hash'
5+
import Utilities from '../utilities/utilities'
76

87
export default class Crypto extends Command {
98
static ENCRYPTION = 'encryption'
@@ -25,7 +24,7 @@ export default class Crypto extends Command {
2524
async run() {
2625
const {args, flags} = this.parse(Crypto)
2726

28-
args.string = Hash.getInputString(this, flags, args) //always add input to args
27+
args.string = Utilities.getInputString(this, flags, args) //always add input to args
2928
args.type = flags.encryption ? flags.encryption : flags.decryption //type like AES,DES
3029

3130
this.checkParameters(flags, args)

src/commands/hash.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {Command, flags} from '@oclif/command'
22
import * as CryptoJS from 'crypto-js'
3-
// import * as Hashes from 'jshashes'
43

54
import Logger from '../utilities/logger'
65
import Utilities from '../utilities/utilities'
6+
// import * as Hashes from 'jshashes'
7+
78
// TODO: all are Hexadecimal encoding for now, can also add b64
89

910
export default class Hash extends Command {
@@ -15,27 +16,17 @@ export default class Hash extends Command {
1516
type: flags.string({char: 't' , description: 'type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]'}),
1617
string: flags.string({char: 's' , description: 'string to be hashed'}),
1718
file: flags.string({char: 'f' , description: 'file to be hashed'}),
19+
outputFile: flags.string({char: 'o' , description: 'output file path'}),
1820
}
1921

2022
static args = [{name: 'string'}]
2123

22-
static getInputString(thisRef: any , flags: any, args: any) { //need to make it static so Crypto can use this
23-
// if -s or -f is not passed we will take it from args
24-
if (flags.string) //if -s given
25-
return flags.string
26-
else if (flags.file) {
27-
Logger.info(thisRef, `reading file: ${flags.file}`)
28-
return Utilities.getStringFromFile(thisRef, flags.file)
29-
} else
30-
return args.string
31-
}
32-
3324
// only 2 parameters required HASH_TYPE and INPUT_STRING
3425
async run() {
3526
const {args, flags} = this.parse(Hash)
3627

3728
flags.type = this.getHashType(flags) //by default let it be sha1
38-
args.string = Hash.getInputString(this, flags, args) // from either -s,-f or args
29+
args.string = Utilities.getInputString(this, flags, args) // from either -s,-f or args
3930

4031
//check params after evaluating all
4132
this.checkParameters(flags, args)
@@ -55,6 +46,10 @@ export default class Hash extends Command {
5546
// @ts-ignore
5647
let hashed: string = hashObject(args.string)
5748
Logger.success(this, `[${flags.type.toUpperCase()}] ${hashed}`)
49+
50+
if (flags.outputFile) { // if output path is provided then write to file also
51+
Utilities.writeStringToFile(this, flags.outputFile, hashed)
52+
}
5853
}
5954

6055
// BACKUP function

src/commands/minify.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default class Minify extends Command {
1212
help: flags.help({char: 'h'}),
1313
type: flags.string({char: 't' , description: 'type of file to be minified, it will try to find type with extension supported: JS, HTML/HTM, CSS'}),
1414
file: flags.string({char: 'f' , description: 'file to be minified'}),
15+
outputFile: flags.string({char: 'o' , description: 'output file path'}),
1516
}
1617

1718
static args = [{name: 'file'}]
@@ -96,9 +97,12 @@ export default class Minify extends Command {
9697
default:
9798
Logger.error(this, 'Invalid Minifier Type')
9899
}
99-
Logger.progressStop(this, 'minified')
100+
Logger.progressStop(this, `file: ${flags.file} minified`)
100101
// }, 1000)
101-
Logger.success(this, `\n${output}`)
102102

103+
if (flags.outputFile) { // if output path is provided then write to file also
104+
Utilities.writeStringToFile(this, flags.outputFile, output)
105+
} else
106+
Logger.success(this, `minified output: \n${output}`)
103107
}
104108
}

src/utilities/utilities.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// tslint:disable-next-line:file-name-casing
2+
import * as chalk from 'chalk'
23
import * as fs from 'fs'
34

45
import Logger from './logger'
@@ -7,7 +8,7 @@ export default class Utilities {
78
public static getStringFromFile(thisRef: any, filePath: string) {
89
let fileStr = ''
910
if (!fs.existsSync(filePath)) {
10-
Logger.error(thisRef, `Could not find file: ${filePath}`) // this will output error and exit command
11+
Logger.error(thisRef, `Could not find file: ${chalk.red(filePath)}`) // this will output error and exit command
1112
} else {
1213
let fileBuffer = fs.readFileSync(filePath)
1314
fileStr = fileBuffer.toString() // by default utf8
@@ -16,7 +17,7 @@ export default class Utilities {
1617
}
1718
public static getJsonObjectFromFile(thisRef: any, filePath: string) {
1819
if (!fs.existsSync(filePath)) {
19-
Logger.error(thisRef, `Could not find file: ${filePath}`) // this will output error and exit command
20+
Logger.error(thisRef, `Could not find file: ${chalk.red(filePath)}`) // this will output error and exit command
2021
} else {
2122
let jsonString = fs.readFileSync(filePath, 'utf8')
2223
try {
@@ -27,4 +28,26 @@ export default class Utilities {
2728
}
2829
}
2930

31+
public static getInputString(thisRef: any , flags: any, args: any) { //need to make it static so Crypto can use this
32+
// if -s or -f is not passed we will take it from args
33+
if (flags.string) //if -s given
34+
return flags.string
35+
else if (flags.file) {
36+
Logger.info(thisRef, `reading file: ${chalk.green(flags.file)}`)
37+
return Utilities.getStringFromFile(thisRef, flags.file)
38+
} else
39+
return args.string
40+
}
41+
42+
public static writeStringToFile(thisRef: any, filePath: string, string: string) {
43+
if (!fs.existsSync(filePath))
44+
Logger.info(thisRef, `Could not find file: ${chalk.yellow(filePath + ', creating new one')}`) // this will output error and exit command
45+
else
46+
Logger.warn(thisRef, `File already exists: ${chalk.green(filePath)}, ${chalk.yellow('overriding content')}`) // this will output error and exit command
47+
48+
fs.writeFileSync(filePath, string)
49+
Logger.success(thisRef, `output written to file: ${chalk.green(filePath)}`) // this will output error and exit command
50+
// return `${chalk.red(pkg)} ${message}`
51+
52+
}
3053
}

0 commit comments

Comments
 (0)