Skip to content

Commit

Permalink
feat(packer): export packer
Browse files Browse the repository at this point in the history
  • Loading branch information
Clunt committed May 16, 2018
1 parent 60e74e9 commit 6b07146
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/Bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Bundle.prototype.getCategory = function (category) {
const item = {
bundle: this,
absolute: fsPath,
path: fsPath.replace(this.dir, ''),
path: '/' + fsPath.replace(this.dir, '').replace(/^\//, ''),
stat: stat
};
if (stat.isDirectory()) {
Expand Down
5 changes: 4 additions & 1 deletion lib/Match.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ Match.prototype.test = function (path) {
release = !!stat;
}
});
return release;
return matched
? release
? 1 : 0
: -1;
};

Match.prototype.stringifyRules = function(rule, base) {
Expand Down
27 changes: 12 additions & 15 deletions lib/Packer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ function Packer(option) {
this.resource = null;
this.time = new Time();
this.initialized = false;

console.log(this.option)
console.log(this.dirs)
}

Packer.prototype.init = function() {
Expand All @@ -34,14 +31,6 @@ Packer.prototype.init = function() {

this.bundle.curr = new Bundle(this.dirs.curr, 'curr');

this.dirs.diff = createDiff({
root: this.option.root,
git: this.option.git,
dir: this.dirs.diff,
tag: this.option.diff
});
this.bundle.diff = new Bundle(this.dirs.diff, 'diff');

this.time.end('init');
};

Expand All @@ -50,7 +39,15 @@ Packer.prototype.diff = function() {

const difference = this.difference = { add: [], del: [], non: [] };

if (!(this.bundle.curr && this.bundle.diff)) return difference;
if (!this.bundle.curr) return difference;

this.dirs.diff = createDiff({
root: this.option.root,
git: this.option.git,
dir: this.dirs.diff,
tag: this.option.diff
});
this.bundle.diff = new Bundle(this.dirs.diff, 'diff');

const diff = {};
this.bundle.diff.getFile().forEach(item => {
Expand Down Expand Up @@ -97,9 +94,9 @@ Packer.prototype.match = function() {
const base = this.root;
const temp = this.dirs.temp;
this.resource = this.resource.concat(
difference.add.map(item => rules.test(item.path) ? item.curr : undefined),
difference.del.map(item => rules.test(item.path) ? undefined : item.diff),
difference.non.map(item => rules.test(item.path) ? item.curr : item.diff)
difference.add.map(item => rules.test(item.path) > 0 ? item.curr : undefined),
difference.del.map(item => rules.test(item.path) > 0 ? undefined : item.diff),
difference.non.map(item => rules.test(item.path) > 0 ? item.curr : item.diff)
).filter(item => !!item).map(file => {
const item = {
path: file.path,
Expand Down
20 changes: 14 additions & 6 deletions lib/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ Rule.prototype.wrap = function (rule) {

Rule.prototype.wrapRegExp = function (rule) {
const reg = new RegExp(rule.slice(1))
return path => reg.test(path);
const fn = path => reg.test(path);
fn.toString = () => reg;
return fn;
};

Rule.prototype.wrapMatch = function(rule) {
const reg = new minimatch.Minimatch(rule.slice(1), {
dot: this.option.dot
});
return path => reg.match(path);
const fn = path => reg.match(path);
fn.toString = () => reg;
return fn;
};

Rule.prototype.wrapAlias = function (rule) {
Expand All @@ -58,24 +62,28 @@ Rule.prototype.wrapAlias = function (rule) {
};

Rule.prototype.wrapPath = function (rule) {
rule = '/' + rule.replace(/^\//, '');

const option = this.option;
const symbol = option.symbol;
rule = '/' + rule.replace(/^\//, '');
let fn;
if (rule.charAt(rule.length - 1) === symbol.file) {
rule = rule.slice(0, rule.length - 1);
fn = path => path === rule;
} else {
rule = rule.replace(/\/$/, '') + '/';
fn = path => path.indexOf(rule) === 0;
}
const length = rule.length;
return path => path.indexOf(rule) === 0;
fn.toString = () => rule;
return fn;
};

Rule.prototype.setNegation = function (negation) {
this.negation = !!negation;
};

Rule.prototype.toString = function () {
return this.rule;
return this.reg && this.reg.toString ? this.reg.toString() : this.rule;
};

exports = module.exports = Rule;
16 changes: 12 additions & 4 deletions lib/freepack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ const leftPad = require('left-pad');
const Packer = require('./Packer');
const validateOption = require('./validateOption');

const freepack = (option, callback) => {
function getPacker(option) {
validateOption(option);
return new Packer(option)
}

const freepack = (option, callback) => {
const packer = getPacker(option);

const packer = new Packer(option);
console.log(packer.option)
console.log(packer.dirs)

packer.clean();

Expand All @@ -32,15 +38,15 @@ exports = module.exports = freepack;


freepack.diff = (option, callback) => {
// const packer = new Packer(option);
// const packer = getPacker(option);
// packer.init();
// packer.diff();
// packer.clean();
// return packer;
};

freepack.test = (option, callback) => {
const packer = new Packer(option);
const packer = getPacker(option);

packer.clean();

Expand All @@ -52,3 +58,5 @@ freepack.test = (option, callback) => {

return packer;
};

freepack.packer = getPacker;

0 comments on commit 6b07146

Please sign in to comment.