forked from sindresorhus/np
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprerequisite-tasks.js
More file actions
94 lines (85 loc) · 2.68 KB
/
prerequisite-tasks.js
File metadata and controls
94 lines (85 loc) · 2.68 KB
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
'use strict';
const Listr = require('listr');
const execa = require('execa');
const version = require('./version');
const git = require('./git-util');
const npm = require('./npm/util');
const {getTagVersionPrefix} = require('./util');
module.exports = (input, pkg, options) => {
const isExternalRegistry = npm.isExternalRegistry(pkg);
let newVersion = null;
const tasks = [
{
title: 'Ping npm registry',
enabled: () => !pkg.private && !isExternalRegistry,
task: async () => npm.checkConnection()
},
{
title: 'Check npm version',
task: async () => npm.verifyRecentNpmVersion()
},
{
title: 'Check yarn version',
enabled: () => options.yarn === true,
task: async () => {
const {stdout: yarnVersion} = await execa('yarn', ['--version']);
version.verifyRequirementSatisfied('yarn', yarnVersion);
}
},
{
title: 'Verify user is authenticated',
enabled: () => process.env.NODE_ENV !== 'test' && !pkg.private,
task: async () => {
const username = await npm.username({
externalRegistry: isExternalRegistry ? pkg.publishConfig.registry : false
});
const collaborators = await npm.collaborators(pkg);
if (!collaborators) {
return;
}
const json = JSON.parse(collaborators);
const permissions = json[username];
if (!permissions || !permissions.includes('write')) {
throw new Error('You do not have write permissions required to publish this package.');
}
}
},
{
title: 'Check git version',
task: async () => git.verifyRecentGitVersion()
},
{
title: 'Check git remote',
task: async () => git.verifyRemoteIsValid()
},
{
title: 'Validate version',
task: () => {
if (!version.isValidInput(input)) {
throw new Error(`Version should be either ${version.SEMVER_INCREMENTS.join(', ')}, or a valid semver version.`);
}
newVersion = version(pkg.version).getNewVersionFrom(input);
if (version(pkg.version).isLowerThanOrEqualTo(newVersion)) {
throw new Error(`New version \`${newVersion}\` should be higher than current version \`${pkg.version}\``);
}
}
},
{
title: 'Check for pre-release version',
task: () => {
if (!pkg.private && version(newVersion).isPrerelease() && !options.tag) {
throw new Error('You must specify a dist-tag using --tag when publishing a pre-release version. This prevents accidentally tagging unstable versions as "latest". https://docs.npmjs.com/cli/dist-tag');
}
}
},
{
title: 'Check git tag existence',
task: async () => {
await git.fetch();
const tagPrefix = await getTagVersionPrefix(options);
await git.verifyTagDoesNotExistOnRemote(`${tagPrefix}${newVersion}`);
}
}
];
return new Listr(tasks);
};