|
| 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 | +const pathTool = require('path'); |
| 21 | +const fs = require('fs'); |
| 22 | + |
| 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 | + |
| 29 | +function eachFile(visit) { |
| 30 | + const excludePatterns = []; |
| 31 | + const extReg = /\.([a-zA-Z0-9_-]+)$/; |
| 32 | + |
| 33 | + prepareExcludePatterns(); |
| 34 | + travel('./'); |
| 35 | + |
| 36 | + function travel(relativePath) { |
| 37 | + if (isExclude(relativePath)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const absolutePath = pathTool.join(ecBasePath, relativePath); |
| 42 | + const stat = fs.statSync(absolutePath); |
| 43 | + |
| 44 | + if (stat.isFile()) { |
| 45 | + visit(absolutePath, getExt(absolutePath)); |
| 46 | + } |
| 47 | + else if (stat.isDirectory()) { |
| 48 | + fs.readdirSync(relativePath).forEach(function (file) { |
| 49 | + travel(pathTool.join(relativePath, file)); |
| 50 | + }); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function prepareExcludePatterns() { |
| 55 | + const content = fs.readFileSync(excludesPath, {encoding: 'utf-8'}); |
| 56 | + content.replace(/\r/g, '\n').split('\n').forEach(function (line) { |
| 57 | + line = line.trim(); |
| 58 | + if (line && line.charAt(0) !== '#') { |
| 59 | + excludePatterns.push(new RegExp(line)); |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + function isExclude(relativePath) { |
| 65 | + for (let i = 0; i < excludePatterns.length; i++) { |
| 66 | + if (excludePatterns[i].test(relativePath)) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + function getExt(path) { |
| 73 | + if (path) { |
| 74 | + const mathResult = path.match(extReg); |
| 75 | + return mathResult && mathResult[1]; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = { |
| 81 | + eachFile: eachFile |
| 82 | +}; |
0 commit comments