1
- const { promisify } = require ( 'util' ) ;
1
+ /**
2
+ * @license
3
+ * Copyright 2017 Google LLC
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
2
18
const { resolve } = require ( 'path' ) ;
3
19
const simpleGit = require ( 'simple-git/promise' ) ;
4
- const chalk = require ( 'chalk' ) ;
5
- const globRaw = require ( 'glob' ) ;
6
20
const fs = require ( 'mz/fs' ) ;
7
21
const ora = require ( 'ora' ) ;
8
22
9
- const glob = promisify ( globRaw ) ;
10
23
const root = resolve ( __dirname , '../..' ) ;
11
24
const git = simpleGit ( root ) ;
12
25
const licenseHeader = `/**
13
26
* @license
14
- * Copyright 2020 Google Inc.
27
+ * Copyright 2020 Google LLC
15
28
*
16
29
* Licensed under the Apache License, Version 2.0 (the "License");
17
30
* you may not use this file except in compliance with the License.
@@ -28,49 +41,72 @@ const licenseHeader = `/**
28
41
29
42
` ;
30
43
31
- async function doLicenseCommit ( ) {
32
- const licenseSpinner = ora ( ' Validating License Headers' ) . start ( ) ;
44
+ const copyrightPattern = / C o p y r i g h t \d { 4 } G o o g l e ( I n c \. | L L C ) / ;
45
+ const oldCopyrightPattern = / ( \s * \* \s * C o p y r i g h t \d { 4 } ) G o o g l e I n c \. / ;
33
46
34
- const paths = await glob ( '**/*.+(ts|js)' , {
35
- ignore : [ '**/node_modules/**' , '**/dist/**' ]
47
+ async function readFiles ( paths ) {
48
+ const fileContents = await Promise . all ( paths . map ( path => fs . readFile ( path ) ) ) ;
49
+ return fileContents . map ( ( buffer , idx ) => ( {
50
+ contents : String ( buffer ) ,
51
+ path : paths [ idx ]
52
+ } ) ) ;
53
+ }
54
+
55
+ function addLicenceTag ( contents ) {
56
+ const lines = contents . split ( '\n' ) ;
57
+ let newLines = [ ] ;
58
+ for ( const line of lines ) {
59
+ if ( line . match ( copyrightPattern ) ) {
60
+ const indent = line . split ( '*' ) [ 0 ] ; // Get whitespace to match
61
+ newLines . push ( indent + '* @license' ) ;
62
+ }
63
+ newLines . push ( line ) ;
64
+ }
65
+ return newLines . join ( '\n' ) ;
66
+ }
67
+
68
+ function rewriteCopyrightLine ( contents ) {
69
+ const lines = contents . split ( '\n' ) ;
70
+ let newLines = lines . map ( line => {
71
+ return line . replace ( oldCopyrightPattern , ( _ , leader ) => {
72
+ return leader + ' Google LLC' ;
73
+ } ) ;
36
74
} ) ;
75
+ return newLines . join ( '\n' ) ;
76
+ }
37
77
38
- // Files with no license block at all.
39
- const fileContents = await Promise . all ( paths . map ( path => fs . readFile ( path ) ) ) ;
40
- const filesMissingLicensePaths = fileContents
41
- . map ( ( buffer , idx ) => ( { buffer, path : paths [ idx ] } ) )
42
- . filter (
43
- ( { buffer } ) =>
44
- String ( buffer ) . match ( / C o p y r i g h t \d { 4 } G o o g l e I n c \. / ) == null
45
- ) ;
78
+ async function doLicenseCommit ( changedFiles ) {
79
+ const licenseSpinner = ora ( ' Validating License Headers' ) . start ( ) ;
46
80
47
- await Promise . all (
48
- filesMissingLicensePaths . map ( ( { buffer, path } ) => {
49
- const contents = Buffer . concat ( [ new Buffer ( licenseHeader ) , buffer ] ) ;
50
- return fs . writeFile ( path , contents , 'utf8' ) ;
51
- } )
52
- ) ;
81
+ const paths = changedFiles . filter ( line => line . match ( / ( j s | t s ) $ / ) ) ;
82
+ if ( paths . length === 0 ) return ;
53
83
54
- // Files with no @license tag.
55
- const appendedFileContents = await Promise . all (
56
- paths . map ( path => fs . readFile ( path ) )
57
- ) ;
58
- const filesMissingTagPaths = appendedFileContents
59
- . map ( ( buffer , idx ) => ( { buffer, path : paths [ idx ] } ) )
60
- . filter ( ( { buffer } ) => String ( buffer ) . match ( / @ l i c e n s e / ) == null ) ;
84
+ const files = await readFiles ( paths ) ;
61
85
62
86
await Promise . all (
63
- filesMissingTagPaths . map ( ( { buffer, path } ) => {
64
- const lines = String ( buffer ) . split ( '\n' ) ;
65
- let newLines = [ ] ;
66
- for ( const line of lines ) {
67
- if ( line . match ( / C o p y r i g h t \d { 4 } G o o g l e I n c \. / ) ) {
68
- const indent = line . split ( '*' ) [ 0 ] ; // Get whitespace to match
69
- newLines . push ( indent + '* @license' ) ;
70
- }
71
- newLines . push ( line ) ;
87
+ files . map ( ( { contents, path } ) => {
88
+ let result = contents ;
89
+
90
+ // Files with no license block at all.
91
+ if ( result . match ( copyrightPattern ) == null ) {
92
+ result = licenseHeader + result ;
93
+ }
94
+
95
+ // Files with no @license tag.
96
+ if ( result . match ( / @ l i c e n s e / ) == null ) {
97
+ result = addLicenceTag ( result ) ;
98
+ }
99
+
100
+ // Files with the old form of copyright notice.
101
+ if ( result . match ( oldCopyrightPattern ) != null ) {
102
+ result = rewriteCopyrightLine ( result ) ;
103
+ }
104
+
105
+ if ( contents !== result ) {
106
+ return fs . writeFile ( path , result , 'utf8' ) ;
107
+ } else {
108
+ return Promise . resolve ( ) ;
72
109
}
73
- return fs . writeFile ( path , newLines . join ( '\n' ) , 'utf8' ) ;
74
110
} )
75
111
) ;
76
112
0 commit comments