Skip to content

Commit 57427c8

Browse files
authored
[INTERNAL] Task createDebugFiles checks path before writes (#43)
- 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.
1 parent 000a6fe commit 57427c8

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

lib/processors/debugFileCreator.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const copier = require("./resourceCopier");
2+
const util = require("util");
23

34
/**
45
* Creates *-dbg.js files for all supplied resources.
@@ -8,12 +9,41 @@ const copier = require("./resourceCopier");
89
* @param {Resource[]} parameters.resources List of resources to be processed
910
* @returns {Promise<Resource[]>} Promise resolving with debug resources
1011
*/
11-
module.exports = function({resources}) {
12-
return copier({
13-
resources: resources,
14-
options: {
15-
pattern: /((\.view|\.fragment|\.controller)?\.js)/,
16-
replacement: "-dbg$1"
17-
}
12+
module.exports = function({resources, fs}) {
13+
const options = {
14+
pattern: /((\.view|\.fragment|\.controller)?\.js)/,
15+
replacement: "-dbg$1"
16+
};
17+
18+
const stat = util.promisify(fs.stat);
19+
20+
return Promise.all(
21+
resources.map((resource) => {
22+
// check whether the debug resource path is already used in the
23+
// previous tasks
24+
return stat(resource.getPath().replace(options.pattern, options.replacement))
25+
.then(
26+
// if the file can be found, it should be filtered out from creating debug file
27+
() => false,
28+
(err) => {
29+
if (err.code === "ENOENT") {
30+
// if the file can't be found, it should be included in creating debug file
31+
return resource;
32+
}
33+
// if it's other error, forward it
34+
throw err;
35+
}
36+
);
37+
})
38+
).then((results) => {
39+
// filter out the resouces whose debug source path is already used
40+
return results.filter((result) => {
41+
return !!result;
42+
});
43+
}).then((filteredResources) => {
44+
return copier({
45+
resources: filteredResources,
46+
options: options
47+
});
1848
});
1949
};

lib/tasks/createDebugFiles.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const dbg = require("../processors/debugFileCreator");
2+
const fsInterface = require("@ui5/fs").fsInterface;
23

34
/**
45
* Task to create dbg files.
@@ -18,6 +19,7 @@ module.exports = async function({workspace, options}) {
1819
allResources = await workspace.byGlob(options.pattern);
1920
}
2021
return dbg({
22+
fs: fsInterface(workspace),
2123
resources: allResources
2224
}).then((processedResources) => {
2325
return Promise.all(processedResources.map((resource) => {

test/lib/tasks/createDebugFiles.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,46 @@ test("test1.js, test2.js: dbg file creation", (t) => {
233233
});
234234
});
235235
});
236+
237+
test("dbg file creation should not overwrite the existing -dbg file", (t) => {
238+
const sourceAdapter = resourceFactory.createAdapter({
239+
virBasePath: "/"
240+
});
241+
const content = "console.log('Hello World');";
242+
const resource = resourceFactory.createResource({
243+
path: "/test1.js",
244+
string: content
245+
});
246+
247+
const contentDebug = "console.log('Hello Debug World')";
248+
const debugResource = resourceFactory.createResource({
249+
path: "/test1-dbg.js",
250+
string: contentDebug
251+
});
252+
253+
const workspace = resourceFactory.createWorkspace({
254+
reader: sourceAdapter
255+
});
256+
257+
return Promise.all([
258+
sourceAdapter.write(resource),
259+
workspace.write(debugResource)
260+
]).then(() => {
261+
return tasks.createDebugFiles({
262+
workspace,
263+
options: {
264+
pattern: "/**/*.js"
265+
}
266+
}).then(() => {
267+
return workspace.byPath("/test1-dbg.js").then((resource) => {
268+
if (!resource) {
269+
t.fail("Could not find the existing /test1-dbg.js");
270+
} else {
271+
return resource.getBuffer();
272+
}
273+
});
274+
}).then((buffer) => {
275+
t.deepEqual(buffer.toString(), contentDebug, "Content of /test1-dbg.js is correct");
276+
});
277+
});
278+
});

0 commit comments

Comments
 (0)