@vida-global/core 1.1.13 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,30 +0,0 @@
1
- const Git = require('./git');
2
- const utils = require('./utils');
3
-
4
-
5
- async function release(env, versionToRelease=null) {
6
- Git.pull();
7
-
8
- if (versionToRelease) {
9
- const branchName = `origin/release/${versionToRelease}`;
10
- const allBranches = Git.branches();
11
- if (!allBranches.includes(branchName)) {
12
- throw new Error(`release/${versionToRelease} does not exist`);
13
- }
14
- } else {
15
- versionToRelease = utils.getCurrentVersion().join('.');
16
- }
17
-
18
- const question = `Do you want to release version ${versionToRelease} to ${env}?`;
19
- const _confirm = await utils.confirmContinue(question);
20
- if (!_confirm) return;
21
-
22
- const sourceBranch = `release/${versionToRelease}`;
23
- const releaseBranch = `release/${env}`;
24
- Git.forceRemotePush(sourceBranch, releaseBranch);
25
- }
26
-
27
-
28
- module.exports = {
29
- release
30
- }
@@ -1,44 +0,0 @@
1
- const Git = require('./git');
2
-
3
-
4
- function getCurrentVersion() {
5
- const branches = Git.branches();
6
- const releases = branches.filter(branchName => /release\/\d+\.\d+\.\d+/.test(branchName));
7
- const versions = releases.map(branchName => {
8
- return branchName.replace('origin/release/', '').split('.').map(n => parseInt(n));
9
- });
10
-
11
- versions.sort((v1,v2) => {
12
- if (v1[0] != v2[0]) return v2[0] - v1[0];
13
- if (v1[1] != v2[1]) return v2[1] - v1[1];
14
- return v2[2] - v1[2];
15
- });
16
-
17
- return versions[0] || [1,0,0];
18
- }
19
-
20
-
21
- async function confirmContinue(question) {
22
- return await getInput(question, ['y','n']) == 'y';
23
- }
24
-
25
-
26
- async function getInput(question, options) {
27
- const readline = require('node:readline/promises').createInterface({
28
- input: process.stdin,
29
- output: process.stdout
30
- });
31
-
32
- const response = await readline.question(`${question} [${options.join(',')}] `);
33
- readline.close();
34
-
35
- if (options.includes(response)) return response;
36
- return getInput(question, options);
37
- }
38
-
39
-
40
- module.exports = {
41
- confirmContinue,
42
- getCurrentVersion,
43
- getInput,
44
- }
@@ -1,75 +0,0 @@
1
- const commander = require('commander');
2
- const { logger } = require('../lib/logger');
3
- const Release = require('../lib/release');
4
-
5
-
6
- function validateIssueNumber(val) {
7
- const issueNumber = parseInt(val, 10);
8
- if (isNaN(issueNumber)) {
9
- throw new commander.InvalidArgumentError('Invalid issue number');
10
- }
11
-
12
- return issueNumber;
13
- }
14
-
15
-
16
- function validateProjectType(val) {
17
- const types = ['bugfix', 'chore', 'feature', 'hotfix', 'refactor'];
18
- if (!types.includes(val)) {
19
- throw new commander.InvalidArgumentError(`must be one of: ${types.join(', ')}`);
20
- }
21
- return val;
22
- }
23
-
24
-
25
- function validateVersionType(val) {
26
- const types = ['major', 'minor', 'point'];
27
- if (!types.includes(val)) {
28
- throw new commander.InvalidArgumentError(`must be one of: ${types.join(', ')}`);
29
- }
30
- return val;
31
- }
32
-
33
-
34
- const program = new commander.Command();
35
- program.name('Vida Release')
36
- .description('A CLI for generating vida releases')
37
- .version('1.0.0');
38
-
39
-
40
- program.command('increment')
41
- .argument('<type>', 'version number to increment', validateVersionType)
42
- .action(async (type) => {
43
- try {
44
- await Release.increment(type);
45
- } catch(err) {
46
- logger.error(err.message);
47
- }
48
- });
49
-
50
-
51
- program.command('production')
52
- .argument('[version]', 'optional version release')
53
- .action(async (version) => {
54
- try {
55
- await Release.release('production', version);
56
- } catch(err) {
57
- logger.error(err.message);
58
- }
59
- });
60
-
61
-
62
- program.command('develop')
63
- .requiredOption('-i, --issue <issueNumber>', 'Must provide an issue number', validateIssueNumber)
64
- .option('-t, --type [projectType]', 'Project type', validateProjectType, 'feature')
65
- .option('-s, --source [sourceBranch]', 'Branch to copy')
66
- .argument('<description...>', 'a description of your project')
67
- .action(async (description, { issue, type, source }) => {
68
- try {
69
- await Release.develop(type, issue, description.join(' '), source);
70
- } catch(err) {
71
- logger.error(err.message);
72
- }
73
- });
74
-
75
- program.parse();
@@ -1,57 +0,0 @@
1
- const { develop } = require('../../lib/release/develop');
2
- const Git = require('../../lib/release/git');
3
- const TestHelpers = require('@vida-global/test-helpers');
4
- const utils = require('../../lib/release/utils');
5
-
6
-
7
- jest.mock('../../lib/release/git', () => {
8
- return { createBranch: jest.fn(), getCurrentUser: jest.fn() }
9
- });
10
- jest.mock('../../lib/release/utils', () => {
11
- return { confirmContinue: jest.fn() };
12
- });
13
-
14
-
15
- let branchType;
16
- let description;
17
- let firstName;
18
- let issueNumber;
19
- let lastName;
20
- let sourceBranch;
21
- beforeEach(() => {
22
- jest.resetAllMocks();
23
- utils.confirmContinue.mockImplementation(() => true);
24
-
25
- firstName = TestHelpers.Faker.User.randomFirstName();
26
- lastName = TestHelpers.Faker.User.randomLastName();
27
- Git.getCurrentUser.mockImplementation(() => `${firstName} ${lastName}`);
28
-
29
- branchType = TestHelpers.Faker.Text.randomString();
30
- sourceBranch = TestHelpers.Faker.Text.randomString();
31
- description = TestHelpers.Faker.Text.randomSentence();
32
- issueNumber = TestHelpers.Faker.Math.randomNumber();
33
- });
34
-
35
-
36
- describe('develop', () => {
37
- it ('confirms and creates a branch once', async () => {
38
- await develop(branchType, issueNumber, description, sourceBranch);
39
- expect(utils.confirmContinue).toHaveBeenCalledTimes(1);
40
- expect(Git.createBranch).toHaveBeenCalledTimes(1);
41
- });
42
-
43
- it ('generates the correct branch name', async () => {
44
- const initials = `${firstName[0]}${lastName[0]}`.toLowerCase();
45
- const branchName = `${branchType}/${issueNumber}/${initials}/${description.replace(/\s+/g, '-').toLowerCase().replace(/[^a-z0-9-]/g, '')}`;
46
- await develop(branchType, issueNumber, description, sourceBranch);
47
-
48
- expect(utils.confirmContinue).toHaveBeenCalledWith(`Do you want to create branch ${branchName}?`);
49
- expect(Git.createBranch).toHaveBeenCalledWith(branchName, sourceBranch);
50
- });
51
-
52
- it ('does not create a branch if the user does not confirm', async () => {
53
- utils.confirmContinue.mockImplementation(() => false);
54
- await develop(branchType, issueNumber, description, sourceBranch);
55
- expect(Git.createBranch).not.toHaveBeenCalled();
56
- });
57
- });
@@ -1,189 +0,0 @@
1
- const { execSync } = require('child_process');
2
- const Git = require('../../lib/release/git');
3
- const TestHelpers = require('@vida-global/test-helpers');
4
-
5
-
6
- jest.mock('child_process', () => {
7
- const execSync = jest.fn();
8
- return { execSync };
9
- });
10
-
11
-
12
- beforeEach(() => {
13
- jest.resetAllMocks();
14
- });
15
-
16
-
17
- describe('Git.add', () => {
18
- it ('calls git add with the file name', () => {
19
- const fileName = TestHelpers.Faker.Text.randomString();
20
- Git.add(fileName);
21
- expect(execSync).toHaveBeenCalledTimes(1);
22
- expect(execSync).toHaveBeenCalledWith(`git add ${fileName}`);
23
- });
24
- });
25
-
26
-
27
- describe('Git.branches', () => {
28
- it ('calls git branch and returns an array of the resulting strings', () => {
29
- const b1 = TestHelpers.Faker.Text.randomString();
30
- const b2 = TestHelpers.Faker.Text.randomString();
31
- const b3 = TestHelpers.Faker.Text.randomString();
32
- const branchStr = ` ${b1} \n ${b2}\n${b3} `;
33
- execSync.mockImplementation(() => branchStr);
34
-
35
- const branches = Git.branches();
36
-
37
- expect(execSync).toHaveBeenCalledTimes(1);
38
- expect(branches).toEqual([b1, b2, b3]);
39
- expect(execSync).toHaveBeenCalledWith(`git branch -r`);
40
- });
41
-
42
- it ('does not include the remote option when needed', () => {
43
- execSync.mockImplementation(() => '');
44
- const branches = Git.branches(false);
45
- expect(execSync).toHaveBeenCalledWith(`git branch`);
46
- });
47
- });
48
-
49
-
50
- describe('Git.checkout', () => {
51
- it ('calls git checkout with the branch name', () => {
52
- const branchName = TestHelpers.Faker.Text.randomString();
53
- Git.checkout(branchName);
54
- expect(execSync).toHaveBeenCalledTimes(1);
55
- expect(execSync).toHaveBeenCalledWith(`git checkout ${branchName}`);
56
- });
57
- });
58
-
59
-
60
- describe('Git.commit', () => {
61
- it ('calls git commit with comment', () => {
62
- const comment = TestHelpers.Faker.Text.randomString();
63
- Git.commit(comment);
64
- expect(execSync).toHaveBeenCalledTimes(1);
65
- expect(execSync).toHaveBeenCalledWith(`git commit -m '${comment}'`);
66
- });
67
- });
68
-
69
-
70
- describe('Git.createBranch', () => {
71
- it ('creates a branch and switches to it', () => {
72
- const branch1 = TestHelpers.Faker.Text.randomString();
73
- const branch2 = TestHelpers.Faker.Text.randomString();
74
-
75
- Git.createBranch(branch1, branch2);
76
-
77
- expect(execSync).toHaveBeenCalledTimes(2);
78
- expect(execSync).toHaveBeenNthCalledWith(1, `git branch ${branch1} ${branch2}`);
79
- expect(execSync).toHaveBeenNthCalledWith(2, `git checkout ${branch1}`);
80
- });
81
-
82
- it ('defaults to the primary branch', () => {
83
- const branch1 = TestHelpers.Faker.Text.randomString();
84
-
85
- execSync.mockImplementation(() => 'origin/main');
86
-
87
- Git.createBranch(branch1);
88
-
89
- expect(execSync).toHaveBeenCalledTimes(3);
90
- expect(execSync).toHaveBeenNthCalledWith(2, `git branch ${branch1} origin/main`);
91
- });
92
- });
93
-
94
-
95
- describe('Git.forceRemotePush', () => {
96
- it ('calls git push with the origin', () => {
97
- const branch1 = TestHelpers.Faker.Text.randomString();
98
- const branch2 = TestHelpers.Faker.Text.randomString();
99
-
100
- Git.forceRemotePush(branch1, branch2);
101
- expect(execSync).toHaveBeenCalledTimes(1);
102
- expect(execSync).toHaveBeenCalledWith(`git push origin origin/${branch1}:${branch2} --force`)
103
- });
104
- });
105
-
106
-
107
- describe('Git.getCurrentUser', () => {
108
- it ('returns the result of git config.name', () => {
109
- const name = TestHelpers.Faker.User.randomName();
110
- execSync.mockImplementation(() => name);
111
-
112
- const response = Git.getCurrentUser();
113
-
114
- expect(execSync).toHaveBeenCalledTimes(1);
115
- expect(execSync).toHaveBeenCalledWith(`git config user.name`);
116
- expect(response).toEqual(name);
117
- });
118
- });
119
-
120
-
121
- describe('Git.getPrimaryBranch', () => {
122
- it ('returns master when that is the primary branch', () => {
123
- const branch1 = TestHelpers.Faker.Text.randomString();
124
- const branch2 = TestHelpers.Faker.Text.randomString();
125
- const branchStr = ` origin/${branch1}\n origin/master \n origin/${branch2}`;
126
- execSync.mockImplementation(() => branchStr);
127
-
128
- const branch = Git.getPrimaryBranch();
129
-
130
- expect(branch).toEqual('origin/master');
131
- });
132
-
133
- it ('returns main when that is the primary branch', () => {
134
- const branch1 = TestHelpers.Faker.Text.randomString();
135
- const branch2 = TestHelpers.Faker.Text.randomString();
136
- const branchStr = `origin/${branch1}\norigin/${branch2}\n origin/main `;
137
- execSync.mockImplementation(() => branchStr);
138
-
139
- const branch = Git.getPrimaryBranch();
140
-
141
- expect(branch).toEqual('origin/main');
142
- });
143
-
144
- it ('returns undefined when it cannot determine the primary branch', () => {
145
- const branch1 = TestHelpers.Faker.Text.randomString();
146
- const branch2 = TestHelpers.Faker.Text.randomString();
147
- const branch3 = TestHelpers.Faker.Text.randomString();
148
- const branchStr = `origin/${branch1}\norigin/${branch2}\n origin/${branch3} `;
149
- execSync.mockImplementation(() => branchStr);
150
-
151
- const branch = Git.getPrimaryBranch();
152
-
153
- expect(branch).toBeUndefined();
154
- });
155
- });
156
-
157
-
158
- describe('Git.getCurrentBranch', () => {
159
- it ('returns the branch with the *', () => {
160
- const branch1 = TestHelpers.Faker.Text.randomString();
161
- const branch2 = TestHelpers.Faker.Text.randomString();
162
- const branch3 = TestHelpers.Faker.Text.randomString();
163
- const branchStr = ` origin/${branch1}\n * ${branch3} \n origin/${branch2}`;
164
- execSync.mockImplementation(() => branchStr);
165
-
166
- const branch = Git.getCurrentBranch();
167
-
168
- expect(branch).toEqual(branch3);
169
- });
170
- });
171
-
172
-
173
- describe('Git.pull', () => {
174
- it ('calls git pull with the prune option', () => {
175
- Git.pull();
176
- expect(execSync).toHaveBeenCalledTimes(1);
177
- expect(execSync).toHaveBeenCalledWith(`git pull --prune`);
178
- });
179
- });
180
-
181
-
182
- describe('Git.push', () => {
183
- it ('pushes to the current branch', () => {
184
- const branch = TestHelpers.Faker.Text.randomString();
185
- execSync.mockImplementation(() => `* ${branch}`);
186
- Git.push();
187
- expect(execSync).toHaveBeenCalledWith(`git push -u origin ${branch}`);
188
- });
189
- });
@@ -1,145 +0,0 @@
1
- const fs = require('fs');
2
- const { increment } = require('../../lib/release/increment');
3
- const Git = require('../../lib/release/git');
4
- const TestHelpers = require('@vida-global/test-helpers');
5
- const utils = require('../../lib/release/utils');
6
-
7
-
8
- jest.mock('../../lib/release/git', () => {
9
- return { add: jest.fn(), createBranch: jest.fn(), commit: jest.fn(), pull: jest.fn(), push: jest.fn() };
10
- });
11
- jest.mock('../../lib/release/utils', () => {
12
- return { getCurrentVersion: jest.fn(), confirmContinue: jest.fn() };
13
- });
14
- jest.mock('fs', () => {
15
- return { readFileSync: jest.fn(), writeFileSync: jest.fn() };
16
- });
17
-
18
-
19
- let config;
20
- let version;
21
- const packageJsonPath = `${process.cwd()}/package.json`;
22
- beforeEach(() => {
23
- jest.resetAllMocks();
24
- version = [TestHelpers.Faker.Math.randomNumber(),
25
- TestHelpers.Faker.Math.randomNumber(),
26
- TestHelpers.Faker.Math.randomNumber()];
27
- utils.getCurrentVersion.mockImplementation(() => version);
28
- utils.confirmContinue.mockImplementation(() => true);
29
-
30
- config = {
31
- [TestHelpers.Faker.Text.randomString()]: TestHelpers.Faker.Text.randomString(),
32
- [TestHelpers.Faker.Text.randomString()]: TestHelpers.Faker.Text.randomString(),
33
- version: version.join('.')
34
- }
35
- fs.readFileSync.mockImplementation(() => JSON.stringify(config, null, 4));
36
- });
37
-
38
-
39
- describe('increment', () => {
40
- it ('runs steps in the proper order', async () => {
41
- await increment('point');
42
-
43
- expect(Git.pull).toHaveBeenCalledTimes(1);
44
- expect(utils.getCurrentVersion).toHaveBeenCalledTimes(1);
45
- expect(Git.pull).toHaveBeenCalledBefore(utils.getCurrentVersion);
46
-
47
- expect(utils.confirmContinue).toHaveBeenCalledTimes(1);
48
- expect(utils.getCurrentVersion).toHaveBeenCalledBefore(utils.confirmContinue);
49
-
50
- expect(Git.createBranch).toHaveBeenCalledTimes(1);
51
- expect(utils.confirmContinue).toHaveBeenCalledBefore(Git.createBranch);
52
-
53
- expect(fs.readFileSync).toHaveBeenCalledTimes(1);
54
- expect(Git.createBranch).toHaveBeenCalledBefore(fs.readFileSync);
55
-
56
- expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
57
- expect(fs.readFileSync).toHaveBeenCalledBefore(fs.writeFileSync);
58
-
59
- expect(Git.push).toHaveBeenCalledTimes(1);
60
- expect(fs.writeFileSync).toHaveBeenCalledBefore(Git.push);
61
- });
62
-
63
-
64
- describe('getNewVersion', () => {
65
- it ('increments the major version', async () => {
66
- await increment('major');
67
- const newVersion = [...version];
68
- newVersion[0] = newVersion[0] + 1;
69
- newVersion[1] = 0;
70
- newVersion[2] = 0;
71
- expect(Git.createBranch).toHaveBeenCalledWith(`release/${newVersion.join('.')}`);
72
- });
73
-
74
- it ('increments the minor version', async () => {
75
- await increment('minor');
76
- const newVersion = [...version];
77
- newVersion[1] = newVersion[1] + 1;
78
- newVersion[2] = 0;
79
- expect(Git.createBranch).toHaveBeenCalledWith(`release/${newVersion.join('.')}`);
80
- });
81
-
82
- it ('increments the point version', async () => {
83
- await increment('point');
84
- const newVersion = [...version];
85
- newVersion[2] = newVersion[2] + 1;
86
- expect(Git.createBranch).toHaveBeenCalledWith(`release/${newVersion.join('.')}`);
87
- });
88
-
89
- it ('throws an error for an invalid type', async () => {
90
- expect(async () => {await increment('foo')}).rejects.toThrow();;
91
- });
92
- });
93
-
94
-
95
- describe('confirmation', () => {
96
- it ('asks the user if they want to upgrade to the new version', async () => {
97
- await increment('point');
98
- const newVersion = [version[0], version[1], version[2]+1].join('.');
99
- expect(utils.confirmContinue).toHaveBeenCalledWith(`Do you want to create version ${newVersion}?`);
100
- });
101
-
102
- it ('does not continue if the user responds "n"', async () => {
103
- utils.confirmContinue.mockImplementation(() => false);
104
- await increment('point');
105
-
106
- expect(Git.createBranch).not.toHaveBeenCalled();
107
- expect(fs.readFileSync).not.toHaveBeenCalled();
108
- expect(fs.writeFileSync).not.toHaveBeenCalled();
109
- expect(Git.push).not.toHaveBeenCalled();
110
- });
111
- });
112
-
113
-
114
- describe('createBranch', () => {
115
- it ('creates a release branch for the new version', async () => {
116
- await increment('point');
117
- const newVersion = [version[0], version[1], version[2]+1].join('.');
118
- expect(Git.createBranch).toHaveBeenCalledWith(`release/${newVersion}`);
119
- });
120
- });
121
-
122
-
123
- describe('updatePackageVersion', () => {
124
- it ('writes the new package version to package.json', async () => {
125
- const newVersion = [version[0], version[1], version[2]+1].join('.');
126
- const newConfig = structuredClone(config);
127
- newConfig.version = newVersion;
128
-
129
- await increment('point');
130
-
131
- expect(fs.writeFileSync).toHaveBeenCalledWith(packageJsonPath, JSON.stringify(newConfig, null, 4));
132
- });
133
-
134
- it ('adds package.json to be committed', async () => {
135
- await increment('point');
136
- expect(Git.add).toHaveBeenCalledWith(packageJsonPath);
137
- });
138
-
139
- it ('adds a descriptive commit message', async () => {
140
- const newVersion = [version[0], version[1], version[2]+1].join('.');
141
- await increment('point');
142
- expect(Git.commit).toHaveBeenCalledWith(`build: incrementing build to ${newVersion}`);
143
- });
144
- });
145
- });
@@ -1,72 +0,0 @@
1
- const { release } = require('../../lib/release/release');
2
- const Git = require('../../lib/release/git');
3
- const TestHelpers = require('@vida-global/test-helpers');
4
- const utils = require('../../lib/release/utils');
5
-
6
-
7
- jest.mock('../../lib/release/git', () => {
8
- return { pull: jest.fn(), forceRemotePush: jest.fn(), branches: jest.fn() };
9
- });
10
- jest.mock('../../lib/release/utils', () => {
11
- return { getCurrentVersion: jest.fn(), confirmContinue: jest.fn() };
12
- });
13
-
14
-
15
- let env;
16
- let version;
17
- beforeEach(() => {
18
- jest.resetAllMocks();
19
- env = TestHelpers.Faker.Text.randomString();
20
- version = [TestHelpers.Faker.Math.randomNumber(),
21
- TestHelpers.Faker.Math.randomNumber(),
22
- TestHelpers.Faker.Math.randomNumber()];
23
- utils.getCurrentVersion.mockImplementation(() => version);
24
- utils.confirmContinue.mockImplementation(() => true);
25
- });
26
-
27
-
28
- describe('release', () => {
29
- it ('runs steps in the proper order', async () => {
30
- await release(env);
31
-
32
- expect(Git.pull).toHaveBeenCalledTimes(1)
33
- expect(utils.confirmContinue).toHaveBeenCalledTimes(1)
34
- expect(Git.pull).toHaveBeenCalledBefore(utils.confirmContinue)
35
-
36
- expect(Git.forceRemotePush).toHaveBeenCalledTimes(1)
37
- expect(utils.confirmContinue).toHaveBeenCalledBefore(Git.forceRemotePush)
38
- });
39
-
40
- it ('defaults to the current version', async () => {
41
- await release(env);
42
- const versionStr = version.join('.');
43
- expect(utils.confirmContinue).toHaveBeenCalledWith(`Do you want to release version ${versionStr} to ${env}?`);
44
- expect(Git.forceRemotePush).toHaveBeenCalledWith(`release/${versionStr}`, `release/${env}`);
45
- });
46
-
47
- it ('accepts an alternate version', async () => {
48
- const altVersion = [TestHelpers.Faker.Math.randomNumber(),
49
- TestHelpers.Faker.Math.randomNumber(),
50
- TestHelpers.Faker.Math.randomNumber()].join('.');
51
- Git.branches.mockImplementation(() => [`origin/release/${version.join('.')}`, `origin/release/${altVersion}`, TestHelpers.Faker.Text.randomString()]);
52
- await release(env, altVersion);
53
- expect(utils.confirmContinue).toHaveBeenCalledWith(`Do you want to release version ${altVersion} to ${env}?`);
54
- expect(Git.forceRemotePush).toHaveBeenCalledWith(`release/${altVersion}`, `release/${env}`);
55
- });
56
-
57
- it ('does not continue if the user responds "n"', async () => {
58
- utils.confirmContinue.mockImplementation(() => false);
59
- await release(env);
60
-
61
- expect(Git.forceRemotePush).not.toHaveBeenCalled();
62
- });
63
-
64
- it ('throws an error if the version does not exist', async () => {
65
- const altVersion = [TestHelpers.Faker.Math.randomNumber(),
66
- TestHelpers.Faker.Math.randomNumber(),
67
- TestHelpers.Faker.Math.randomNumber()].join('.');
68
- Git.branches.mockImplementation(() => [`origin/release/${version.join('.')}`, TestHelpers.Faker.Text.randomString()]);
69
- expect(async () => { await release(env, altVersion)}).rejects.toThrow();
70
- });
71
- });
72
-