diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..8eb26be --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,65 @@ +--- +version: 2 +jobs: + node-latest: &test + docker: + - image: node:latest + working_directory: ~/cli + steps: + - checkout + - restore_cache: &restore_cache + keys: + - v1-npm-{{checksum ".circleci/config.yml"}}-{{ checksum "package-lock.json"}} + - v1-npm-{{checksum ".circleci/config.yml"}} + - run: + name: Install dependencies + command: .circleci/greenkeeper + - run: ./bin/run --version + - run: ./bin/run --help + - run: + name: Testing + command: npm test + - run: + name: Submitting code coverage to codecov + command: | + ./node_modules/.bin/nyc report --reporter text-lcov > coverage.lcov + curl -s https://codecov.io/bash | bash + node-8: + <<: *test + docker: + - image: node:8 + release: + <<: *test + steps: + - add_ssh_keys + - checkout + - restore_cache: *restore_cache + - run: + name: Install dependencies + command: | + npm install -g @oclif/semantic-release@3 semantic-release@15 + npm install + - run: + name: Cutting release + command: | + semantic-release -e @oclif/semantic-release + - save_cache: + key: v1-npm-{{checksum ".circleci/config.yml"}}-{{checksum "package-lock.json"}} + paths: + - ~/cli/node_modules + - ~/.npm + - /usr/local/lib/node_modules + +workflows: + version: 2 + "cdt": + jobs: + - node-latest + - node-8 + - release: + context: org-global + filters: + branches: {only: master} + requires: + - node-latest + - node-8 diff --git a/.circleci/greenkeeper b/.circleci/greenkeeper new file mode 100644 index 0000000..6421877 --- /dev/null +++ b/.circleci/greenkeeper @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -ex + +PATH=/usr/local/share/.config/npm/global/node_modules/.bin:$PATH + +if [[ "$CIRCLE_BRANCH" != greenkeeper/* ]]; then + npm install + exit 0 +fi + +if [[ ! -z "$GIT_EMAIL" ]] & [[ ! -z "$GIT_USERNAME" ]]; then + git config --global push.default simple + git config --global user.email "$GIT_EMAIL" + git config --global user.name "$GIT_USERNAME" +fi + +if [[ ! -x "$(command -v greenkeeper-lockfile-update)" ]]; then + npm install -g greenkeeper-lockfile@1 +fi + +greenkeeper-lockfile-update +npm install +greenkeeper-lockfile-upload diff --git a/src/commands/hash.ts b/src/commands/hash.ts index 78557f5..11e58b4 100644 --- a/src/commands/hash.ts +++ b/src/commands/hash.ts @@ -1,4 +1,5 @@ import {Command, flags} from '@oclif/command' +import * as fs from 'fs' // @ts-ignore import * as Hashes from 'jshashes' @@ -22,32 +23,63 @@ export default class Hash extends Command { const type: string = flags.type || 'sha1' //by default let it be sha1 - // if -s is not passed we will take it from args + // if -s or -f is not passed we will take it from args + let str = '' - let str: string - - if (flags.string) + if (flags.string) //if -s given str = flags.string - else - str = args.string + else if (flags.file) { + str = this.getStringFromFile(flags.file) + } else + str = args.string + + this.log(str) + this.calculateHash(type, str) + } + private calculateHash(type: string, str: string) { let hash: Hashes switch (type.toUpperCase()) { case 'SHA1': - hash = new Hashes.SHA1(); break + hash = new Hashes.SHA1() + break case 'SHA256': - hash = new Hashes.SHA256(); break + hash = new Hashes.SHA256() + break case 'SHA512': - hash = new Hashes.SHA512(); break + hash = new Hashes.SHA512() + break case 'MD5': - hash = new Hashes.MD5(); break + hash = new Hashes.MD5() + break case 'RMD160': - hash = new Hashes.RMD160(); break + hash = new Hashes.RMD160() + break default: hash = undefined } - let hashed: string = hash.hex(str) - this.log(`[HASH]: ${hashed}`) + if (hash) { + let hashed: string = hash.hex(str) + this.log(`[HASH]: ${hashed}`) + } else { + this.log('[ERROR]: invalid hash type') + } + } + + private getStringFromFile(filePath: string) { + let fileStr = '' + if (!fs.existsSync(filePath)) { + this.error('reading File') // this will output error and exit command + } else { + fileStr = fs.readFileSync(filePath, 'utf8') + + // TODO: fix this Issue #3 + if (fileStr.charAt(fileStr.length - 1) === '\n') { + fileStr = fileStr.substring(0, fileStr.length - 1) + } + } + return fileStr + } } diff --git a/test/commands/hash.test.ts b/test/commands/hash.test.ts index 3758c16..7d815c4 100644 --- a/test/commands/hash.test.ts +++ b/test/commands/hash.test.ts @@ -11,7 +11,7 @@ describe('hash', () => { // passing sha1 as option test .stdout() - .command(['hash', 'ashish' ,'-t','sha1']) + .command(['hash', 'ashish' , '-t', 'sha1']) .it("cdt hash 'ashish' -t 'sha1'", ctx => { expect(ctx.stdout).to.contain('428b6da53085b8fd7b37e9fb259c0c609bd09984') }) @@ -63,4 +63,23 @@ describe('hash', () => { .it("cdt hash --type rmd160 'ashish'", ctx => { expect(ctx.stdout).to.contain('a85a72b0a240abecdf27f127aa75fd8663d6d5be') }) + + //file input - file not found TODO: solve issue #4 +/* + test + .stderr() + .command(['hash', '-f', 'test/resources/filenotfound.txt']) + .it("cdt hash -f 'test/resources/filenotfound.txt'", ctx => { + expect(ctx.stderr).to.contain('Error: reading File') + }) +*/ + + //file input + test + .stdout() + .command(['hash', '-f', 'test/resources/test.txt']) + .it("cdt hash -f 'test/resources/test.txt'", ctx => { + expect(ctx.stdout).to.contain('97ee6255ffc855e79e2324d5495b6538e29034f9') + }) + }) diff --git a/test/resources/test.txt b/test/resources/test.txt new file mode 100644 index 0000000..396a965 --- /dev/null +++ b/test/resources/test.txt @@ -0,0 +1,2 @@ +Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. +t is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).