Skip to content

Feature/add output functionality to file#21 #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ USAGE
$ cdt hash [STRING]

OPTIONS
-f, --file=file file to be hashed
-h, --help show CLI help
-s, --string=string string to be hashed
-t, --type=type type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]
-f, --file=file file to be hashed
-h, --help show CLI help
-o, --outputFile=outputFile output file path
-s, --string=string string to be hashed
-t, --type=type type of hash [SHA1(default), MD5, SHA256, SHA512, RMD160 or RIPEMD160]
```

_See code: [src/commands/hash.ts](https://github.com/codingtools/cdt/blob/v0.1.5/src/commands/hash.ts)_
Expand Down Expand Up @@ -175,9 +176,12 @@ USAGE
$ cdt minify [FILE]

OPTIONS
-f, --file=file file to be minified
-h, --help show CLI help
-t, --type=type type of file to be minified, it will try to find type with extension supported: JS, HTML/HTM, CSS
-f, --file=file file to be minified
-h, --help show CLI help
-o, --outputFile=outputFile output file path

-t, --type=type type of file to be minified, it will try to find type with extension supported: JS,
HTML/HTM, CSS
```

_See code: [src/commands/minify.ts](https://github.com/codingtools/cdt/blob/v0.1.5/src/commands/minify.ts)_
Expand Down
5 changes: 2 additions & 3 deletions src/commands/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {Command, flags} from '@oclif/command'
import * as CryptoJS from 'crypto-js'

import Logger from '../utilities/logger'

import Hash from './hash'
import Utilities from '../utilities/utilities'

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

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

this.checkParameters(flags, args)
Expand Down
21 changes: 8 additions & 13 deletions src/commands/hash.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Command, flags} from '@oclif/command'
import * as CryptoJS from 'crypto-js'
// import * as Hashes from 'jshashes'

import Logger from '../utilities/logger'
import Utilities from '../utilities/utilities'
// import * as Hashes from 'jshashes'

// TODO: all are Hexadecimal encoding for now, can also add b64

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

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

static getInputString(thisRef: any , flags: any, args: any) { //need to make it static so Crypto can use this
// if -s or -f is not passed we will take it from args
if (flags.string) //if -s given
return flags.string
else if (flags.file) {
Logger.info(thisRef, `reading file: ${flags.file}`)
return Utilities.getStringFromFile(thisRef, flags.file)
} else
return args.string
}

// only 2 parameters required HASH_TYPE and INPUT_STRING
async run() {
const {args, flags} = this.parse(Hash)

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

//check params after evaluating all
this.checkParameters(flags, args)
Expand All @@ -55,6 +46,10 @@ export default class Hash extends Command {
// @ts-ignore
let hashed: string = hashObject(args.string)
Logger.success(this, `[${flags.type.toUpperCase()}] ${hashed}`)

if (flags.outputFile) { // if output path is provided then write to file also
Utilities.writeStringToFile(this, flags.outputFile, hashed)
}
}

// BACKUP function
Expand Down
8 changes: 6 additions & 2 deletions src/commands/minify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class Minify extends Command {
help: flags.help({char: 'h'}),
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'}),
file: flags.string({char: 'f' , description: 'file to be minified'}),
outputFile: flags.string({char: 'o' , description: 'output file path'}),
}

static args = [{name: 'file'}]
Expand Down Expand Up @@ -96,9 +97,12 @@ export default class Minify extends Command {
default:
Logger.error(this, 'Invalid Minifier Type')
}
Logger.progressStop(this, 'minified')
Logger.progressStop(this, `file: ${flags.file} minified`)
// }, 1000)
Logger.success(this, `\n${output}`)

if (flags.outputFile) { // if output path is provided then write to file also
Utilities.writeStringToFile(this, flags.outputFile, output)
} else
Logger.success(this, `minified output: \n${output}`)
}
}
27 changes: 25 additions & 2 deletions src/utilities/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// tslint:disable-next-line:file-name-casing
import * as chalk from 'chalk'
import * as fs from 'fs'

import Logger from './logger'
Expand All @@ -7,7 +8,7 @@ export default class Utilities {
public static getStringFromFile(thisRef: any, filePath: string) {
let fileStr = ''
if (!fs.existsSync(filePath)) {
Logger.error(thisRef, `Could not find file: ${filePath}`) // this will output error and exit command
Logger.error(thisRef, `Could not find file: ${chalk.red(filePath)}`) // this will output error and exit command
} else {
let fileBuffer = fs.readFileSync(filePath)
fileStr = fileBuffer.toString() // by default utf8
Expand All @@ -16,7 +17,7 @@ export default class Utilities {
}
public static getJsonObjectFromFile(thisRef: any, filePath: string) {
if (!fs.existsSync(filePath)) {
Logger.error(thisRef, `Could not find file: ${filePath}`) // this will output error and exit command
Logger.error(thisRef, `Could not find file: ${chalk.red(filePath)}`) // this will output error and exit command
} else {
let jsonString = fs.readFileSync(filePath, 'utf8')
try {
Expand All @@ -27,4 +28,26 @@ export default class Utilities {
}
}

public static getInputString(thisRef: any , flags: any, args: any) { //need to make it static so Crypto can use this
// if -s or -f is not passed we will take it from args
if (flags.string) //if -s given
return flags.string
else if (flags.file) {
Logger.info(thisRef, `reading file: ${chalk.green(flags.file)}`)
return Utilities.getStringFromFile(thisRef, flags.file)
} else
return args.string
}

public static writeStringToFile(thisRef: any, filePath: string, string: string) {
if (!fs.existsSync(filePath))
Logger.info(thisRef, `Could not find file: ${chalk.yellow(filePath + ', creating new one')}`) // this will output error and exit command
else
Logger.warn(thisRef, `File already exists: ${chalk.green(filePath)}, ${chalk.yellow('overriding content')}`) // this will output error and exit command

fs.writeFileSync(filePath, string)
Logger.success(thisRef, `output written to file: ${chalk.green(filePath)}`) // this will output error and exit command
// return `${chalk.red(pkg)} ${message}`

}
}