Skip to content

Commit

Permalink
feat(ignore): add option.ignore support
Browse files Browse the repository at this point in the history
  • Loading branch information
Clunt committed May 16, 2018
1 parent 1ac4c23 commit c82f35c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
20 changes: 13 additions & 7 deletions lib/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ const fs = require('fs');
const path = require('path');
const glob = require('glob');

function noop() {}

function Bundle(dir, type) {
this.type = type;
function Bundle(dir, option) {
this.type = option.type || 'unkonw';
this.ignore = option.ignore || noop;
this.dir = dir;
this.list = glob.sync(path.join(dir, '**/*'), {
dot: true
Expand All @@ -21,17 +23,21 @@ Bundle.prototype.getCategory = function (category) {
if (!categories[category]) return [];
this.list = this.list.map(fsPath => {
if (!fs.existsSync(fsPath)) return;
let itemPath = '/' + fsPath.replace(this.dir, '').replace(/^\//, '');
const stat = fs.statSync(fsPath);
const item = {
bundle: this,
absolute: fsPath,
path: '/' + fsPath.replace(this.dir, '').replace(/^\//, ''),
path: itemPath,
stat: stat
};
if (stat.isDirectory()) {
categories.directory.push(item)
} else if (stat.isFile()) {
categories.file.push(item)
if (!this.ignore(item.path, stat)) {
if (stat.isDirectory()) {
item.path = item.path.replace(/\/$/, '') + '/';
categories.directory.push(item)
} else if (stat.isFile()) {
categories.file.push(item)
}
}
return item;
}).filter(arg => !!arg);
Expand Down
25 changes: 25 additions & 0 deletions lib/Ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
exports = module.exports = function ignore(rules) {
rules = (Array.isArray(rules) ? rules : []).map(rule => {
if (typeof rule !== 'string') {
throw new Error('ignore must be string');
}
if (rule.length === 0) {
throw new Error('ignore can\'t be empty');
}
return rule.charAt(0) === '/'
? path => path === rule
: path => path.lastIndexOf('/' + rule) === path.length - rule.length - 1;
});

return function(path, stat) {
if (!stat.isFile()) return false;

for (var i = 0; i < rules.length; i++) {
if (rules[i](path, stat)) {
return true;
}
}

return false;
};
};
12 changes: 10 additions & 2 deletions lib/Packer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fsExtra = require('fs-extra');
const Time = require('./Time');
const Match = require('./Match');
const Bundle = require('./Bundle');
const Ignore = require('./Ignore');
const createDiff = require('./createDiff');


Expand All @@ -19,6 +20,7 @@ function Packer(option) {
this.bundle = {}; // curr: Bundle, diff: Bundle
this.difference = {}; // add 新增, del 删除, non 都存在(=> same change)
this.rules = new Match(option, option.release);
this.ignore = new Ignore(option.ignore);
this.resource = null;
this.time = new Time();
this.initialized = false;
Expand All @@ -29,7 +31,10 @@ Packer.prototype.init = function() {

this.initialized = true;

this.bundle.curr = new Bundle(this.dirs.curr, 'curr');
this.bundle.curr = new Bundle(this.dirs.curr, {
type: 'curr',
ignore: this.ignore,
});

this.time.end('init');
};
Expand All @@ -47,7 +52,10 @@ Packer.prototype.diff = function() {
dir: this.dirs.diff,
tag: this.option.diff
});
this.bundle.diff = new Bundle(this.dirs.diff, 'diff');
this.bundle.diff = new Bundle(this.dirs.diff, {
type: 'diff',
ignore: this.ignore,
});

const diff = {};
this.bundle.diff.getFile().forEach(item => {
Expand Down

0 comments on commit c82f35c

Please sign in to comment.