1
1
#! /usr/bin/env node
2
2
3
3
const fs = require ( 'fs' ) ;
4
+ const path = require ( 'path' ) ;
4
5
const program = require ( 'commander' ) ;
5
6
const username = require ( 'username' ) ;
6
7
const placeholders = require ( './placeholders' ) ;
8
+ const commentator = require ( '@captainsafia/commentator' ) ;
7
9
const firstCommitDate = require ( 'first-commit-date' ) ;
8
10
9
- const licensesPath = __dirname + '/../licenses/' ;
11
+ const licensesPath = path . join ( __dirname , '/../licenses/' ) ;
10
12
11
13
function validateLicense ( license ) {
12
14
license = license . toLowerCase ( ) ;
@@ -15,7 +17,7 @@ function validateLicense(license) {
15
17
}
16
18
17
19
program
18
- . version ( '2.0.0' )
20
+ . version ( '2.0.0' ) ;
19
21
20
22
program
21
23
. command ( 'list' ) . alias ( 'l' )
@@ -28,38 +30,86 @@ program
28
30
} ) ;
29
31
30
32
program
31
- . command ( 'put <license> [user] [year]' ) . alias ( 'p' )
33
+ . command ( 'put <license>' ) . alias ( 'p' )
34
+ . option ( '-f --file [file]' , 'The file to add a header to' )
35
+ . option ( '-u --user [user]' , 'The user/organization who holds the license' )
36
+ . option ( '-y --year [year]' , 'The year the license is in effect' )
32
37
. description ( 'Put a license in this directory' )
33
- . action ( function ( licenseArg , userArg , yearArg ) {
38
+ . action ( function ( licenseArg ) {
39
+ const cwd = process . cwd ( ) ;
40
+ const fileArg = this . file ;
41
+ var yearArg ;
42
+ const userArg = this . user || username . sync ( ) ;
43
+
44
+ if ( this . year === "range" ) {
45
+ var firstCommitYear = firstCommitDate . sync ( cwd + '/.git' ) . getFullYear ( ) ;
46
+ var currentYear = new Date ( ) . getFullYear ( ) ;
47
+ if ( currentYear === firstCommitYear ) {
48
+ yearArg = currentYear ;
49
+ } else {
50
+ yearArg = firstCommitYear + "-" + currentYear ;
51
+ }
52
+ } else {
53
+ yearArg = this . year || new Date ( ) . getFullYear ( ) ;
54
+ }
55
+
56
+ const user = placeholders [ licenseArg ] [ 'user' ] ;
57
+ const year = placeholders [ licenseArg ] [ 'year' ] ;
58
+
34
59
if ( ! validateLicense ( licenseArg ) ) {
35
60
return console . log ( 'Please choose one of the licenses under `legit list`!' ) ;
36
61
}
37
62
38
- const cwd = process . cwd ( ) ;
39
- const licenseFile = licensesPath + licenseArg ;
40
- fs . readFile ( licenseFile , 'utf8' , function ( error , data ) {
41
- if ( error ) console . log ( error ) ;
42
- if ( yearArg === "auto" ) {
43
- var firstCommitYear = firstCommitDate . sync ( cwd + '/.git' ) . getFullYear ( ) ;
44
- var currentYear = new Date ( ) . getFullYear ( ) ;
45
- if ( currentYear === firstCommitYear ) {
46
- yearArg = currentYear ;
47
- } else {
48
- yearArg = firstCommitYear + "-" + currentYear ;
49
- }
50
- } else {
51
- yearArg = yearArg || new Date ( ) . getFullYear ( ) ;
63
+ if ( fileArg ) {
64
+ const headerFile = path . join ( __dirname , '/../licenses/headers/' , licenseArg ) ;
65
+ const fileExtension = fileArg . split ( '.' ) . pop ( ) ;
66
+ if ( ! fs . existsSync ( headerFile ) ) {
67
+ return console . log ( 'Header not available for' , licenseArg , 'license!' ) ;
52
68
}
53
- userArg = userArg || username . sync ( ) ;
54
- if ( placeholders [ licenseArg ] ) {
55
- const user = placeholders [ licenseArg ] [ 'user' ] ;
56
- const year = placeholders [ licenseArg ] [ 'year' ] ;
57
- var result = data . replace ( user , userArg ) . replace ( year , yearArg ) ;
58
- }
59
- fs . writeFile ( cwd + '/LICENSE' , result || data , 'utf8' , function ( error ) {
69
+
70
+ fs . readFile ( headerFile , 'utf8' , function ( error , data ) {
71
+ if ( error ) return console . log ( error ) ;
72
+ var result ;
73
+
74
+ try {
75
+ result = commentator . makeBlockComment (
76
+ data . replace ( user , userArg ) . replace ( year , yearArg ) , fileExtension ) ;
77
+ } catch ( error ) {
78
+ if ( error . message . includes ( 'Block comment' ) ) {
79
+ try {
80
+ result = commentator . makeInlineComment (
81
+ data . replace ( user , userArg ) . replace ( year , yearArg ) , fileExtension ) ;
82
+ } catch ( error ) {
83
+ if ( error . message . includes ( 'Inline comment' ) ) {
84
+ return console . log ( '@captainsafia/commentator doesn\'t support block comments' ,
85
+ 'for files with this extension, please open an issue at https://github.com/captainsafia/commentator/issues' ,
86
+ 'to add support for this programming language.' ) ;
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ const filePath = path . join ( cwd , '/' , fileArg ) ;
93
+
94
+ fs . readFile ( filePath , 'utf8' , function ( error , data ) {
95
+ const newData = result + '\n' + data ;
96
+ fs . writeFile ( filePath , newData , 'utf8' , function ( error ) {
97
+ if ( error ) return console . log ( error ) ;
98
+ } ) ;
99
+ } ) ;
100
+ } ) ;
101
+ } else {
102
+ const licenseFile = licensesPath + licenseArg ;
103
+ fs . readFile ( licenseFile , 'utf8' , function ( error , data ) {
60
104
if ( error ) return console . log ( error ) ;
105
+ if ( placeholders [ licenseArg ] ) {
106
+ var result = data . replace ( user , userArg ) . replace ( year , yearArg ) ;
107
+ }
108
+ fs . writeFile ( path . join ( cwd , '/LICENSE' ) , result || data , 'utf8' , function ( error ) {
109
+ if ( error ) return console . log ( error ) ;
110
+ } ) ;
61
111
} ) ;
62
- } ) ;
112
+ }
63
113
} ) ;
64
114
65
- program . parse ( process . argv ) ;
115
+ program . parse ( process . argv ) ;
0 commit comments