addHeader.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. const fs = require('fs');
  20. const preamble = require('./preamble');
  21. const pathTool = require('path');
  22. const chalk = require('chalk');
  23. // In the `.headerignore`, each line is a pattern in RegExp.
  24. // all relative path (based on the echarts base directory) is tested.
  25. // The pattern should match the relative path completely.
  26. const excludesPath = pathTool.join(__dirname, '../.headerignore');
  27. const ecBasePath = pathTool.join(__dirname, '../');
  28. const isVerbose = process.argv[2] === '--verbose';
  29. // const lists = [
  30. // '../src/**/*.js',
  31. // '../build/*.js',
  32. // '../benchmark/src/*.js',
  33. // '../benchmark/src/gulpfile.js',
  34. // '../extension-src/**/*.js',
  35. // '../extension/**/*.js',
  36. // '../map/js/**/*.js',
  37. // '../test/build/**/*.js',
  38. // '../test/node/**/*.js',
  39. // '../test/ut/core/*.js',
  40. // '../test/ut/spe/*.js',
  41. // '../test/ut/ut.js',
  42. // '../test/*.js',
  43. // '../theme/*.js',
  44. // '../theme/tool/**/*.js',
  45. // '../echarts.all.js',
  46. // '../echarts.blank.js',
  47. // '../echarts.common.js',
  48. // '../echarts.simple.js',
  49. // '../index.js',
  50. // '../index.common.js',
  51. // '../index.simple.js'
  52. // ];
  53. function run() {
  54. const updatedFiles = [];
  55. const passFiles = [];
  56. const pendingFiles = [];
  57. eachFile(function (absolutePath, fileExt) {
  58. const fileStr = fs.readFileSync(absolutePath, 'utf-8');
  59. const existLicense = preamble.extractLicense(fileStr, fileExt);
  60. if (existLicense) {
  61. passFiles.push(absolutePath);
  62. return;
  63. }
  64. // Conside binary files, only add for files with known ext.
  65. if (!preamble.hasPreamble(fileExt)) {
  66. pendingFiles.push(absolutePath);
  67. return;
  68. }
  69. fs.writeFileSync(absolutePath, preamble.addPreamble(fileStr, fileExt), 'utf-8');
  70. updatedFiles.push(absolutePath);
  71. });
  72. console.log('\n');
  73. console.log('----------------------------');
  74. console.log(' Files that exists license: ');
  75. console.log('----------------------------');
  76. if (passFiles.length) {
  77. if (isVerbose) {
  78. passFiles.forEach(function (path) {
  79. console.log(chalk.green(path));
  80. });
  81. }
  82. else {
  83. console.log(chalk.green(passFiles.length + ' files. (use argument "--verbose" see details)'));
  84. }
  85. }
  86. else {
  87. console.log('Nothing.');
  88. }
  89. console.log('\n');
  90. console.log('--------------------');
  91. console.log(' License added for: ');
  92. console.log('--------------------');
  93. if (updatedFiles.length) {
  94. updatedFiles.forEach(function (path) {
  95. console.log(chalk.green(path));
  96. });
  97. }
  98. else {
  99. console.log('Nothing.');
  100. }
  101. console.log('\n');
  102. console.log('----------------');
  103. console.log(' Pending files: ');
  104. console.log('----------------');
  105. if (pendingFiles.length) {
  106. pendingFiles.forEach(function (path) {
  107. console.log(chalk.red(path));
  108. });
  109. }
  110. else {
  111. console.log('Nothing.');
  112. }
  113. console.log('\nDone.');
  114. }
  115. function eachFile(visit) {
  116. const excludePatterns = [];
  117. const extReg = /\.([a-zA-Z0-9_-]+)$/;
  118. prepareExcludePatterns();
  119. travel('./');
  120. function travel(relativePath) {
  121. if (isExclude(relativePath)) {
  122. return;
  123. }
  124. const absolutePath = pathTool.join(ecBasePath, relativePath);
  125. const stat = fs.statSync(absolutePath);
  126. if (stat.isFile()) {
  127. visit(absolutePath, getExt(absolutePath));
  128. }
  129. else if (stat.isDirectory()) {
  130. fs.readdirSync(relativePath).forEach(function (file) {
  131. travel(pathTool.join(relativePath, file));
  132. });
  133. }
  134. }
  135. function prepareExcludePatterns() {
  136. const content = fs.readFileSync(excludesPath, {encoding: 'utf-8'});
  137. content.replace(/\r/g, '\n').split('\n').forEach(function (line) {
  138. line = line.trim();
  139. if (line && line.charAt(0) !== '#') {
  140. excludePatterns.push(new RegExp(line));
  141. }
  142. });
  143. }
  144. function isExclude(relativePath) {
  145. for (let i = 0; i < excludePatterns.length; i++) {
  146. if (excludePatterns[i].test(relativePath)) {
  147. return true;
  148. }
  149. }
  150. }
  151. function getExt(path) {
  152. if (path) {
  153. const mathResult = path.match(extReg);
  154. return mathResult && mathResult[1];
  155. }
  156. }
  157. }
  158. run();