Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 59 additions & 57 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ function prep(fs) {

case 'ignore-target-content':
return symLinkIgnoreTargetContent(name, target, options, isRootElement);

case 'resolve':
return symLinkResolve(name, dir, target, options, isRootElement);

default:
throw `Invalid option: symbolicLinks.follow = "${options.symbolicLinks.follow}"`;
}
Expand All @@ -178,7 +178,7 @@ function prep(fs) {
delete options.skipMatching // only used for the root level
log.symlink('ignoring symbolic link target content');
const hash = crypto.createHash(options.algo);
if (!options.symbolicLinks.ignoreBasename &&
if (!options.symbolicLinks.ignoreBasename &&
!(isRootElement && options.files.ignoreRootName)) {
log.symlink('hash basename');
hash.update(name);
Expand Down Expand Up @@ -233,20 +233,22 @@ function prep(fs) {
function ignore(name, path, rules) {
if (rules.exclude) {
if (rules.matchBasename && rules.exclude(name)) {
log.match(`exclude basename '${path}'`);
log.match(`exclude basename '${name}'`);
return true;
} else if (rules.matchPath && rules.exclude(path)) {
log.match(`exclude path '${path}'`);
return true;
}
} else if (rules.include) {
}
if (rules.include) {
if (rules.matchBasename && rules.include(name)) {
log.match(`include basename '${path}'`);
log.match(`include basename '${name}'`);
return false;
} else if (rules.matchPath && rules.include(path)) {
log.match(`include path '${path}'`);
return false;
} else {
log.match(`include rule failed for path '${path}'`);
return true;
}
}
Expand All @@ -255,57 +257,6 @@ function prep(fs) {
return false;
}

const HashedFolder = function HashedFolder(name, children, options, isRootElement = false) {
this.name = name;
this.children = children;

const hash = crypto.createHash(options.algo);
if (options.folders.ignoreBasename ||
options.ignoreBasenameOnce ||
(isRootElement && options.folders.ignoreRootName))
{
delete options.ignoreBasenameOnce;
log.match(`omitted name of folder ${name} from hash`)
} else {
hash.update(name);
}
children.forEach(child => {
if (child.hash) {
hash.update(child.hash);
}
});

this.hash = hash.digest(options.encoding);
};

HashedFolder.prototype.toString = function (padding = '') {
const first = `${padding}{ name: '${this.name}', hash: '${this.hash},'\n`;
padding += ' ';

return `${first}${padding}children: ${this.childrenToString(padding)}}`;
};

HashedFolder.prototype.childrenToString = function (padding = '') {
if (this.children.length === 0) {
return '[]';
} else {
const nextPadding = padding + ' ';
const children = this.children
.map(child => child.toString(nextPadding))
.join('\n');
return `[\n${children}\n${padding}]`;
}
};

const HashedFile = function HashedFile(name, hash, encoding) {
this.name = name;
this.hash = hash.digest(encoding);
};

HashedFile.prototype.toString = function (padding = '') {
return padding + '{ name: \'' + this.name + '\', hash: \'' + this.hash + '\' }';
};

return hashElement;
}

Expand Down Expand Up @@ -344,6 +295,57 @@ function parseParameters(args) {
return Promise.resolve(log.params({ basename, dir, options }));
}

const HashedFolder = function HashedFolder(name, children, options, isRootElement = false) {
this.name = name;
this.children = children;

const hash = crypto.createHash(options.algo);
if (options.folders.ignoreBasename ||
options.ignoreBasenameOnce ||
(isRootElement && options.folders.ignoreRootName))
{
delete options.ignoreBasenameOnce;
log.match(`omitted name of folder ${name} from hash`)
} else {
hash.update(name);
}
children.forEach(child => {
if (child.hash) {
hash.update(child.hash);
}
});

this.hash = hash.digest(options.encoding);
};

HashedFolder.prototype.toString = function (padding = '') {
const first = `${padding}{ name: '${this.name}', hash: '${this.hash},'\n`;
padding += ' ';

return `${first}${padding}children: ${this.childrenToString(padding)}}`;
};

HashedFolder.prototype.childrenToString = function (padding = '') {
if (this.children.length === 0) {
return '[]';
} else {
const nextPadding = padding + ' ';
const children = this.children
.map(child => child.toString(nextPadding))
.join('\n');
return `[\n${children}\n${padding}]`;
}
};

const HashedFile = function HashedFile(name, hash, encoding) {
this.name = name;
this.hash = hash.digest(encoding);
};

HashedFile.prototype.toString = function (padding = '') {
return padding + '{ name: \'' + this.name + '\', hash: \'' + this.hash + '\' }';
};

function isFunction(any) {
return typeof any === 'function';
}
Expand Down
45 changes: 28 additions & 17 deletions test/folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,37 @@ describe('Generating a hash over a folder, it', function () {
});
});

it('ignores a folder it is both included and excluded', function () {
it('ignores a folder it is both included and excluded', async function () {
const hashElement = prep(Volume.fromJSON({
'base/file1': 'content',
'base/folder/file2': '2',
'base/folder2/file3': '3'
}));

return hashElement('base', {
async function verify(options) {
const result = await hashElement('base', options);
should.exist(result.hash);
should.exist(result.children);
result.children.length.should.equal(2);
result.children[0].name.should.equal('file1');
result.children[1].name.should.equal('folder2');
}

await verify({
folders: {
exclude: ['**/folder', '**folder'], include: ['*'],
exclude: ['**/folder'],
include: ['**/*'],
matchBasename: false, matchPath: true
}
})
.then(result => {
should.exist(result.hash);
should.exist(result.children);
result.children.length.should.equal(2);
result.children[0].name.should.equal('file1');
result.children[1].name.should.equal('folder2');
});
});

await verify({
folders: {
exclude: ['folder'],
include: ['*'],
matchBasename: true, matchPath: false
}
});
});

it('only includes the wanted folders', function () {
Expand Down Expand Up @@ -280,15 +291,15 @@ describe('Generating a hash over a folder, it', function () {
'def/file1.js': '//just a comment',
'def/def/.ignored': 'ignored'
}));
const options = {
const options = {
folders: { ignoreRootName: true },
files: { exclude: ['.*'] }
files: { exclude: ['.*'] }
};

return Promise.all([
hashElement('abc', options),
hashElement('def', options)
]).then(function (hashes) {
]).then(function (hashes) {
return hashes[0].hash.should.equal(hashes[1].hash);
});
});
Expand All @@ -301,15 +312,15 @@ describe('Generating a hash over a folder, it', function () {
'def/file1.js': '//just a comment',
'def/def/.ignored': 'ignored'
}));
const options = {
const options = {
folders: { ignoreBasename: true },
files: { exclude: ['.*'] }
files: { exclude: ['.*'] }
};

return Promise.all([
hashElement('abc', options),
hashElement('def', options)
]).then(function (hashes) {
]).then(function (hashes) {
return hashes[1].hash.should.equal(hashes[0].hash);
});
});
Expand Down