123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- const fs = require('fs');
- const preamble = require('./preamble');
- const pathTool = require('path');
- const {color} = require('zrender/build/helper');
- const excludesPath = pathTool.join(__dirname, '../.headerignore');
- const ecBasePath = pathTool.join(__dirname, '../');
- const isVerbose = process.argv[2] === '--verbose';
- function run() {
- const updatedFiles = [];
- const passFiles = [];
- const pendingFiles = [];
- eachFile(function (absolutePath, fileExt) {
- const fileStr = fs.readFileSync(absolutePath, 'utf-8');
- const existLicense = preamble.extractLicense(fileStr, fileExt);
- if (existLicense) {
- passFiles.push(absolutePath);
- return;
- }
-
- if (!preamble.hasPreamble(fileExt)) {
- pendingFiles.push(absolutePath);
- return;
- }
- fs.writeFileSync(absolutePath, preamble.addPreamble(fileStr, fileExt), 'utf-8');
- updatedFiles.push(absolutePath);
- });
- console.log('\n');
- console.log('----------------------------');
- console.log(' Files that exists license: ');
- console.log('----------------------------');
- if (passFiles.length) {
- if (isVerbose) {
- passFiles.forEach(function (path) {
- console.log(color('fgGreen', 'dim')(path));
- });
- }
- else {
- console.log(color('fgGreen', 'dim')(passFiles.length + ' files. (use argument "--verbose" see details)'));
- }
- }
- else {
- console.log('Nothing.');
- }
- console.log('\n');
- console.log('--------------------');
- console.log(' License added for: ');
- console.log('--------------------');
- if (updatedFiles.length) {
- updatedFiles.forEach(function (path) {
- console.log(color('fgGreen', 'bright')(path));
- });
- }
- else {
- console.log('Nothing.');
- }
- console.log('\n');
- console.log('----------------');
- console.log(' Pending files: ');
- console.log('----------------');
- if (pendingFiles.length) {
- pendingFiles.forEach(function (path) {
- console.log(color('fgRed', 'dim')(path));
- });
- }
- else {
- console.log('Nothing.');
- }
- console.log('\nDone.');
- }
- function eachFile(visit) {
- const excludePatterns = [];
- const extReg = /\.([a-zA-Z0-9_-]+)$/;
- prepareExcludePatterns();
- travel('./');
- function travel(relativePath) {
- if (isExclude(relativePath)) {
- return;
- }
- const absolutePath = pathTool.join(ecBasePath, relativePath);
- const stat = fs.statSync(absolutePath);
- if (stat.isFile()) {
- visit(absolutePath, getExt(absolutePath));
- }
- else if (stat.isDirectory()) {
- fs.readdirSync(relativePath).forEach(function (file) {
- travel(pathTool.join(relativePath, file));
- });
- }
- }
- function prepareExcludePatterns() {
- const content = fs.readFileSync(excludesPath, {encoding: 'utf-8'});
- content.replace(/\r/g, '\n').split('\n').forEach(function (line) {
- line = line.trim();
- if (line && line.charAt(0) !== '#') {
- excludePatterns.push(new RegExp(line));
- }
- });
- }
- function isExclude(relativePath) {
- for (let i = 0; i < excludePatterns.length; i++) {
- if (excludePatterns[i].test(relativePath)) {
- return true;
- }
- }
- }
- function getExt(path) {
- if (path) {
- const mathResult = path.match(extReg);
- return mathResult && mathResult[1];
- }
- }
- }
- run();
|