Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL] Task createDebugFiles checks path before writes #43

Merged
merged 1 commit into from
Jul 2, 2018
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
44 changes: 37 additions & 7 deletions lib/processors/debugFileCreator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const copier = require("./resourceCopier");
const util = require("util");

/**
* Creates *-dbg.js files for all supplied resources.
Expand All @@ -8,12 +9,41 @@ const copier = require("./resourceCopier");
* @param {Resource[]} parameters.resources List of resources to be processed
* @returns {Promise<Resource[]>} Promise resolving with debug resources
*/
module.exports = function({resources}) {
return copier({
resources: resources,
options: {
pattern: /((\.view|\.fragment|\.controller)?\.js)/,
replacement: "-dbg$1"
}
module.exports = function({resources, fs}) {
const options = {
pattern: /((\.view|\.fragment|\.controller)?\.js)/,
replacement: "-dbg$1"
};

const stat = util.promisify(fs.stat);

return Promise.all(
resources.map((resource) => {
// check whether the debug resource path is already used in the
// previous tasks
return stat(resource.getPath().replace(options.pattern, options.replacement))
.then(
// if the file can be found, it should be filtered out from creating debug file
() => false,
(err) => {
if (err.code === "ENOENT") {
// if the file can't be found, it should be included in creating debug file
return resource;
}
// if it's other error, forward it
throw err;
}
);
})
).then((results) => {
// filter out the resouces whose debug source path is already used
return results.filter((result) => {
return !!result;
});
}).then((filteredResources) => {
return copier({
resources: filteredResources,
options: options
});
});
};
2 changes: 2 additions & 0 deletions lib/tasks/createDebugFiles.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dbg = require("../processors/debugFileCreator");
const fsInterface = require("@ui5/fs").fsInterface;

/**
* Task to create dbg files.
Expand All @@ -18,6 +19,7 @@ module.exports = async function({workspace, options}) {
allResources = await workspace.byGlob(options.pattern);
}
return dbg({
fs: fsInterface(workspace),
resources: allResources
}).then((processedResources) => {
return Promise.all(processedResources.map((resource) => {
Expand Down
43 changes: 43 additions & 0 deletions test/lib/tasks/createDebugFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,46 @@ test("test1.js, test2.js: dbg file creation", (t) => {
});
});
});

test("dbg file creation should not overwrite the existing -dbg file", (t) => {
const sourceAdapter = resourceFactory.createAdapter({
virBasePath: "/"
});
const content = "console.log('Hello World');";
const resource = resourceFactory.createResource({
path: "/test1.js",
string: content
});

const contentDebug = "console.log('Hello Debug World')";
const debugResource = resourceFactory.createResource({
path: "/test1-dbg.js",
string: contentDebug
});

const workspace = resourceFactory.createWorkspace({
reader: sourceAdapter
});

return Promise.all([
sourceAdapter.write(resource),
workspace.write(debugResource)
]).then(() => {
return tasks.createDebugFiles({
workspace,
options: {
pattern: "/**/*.js"
}
}).then(() => {
return workspace.byPath("/test1-dbg.js").then((resource) => {
if (!resource) {
t.fail("Could not find the existing /test1-dbg.js");
} else {
return resource.getBuffer();
}
});
}).then((buffer) => {
t.deepEqual(buffer.toString(), contentDebug, "Content of /test1-dbg.js is correct");
});
});
});