UNPKG

5.32 kBJavaScriptView Raw
1/*
2* Licensed to the Apache Software Foundation (ASF) under one
3* or more contributor license agreements. See the NOTICE file
4* distributed with this work for additional information
5* regarding copyright ownership. The ASF licenses this file
6* to you under the Apache License, Version 2.0 (the
7* "License"); you may not use this file except in compliance
8* with the License. You may obtain a copy of the License at
9*
10* http://www.apache.org/licenses/LICENSE-2.0
11*
12* Unless required by applicable law or agreed to in writing,
13* software distributed under the License is distributed on an
14* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15* KIND, either express or implied. See the License for the
16* specific language governing permissions and limitations
17* under the License.
18*/
19
20
21const fs = require('fs');
22const preamble = require('./preamble');
23const pathTool = require('path');
24const chalk = require('chalk');
25
26// In the `.headerignore`, each line is a pattern in RegExp.
27// all relative path (based on the echarts base directory) is tested.
28// The pattern should match the relative path completely.
29const excludesPath = pathTool.join(__dirname, '../.headerignore');
30const ecBasePath = pathTool.join(__dirname, '../');
31
32const isVerbose = process.argv[2] === '--verbose';
33
34// const lists = [
35// '../src/**/*.js',
36// '../build/*.js',
37// '../benchmark/src/*.js',
38// '../benchmark/src/gulpfile.js',
39// '../extension-src/**/*.js',
40// '../extension/**/*.js',
41// '../map/js/**/*.js',
42// '../test/build/**/*.js',
43// '../test/node/**/*.js',
44// '../test/ut/core/*.js',
45// '../test/ut/spe/*.js',
46// '../test/ut/ut.js',
47// '../test/*.js',
48// '../theme/*.js',
49// '../theme/tool/**/*.js',
50// '../echarts.all.js',
51// '../echarts.blank.js',
52// '../echarts.common.js',
53// '../echarts.simple.js',
54// '../index.js',
55// '../index.common.js',
56// '../index.simple.js'
57// ];
58
59function 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 // Conside binary files, only add for files with known ext.
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
131function 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
183run();