1
+ const process = require ( 'process' ) ;
1
2
const { template} = require ( 'lodash' ) ;
2
3
const marked = require ( 'marked' ) ;
3
4
const TerminalRenderer = require ( 'marked-terminal' ) ;
@@ -19,47 +20,47 @@ const {COMMIT_NAME, COMMIT_EMAIL} = require('./lib/definitions/constants');
19
20
20
21
marked . setOptions ( { renderer : new TerminalRenderer ( ) } ) ;
21
22
22
- async function run ( options , plugins ) {
23
- const { isCi, branch, isPr} = envCi ( ) ;
23
+ async function run ( context , plugins ) {
24
+ const { isCi, branch : ciBranch , isPr} = envCi ( ) ;
25
+ const { cwd, env, options, logger} = context ;
24
26
25
27
if ( ! isCi && ! options . dryRun && ! options . noCi ) {
26
28
logger . log ( 'This run was not triggered in a known CI environment, running in dry-run mode.' ) ;
27
29
options . dryRun = true ;
28
30
} else {
29
31
// When running on CI, set the commits author and commiter info and prevent the `git` CLI to prompt for username/password. See #703.
30
- process . env = {
32
+ Object . assign ( env , {
31
33
GIT_AUTHOR_NAME : COMMIT_NAME ,
32
34
GIT_AUTHOR_EMAIL : COMMIT_EMAIL ,
33
35
GIT_COMMITTER_NAME : COMMIT_NAME ,
34
36
GIT_COMMITTER_EMAIL : COMMIT_EMAIL ,
35
- ...process . env ,
36
37
GIT_ASKPASS : 'echo' ,
37
38
GIT_TERMINAL_PROMPT : 0 ,
38
- } ;
39
+ } ) ;
39
40
}
40
41
41
42
if ( isCi && isPr && ! options . noCi ) {
42
43
logger . log ( "This run was triggered by a pull request and therefore a new version won't be published." ) ;
43
44
return ;
44
45
}
45
46
46
- if ( branch !== options . branch ) {
47
+ if ( ciBranch !== options . branch ) {
47
48
logger . log (
48
- `This test run was triggered on the branch ${ branch } , while semantic-release is configured to only publish from ${
49
+ `This test run was triggered on the branch ${ ciBranch } , while semantic-release is configured to only publish from ${
49
50
options . branch
50
51
} , therefore a new version won’t be published.`
51
52
) ;
52
53
return false ;
53
54
}
54
55
55
- await verify ( options ) ;
56
+ await verify ( context ) ;
56
57
57
- options . repositoryUrl = await getGitAuthUrl ( options ) ;
58
+ options . repositoryUrl = await getGitAuthUrl ( context ) ;
58
59
59
60
try {
60
- await verifyAuth ( options . repositoryUrl , options . branch ) ;
61
+ await verifyAuth ( options . repositoryUrl , options . branch , { cwd , env } ) ;
61
62
} catch ( err ) {
62
- if ( ! ( await isBranchUpToDate ( options . branch ) ) ) {
63
+ if ( ! ( await isBranchUpToDate ( options . branch , { cwd , env } ) ) ) {
63
64
logger . log (
64
65
"The local branch %s is behind the remote one, therefore a new version won't be published." ,
65
66
options . branch
@@ -72,92 +73,93 @@ async function run(options, plugins) {
72
73
73
74
logger . log ( 'Run automated release from branch %s' , options . branch ) ;
74
75
75
- await plugins . verifyConditions ( { options , logger } ) ;
76
+ await plugins . verifyConditions ( context ) ;
76
77
77
- await fetch ( options . repositoryUrl ) ;
78
+ await fetch ( options . repositoryUrl , { cwd , env } ) ;
78
79
79
- const lastRelease = await getLastRelease ( options . tagFormat , logger ) ;
80
- const commits = await getCommits ( lastRelease . gitHead , options . branch , logger ) ;
80
+ context . lastRelease = await getLastRelease ( context ) ;
81
+ context . commits = await getCommits ( context ) ;
81
82
82
- const type = await plugins . analyzeCommits ( { options, logger, lastRelease, commits} ) ;
83
- if ( ! type ) {
83
+ const nextRelease = { type : await plugins . analyzeCommits ( context ) , gitHead : await getGitHead ( { cwd, env} ) } ;
84
+
85
+ if ( ! nextRelease . type ) {
84
86
logger . log ( 'There are no relevant changes, so no new version is released.' ) ;
85
87
return ;
86
88
}
87
- const version = getNextVersion ( type , lastRelease , logger ) ;
88
- const nextRelease = { type, version, gitHead : await getGitHead ( ) , gitTag : template ( options . tagFormat ) ( { version} ) } ;
89
-
90
- await plugins . verifyRelease ( { options, logger, lastRelease, commits, nextRelease} ) ;
89
+ context . nextRelease = nextRelease ;
90
+ nextRelease . version = getNextVersion ( context ) ;
91
+ nextRelease . gitTag = template ( options . tagFormat ) ( { version : nextRelease . version } ) ;
91
92
92
- const generateNotesParam = { options , logger , lastRelease , commits , nextRelease } ;
93
+ await plugins . verifyRelease ( context ) ;
93
94
94
95
if ( options . dryRun ) {
95
- const notes = await plugins . generateNotes ( generateNotesParam ) ;
96
+ const notes = await plugins . generateNotes ( context ) ;
96
97
logger . log ( 'Release note for version %s:\n' , nextRelease . version ) ;
97
98
if ( notes ) {
98
- process . stdout . write ( `${ marked ( notes ) } \n` ) ;
99
+ logger . stdout ( `${ marked ( notes ) } \n` ) ;
99
100
}
100
101
} else {
101
- nextRelease . notes = await plugins . generateNotes ( generateNotesParam ) ;
102
- await plugins . prepare ( { options , logger , lastRelease , commits , nextRelease } ) ;
102
+ nextRelease . notes = await plugins . generateNotes ( context ) ;
103
+ await plugins . prepare ( context ) ;
103
104
104
105
// Create the tag before calling the publish plugins as some require the tag to exists
105
106
logger . log ( 'Create tag %s' , nextRelease . gitTag ) ;
106
- await tag ( nextRelease . gitTag ) ;
107
- await push ( options . repositoryUrl , branch ) ;
107
+ await tag ( nextRelease . gitTag , { cwd , env } ) ;
108
+ await push ( options . repositoryUrl , options . branch , { cwd , env } ) ;
108
109
109
- const releases = await plugins . publish ( { options , logger , lastRelease , commits , nextRelease } ) ;
110
+ context . releases = await plugins . publish ( context ) ;
110
111
111
- await plugins . success ( { options , logger , lastRelease , commits , nextRelease , releases } ) ;
112
+ await plugins . success ( context ) ;
112
113
113
114
logger . log ( 'Published release: %s' , nextRelease . version ) ;
114
115
}
115
116
return true ;
116
117
}
117
118
118
- function logErrors ( err ) {
119
+ function logErrors ( { logger } , err ) {
119
120
const errors = extractErrors ( err ) . sort ( error => ( error . semanticRelease ? - 1 : 0 ) ) ;
120
121
for ( const error of errors ) {
121
122
if ( error . semanticRelease ) {
122
123
logger . log ( `%s ${ error . message } ` , error . code ) ;
123
124
if ( error . details ) {
124
- process . stdout . write ( `${ marked ( error . details ) } \n` ) ;
125
+ logger . stderr ( `${ marked ( error . details ) } \n` ) ;
125
126
}
126
127
} else {
127
128
logger . error ( 'An error occurred while running semantic-release: %O' , error ) ;
128
129
}
129
130
}
130
131
}
131
132
132
- async function callFail ( plugins , options , error ) {
133
+ async function callFail ( context , plugins , error ) {
133
134
const errors = extractErrors ( error ) . filter ( error => error . semanticRelease ) ;
134
135
if ( errors . length > 0 ) {
135
136
try {
136
- await plugins . fail ( { options , logger , errors} ) ;
137
+ await plugins . fail ( { ... context , errors} ) ;
137
138
} catch ( err ) {
138
- logErrors ( err ) ;
139
+ logErrors ( context , err ) ;
139
140
}
140
141
}
141
142
}
142
143
143
- module . exports = async opts => {
144
- logger . log ( `Running %s version %s` , pkg . name , pkg . version ) ;
145
- const { unhook} = hookStd ( { silent : false } , hideSensitive ) ;
144
+ module . exports = async ( opts , { cwd = process . cwd ( ) , env = process . env } = { } ) => {
145
+ const context = { cwd, env, logger} ;
146
+ context . logger . log ( `Running %s version %s` , pkg . name , pkg . version ) ;
147
+ const { unhook} = hookStd ( { silent : false } , hideSensitive ( context . env ) ) ;
146
148
try {
147
- const config = await getConfig ( opts , logger ) ;
148
- const { plugins , options} = config ;
149
+ const { plugins , options } = await getConfig ( context , opts ) ;
150
+ context . options = options ;
149
151
try {
150
- const result = await run ( options , plugins ) ;
152
+ const result = await run ( context , plugins ) ;
151
153
unhook ( ) ;
152
154
return result ;
153
155
} catch ( err ) {
154
156
if ( ! options . dryRun ) {
155
- await callFail ( plugins , options , err ) ;
157
+ await callFail ( context , plugins , err ) ;
156
158
}
157
159
throw err ;
158
160
}
159
161
} catch ( err ) {
160
- logErrors ( err ) ;
162
+ logErrors ( context , err ) ;
161
163
unhook ( ) ;
162
164
throw err ;
163
165
}
0 commit comments