1
1
#!/usr/bin/env node
2
- 'use strict' ;
2
+ 'use strict'
3
3
4
- var hogan = require ( " hogan.js" )
5
- var fs = require ( "fs" )
6
- var path = require ( " path" )
7
- var util = require ( " util" )
4
+ var hogan = require ( ' hogan.js' )
5
+ var fs = require ( 'fs' )
6
+ var path = require ( ' path' )
7
+ var util = require ( ' util' )
8
8
var argv = require ( 'yargs' )
9
9
. usage ( 'Usage: readme path/to/package.json' )
10
- . check ( function ( argv ) {
11
- if ( ! argv . _ . length ) throw 'A path to a valid package.json is required' ;
12
- return true ;
10
+ . check ( function ( argv ) {
11
+ if ( ! argv . _ . length ) throw new Error ( 'A path to a valid package.json is required' )
12
+ return true
13
13
} )
14
14
. option ( 'r' , {
15
15
alias : 'travis' ,
@@ -19,80 +19,75 @@ var argv = require('yargs')
19
19
alias : 'test' ,
20
20
description : 'include test output in readme'
21
21
} )
22
- . option ( 'n' , {
23
- alias : 'no-footer' ,
24
- description : 'disable the promotional footer message'
25
- } )
26
22
. alias ( 't' , 'tests' )
27
23
. help ( 'help' )
28
24
. alias ( 'h' , 'help' )
29
- . argv ;
30
- var gh = require ( " github-url-to-object" )
31
- var execSync = require ( 'sync-exec' ) ;
32
- var stripAnsi = require ( 'strip-ansi' ) ;
25
+ . argv
26
+ var gh = require ( ' github-url-to-object' )
27
+ var execSync = require ( 'sync-exec' )
28
+ var stripAnsi = require ( 'strip-ansi' )
33
29
34
30
var pkgPath = path . resolve ( process . cwd ( ) , argv . _ [ 0 ] )
35
31
36
32
try {
37
33
var pkg = require ( pkgPath )
38
- } catch ( e ) {
39
- return console . error ( "Invalid JSON file: %s" , pkgPath )
34
+ } catch ( e ) {
35
+ console . error ( 'Invalid JSON file: %s' , pkgPath )
36
+ process . exit ( )
40
37
}
41
38
42
- pkg . private = pkg . private || pkg . license === " private" || false ;
39
+ pkg . private = pkg . private || pkg . license === ' private' || false
43
40
44
41
if ( argv . travis ) {
45
42
if ( pkg . repository && pkg . repository . url && gh ( pkg . repository . url ) ) {
46
43
pkg . travis_url = gh ( pkg . repository . url ) . travis_url
47
44
} else {
48
- return console . error ( "`repository.url` must be a GitHub repository URL for Travis to work" )
45
+ console . error ( '`repository.url` must be a GitHub repository URL for Travis to work' )
46
+ process . exit ( )
49
47
}
50
48
}
51
49
52
50
// Run tests and fetch output
53
51
if ( argv . tests || argv . test ) {
54
52
pkg . testOutput = stripAnsi ( execSync ( 'npm test' ) . stdout )
55
- . replace ( / \r / g, "" ) // remove weird newlines
56
- . replace ( / \n + / g, "\n" ) ; // remove excess newlines
53
+ . replace ( / \r / g, '' ) // remove weird newlines
54
+ . replace ( / \n + / g, '\n' ) // remove excess newlines
57
55
}
58
56
59
57
// Look for example.js or example.sh in package.json directory
60
- [ "js" , "sh" ] . forEach ( function ( language ) {
61
-
62
- var exampleFile = path . resolve ( path . dirname ( argv . _ [ 0 ] ) ) + " /example." + language ;
58
+ var extensions = [ 'js' , 'sh' ]
59
+ extensions . forEach ( function ( language ) {
60
+ var exampleFile = path . resolve ( path . dirname ( argv . _ [ 0 ] ) ) + ' /example.' + language
63
61
if ( fs . existsSync ( exampleFile ) ) {
64
62
pkg . usage = {
65
63
language : language ,
66
64
content : fs . readFileSync ( exampleFile ) . toString ( )
67
65
}
68
66
69
67
// replace require('./') statement with the package name
70
- if ( language === "js" ) {
68
+ if ( language === 'js' ) {
71
69
pkg . usage . content = pkg . usage . content . replace (
72
70
/ r e q u i r e \( [ ' " ] ? \. \/ [ ' " ] ? \) / ,
73
- util . format ( " require(\ "%s\")" , pkg . name )
71
+ util . format ( ' require("%s")' , pkg . name )
74
72
)
75
73
}
76
-
77
74
}
78
75
} )
79
76
80
- // Disable generated-by footer with --no-footer
81
- pkg . footer = argv [ 'footer' ] !== false
82
-
83
- var getDeps = function ( deps ) {
84
- return Object . keys ( deps ) . map ( function ( depname ) {
85
- var dep = require ( path . resolve ( path . dirname ( argv . _ [ 0 ] ) ) + "/node_modules/" + depname + "/package.json" )
77
+ var getDeps = function ( deps ) {
78
+ return Object . keys ( deps ) . map ( function ( depname ) {
79
+ var dep = require ( path . resolve ( path . dirname ( argv . _ [ 0 ] ) ) + '/node_modules/' + depname + '/package.json' )
86
80
if ( dep . repository && dep . repository . url && gh ( dep . repository . url ) ) {
87
81
dep . repository . url = gh ( dep . repository . url ) . https_url
88
82
}
89
83
return dep
90
84
} )
91
85
}
92
86
93
- if ( pkg . dependencies ) pkg . depDetails = getDeps ( pkg . dependencies ) ;
94
- if ( pkg . devDependencies ) pkg . devDepDetails = getDeps ( pkg . devDependencies ) ;
87
+ if ( pkg . dependencies ) pkg . depDetails = getDeps ( pkg . dependencies )
88
+ if ( pkg . devDependencies ) pkg . devDepDetails = getDeps ( pkg . devDependencies )
95
89
96
- var template = hogan . compile ( fs . readFileSync ( __dirname + "/template.md" ) . toString ( ) )
90
+ var templatePath = path . join ( __dirname , 'template.md' )
91
+ var template = hogan . compile ( fs . readFileSync ( templatePath ) . toString ( ) )
97
92
98
93
process . stdout . write ( template . render ( pkg ) )
0 commit comments