forked from conventional-changelog/standard-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·111 lines (103 loc) · 3.27 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
const bump = require('./lib/lifecycles/bump');
const changelog = require('./lib/lifecycles/changelog');
const commit = require('./lib/lifecycles/commit');
const fs = require('fs');
const { latestSemverTag } = require('./lib/latest-semver-tag');
const path = require('path');
const printError = require('./lib/print-error');
const tag = require('./lib/lifecycles/tag');
const { resolveUpdaterObjectFromArgument } = require('./lib/updaters');
module.exports = async function standardVersion(argv) {
const defaults = require('./defaults');
/**
* `--message` (`-m`) support will be removed in the next major version.
*/
const message = argv.m || argv.message;
if (message) {
/**
* The `--message` flag uses `%s` for version substitutions, we swap this
* for the substitution defined in the config-spec for future-proofing upstream
* handling.
*/
argv.releaseCommitMessageFormat = message.replace(/%s/g, '{{currentTag}}');
if (!argv.silent) {
console.warn(
'[standard-version]: --message (-m) will be removed in the next major release. Use --releaseCommitMessageFormat.'
);
}
}
if (argv.changelogHeader) {
argv.header = argv.changelogHeader;
if (!argv.silent) {
console.warn(
'[standard-version]: --changelogHeader will be removed in the next major release. Use --header.'
);
}
}
if (
argv.header &&
argv.header.search(changelog.START_OF_LAST_RELEASE_PATTERN) !== -1
) {
throw Error(
`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`
);
}
/**
* If an argument for `packageFiles` provided, we include it as a "default" `bumpFile`.
*/
if (argv.packageFiles) {
defaults.bumpFiles = defaults.bumpFiles.concat(argv.packageFiles);
}
const args = Object.assign({}, defaults, argv);
let pkg;
for (const packageFile of args.packageFiles) {
const updater = resolveUpdaterObjectFromArgument(packageFile);
if (!updater) return;
const pkgPath = path.resolve(process.cwd(), updater.filename);
try {
const contents = fs.readFileSync(pkgPath, 'utf8');
pkg = {
version: updater.updater.readVersion(contents),
private:
typeof updater.updater.isPrivate === 'function'
? updater.updater.isPrivate(contents)
: false,
};
// eslint-disable-next-line no-empty
} catch {}
}
if (args.regenerateChangelog) {
args.releaseCount = (
await latestSemverTag(0, {
tagPrefix: args.tagPrefix,
})
).length;
return {
changelog: await changelog(args),
};
}
try {
let version;
if (pkg) {
version = pkg.version;
} else if (args.gitTagFallback) {
[version] = await latestSemverTag(1, {
tagPrefix: args.tagPrefix,
});
} else {
throw new Error('no package file found');
}
const newVersion = await bump(args, version);
const generatedChangelog = await changelog(args, newVersion);
const commitMsg = await commit(args, newVersion);
await tag(newVersion, pkg ? pkg.private : false, args);
return {
changelog: generatedChangelog,
version: args.tagPrefix + newVersion,
commit: commitMsg,
};
} catch (err) {
printError(args, err.message);
throw err;
}
};