forked from plone/volto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-theme-addons-loader.js
79 lines (69 loc) · 2.09 KB
/
create-theme-addons-loader.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
const path = require('path');
const fs = require('fs');
const tmp = require('tmp');
const cryptoRandomString = require('crypto-random-string');
const titleCase = (w) => w.slice(0, 1).toUpperCase() + w.slice(1, w.length);
/*
* Transforms a package name to javascript variable name
*/
function nameFromPackage(name) {
name =
name.replace(/[@~./\\:\s]/gi, '') ||
cryptoRandomString({ length: 10, characters: 'abcdefghijk' });
return name
.split('-')
.map((w, i) => (i > 0 ? titleCase(w) : w))
.join('');
}
/*
* Creates a static file with code necessary to load the addons configuration
*
*/
function getAddonsLoaderCode(name, customThemeAddons = []) {
let buf = `/*
This file is autogenerated. Don't change it directly.
Add a ./theme/_${name}.scss in your add-on to load your theme customizations in the current theme.
*/
`;
customThemeAddons.forEach((addon) => {
const customization = `${addon}/theme/${name}`;
const line = `@import '${customization}';\n`;
buf += line;
});
return buf;
}
module.exports = ({ main, variables }) => {
// const addonsThemeLoaderVariablesPath = path.join(
// process.cwd(),
// 'src',
// '_variables.scss',
// );
// const addonsThemeLoaderMainPath = path.join(
// process.cwd(),
// 'src',
// '_main.scss',
// );
// const addonsThemeLoaderVariablesPath = path.join(
// process.cwd(),
// 'src',
// '_variables.scss',
// );
// const addonsThemeLoaderMainPath = path.join(
// process.cwd(),
// 'src',
// '_main.scss',
// );
const addonsThemeLoaderVariablesPath = tmp.tmpNameSync({ postfix: '.scss' });
const addonsThemeLoaderMainPath = tmp.tmpNameSync({ postfix: '.scss' });
fs.writeFileSync(
addonsThemeLoaderVariablesPath,
new Buffer.from(getAddonsLoaderCode('variables', variables)),
);
fs.writeFileSync(
addonsThemeLoaderMainPath,
new Buffer.from(getAddonsLoaderCode('main', main)),
);
return [addonsThemeLoaderVariablesPath, addonsThemeLoaderMainPath];
};
module.exports.getAddonsLoaderCode = getAddonsLoaderCode;
module.exports.nameFromPackage = nameFromPackage;