Skip to content

Commit

Permalink
feat(modular-css-core): processor.remove returns removed files (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
tivac committed Dec 13, 2017
1 parent fa0c303 commit b560651
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/core/processor.js
Expand Up @@ -147,7 +147,7 @@ Processor.prototype = {
// Remove a file from the dependency graph
remove : function(input) {
var order = this._graph.overallOrder(),
files;
files, removed;

// Only want files actually in the array
files = (Array.isArray(input) ? input : [ input ])
Expand All @@ -160,21 +160,23 @@ Processor.prototype = {

// Remove everything that depends on files to be removed as well
// since it will also have to be recalculated
files = files
.reduce(
(prev, curr) => prev.concat(
this._graph.dependantsOf(curr)
.concat(curr)
),
[]
)
.sort((a, b) => order.indexOf(a) - order.indexOf(b));

unique(files).forEach((file) => {
files = files.reduce((prev, curr) =>
prev.concat(
this._graph.dependantsOf(curr).concat(curr)
),
[]
)
.sort((a, b) => order.indexOf(a) - order.indexOf(b));

removed = unique(files);
removed.forEach((file) => {
delete this._files[file];

this._graph.removeNode(file);
});

return removed;
},

// Get the dependency order for a file or the entire tree
Expand Down
7 changes: 7 additions & 0 deletions packages/core/test/__snapshots__/api.test.js.snap
Expand Up @@ -178,6 +178,13 @@ Array [
]
`;

exports[`/processor.js API .remove() should return an array of removed files 1`] = `
Array [
"a.css",
"b.css",
]
`;

exports[`/processor.js API .string() should process a string 1`] = `
Object {
"wooga": Array [
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/api.test.js
Expand Up @@ -114,6 +114,24 @@ describe("/processor.js", () => {
expect(processor.dependencies()).toEqual([]);
})
);

it("should return an array of removed files", () =>
Promise.all([
processor.string("./a.css", ".a { }"),
processor.string("./b.css", ".b { }"),
processor.string("./c.css", ".c { }")
])
.then(() => {
expect(
relative(
processor.remove([
"./a.css",
"./b.css"
])
)
).toMatchSnapshot();
})
);
});

describe(".dependencies()", () => {
Expand Down

0 comments on commit b560651

Please sign in to comment.