UNPKG

4.04 kBJavaScriptView Raw
1'use strict';
2require('any-observable/register/rxjs-all'); // eslint-disable-line import/no-unassigned-import
3const execa = require('execa');
4const del = require('del');
5const Listr = require('listr');
6const split = require('split');
7const {merge} = require('rxjs');
8const {filter} = require('rxjs/operators');
9const streamToObservable = require('@samverschueren/stream-to-observable');
10const readPkgUp = require('read-pkg-up');
11const hasYarn = require('has-yarn');
12const prerequisiteTasks = require('./lib/prerequisite');
13const gitTasks = require('./lib/git');
14const util = require('./lib/util');
15const publish = require('./lib/publish');
16
17const exec = (cmd, args) => {
18 // Use `Observable` support if merged https://github.com/sindresorhus/execa/pull/26
19 const cp = execa(cmd, args);
20
21 return merge(
22 streamToObservable(cp.stdout.pipe(split()), {await: cp}),
23 streamToObservable(cp.stderr.pipe(split()), {await: cp})
24 ).pipe(filter(Boolean));
25};
26
27module.exports = (input, opts) => {
28 input = input || 'patch';
29
30 opts = Object.assign({
31 cleanup: true,
32 publish: true
33 }, opts);
34
35 if (!hasYarn() && opts.yarn) {
36 throw new Error('Could not use Yarn without yarn.lock file');
37 }
38
39 // TODO: remove sometime far in the future
40 if (opts.skipCleanup) {
41 opts.cleanup = false;
42 }
43
44 const runTests = !opts.yolo;
45 const runCleanup = opts.cleanup && !opts.yolo;
46 const runPublish = opts.publish;
47 const pkg = util.readPkg();
48
49 const tasks = new Listr([
50 {
51 title: 'Prerequisite check',
52 enabled: () => runPublish,
53 task: () => prerequisiteTasks(input, pkg, opts)
54 },
55 {
56 title: 'Git',
57 task: () => gitTasks(opts)
58 }
59 ], {
60 showSubtasks: false
61 });
62
63 if (runCleanup) {
64 tasks.add([
65 {
66 title: 'Cleanup',
67 task: () => del('node_modules')
68 },
69 {
70 title: 'Installing dependencies using Yarn',
71 enabled: () => opts.yarn === true,
72 task: () => exec('yarn', ['install', '--frozen-lockfile', '--production=false']).catch(err => {
73 if (err.stderr.startsWith('error Your lockfile needs to be updated')) {
74 throw new Error('yarn.lock file is outdated. Run yarn, commit the updated lockfile and try again.');
75 }
76 throw err;
77 })
78 },
79 {
80 title: 'Installing dependencies using npm',
81 enabled: () => opts.yarn === false,
82 task: () => exec('npm', ['install', '--no-package-lock', '--no-production'])
83 }
84 ]);
85 }
86
87 if (runTests) {
88 tasks.add([
89 {
90 title: 'Running tests using npm',
91 enabled: () => opts.yarn === false,
92 task: () => exec('npm', ['test'])
93 },
94 {
95 title: 'Running tests using Yarn',
96 enabled: () => opts.yarn === true,
97 task: () => exec('yarn', ['test'])
98 }
99 ]);
100 }
101
102 tasks.add([
103 {
104 title: 'Bumping version using Yarn',
105 enabled: () => opts.yarn === true,
106 skip: () => {
107 if (runPublish && !pkg.private) {
108 return 'Public package: version will be bumped using yarn publish.';
109 }
110 },
111 task: () => exec('yarn', ['version', '--new-version', input])
112 },
113 {
114 title: 'Bumping version using npm',
115 enabled: () => opts.yarn === false,
116 task: () => exec('npm', ['version', input])
117 }
118 ]);
119
120 if (runPublish) {
121 tasks.add([
122 {
123 title: 'Publishing package using Yarn',
124 enabled: () => opts.yarn === true,
125 skip: () => {
126 if (pkg.private) {
127 return 'Private package: not publishing to Yarn.';
128 }
129 },
130 task: () => {
131 const args = ['publish'];
132
133 if (opts.contents) {
134 args.push(opts.contents);
135 }
136
137 args.push('--new-version', input);
138
139 if (opts.tag) {
140 args.push('--tag', opts.tag);
141 }
142
143 return exec('yarn', args);
144 }
145 },
146 {
147 title: 'Publishing package using npm',
148 enabled: () => opts.yarn === false,
149 skip: () => {
150 if (pkg.private) {
151 return 'Private package: not publishing to npm.';
152 }
153 },
154 task: (ctx, task) => publish(task, opts)
155 }
156 ]);
157 }
158
159 tasks.add({
160 title: 'Pushing tags',
161 task: () => exec('git', ['push', '--follow-tags'])
162 });
163
164 return tasks.run()
165 .then(() => readPkgUp())
166 .then(result => result.pkg);
167};