1
- const path = require ( 'path' ) ;
2
- const { pathExists} = require ( 'fs-extra' ) ;
3
- const { isUndefined, isPlainObject, isArray, template, castArray, uniq} = require ( 'lodash' ) ;
4
- const micromatch = require ( 'micromatch' ) ;
5
- const dirGlob = require ( 'dir-glob' ) ;
6
- const pReduce = require ( 'p-reduce' ) ;
1
+ const { template} = require ( 'lodash' ) ;
7
2
const debug = require ( 'debug' ) ( 'semantic-release:git' ) ;
8
3
const resolveConfig = require ( './resolve-config' ) ;
9
- const { getModifiedFiles, add, commit, push} = require ( './git' ) ;
10
-
11
- const CHANGELOG = 'CHANGELOG.md' ;
12
- const PACKAGE_JSON = 'package.json' ;
13
- const PACKAGE_LOCK_JSON = 'package-lock.json' ;
14
- const SHRINKWRAP_JSON = 'npm-shrinkwrap.json' ;
15
-
16
- // TODO Temporary workaround for https://github.com/kevva/dir-glob/issues/7
17
- const resolvePaths = ( files , cwd ) =>
18
- files . map (
19
- file => `${ file . startsWith ( '!' ) ? '!' : '' } ${ path . resolve ( cwd , file . startsWith ( '!' ) ? file . slice ( 1 ) : file ) } `
20
- ) ;
4
+ const globAssets = require ( './glob-assets.js' ) ;
5
+ const { filterModifiedFiles, add, commit, push} = require ( './git' ) ;
21
6
22
7
/**
23
8
* Prepare a release commit including configurable files.
@@ -31,75 +16,36 @@ const resolvePaths = (files, cwd) =>
31
16
* @param {Object } context.nextRelease The next release.
32
17
* @param {Object } logger Global logger.
33
18
*/
34
- module . exports = async (
35
- pluginConfig ,
36
- { env, cwd, options : { branch, repositoryUrl} , lastRelease, nextRelease, logger}
37
- ) => {
19
+ module . exports = async ( pluginConfig , context ) => {
20
+ const {
21
+ env,
22
+ cwd,
23
+ options : { branch, repositoryUrl} ,
24
+ lastRelease,
25
+ nextRelease,
26
+ logger,
27
+ } = context ;
38
28
const { message, assets} = resolveConfig ( pluginConfig , logger ) ;
39
- const patterns = [ ] ;
40
- const modifiedFiles = await getModifiedFiles ( { env, cwd} ) ;
41
- const changelogPath = path . resolve ( cwd , CHANGELOG ) ;
42
- const pkgPath = path . resolve ( cwd , PACKAGE_JSON ) ;
43
- const pkgLockPath = path . resolve ( cwd , PACKAGE_LOCK_JSON ) ;
44
- const shrinkwrapPath = path . resolve ( cwd , SHRINKWRAP_JSON ) ;
45
-
46
- if ( isUndefined ( assets ) && ( await pathExists ( changelogPath ) ) ) {
47
- logger . log ( 'Add %s to the release commit' , changelogPath ) ;
48
- patterns . push ( changelogPath ) ;
49
- }
50
- if ( isUndefined ( assets ) && ( await pathExists ( pkgPath ) ) ) {
51
- logger . log ( 'Add %s to the release commit' , pkgPath ) ;
52
- patterns . push ( pkgPath ) ;
53
- }
54
- if ( isUndefined ( assets ) && ( await pathExists ( pkgLockPath ) ) ) {
55
- logger . log ( 'Add %s to the release commit' , pkgLockPath ) ;
56
- patterns . push ( pkgLockPath ) ;
57
- }
58
- if ( isUndefined ( assets ) && ( await pathExists ( shrinkwrapPath ) ) ) {
59
- logger . log ( 'Add %s to the release commit' , shrinkwrapPath ) ;
60
- patterns . push ( shrinkwrapPath ) ;
61
- }
62
-
63
- patterns . push (
64
- ...( assets || [ ] ) . map ( pattern => ( ! isArray ( pattern ) && isPlainObject ( pattern ) ? pattern . path : pattern ) )
65
- ) ;
66
29
67
- const filesToCommit = uniq (
68
- await pReduce (
69
- patterns ,
70
- async ( result , pattern ) => {
71
- const glob = castArray ( pattern ) ;
72
- let nonegate ;
73
- // Skip solo negated pattern (avoid to include every non js file with `!**/*.js`)
74
- if ( glob . length <= 1 && glob [ 0 ] . startsWith ( '!' ) ) {
75
- nonegate = true ;
76
- debug (
77
- 'skipping the negated glob %o as its alone in its group and would retrieve a large amount of files ' ,
78
- glob [ 0 ]
79
- ) ;
80
- }
81
- result . push (
82
- ...micromatch ( resolvePaths ( modifiedFiles , cwd ) , await dirGlob ( resolvePaths ( glob , cwd ) ) , { dot : true , nonegate} )
83
- ) ;
84
- return result ;
85
- } ,
86
- [ ]
87
- )
88
- ) ;
89
-
90
- if ( filesToCommit . length > 0 ) {
91
- logger . log ( 'Found %d file(s) to commit' , filesToCommit . length ) ;
92
- await add ( filesToCommit , { env, cwd} ) ;
93
- debug ( 'commited files: %o' , filesToCommit ) ;
94
- await commit (
95
- message
96
- ? template ( message ) ( { branch, lastRelease, nextRelease} )
97
- : `chore(release): ${ nextRelease . version } [skip ci]\n\n${ nextRelease . notes } ` ,
98
- { env, cwd}
99
- ) ;
30
+ if ( assets && assets . length > 0 ) {
31
+ const globbedAssets = await globAssets ( context , assets ) ;
32
+ debug ( 'globed assets: %o' , globbedAssets ) ;
33
+
34
+ const filesToCommit = await filterModifiedFiles ( globbedAssets , { cwd, env} ) ;
35
+
36
+ if ( filesToCommit . length > 0 ) {
37
+ logger . log ( 'Found %d file(s) to commit' , filesToCommit . length ) ;
38
+ await add ( filesToCommit , { env, cwd} ) ;
39
+ debug ( 'commited files: %o' , filesToCommit ) ;
40
+ await commit (
41
+ message
42
+ ? template ( message ) ( { branch, lastRelease, nextRelease} )
43
+ : `chore(release): ${ nextRelease . version } [skip ci]\n\n${ nextRelease . notes } ` ,
44
+ { env, cwd}
45
+ ) ;
46
+ }
47
+
48
+ await push ( repositoryUrl , branch , { env, cwd} ) ;
49
+ logger . log ( 'Prepared Git release: %s' , nextRelease . gitTag ) ;
100
50
}
101
-
102
- logger . log ( 'Creating tag %s' , nextRelease . gitTag ) ;
103
- await push ( repositoryUrl , branch , { env, cwd} ) ;
104
- logger . log ( 'Prepared Git release: %s' , nextRelease . gitTag ) ;
105
51
} ;
0 commit comments