Skip to content

Commit 2728f82

Browse files
authored
feat(params): add --quiet option
1 parent 98a545f commit 2728f82

File tree

8 files changed

+216
-9
lines changed

8 files changed

+216
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"prepare": "npm run build",
2929
"test": "NOCK_RECORD=0 npm run build && npm run test-unit && npm run test-integration && npm run test-e2e && npm run lint",
3030
"test-watch": "npm run test-unit -- --watch",
31-
"test-unit": "NODE_ENV=test mocha --require test/setup-unit.js --recursive 'test/unit/lib/**/*.spec.{js,ts}'",
31+
"test-unit": "NODE_ENV=test mocha --require test/setup-unit.js --recursive 'test/unit/**/**/*.spec.{js,ts}'",
3232
"test-integration": "NODE_ENV=test mocha --require test/integration/setup.js 'test/integration/**/*.spec.js'",
3333
"test-e2e": "NODE_ENV=test mocha 'test/end-to-end/**/*.spec.js'",
3434
"cover-unit": "nyc npm run test-unit",

src/bin/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const createRun = ({ shouldThrow }) => async function run (argv) {
118118
terminate(new ManyError('Runtime Errors', parseResult.getRuntimeErrors()))
119119
}
120120

121-
await renderPlan(batches, argv.environmentId)
121+
await renderPlan(batches, argv.environmentId, argv.quiet)
122122

123123
const serverErrorsWritten = []
124124

src/bin/lib/render-migration.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
import chalk from 'chalk'
22
import { RequestBatch } from '../../lib/offline-api/index'
33

4-
const renderBatch = function (batch: RequestBatch) {
4+
const renderBatch = function (batch: RequestBatch, isQuiet: boolean = false) {
55
const planMessage = batch.intent.toPlanMessage()
66
const message = []
77
message.push(chalk`{bold.underline ${planMessage.heading}}`)
8-
for (const detail of planMessage.details) {
9-
message.push(chalk` - ${detail}`)
8+
9+
if (!isQuiet) {
10+
for (const detail of planMessage.details) {
11+
message.push(chalk` - ${detail}`)
12+
}
1013
}
1114
for (const section of planMessage.sections) {
1215
message.push(chalk`\n {bold ${section.heading}}`)
13-
for (const sectionDetail of section.details) {
14-
message.push(chalk` - ${sectionDetail}`)
16+
17+
if (!isQuiet) {
18+
for (const sectionDetail of section.details) {
19+
message.push(chalk` - ${sectionDetail}`)
20+
}
1521
}
1622
}
1723

1824
console.log(message.join('\n'))
1925
}
2026

21-
const renderPlan = (batches: RequestBatch[], environment: string) => {
27+
const renderPlan = (batches: RequestBatch[], environment: string, isQuiet: boolean = false) => {
2228
console.log(chalk`{bold.green The following migration has been planned}\n`)
2329
console.log(chalk`{bold.underline Environment}: {bold.yellow ${environment}}\n`)
30+
2431
for (const batch of batches) {
25-
renderBatch(batch)
32+
renderBatch(batch, isQuiet)
2633

2734
if (batch.validationErrors.length > 0) {
2835
console.log('\n')

src/bin/usage-params.ts

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export default yargs
4343
describe: 'Skips any confirmation before applying the migration script',
4444
default: false
4545
})
46+
.option('quiet', {
47+
alias: 'q',
48+
boolean: false,
49+
describe: 'Reduce verbosity of information for the execution',
50+
default: false
51+
})
4652
.demandOption(['space-id'], 'Please provide a space ID')
4753
.help('h')
4854
.alias('h', 'help')

test/end-to-end/assertions.js

+15
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ module.exports = {
230230
if (result.code === 1) {
231231
stdtype = result.stderr;
232232
};
233+
233234
expect(stdtype).not.to.be.empty();
234235

235236
const withoutAnsiCodes = stripAnsi(stdtype);
@@ -279,6 +280,20 @@ module.exports = {
279280
};
280281
}
281282
},
283+
logs: {
284+
helpMessage: function () {
285+
return result => {
286+
let stdtype = result.stdout;
287+
if (result.code === 1) {
288+
stdtype = result.stderr;
289+
};
290+
expect(stdtype).not.to.be.empty();
291+
292+
const withoutAnsiCodes = stripAnsi(stdtype);
293+
expect(withoutAnsiCodes).to.include(`--quiet, -q`);
294+
};
295+
}
296+
},
282297
payload: {
283298
notDefined: function (method) {
284299
return result => {

test/end-to-end/flags.spec.js

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ describe('contentful-migration CLI flags', function () {
3434
.expect(assert.confirmation.noConfirmationMessage())
3535
.end(done);
3636
});
37+
it('includes "--quiet, -q" as options in the help message', function (done) {
38+
cli()
39+
.run(`--help`)
40+
.expect(assert.logs.helpMessage())
41+
.end(done);
42+
});
3743

3844
describe('file path resolution', function () {
3945
// Use the Travis build dir for integration tests, fallback to cwd for local tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { expect } from 'chai'
2+
import * as sinon from 'sinon'
3+
import renderSetup from './render-setup'
4+
5+
import { renderPlan } from '../../../../src/bin/lib/render-migration'
6+
7+
describe('Rendering', async () => {
8+
const requestBatches = await renderSetup()
9+
10+
describe('Render Plan ', () => {
11+
let logSpy
12+
13+
beforeEach(function () {
14+
logSpy = sinon.spy(console, 'log')
15+
})
16+
17+
afterEach(function () {
18+
logSpy.restore()
19+
})
20+
21+
describe('when --quiet option is passed', () => {
22+
it ('logs less verbose information of the execution', () => {
23+
renderPlan(requestBatches,'env-unit', true)
24+
25+
expect(logSpy.called).to.eq(true)
26+
expect(logSpy.getCall(2).args).not.to.match(/from:/)
27+
expect(logSpy.getCall(2).args).not.to.match(/to:/)
28+
expect(logSpy.getCall(2).args).not.to.match(/via:/)
29+
})
30+
})
31+
32+
describe('when --quiet option is not passed', () => {
33+
it ('logs verbose information of the execution', () => {
34+
renderPlan(requestBatches,'env-unit')
35+
36+
expect(logSpy.called).to.eq(true)
37+
expect(logSpy.getCall(2).args).to.match(/from:/)
38+
expect(logSpy.getCall(2).args).to.match(/to:/)
39+
expect(logSpy.getCall(2).args).to.match(/via:/)
40+
})
41+
})
42+
})
43+
})

test/unit/bin/lib/render-setup.ts

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import actionCreators from '../../../../src/lib/migration-steps/action-creators'
2+
import EntryDerive from '../../../../src/lib/interfaces/entry-derive'
3+
import EntryDeriveIntent from '../../../../src/lib/intent/entry-derive'
4+
import runIntent from '../../lib/intent/run-intent'
5+
import fakeCallsite from '../../../helpers/fake-callsite'
6+
import makeApiEntry from '../../../helpers/make-api-entry'
7+
8+
export default async function renderSetup () {
9+
const step: EntryDerive = {
10+
derivedContentType: 'author',
11+
from: ['authorName', 'authorTwitterHandle'],
12+
toReferenceField: 'author',
13+
derivedFields: ['firstName', 'lastName', 'twitterHandle'],
14+
identityKey: async (fromFields: any) => fromFields.authorTwitterHandle['en-US'],
15+
shouldPublish: true,
16+
deriveEntryForLocale: async (inputFields: any, locale: string) => {
17+
if (locale === 'de-DE') {
18+
return
19+
}
20+
const [firstName, lastName] = inputFields.authorName[locale].split(' ')
21+
const twitterHandle = inputFields.authorTwitterHandle[locale]
22+
23+
return {
24+
firstName,
25+
lastName,
26+
twitterHandle
27+
}
28+
}
29+
}
30+
const intent: EntryDeriveIntent = actionCreators.contentType.deriveLinkedEntries('entry', 0, step, fakeCallsite())
31+
32+
const contentTypes = [{
33+
name: 'Entry',
34+
sys: {
35+
id: 'entry',
36+
version: 1
37+
},
38+
fields: [
39+
{
40+
id: 'text',
41+
type: 'Text'
42+
}, {
43+
id: 'authorName',
44+
type: 'Symbol'
45+
}, {
46+
id: 'authorTwitterHandle',
47+
type: 'Symbol'
48+
}, {
49+
id: 'author',
50+
type: 'Link',
51+
linkType: 'Entry'
52+
}
53+
]
54+
}, {
55+
name: 'Author',
56+
sys: {
57+
id: 'author',
58+
version: 1
59+
},
60+
fields: [
61+
{
62+
id: 'firstName',
63+
type: 'Text'
64+
}, {
65+
id: 'lastName',
66+
type: 'Symbol'
67+
}, {
68+
id: 'twitterHandle',
69+
type: 'Symbol'
70+
}
71+
]
72+
}]
73+
const locales = ['de-DE', 'en-US']
74+
const entries = [
75+
makeApiEntry({
76+
id: 'doge',
77+
contentTypeId: 'entry',
78+
version: 1,
79+
fields: {
80+
text: {
81+
'en-US': 'Such text, wow',
82+
'de-DE': 'Solch Text, wow'
83+
},
84+
authorName: {
85+
'en-US': 'Author McAuthorface'
86+
},
87+
authorTwitterHandle: {
88+
'en-US': 'mcauthorface'
89+
}
90+
}
91+
}),
92+
makeApiEntry({
93+
id: 'clickbait',
94+
contentTypeId: 'entry',
95+
version: 1,
96+
fields: {
97+
text: {
98+
'en-US': 'You won\'t believe what happened next',
99+
'de-DE': 'Du wirst nicht glauben was als nächstes passierte'
100+
},
101+
authorName: {
102+
'en-US': 'Author McAuthorface'
103+
},
104+
authorTwitterHandle: {
105+
'en-US': 'mcauthorface'
106+
}
107+
}
108+
}),
109+
makeApiEntry({
110+
id: 'categorical-imperative',
111+
contentTypeId: 'entry',
112+
version: 1,
113+
fields: {
114+
text: {
115+
'en-US': 'Act only according to that maxim whereby you can, at the same time, will that it should become a universal law',
116+
'de-DE': 'Handle nur nach derjenigen Maxime, durch die du zugleich wollen kannst, dass sie ein allgemeines Gesetz werde'
117+
},
118+
authorName: {
119+
'en-US': 'Immanuel Kant'
120+
},
121+
authorTwitterHandle: {
122+
'en-US': 'ikant'
123+
}
124+
}
125+
})
126+
]
127+
128+
const api = await runIntent(intent, contentTypes, entries, locales)
129+
return await api.getRequestBatches()
130+
}

0 commit comments

Comments
 (0)