1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const fs = require('fs');
|
22 | const preamble = require('./preamble');
|
23 | const pathTool = require('path');
|
24 | const chalk = require('chalk');
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const excludesPath = pathTool.join(__dirname, '../.headerignore');
|
30 | const ecBasePath = pathTool.join(__dirname, '../');
|
31 |
|
32 | const isVerbose = process.argv[2] === '--verbose';
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function run() {
|
60 | const updatedFiles = [];
|
61 | const passFiles = [];
|
62 | const pendingFiles = [];
|
63 |
|
64 | eachFile(function (absolutePath, fileExt) {
|
65 | const fileStr = fs.readFileSync(absolutePath, 'utf-8');
|
66 |
|
67 | const existLicense = preamble.extractLicense(fileStr, fileExt);
|
68 |
|
69 | if (existLicense) {
|
70 | passFiles.push(absolutePath);
|
71 | return;
|
72 | }
|
73 |
|
74 |
|
75 | if (!preamble.hasPreamble(fileExt)) {
|
76 | pendingFiles.push(absolutePath);
|
77 | return;
|
78 | }
|
79 |
|
80 | fs.writeFileSync(absolutePath, preamble.addPreamble(fileStr, fileExt), 'utf-8');
|
81 | updatedFiles.push(absolutePath);
|
82 | });
|
83 |
|
84 | console.log('\n');
|
85 | console.log('----------------------------');
|
86 | console.log(' Files that exists license: ');
|
87 | console.log('----------------------------');
|
88 | if (passFiles.length) {
|
89 | if (isVerbose) {
|
90 | passFiles.forEach(function (path) {
|
91 | console.log(chalk.green(path));
|
92 | });
|
93 | }
|
94 | else {
|
95 | console.log(chalk.green(passFiles.length + ' files. (use argument "--verbose" see details)'));
|
96 | }
|
97 | }
|
98 | else {
|
99 | console.log('Nothing.');
|
100 | }
|
101 |
|
102 | console.log('\n');
|
103 | console.log('--------------------');
|
104 | console.log(' License added for: ');
|
105 | console.log('--------------------');
|
106 | if (updatedFiles.length) {
|
107 | updatedFiles.forEach(function (path) {
|
108 | console.log(chalk.green(path));
|
109 | });
|
110 | }
|
111 | else {
|
112 | console.log('Nothing.');
|
113 | }
|
114 |
|
115 | console.log('\n');
|
116 | console.log('----------------');
|
117 | console.log(' Pending files: ');
|
118 | console.log('----------------');
|
119 | if (pendingFiles.length) {
|
120 | pendingFiles.forEach(function (path) {
|
121 | console.log(chalk.red(path));
|
122 | });
|
123 | }
|
124 | else {
|
125 | console.log('Nothing.');
|
126 | }
|
127 |
|
128 | console.log('\nDone.');
|
129 | }
|
130 |
|
131 | function eachFile(visit) {
|
132 |
|
133 | const excludePatterns = [];
|
134 | const extReg = /\.([a-zA-Z0-9_-]+)$/;
|
135 |
|
136 | prepareExcludePatterns();
|
137 | travel('./');
|
138 |
|
139 | function travel(relativePath) {
|
140 | if (isExclude(relativePath)) {
|
141 | return;
|
142 | }
|
143 |
|
144 | const absolutePath = pathTool.join(ecBasePath, relativePath);
|
145 | const stat = fs.statSync(absolutePath);
|
146 |
|
147 | if (stat.isFile()) {
|
148 | visit(absolutePath, getExt(absolutePath));
|
149 | }
|
150 | else if (stat.isDirectory()) {
|
151 | fs.readdirSync(relativePath).forEach(function (file) {
|
152 | travel(pathTool.join(relativePath, file));
|
153 | });
|
154 | }
|
155 | }
|
156 |
|
157 | function prepareExcludePatterns() {
|
158 | const content = fs.readFileSync(excludesPath, {encoding: 'utf-8'});
|
159 | content.replace(/\r/g, '\n').split('\n').forEach(function (line) {
|
160 | line = line.trim();
|
161 | if (line && line.charAt(0) !== '#') {
|
162 | excludePatterns.push(new RegExp(line));
|
163 | }
|
164 | });
|
165 | }
|
166 |
|
167 | function isExclude(relativePath) {
|
168 | for (let i = 0; i < excludePatterns.length; i++) {
|
169 | if (excludePatterns[i].test(relativePath)) {
|
170 | return true;
|
171 | }
|
172 | }
|
173 | }
|
174 |
|
175 | function getExt(path) {
|
176 | if (path) {
|
177 | const mathResult = path.match(extReg);
|
178 | return mathResult && mathResult[1];
|
179 | }
|
180 | }
|
181 | }
|
182 |
|
183 | run();
|