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