forked from maplibre/maplibre-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style-code.js
70 lines (58 loc) · 1.83 KB
/
style-code.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
// Global functions //
const ejs = require('ejs');
const fs = require('fs');
const path = require('path');
global.iff = function (condition, val) {
return condition() ? val : "";
};
global.camelize = function (str) {
return str.replace(/(?:^|-)(.)/g, function (_, x) {
return x.toUpperCase();
});
};
global.camelizeWithLeadingLowercase = function (str) {
return str.replace(/-(.)/g, function (_, x) {
return x.toUpperCase();
});
};
global.snakeCaseUpper = function snakeCaseUpper(str) {
return str.replace(/-/g, "_").toUpperCase();
};
global.unhyphenate = function (str) {
return str.replace(/-/g, " ");
};
// Write out a list of files that this script is modifying so that we can check
var files = [];
process.on('exit', function() {
const list = path.join(path.dirname(process.argv[1]), path.basename(process.argv[1], '.js') + '.list');
console.log(`Writing files that this script modifies to: ${list}`);
fs.writeFileSync(list, files.join("\n"));
});
global.writeIfModified = function(filename, newContent, output) {
if (output) {
filename = path.resolve(path.join(output, filename));
}
const info = path.parse(filename);
if (!fs.existsSync(info.dir)) {
fs.mkdirSync(info.dir, {recursive: true});
}
files.push(filename);
try {
const oldContent = fs.readFileSync(filename, 'utf8');
if (oldContent == newContent) {
console.warn(`* Skipping file '${filename}' because it is up-to-date`);
return;
}
} catch(err) {
}
if (['0', 'false'].indexOf(process.env.DRY_RUN || '0') !== -1) {
fs.writeFileSync(filename, newContent);
}
console.warn(`* Updating outdated file '${filename}'`);
};
global.readAndCompile = function(filename, root) {
if (root) {
filename = path.resolve(path.join(root, filename));
}
return ejs.compile(fs.readFileSync(filename, 'utf8'), {strict: true});
}