Skip to content

Commit

Permalink
[INTERNAL] Task createDebugFiles checks path before writes
Browse files Browse the repository at this point in the history
- The debugFileCreator processor checks whether the debug file path
  is used and only writes the content to the path when it's not
  used yet.

- The debug file path can be used, for example, by the createBundle
  task which may create a specific bundle for the debug version of
  another bundle. In this case, the debugFileCreator shouldn't
  overwrite the created debug bundle.
  • Loading branch information
stopcoder committed Jul 2, 2018
1 parent e9823f6 commit e45a0db
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
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 createing 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((stats) => {
// filter out the resouces whose debug source path is already used
return stats.filter((stat) => {
return !!stat;
});
}).then((filteredResource) => {
return copier({
resources: filteredResource,
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");
});
});
});

0 comments on commit e45a0db

Please sign in to comment.