-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
138 lines (120 loc) · 3.62 KB
/
index.ts
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
/* eslint-disable @typescript-eslint/promise-function-async */
/* eslint-disable @typescript-eslint/unbound-method */
import chalk from 'chalk';
import log from 'npmlog';
import os from 'os';
import {
getWorkspaces,
toWorkspacesRunOptions,
setWorkspaceVersion,
updatePackageVersions,
} from '../../utils/workspaces';
import { getNextVersionsByWorkspaces } from '../../utils/versions';
import {
outputFormat,
gitCommit,
gitAdd,
addTag,
gitPush,
} from '../../utils';
import {
IFlags, IWorkspaceVersion,
} from '../../interfaces';
import { IPackagePrint } from '../workspaces';
function gitCommitAndTagVersion(
workspaces: IWorkspaceVersion[],
{
message: subject = 'Publish',
gitTagVersion = true,
commit = true,
...flags
}: IFlags,
): Promise<IWorkspaceVersion[]> {
const tags = workspaces.map((
{
workspace,
nextVersion,
},
) => `${workspace.getName()}-v${nextVersion}`);
const message = tags.reduce((msg, tag) => `${msg}${os.EOL} - ${tag}`, `${subject}${os.EOL}`);
return Promise.resolve()
.then(() => commit && gitCommit(message, flags))
.then(() => gitTagVersion && Promise.all(tags.map(tag => addTag(tag))))
.then(() => workspaces);
}
function updateWorkspaceVersion(
workspaces: IWorkspaceVersion[],
{
updatePackage = true,
...flags
}: IFlags,
): Promise<IWorkspaceVersion[]> {
let chain: Promise<any> = Promise.resolve();
if (updatePackage && workspaces.length > 0) {
const promises = workspaces.map(({ workspace, nextVersion }) => {
const { config: { filePath } } = workspace;
return Promise.resolve()
.then(() => setWorkspaceVersion(workspace, nextVersion))
.then(() => filePath);
});
const versions = workspaces.reduce((acc, { workspace, nextVersion }) => ({
...acc,
[workspace.getName()]: nextVersion,
}), {});
chain = chain
.then(() => Promise.all(promises))
.then(filePaths => gitAdd(filePaths))
.then(() => updatePackageVersions(versions))
.then(filePaths => gitAdd(filePaths))
.then(() => gitCommitAndTagVersion(workspaces, flags));
}
return chain.then(() => workspaces);
}
function outputCommand(
workspaces: IWorkspaceVersion[],
flags: IFlags,
): IWorkspaceVersion[] {
const workspacesToPrint = workspaces
.map((
{
workspace,
nextVersion,
}: IWorkspaceVersion,
): IPackagePrint => {
const { config: { json: { description, private: $private } } } = workspace;
return {
name: workspace.getName(),
version: chalk.green(nextVersion || 'No next version'),
description,
private: $private,
location: workspace.dir,
};
});
outputFormat(workspacesToPrint, flags);
return workspaces;
}
function gitPushToRemote(
workspaces: IWorkspaceVersion[],
{
gitRemote = 'origin',
branch = 'master',
push,
}: IFlags,
): Promise<IWorkspaceVersion[]> {
log.info('git', 'Pushing tags...');
return push && workspaces.length
&& gitPush(gitRemote, branch).then(() => workspaces);
}
export async function commandGetVersionsByWorkspaces(
_args: string[],
flags: IFlags,
): Promise<IWorkspaceVersion[]> {
const opts = toWorkspacesRunOptions(_args, flags);
return getWorkspaces(opts.filterOpts)
.then(getNextVersionsByWorkspaces)
.then(workspaces => outputCommand(workspaces, flags))
.then(workspaces => workspaces.filter(({ nextVersion }) => nextVersion !== null))
.then(workspaces => updateWorkspaceVersion(workspaces, flags))
.then(workspaces => outputCommand(workspaces, flags))
.then(workspaces => gitPushToRemote(workspaces, flags));
}