Skip to content

Commit

Permalink
feat(params): add --quiet option
Browse files Browse the repository at this point in the history
  • Loading branch information
makinwab authored Mar 21, 2019
1 parent 98a545f commit 2728f82
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"prepare": "npm run build",
"test": "NOCK_RECORD=0 npm run build && npm run test-unit && npm run test-integration && npm run test-e2e && npm run lint",
"test-watch": "npm run test-unit -- --watch",
"test-unit": "NODE_ENV=test mocha --require test/setup-unit.js --recursive 'test/unit/lib/**/*.spec.{js,ts}'",
"test-unit": "NODE_ENV=test mocha --require test/setup-unit.js --recursive 'test/unit/**/**/*.spec.{js,ts}'",
"test-integration": "NODE_ENV=test mocha --require test/integration/setup.js 'test/integration/**/*.spec.js'",
"test-e2e": "NODE_ENV=test mocha 'test/end-to-end/**/*.spec.js'",
"cover-unit": "nyc npm run test-unit",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const createRun = ({ shouldThrow }) => async function run (argv) {
terminate(new ManyError('Runtime Errors', parseResult.getRuntimeErrors()))
}

await renderPlan(batches, argv.environmentId)
await renderPlan(batches, argv.environmentId, argv.quiet)

const serverErrorsWritten = []

Expand Down
21 changes: 14 additions & 7 deletions src/bin/lib/render-migration.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
import chalk from 'chalk'
import { RequestBatch } from '../../lib/offline-api/index'

const renderBatch = function (batch: RequestBatch) {
const renderBatch = function (batch: RequestBatch, isQuiet: boolean = false) {
const planMessage = batch.intent.toPlanMessage()
const message = []
message.push(chalk`{bold.underline ${planMessage.heading}}`)
for (const detail of planMessage.details) {
message.push(chalk` - ${detail}`)

if (!isQuiet) {
for (const detail of planMessage.details) {
message.push(chalk` - ${detail}`)
}
}
for (const section of planMessage.sections) {
message.push(chalk`\n {bold ${section.heading}}`)
for (const sectionDetail of section.details) {
message.push(chalk` - ${sectionDetail}`)

if (!isQuiet) {
for (const sectionDetail of section.details) {
message.push(chalk` - ${sectionDetail}`)
}
}
}

console.log(message.join('\n'))
}

const renderPlan = (batches: RequestBatch[], environment: string) => {
const renderPlan = (batches: RequestBatch[], environment: string, isQuiet: boolean = false) => {
console.log(chalk`{bold.green The following migration has been planned}\n`)
console.log(chalk`{bold.underline Environment}: {bold.yellow ${environment}}\n`)

for (const batch of batches) {
renderBatch(batch)
renderBatch(batch, isQuiet)

if (batch.validationErrors.length > 0) {
console.log('\n')
Expand Down
6 changes: 6 additions & 0 deletions src/bin/usage-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export default yargs
describe: 'Skips any confirmation before applying the migration script',
default: false
})
.option('quiet', {
alias: 'q',
boolean: false,
describe: 'Reduce verbosity of information for the execution',
default: false
})
.demandOption(['space-id'], 'Please provide a space ID')
.help('h')
.alias('h', 'help')
Expand Down
15 changes: 15 additions & 0 deletions test/end-to-end/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ module.exports = {
if (result.code === 1) {
stdtype = result.stderr;
};

expect(stdtype).not.to.be.empty();

const withoutAnsiCodes = stripAnsi(stdtype);
Expand Down Expand Up @@ -279,6 +280,20 @@ module.exports = {
};
}
},
logs: {
helpMessage: function () {
return result => {
let stdtype = result.stdout;
if (result.code === 1) {
stdtype = result.stderr;
};
expect(stdtype).not.to.be.empty();

const withoutAnsiCodes = stripAnsi(stdtype);
expect(withoutAnsiCodes).to.include(`--quiet, -q`);
};
}
},
payload: {
notDefined: function (method) {
return result => {
Expand Down
6 changes: 6 additions & 0 deletions test/end-to-end/flags.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('contentful-migration CLI flags', function () {
.expect(assert.confirmation.noConfirmationMessage())
.end(done);
});
it('includes "--quiet, -q" as options in the help message', function (done) {
cli()
.run(`--help`)
.expect(assert.logs.helpMessage())
.end(done);
});

describe('file path resolution', function () {
// Use the Travis build dir for integration tests, fallback to cwd for local tests
Expand Down
43 changes: 43 additions & 0 deletions test/unit/bin/lib/render-migrations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect } from 'chai'
import * as sinon from 'sinon'
import renderSetup from './render-setup'

import { renderPlan } from '../../../../src/bin/lib/render-migration'

describe('Rendering', async () => {
const requestBatches = await renderSetup()

describe('Render Plan ', () => {
let logSpy

beforeEach(function () {
logSpy = sinon.spy(console, 'log')
})

afterEach(function () {
logSpy.restore()
})

describe('when --quiet option is passed', () => {
it ('logs less verbose information of the execution', () => {
renderPlan(requestBatches,'env-unit', true)

expect(logSpy.called).to.eq(true)
expect(logSpy.getCall(2).args).not.to.match(/from:/)
expect(logSpy.getCall(2).args).not.to.match(/to:/)
expect(logSpy.getCall(2).args).not.to.match(/via:/)
})
})

describe('when --quiet option is not passed', () => {
it ('logs verbose information of the execution', () => {
renderPlan(requestBatches,'env-unit')

expect(logSpy.called).to.eq(true)
expect(logSpy.getCall(2).args).to.match(/from:/)
expect(logSpy.getCall(2).args).to.match(/to:/)
expect(logSpy.getCall(2).args).to.match(/via:/)
})
})
})
})
130 changes: 130 additions & 0 deletions test/unit/bin/lib/render-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import actionCreators from '../../../../src/lib/migration-steps/action-creators'
import EntryDerive from '../../../../src/lib/interfaces/entry-derive'
import EntryDeriveIntent from '../../../../src/lib/intent/entry-derive'
import runIntent from '../../lib/intent/run-intent'
import fakeCallsite from '../../../helpers/fake-callsite'
import makeApiEntry from '../../../helpers/make-api-entry'

export default async function renderSetup () {
const step: EntryDerive = {
derivedContentType: 'author',
from: ['authorName', 'authorTwitterHandle'],
toReferenceField: 'author',
derivedFields: ['firstName', 'lastName', 'twitterHandle'],
identityKey: async (fromFields: any) => fromFields.authorTwitterHandle['en-US'],
shouldPublish: true,
deriveEntryForLocale: async (inputFields: any, locale: string) => {
if (locale === 'de-DE') {
return
}
const [firstName, lastName] = inputFields.authorName[locale].split(' ')
const twitterHandle = inputFields.authorTwitterHandle[locale]

return {
firstName,
lastName,
twitterHandle
}
}
}
const intent: EntryDeriveIntent = actionCreators.contentType.deriveLinkedEntries('entry', 0, step, fakeCallsite())

const contentTypes = [{
name: 'Entry',
sys: {
id: 'entry',
version: 1
},
fields: [
{
id: 'text',
type: 'Text'
}, {
id: 'authorName',
type: 'Symbol'
}, {
id: 'authorTwitterHandle',
type: 'Symbol'
}, {
id: 'author',
type: 'Link',
linkType: 'Entry'
}
]
}, {
name: 'Author',
sys: {
id: 'author',
version: 1
},
fields: [
{
id: 'firstName',
type: 'Text'
}, {
id: 'lastName',
type: 'Symbol'
}, {
id: 'twitterHandle',
type: 'Symbol'
}
]
}]
const locales = ['de-DE', 'en-US']
const entries = [
makeApiEntry({
id: 'doge',
contentTypeId: 'entry',
version: 1,
fields: {
text: {
'en-US': 'Such text, wow',
'de-DE': 'Solch Text, wow'
},
authorName: {
'en-US': 'Author McAuthorface'
},
authorTwitterHandle: {
'en-US': 'mcauthorface'
}
}
}),
makeApiEntry({
id: 'clickbait',
contentTypeId: 'entry',
version: 1,
fields: {
text: {
'en-US': 'You won\'t believe what happened next',
'de-DE': 'Du wirst nicht glauben was als nächstes passierte'
},
authorName: {
'en-US': 'Author McAuthorface'
},
authorTwitterHandle: {
'en-US': 'mcauthorface'
}
}
}),
makeApiEntry({
id: 'categorical-imperative',
contentTypeId: 'entry',
version: 1,
fields: {
text: {
'en-US': 'Act only according to that maxim whereby you can, at the same time, will that it should become a universal law',
'de-DE': 'Handle nur nach derjenigen Maxime, durch die du zugleich wollen kannst, dass sie ein allgemeines Gesetz werde'
},
authorName: {
'en-US': 'Immanuel Kant'
},
authorTwitterHandle: {
'en-US': 'ikant'
}
}
})
]

const api = await runIntent(intent, contentTypes, entries, locales)
return await api.getRequestBatches()
}

0 comments on commit 2728f82

Please sign in to comment.