-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (94 loc) · 3.3 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
const mergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
const fs = require('fs');
const { name, version } = require('./package');
const DEFAULT_BRAND = 'default';
const DEFAULT_PAGE_TITLE = 'Upfluence Software';
module.exports = {
name,
version,
options: {
'@embroider/macros': {
setOwnConfig: {
brand: process.env.BRAND || DEFAULT_BRAND,
brandPageTitle: process.env.BRAND_PAGE_TITLE || DEFAULT_PAGE_TITLE
}
}
},
included(parent) {
this._super.included.apply(this, arguments);
if (parent.project.pkg.name === name) {
this.options.babel.plugins.push(...require('ember-cli-code-coverage').buildBabelPlugin());
}
},
contentFor(type) {
const targetBrand = this.options['@embroider/macros'].setOwnConfig.brand;
if (type === 'page-title') {
return this.options['@embroider/macros'].setOwnConfig.brandPageTitle;
}
return colorSchemeStyle(targetBrand, this.parent.root, type);
},
treeForPublic(tree) {
let trees = [];
const targetBrand = this.options['@embroider/macros'].setOwnConfig.brand;
const ebmCandidates = this.parent.addons
.map((addon) => addon.addons)
.flat()
.filter((parentAddon) =>
parentAddon.addons.find((addon) => addon.pkg.name.includes('@upfluence/ember-brand-manager'))
)
.reduce(
(acc, addon) => {
acc.push({ pkgName: addon.name, isAddon: true, srcDir: addon.pkg.root });
return acc;
},
[{ pkgName: this.parent.pkg.name, isAddon: checkIfAddon(this.parent.pkg) }]
);
if (tree) {
trees.push(tree);
}
debugLog(`\n[EBM] Target brand : [${targetBrand}]`);
ebmCandidates.forEach((ebmCandidate) => {
debugLog(`[EBM] Pkg name : [${ebmCandidate.pkgName}]`);
this.setPublicAssets(trees, DEFAULT_BRAND, ebmCandidate.pkgName, ebmCandidate.isAddon, ebmCandidate.srcDir);
if (targetBrand !== DEFAULT_BRAND) {
this.setPublicAssets(trees, targetBrand, ebmCandidate.pkgName, ebmCandidate.isAddon, ebmCandidate.srcDir);
}
});
return mergeTrees(trees, { overwrite: true });
},
setPublicAssets(trees, brand, origin, isAddon, srcDir) {
debugLog(`[EBM] Funneling ${brand} assets to dist/${isAddon ? origin : 'assets'}`);
const _srcDir = srcDir ?? (isAddon ? this.parent.pkg.root : './');
const destDir = isAddon ? origin : '.';
trees.push(
new Funnel(_srcDir, {
srcDir: `brand-assets/${brand}/public`,
destDir
})
);
}
};
function checkIfAddon(parentPkg) {
return parentPkg.keywords && parentPkg.keywords.includes('ember-addon');
}
function colorSchemeStyle(targetBrand, root, type) {
if (
type === 'head-footer' &&
targetBrand !== DEFAULT_BRAND &&
fs.existsSync(`${root}/brand-assets/${targetBrand}/color-scheme.json`)
) {
const colors = JSON.parse(fs.readFileSync(`${root}/brand-assets/${targetBrand}/color-scheme.json`, 'utf8'));
const formattedStyle = Object.keys(colors)
.map((v) => {
return `${v}: ${colors[v]};`;
})
.join('');
debugLog(`[EBM] Color Scheme: ${formattedStyle}`);
return `<style>:root { ${formattedStyle} }</style>`;
}
}
function debugLog(message) {
if (process.env.EBM_DEBUG === 'true') console.info(message);
}