-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathsyncFromNewRepo.js
78 lines (67 loc) · 3.36 KB
/
syncFromNewRepo.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const _ = require("lodash");
const fs = require("fs");
// merge deep function - goes through each leaf in the object and replaces the key with the same
// key in the new tokens
function mergeDeep(target, source) {
if (_.isObject(target) && _.isObject(source)) {
Object.keys(target).forEach((key) => {
if (_.isObject(source[key])) {
mergeDeep(target[key], source[key]);
} else if (source.hasOwnProperty(key)) {
let replacement = source[key];
// this is for replacing linear-gradients (since in the old tokens the format for gradients is [start, end])
if (replacement.startsWith("linear-gradient(")) {
let gradientParts = replacement.split(",");
gradientParts.shift();
gradientParts = gradientParts.map((part) => {
return part.trim().split(" ")[0];
});
replacement = gradientParts;
}
target[key] = replacement;
}
});
}
return target;
}
// Function to update the existing JSON file with values from the new JSON file
const updateJsonFile = (existingFilePath, newFilePath) => {
// Read the existing JSON file
fs.readFile(existingFilePath, "utf8", (err, data) => {
if (err) {
console.error(`Error reading the existing file: ${err}`);
return;
}
// Parse the existing JSON data
let existingData = JSON.parse(data);
// Read the new JSON file
fs.readFile(newFilePath, "utf8", (err, newData) => {
if (err) {
console.error(`Error reading the new file: ${err}`);
return;
}
const newJsonData = mergeDeep(_.cloneDeep(existingData), JSON.parse(newData).color);
// Update the existing JSON data with the new JSON data
existingData = { ...existingData, ...newJsonData };
// Write the updated JSON data back to the existing file
fs.writeFile(existingFilePath, JSON.stringify(existingData, null, 2), (err) => {
if (err) {
console.error(`Error writing the updated data to the file: ${err}`);
} else {
console.log(`The file has been updated successfully.`);
}
});
});
});
};
// dark:
updateJsonFile("./theme-data/common/common.json", "./node_modules/@momentum-design/tokens/dist/json-minimal/theme/webex/dark-stable.json");
updateJsonFile("./theme-data/dark/dark-common.json", "./node_modules/@momentum-design/tokens/dist/json-minimal/theme/webex/dark-stable.json");
updateJsonFile("./theme-data/dark/dark-additional.json", "./node_modules/@momentum-design/tokens/dist/json-minimal/theme/webex/dark-stable.json");
updateJsonFile("./theme-data/dark/dark-webex.json", "./node_modules/@momentum-design/tokens/dist/json-minimal/theme/webex/dark-stable.json");
// light:
updateJsonFile("./theme-data/common/common.json", "./node_modules/@momentum-design/tokens/dist/json-minimal/theme/webex/light-stable.json");
updateJsonFile("./theme-data/light/light-common.json", "./node_modules/@momentum-design/tokens/dist/json-minimal/theme/webex/light-stable.json");
updateJsonFile("./theme-data/light/light-additional.json", "./node_modules/@momentum-design/tokens/dist/json-minimal/theme/webex/light-stable.json");
updateJsonFile("./theme-data/light/light-webex.json", "./node_modules/@momentum-design/tokens/dist/json-minimal/theme/webex/light-stable.json");
// TODO: other themes (bronze, lavender, etc.), high contrast for win (separate script or by providing arg)