Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: add convertToCached
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2018
1 parent 2d54133 commit 3fd39b9
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 252 deletions.
31 changes: 19 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ jobs:
- checkout
- restore_cache: &restore_cache
keys:
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/config.yml"}}-master-
- run: ./.circleci/setup_git
- run: ./.circleci/greenkeeper
- run: yarn exec nps ci.test
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-
- v0-yarn-{{checksum ".circleci/config.yml"}}-master-
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn test
- run: curl -s https://codecov.io/bash | bash
- store_test_results:
- store_test_results: &store_test_results
path: ~/cli/reports
- save_cache: &save_cache
key: v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
key: v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
paths:
- ~/cli/node_modules
- /usr/local/share/.cache/yarn
Expand All @@ -28,16 +28,23 @@ jobs:
<<: *test
docker:
- image: node:8
steps:
- checkout
- restore_cache: *restore_cache
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn test
- run: curl -s https://codecov.io/bash | bash
- store_test_results: *store_test_results
release:
<<: *test
steps:
- add_ssh_keys
- checkout
- restore_cache: *restore_cache
- run: ./.circleci/setup_git
- run: yarn --frozen-lockfile
- run: yarn exec nps ci.release
- save_cache: *save_cache
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn exec nps release

workflows:
version: 2
Expand Down
2 changes: 1 addition & 1 deletion .circleci/setup_git
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
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.user "$GIT_USERNAME"
git config --global user.name "$GIT_USERNAME"
fi

git submodule sync
Expand Down
22 changes: 22 additions & 0 deletions .circleci/yarn
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -e

PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH

CLI_ENGINE_UTIL_YARN_ARGS="--frozen-lockfile"

if [[ "$CIRCLE_BRANCH" == greenkeeper/* ]]; then
CLI_ENGINE_GREENKEEPER_BRANCH=1
CLI_ENGINE_UTIL_YARN_ARGS=""
if [[ ! -x "$(command -v greenkeeper-lockfile-update)" ]]; then
yarn global add greenkeeper-lockfile@1
fi
greenkeeper-lockfile-update
fi

yarn install $CLI_ENGINE_UTIL_YARN_ARGS

if [[ "$CLI_ENGINE_GREENKEEPER_BRANCH" == 1 ]]; then
greenkeeper-lockfile-upload
fi
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install:
- git config --global user.name "dxcli"
- yarn
test_script:
- .\node_modules\.bin\nps ci.test
- yarn test
after_test:
- ps: |
$env:PATH = 'C:\msys64\usr\bin;' + $env:PATH
Expand Down
114 changes: 33 additions & 81 deletions package-scripts.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,44 @@
const {
setColors,
concurrent,
crossEnv,
ifNotWindows,
ifWindows,
mkdirp,
series,
setColors,
} = require('nps-utils')
const pjson = require('./package.json')
const release = pjson.devDependencies.typedoc ? ['ci.release.semantic-release', 'ci.release.typedoc'] : ['ci.release.semantic-release']
const script = (script, description) => description ? {script, description} : {script}
const hidden = script => ({script, hiddenFromHelp: true})
const unixOrWindows = (unix, windows) => series(ifNotWindows(unix), ifWindows(windows))

setColors(['dim'])

let ciTests = [
'ci.test.eslint',
'ci.test.mocha',
'ci.test.tslint',
]
const script = (script, description) => description ? {script, description} : {script}

module.exports = {
scripts: {
build: 'rm -rf lib && tsc',
lint: {
default: concurrent.nps('lint.eslint', 'lint.commitlint', 'lint.tsc', 'lint.tslint'),
eslint: script('eslint .', 'lint js files'),
commitlint: script('commitlint --from origin/master', 'ensure that commits are in valid conventional-changelog format'),
tsc: script('tsc -p test --noEmit', 'syntax check with tsc'),
tslint: script('tslint -p test', 'lint ts files'),
},
test: {
default: script(concurrent.nps('lint', 'test.mocha'), 'lint and run all tests'),
series: script(series.nps('lint', 'test.mocha'), 'lint and run all tests in series'),
mocha: {
default: script('mocha --forbid-only "test/**/*.test.ts"', 'run all mocha tests'),
coverage: {
default: hidden(series.nps('test.mocha.nyc nps test.mocha', 'test.mocha.coverage.report')),
report: hidden(series('nps "test.mocha.nyc report --reporter text-lcov" > coverage.lcov')),
},
junit: hidden(series(
crossEnv('MOCHA_FILE="reports/mocha.xml" ') + series.nps('test.mocha.nyc nps \\"test.mocha --reporter mocha-junit-reporter\\"'),
series.nps('test.mocha.coverage.report'),
)),
nyc: hidden('nyc --nycrc-path node_modules/@dxcli/dev-nyc-config/.nycrc'),
},
},
ci: {
test: {
default: hidden(series(
mkdirp('reports'),
unixOrWindows(
concurrent.nps(...ciTests),
series.nps(...ciTests),
),
)),
mocha: hidden(
unixOrWindows(
series.nps('test.mocha.junit'),
series.nps('test.mocha.coverage'),
)
),
eslint: hidden(
unixOrWindows(
series.nps('lint.eslint --format junit --output-file reports/eslint.xml'),
series.nps('lint.eslint'),
)
),
tslint: hidden(
unixOrWindows(
series.nps('lint.tslint --format junit > reports/tslint.xml'),
series.nps('lint.tslint'),
)
),
},
typedoc: hidden('typedoc --out /tmp/docs src/index.ts --excludeNotExported --mode file'),
release: {
default: hidden(series.nps(...release)),
'semantic-release': hidden('semantic-release -e @dxcli/dev-semantic-release'),
typedoc: hidden(series(
'git clone -b gh-pages $CIRCLE_REPOSITORY_URL gh-pages',
'nps ci.typedoc',
'rm -rf ./gh-pages/*',
'mv /tmp/docs/* ./gh-pages',
'cd gh-pages && git add . && git commit -m "updates from $CIRCLE_SHA1 [skip ci]" && git push',
)),
},
},
},
const linters = {
eslint: script('eslint .', 'lint js files'),
commitlint: script('commitlint --from origin/master', 'ensure that commits are in valid conventional-changelog format'),
tsc: script('tsc -p test --noEmit', 'syntax check with tsc'),
tslint: script('tslint -p test', 'lint ts files'),
}

const scripts = {
...linters,
lint: concurrent(linters),
test: script(concurrent.nps(...Object.keys(linters), 'mocha'), 'lint and run all tests'),
mocha: script('mocha --forbid-only "test/**/*.test.ts"', 'run all mocha tests'),
}

if (process.env.CI) {
if (process.env.CIRCLECI) {
scripts.test.script = series(mkdirp('reports'), scripts.test.script)
// add mocha junit reporter
scripts.mocha.script = crossEnv(`MOCHA_FILE=reports/mocha.xml ${scripts.mocha.script} --reporter mocha-junit-reporter`)
// add eslint reporter
scripts.eslint.script = `${scripts.eslint.script} --format junit --output-file reports/eslint.xml`
// add tslint reporter
scripts.tslint.script = `${scripts.tslint.script} --format junit > reports/tslint.xml`
scripts.release = 'semantic-release -e @dxcli/dev-semantic-release'
}
// add code coverage reporting with nyc
const nyc = 'nyc --nycrc-path node_modules/@dxcli/dev-nyc-config/.nycrc'
const nycReport = `${nyc} report --reporter text-lcov > coverage.lcov`
scripts.mocha.script = series(`${nyc} ${scripts.mocha.script}`, nycReport)
}

module.exports = {scripts}
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
"dependencies": {
"@dxcli/config": "^0.1.26",
"@dxcli/parser": "^0.0.5",
"cli-ux": "^3.2.1",
"cli-ux": "^3.2.2",
"debug": "^3.1.0",
"tslib": "^1.9.0"
},
"devDependencies": {
"@dxcli/dev": "^2.0.2",
"fancy-mocha": "^0.3.3",
"@dxcli/dev": "^2.0.5",
"chai": "^4.1.2",
"eslint": "^4.16.0",
"fancy-test": "^0.5.5",
"http-call": "^5.0.2",
"husky": "^0.14.3",
"mocha": "^5.0.0",
"nps": "^5.7.1",
"nps-utils": "^1.5.0",
"typedoc": "^0.9.0"
"typedoc": "^0.9.0",
"typescript": "^2.6.2"
},
"engines": {
"node": ">=8.0.0"
Expand Down
18 changes: 18 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ const g = global as any

const parentModule = module.parent && module.parent.parent && module.parent.parent.filename

export function convertToCached(c: Config.ICommand): Config.ICachedCommand {
return {
_base: c._base,
id: c.id,
description: c.description,
usage: c.usage,
plugin: c.plugin!,
hidden: c.hidden,
aliases: c.aliases || [],
help: c.help,
load: async () => c,
}
}

export default abstract class Command {
static _base = `${pjson.name}@${pjson.version}`
static id: string
Expand All @@ -39,6 +53,10 @@ export default abstract class Command {
}
static async load() { return this }

static convertToCached(): Config.ICachedCommand {
return convertToCached(this)
}

config: Config.IConfig
flags: { [name: string]: any } = {}
argv: string[]
Expand Down
44 changes: 27 additions & 17 deletions test/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import * as Config from '@dxcli/config'
import cli from 'cli-ux'
import {expect, fancy} from 'fancy-mocha'
import {expect, fancy} from 'fancy-test'

import Base from '../src'

class Command extends Base {
static description = 'test command'
async run() {
cli.log('foo')
}
}

describe('run', () => {
fancy()
fancy
.stdout()
.run(() => Command.run([]))
.run(output => expect(output.stdout).to.equal('foo\n'))
.end('logs to stdout')
.do(() => Command.run([]))
.do(output => expect(output.stdout).to.equal('foo\n'))
.it('logs to stdout')

fancy()
.run(() => {
fancy
.do(() => {
const Command: Config.ICommand = class extends Base {
async run() {
throw new Error('new x error')
Expand All @@ -28,11 +29,11 @@ describe('run', () => {
return Command.run([])
})
.catch(/new x error/)
.end('errors out')
.it('errors out')

fancy()
fancy
.stdout()
.run(() => {
.do(() => {
const Command: Config.ICommand = class extends Base {
async run() {
cli.exit(0)
Expand All @@ -41,19 +42,28 @@ describe('run', () => {
return Command.run([])
})
.catch(/EEXIT: 0/)
.end('exits with 0')
.it('exits with 0')

describe('help error', () => {
['-h', '--help', 'help'].forEach(arg => {
fancy()
.run(() => Command.run([arg]))
fancy
.do(() => Command.run([arg]))
.catch((err: any) => expect(err.code).to.equal('EHELP'))
.end(`throws help error when passed "${arg}"`)
.it(`throws help error when passed "${arg}"`)
})

fancy()
.run(() => Command.run(['foo']))
fancy
.do(() => Command.run(['foo']))
.catch((err: any) => expect(err.code).not.to.equal('EHELP'))
.end('does not throw help error when passed "foo"')
.it('does not throw help error when passed "foo"')
})

describe('convertToCached', () => {
fancy
.do(() => {
const c = Command.convertToCached()
expect(c.description).to.equal('test command')
})
.it('converts to cached')
})
})
3 changes: 2 additions & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../tsconfig",
"include": [
"./**/*"
"./**/*",
"../src/**/*"
]
}

0 comments on commit 3fd39b9

Please sign in to comment.