@@ -7,14 +7,17 @@ const detectReadmeBadges = require('detect-readme-badges');
7
7
const detectRepoChangelog = require ( 'detect-repo-changelog' ) ;
8
8
const fetchCoverage = require ( 'fetch-coverage' ) ;
9
9
const loadJsonFile = require ( 'load-json-file' ) ;
10
- const readFile = Promise . promisify ( require ( 'fs' ) . readFile ) ;
11
10
const deepCompact = require ( 'deep-compact' ) ;
12
11
const isRegularFile = require ( 'is-regular-file' ) ;
13
12
const got = require ( 'got' ) ;
14
13
const fileSize = require ( './util/fileSize' ) ;
15
14
const promisePropsSettled = require ( './util/promisePropsSettled' ) ;
16
15
const exec = require ( '../util/exec' ) ;
17
16
const gotRetry = require ( '../util/gotRetry' ) ;
17
+ const fileContents = require ( './util/fileContents' ) ;
18
+ const uniqWith = require ( 'lodash/uniqWith' ) ;
19
+ const isEqual = require ( 'lodash/isEqual' ) ;
20
+ const { uniq } = require ( 'lodash' ) ;
18
21
19
22
const davidBin = path . normalize ( `${ __dirname } /bin/david-json` ) ;
20
23
const log = logger . child ( { module : 'collect/source' } ) ;
@@ -28,8 +31,11 @@ const log = logger.child({ module: 'collect/source' });
28
31
* @returns {Promise } The promise for the inspection result.
29
32
*/
30
33
function inspectFiles ( data , downloaded ) {
31
- // Readme must be located in the package dir
32
- const readmeSize = data . readmeFilename ? fileSize ( `${ downloaded . packageDir } /${ data . readmeFilename } ` ) : 0 ;
34
+ // Sum readme file sizes of the one in the package dir with the root
35
+ const readmeSize = fileSize ( [
36
+ `${ downloaded . packageDir } /${ data . readmeFilename || 'README.md' } ` ,
37
+ downloaded . dir !== downloaded . packageDir ? `${ downloaded . DIR } /${ data . readmeFilename || 'README.md' } ` : '' ,
38
+ ] . filter ( ( path ) => path ) ) ;
33
39
// Prefer tests located in the package dir and fallback to the root
34
40
const testsSize = (
35
41
detectRepoTestFiles ( downloaded . packageDir )
@@ -64,20 +70,17 @@ function inspectFiles(data, downloaded) {
64
70
* @returns {Promise } The promise for the badges result.
65
71
*/
66
72
function getReadmeBadges ( data , downloaded ) {
67
- // Prefer README badges from the package dir but usually badges are at the root README
68
- // Need to use typeof because there's some old packages in which the README is an object, e.g.: `flatsite`
69
- return Promise . try ( ( ) => typeof data . readme === 'string' ? detectReadmeBadges ( data . readme ) : [ ] )
70
- . then ( ( badges ) => {
71
- if ( ! badges . length && downloaded . dir !== downloaded . packageDir && data . readmeFilename ) {
72
- return readFile ( `${ downloaded . dir } /${ data . readmeFilename } ` )
73
- . then ( ( readme ) => detectReadmeBadges ( readme . toString ( ) ) )
74
- // Ignore if file does not exist or is actually a directory
75
- . catch ( { code : 'ENOENT' } , ( ) => [ ] )
76
- . catch ( { code : 'EISDIR' } , ( ) => [ ] ) ;
77
- }
78
-
79
- return badges ;
80
- } ) ;
73
+ // Use badges from both the package dir and root README
74
+ return Promise . props ( {
75
+ onPackageDir : typeof data . readme === 'string' && data . readme ? data . readme : fileContents ( `${ downloaded . packageDir } /${ data . readmeFilename || 'README.md' } ` ) ,
76
+ onRoot : downloaded . dir !== downloaded . packageDir ? fileContents ( `${ downloaded . dir } /${ data . readmeFilename || 'README.md' } ` ) : '' ,
77
+ } )
78
+ . then ( ( readmes ) => Promise . props ( {
79
+ onRoot : detectReadmeBadges ( readmes . onRoot || '' ) ,
80
+ onPackageDir : detectReadmeBadges ( readmes . onPackageDir || '' ) ,
81
+ } ) )
82
+ // Cleanup duplicates
83
+ . then ( ( badges ) => uniqWith ( [ ...badges . onPackageDir , ...badges . onRoot ] , isEqual ) ) ;
81
84
}
82
85
83
86
/**
@@ -88,23 +91,22 @@ function getReadmeBadges(data, downloaded) {
88
91
* @returns {Promise } The promise for the linters result.
89
92
*/
90
93
function getRepoLinters ( downloaded ) {
91
- // Linters usually are at the root but prefer the ones within the package just in case..
92
- return detectRepoLinters ( downloaded . packageDir )
93
- . then ( ( linters ) => {
94
- if ( linters . length || downloaded . dir === downloaded . packageDir ) {
95
- return linters ;
96
- }
97
-
98
- return detectRepoLinters ( downloaded . dir )
99
- // A JSON error might occur if `detect-repo-linters`fails to parse `package.json` as JSON
100
- // Since the `package.json` at the root was not validated, it can have errors
101
- // If that's the case, we want to skip them here
102
- . catch ( { name : 'JSONError' } , ( ) => {
103
- log . warn ( { dir : downloaded . dir } , 'Error reading downloaded package.json when scanning for linters' ) ;
94
+ // Linters usually are at the root but we consider both just in case..
95
+ return Promise . props ( {
96
+ onPackageDir : detectRepoLinters ( downloaded . packageDir ) ,
97
+ onRootDir : downloaded . dir !== downloaded . packageDir ?
98
+ detectRepoLinters ( downloaded . dir )
99
+ // A JSON error might occur if `detect-repo-linters`fails to parse `package.json` as JSON
100
+ // Since the `package.json` at the root was not validated, it can have errors
101
+ // If that's the case, we want to skip them here
102
+ . catch ( { name : 'JSONError' } , ( ) => {
103
+ log . warn ( { dir : downloaded . dir } , 'Error reading downloaded package.json when scanning for linters' ) ;
104
104
105
- return [ ] ;
106
- } ) ;
107
- } ) ;
105
+ return [ ] ;
106
+ } ) :
107
+ [ ] ,
108
+ } )
109
+ . then ( ( linters ) => uniq ( [ ...linters . onPackageDir , ...linters . onRootDir ] ) ) ;
108
110
}
109
111
110
112
/**
0 commit comments