1 | #! /usr/bin/env node
|
2 |
|
3 | const c = require('chalk');
|
4 | const _ = require('lodash/fp');
|
5 | const Promise = require('bluebird');
|
6 | const updateNvmrc = require('./updatees/nvmrc');
|
7 | const updateTravis = require('./updatees/travis');
|
8 | const {
|
9 | updateLock,
|
10 | updateDependencies,
|
11 | updateDevDependencies,
|
12 | updatePackageEngines
|
13 | } = require('./updatees/package');
|
14 | const updateDockerfile = require('./updatees/dockerfile');
|
15 | const {commitFiles} = require('./core/git');
|
16 | const {syncGithub} = require('./core/github');
|
17 | const {findLatest} = require('./core/node');
|
18 | const {makeError} = require('./core/utils');
|
19 |
|
20 | const parseArgvToArray = _.pipe(_.split(','), _.compact);
|
21 |
|
22 | const bumpNodeVersion = async (latestNode, config) => {
|
23 | process.stdout.write(c.bold.blue(`\n\nā¬ļø About to bump node version:\n`));
|
24 | const nodeVersion = _.trimCharsStart('v', latestNode.version);
|
25 | await Promise.all([
|
26 | updateTravis(nodeVersion, config.node.travis),
|
27 | updatePackageEngines(nodeVersion, latestNode.npm, config.node.package, !!config.exact),
|
28 | updateNvmrc(nodeVersion, config.node.nvmrc),
|
29 | updateDockerfile(nodeVersion, config.node.dockerfile)
|
30 | ]);
|
31 |
|
32 | process.stdout.write(`+ Successfully bumped Node version to v${c.bold.blue(nodeVersion)}\n`);
|
33 | return {
|
34 | branch: `update-node-v${nodeVersion}`,
|
35 | message: `Upgrade Node to v${nodeVersion}`,
|
36 | pullRequest: {
|
37 | title: `Upgrade Node to v${nodeVersion}`,
|
38 | body: `:rocket: Upgraded Node version to v${nodeVersion}`
|
39 | }
|
40 | };
|
41 | };
|
42 |
|
43 | const bumpDependencies = async (pkg, cluster) => {
|
44 | process.stdout.write(
|
45 | c.bold.blue(`\n\nā¬ļø About to bump depencies cluster ${c.bold.white(cluster.name)}:\n`)
|
46 | );
|
47 | const installedDependencies = await updateDependencies(pkg, cluster.dependencies);
|
48 | const installedDevDependencies = await updateDevDependencies(pkg, cluster.devDependencies);
|
49 | const allInstalledDependencies = installedDependencies.concat(installedDevDependencies);
|
50 | if (_.isEmpty(allInstalledDependencies)) {
|
51 | process.stdout.write('+ No dependencies to update were found');
|
52 | return {};
|
53 | }
|
54 | process.stdout.write(
|
55 | `+ Successfully updated ${
|
56 | allInstalledDependencies.length
|
57 | } dependencies of cluster ${c.bold.blue(cluster.name)}:\n${allInstalledDependencies
|
58 | .map(
|
59 | ([dep, oldVersion, newVersion]) =>
|
60 | ` - ${c.bold(dep)}: ${c.dim(oldVersion)} -> ${c.blue.bold(newVersion)}`
|
61 | )
|
62 | .join('\n')}\n`
|
63 | );
|
64 | return {
|
65 | branch: cluster.branch || `update-dependencies-${cluster.name}`,
|
66 | message: `${
|
67 | cluster.message || 'Upgrade dependencies'
|
68 | }\n\nUpgraded dependencies:\n${allInstalledDependencies
|
69 | .map(([dep, oldVersion, newVersion]) => `- ${dep} ${oldVersion} -> ${newVersion}`)
|
70 | .join('\n')}\n`,
|
71 | pullRequest: {
|
72 | title: cluster.message || 'Upgrade dependencies',
|
73 | body: `### :outbox_tray: Upgraded dependencies:\n${allInstalledDependencies
|
74 | .map(
|
75 | ([dep, oldVersion, newVersion]) =>
|
76 | `- [\`${dep}\`](https://www.npmjs.com/package/${dep}): ${oldVersion} -> ${newVersion}`
|
77 | )
|
78 | .join('\n')}\n`
|
79 | }
|
80 | };
|
81 | };
|
82 |
|
83 | const commitAndMakePullRequest = config => async options => {
|
84 | const {branch, message, pullRequest} = options;
|
85 |
|
86 | if (!config.baseBranch) throw makeError('No base branch is defined');
|
87 | if (config.local) {
|
88 | return commitFiles(null, message);
|
89 | }
|
90 | const status = await syncGithub(
|
91 | config.repoSlug,
|
92 | config.baseBranch,
|
93 | branch,
|
94 | message,
|
95 | {
|
96 | body: _.get('body', pullRequest),
|
97 | title: _.get('title', pullRequest),
|
98 | label: config.label,
|
99 | reviewers: parseArgvToArray(config.reviewers),
|
100 | team_reviewers: parseArgvToArray(config.teamReviewers)
|
101 | },
|
102 | config.token
|
103 | );
|
104 | if (!status.commit)
|
105 | process.stdout.write('+ Did not made a Pull request, nothing has changed š“\n');
|
106 | else if (status.pullRequest) {
|
107 | process.stdout.write(
|
108 | `+ Successfully handled pull request ${c.bold.blue(`(#${status.pullRequest.number})`)}
|
109 | - š ${c.bold.cyan(status.pullRequest.html_url)}
|
110 | - š ${c.bold.dim(status.commit)}
|
111 | - š³ ${c.bold.green(status.branch)}\n`
|
112 | );
|
113 | } else {
|
114 | process.stdout.write(
|
115 | `+ Some issue seems to have occured with publication of changes ${status.commit}\n`
|
116 | );
|
117 | }
|
118 | return status;
|
119 | };
|
120 |
|
121 | module.exports = async config => {
|
122 | const _commitAndMakePullRequest = commitAndMakePullRequest(config);
|
123 | const clusters = config.dependencies;
|
124 |
|
125 | const RANGE = config.node_range || _.getOr('^8', 'packageContent.engines.node', config);
|
126 | const latestNode = await findLatest(RANGE);
|
127 | if (config.node) {
|
128 | const bumpCommitConfig = await bumpNodeVersion(latestNode, config);
|
129 | await _commitAndMakePullRequest(bumpCommitConfig);
|
130 | }
|
131 | const clusterDetails = await Promise.mapSeries(clusters, async cluster => {
|
132 | const branchDetails = await bumpDependencies(config.package, cluster);
|
133 | if (!branchDetails.branch) return {};
|
134 | await updateLock(config.packageManager);
|
135 | const {branch, commit, pullRequest} = await _commitAndMakePullRequest(branchDetails);
|
136 | return {branchDetails, pullRequest, branch, commit};
|
137 | }).catch(err => {
|
138 | process.stdout.write(`${err}\n`);
|
139 | process.stdout.write(`${err.stack}\n`);
|
140 | return process.exit(1);
|
141 | });
|
142 | process.stdout.write(c.bold.green('\n\nUpdate-node run with success š¤\n'));
|
143 | _.forEach(clusterDetail => {
|
144 | if (clusterDetail.branch)
|
145 | process.stdout.write(
|
146 | `- ${c.bold.green(clusterDetail.branch)}: ${c.dim.bold(
|
147 | clusterDetail.pullRequest.html_url
|
148 | )}\n`
|
149 | );
|
150 | }, clusterDetails);
|
151 | process.stdout.write('\n');
|
152 | };
|