forked from matthiasbergneels/scriptable_prusaconnectlocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup-plugin-add-file-icon-settings.ts
60 lines (52 loc) · 1.77 KB
/
rollup-plugin-add-file-icon-settings.ts
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
import { Plugin } from "rollup";
import { ScriptSettings, SETTINGS } from "./scriptIconSettings";
const COMMENT_ATOMS = {
line1: "Variables used by Scriptable.",
line2: "These must be at the very top of the file. Do not edit.",
runInApp: "always-run-in-app: true;",
};
const getScriptableSettingsCommentLines = ({
iconColor,
iconGlyph,
alwaysRunInApp,
}: ScriptSettings) => {
const colorAtom = `icon-color: ${iconColor};`;
const iconAtom = `icon-glyph: ${iconGlyph};`;
const line3 = alwaysRunInApp
? [COMMENT_ATOMS.runInApp, colorAtom]
: [colorAtom, iconAtom];
const line4 = alwaysRunInApp ? iconAtom : null;
return [COMMENT_ATOMS.line1, COMMENT_ATOMS.line2, line3.join(" "), line4]
.filter(Boolean)
.map(text => `// ${text}`)
.join("\n");
};
const fallbackIconSettings: ScriptSettings = {
alwaysRunInApp: true,
iconColor: "yellow",
iconGlyph: "exclamation-triangle",
};
const getBannerForFilePath = (filePath: string) => {
const matchForTsFiles = filePath.match(/.*\/([a-z0-9\.]+)\.ts/i);
if (!matchForTsFiles?.[1]) return null;
const filename = matchForTsFiles[1];
const settingsForFile = SETTINGS[filename];
if (settingsForFile) {
return getScriptableSettingsCommentLines(settingsForFile);
}
const DIVIDER = "-".repeat(50);
// eslint-disable-next-line no-console
console.log(
["", DIVIDER, `Missing settings for ${filename}!`, DIVIDER, ""].join("\n")
);
return getScriptableSettingsCommentLines(fallbackIconSettings);
};
//
const addFileIconSettings = (filePath: string): Plugin => ({
name: "rollup-plugin-scriptable-icon-settings",
renderChunk: code => {
const commentLines = getBannerForFilePath(filePath);
return commentLines ? [commentLines, code].join("\n") : code;
},
});
export default addFileIconSettings;