Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 93a6fc0

Browse files
authored
fix(envs): allow add env with one valid option, liv-7291 (#23)
* fix(envs): allow add env with one valid option * Update package.json
1 parent b3cc1d7 commit 93a6fc0

File tree

3 files changed

+33
-50
lines changed

3 files changed

+33
-50
lines changed

__tests__/cmds/envs/add.spec.ts

+26-44
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,39 @@ import envs from '../../../src/cmds/envs'
33

44
import { fromEnvsAddCmd } from '../../fixtures/configs'
55

6-
jest.mock('configstore');
6+
const makeOutput = config => {
7+
const output = {
8+
...config
9+
}
10+
11+
// @ts-ignore
12+
output.apiToken = config['api-token']
13+
delete output['api-token']
714

15+
return output
16+
}
17+
18+
jest.mock('configstore');
819
const mockedConfigstore = jest.mocked(configStore, true)
920

10-
describe('Add command', () => {
11-
const envName = 'boron'
21+
it('should call correctly the config store to set an entry', () => {
1222
const set = mockedConfigstore.mock.instances[0].set as jest.Mock
1323
envs({
14-
_: ['add', envName],
24+
_: ['add', 'env1'],
1525
...fromEnvsAddCmd
1626
})
27+
expect(set).toHaveBeenCalledTimes(1)
1728

18-
it('should call correctly the config store to set an entry', () => {
19-
expect(set).toHaveBeenCalled()
20-
21-
const [ key, config ] = set.mock.calls[0]
22-
23-
expect(key).toBe(`envs.${envName}`)
24-
25-
const output = {
26-
...fromEnvsAddCmd
27-
}
29+
expect(set.mock.calls[0][0]).toBe(`envs.env1`)
30+
expect(set.mock.calls[0][1]).toStrictEqual(makeOutput(fromEnvsAddCmd))
2831

29-
// @ts-ignore
30-
output.apiToken = fromEnvsAddCmd['api-token']
31-
delete output['api-token']
32-
expect(config).toStrictEqual(output)
33-
});
34-
35-
it('should update correctly an entry in the config store ', () => {
36-
const dataTopdate = {
37-
...fromEnvsAddCmd,
38-
endpoint: 'https://endpoint.fake'
39-
}
40-
envs({
41-
_: ['add', envName],
42-
...dataTopdate
43-
})
44-
45-
const [ key, config ] = set.mock.calls[1]
46-
47-
expect(key).toBe(`envs.${envName}`)
48-
49-
const output = {
50-
...fromEnvsAddCmd,
51-
...dataTopdate
52-
}
32+
// With --api-token only
33+
envs({
34+
_: ['add', 'env2'],
35+
'api-token': fromEnvsAddCmd['api-token']
36+
})
5337

54-
// @ts-ignore
55-
output.apiToken = fromEnvsAddCmd['api-token']
56-
delete output['api-token']
57-
expect(config).toStrictEqual(output)
58-
});
59-
})
38+
expect(set).toHaveBeenCalledTimes(2)
39+
expect(set.mock.calls[1][0]).toBe(`envs.env2`)
40+
expect(set.mock.calls[1][1]).toStrictEqual(makeOutput({ 'api-token': fromEnvsAddCmd['api-token'] }))
41+
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@livestorm/cli",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "CLI that allows you to build and deploy your Livestorm plugin",
55
"main": "index.js",
66
"bin": {

src/cmds/envs.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@ const ENV_CONFIG_FIELDS = [
1212
function add(envName, data) {
1313
if (!envName) {
1414
return console.log('\x1b[31m', 'The environment name is missing.')
15+
1516
}
1617

17-
const fileredData = ENV_CONFIG_FIELDS.reduce((env, key) => {
18+
const output = ENV_CONFIG_FIELDS.reduce((env, key) => {
1819
if (key in data) {
1920
env[key] = data[key]
2021
}
2122
return env
2223
}, {})
2324

2425
if (data['api-token']) {
25-
fileredData.apiToken = data['api-token']
26+
output.apiToken = data['api-token']
2627
}
2728

28-
const dataIsValid = Object.keys(data).some(key => ENV_CONFIG_FIELDS.includes(key))
29+
const outputIsValid = Object.keys(output).some(key => ENV_CONFIG_FIELDS.includes(key))
2930

30-
if (!dataIsValid) {
31+
if (!outputIsValid) {
3132
return console.log('\x1b[31m', 'The configuration should contain at least one property.')
3233
}
3334

34-
configStore.set(`envs.${envName}`, fileredData)
35+
configStore.set(`envs.${envName}`, output)
3536

3637
if (configStore.has(`envs.${envName}`)) {
3738
console.log('\x1b[32m', `The configuration for the environment ${envName} has been added.`)

0 commit comments

Comments
 (0)