-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathreplaceCopyright.js
More file actions
58 lines (53 loc) · 1.76 KB
/
replaceCopyright.js
File metadata and controls
58 lines (53 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import stringReplacer from "../processors/stringReplacer.js";
/**
* @public
* @module @ui5/builder/tasks/replaceCopyright
*/
/**
* Task to to replace the copyright.
*
* The following placeholders are replaced with corresponding values:
* <ul>
* <li>${copyright}</li>
* <li>@copyright@</li>
* </ul>
*
* If the copyright string contains the optional placeholder ${currentYear}
* it will be replaced with the current year.
* If no copyright string is given, no replacement is being done.
*
* @public
* @function default
* @static
*
* @param {object} parameters Parameters
* @param {@ui5/fs/DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {object} parameters.options Options
* @param {string} parameters.options.copyright Replacement copyright
* @param {string} parameters.options.pattern Pattern to locate the files to be processed
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
export default async function({workspace, buildCache, options: {copyright, pattern}}) {
if (!copyright) {
return;
}
// Replace optional placeholder ${currentYear} with the current year
copyright = copyright.replace(/(?:\$\{currentYear\})/, new Date().getFullYear());
let resources = await workspace.byGlob(pattern);
if (buildCache.hasCache()) {
const changedPaths = buildCache.getChangedProjectResourcePaths();
resources = resources.filter((resource) => changedPaths.has(resource.getPath()));
}
const processedResources = await stringReplacer({
resources,
options: {
pattern: /(?:\$\{copyright\}|@copyright@)/g,
replacement: copyright
}
});
return Promise.all(processedResources.map((resource) => {
if (resource) {
return workspace.write(resource);
}
}));
}