/
valletta.js
139 lines (118 loc) · 3.69 KB
/
valletta.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const _ = require('lodash')
const findConfig = require('find-config')
const fs = require('fs')
const path = require('path')
const sanitize = require('sanitize-filename')
const mkdirp = require('mkdirp')
const Promise = require('bluebird')
const defaultConfigFiles = {
// CSS
stylesheet: 'stylesheet.css',
// Header
header: 'header.tpl.html',
// Top
top: 'top.tpl.html',
// Footer
footer: 'footer.tpl.html',
// </head>
head_tag: 'head_tag.tpl.html',
// </body>
body_tag: 'body_tag.tpl.html',
// Embedded CSS
embedded_css: 'embedded_css.css',
// CSS in the 📱 section
mobile_stylesheet: 'mobile_stylesheet.css',
// Header in the 📱 section
mobile_header: 'mobile_header.tpl.html',
// Top in the 📱 section
mobile_top: 'mobile_top.tpl.html',
// Footer in the 📱 section
mobile_footer: 'mobile_footer.tpl.html',
}
// Initilises empty theme files in your current working directory
exports.init = (cli = false) => {
// Default to the directory of the calling module
const callingDir = cli ?
process.cwd() :
path.dirname(module.parent.filename)
if (cli) {
console.log('Initialising theme files...')
}
_.forIn(defaultConfigFiles, (value) => {
const filePath = path.join(callingDir, value)
fs.openSync(filePath, 'w')
if (cli) {
console.log('Created', filePath)
}
})
}
exports.generate = (options, cli = false) => {
// Default to the directory of the calling module
const callingDir = cli ?
process.cwd() :
path.dirname(module.parent.filename)
let defaultConfig = {
name: 'custom theme',
srcDir: callingDir,
outputDir: callingDir,
enabled: false,
}
let config
if (options) {
config = _.defaultsDeep(options, defaultConfig)
} else {
let configPath = findConfig('valletta.config.js')
if (configPath) {
config = _.defaultsDeep(require(configPath), defaultConfig)
} else {
config = defaultConfig
}
}
// Unless the outputDir is absolute, join it to the calling directory
config.srcDir = config.srcDir.charAt(0) === '/' ?
path.normalize(config.srcDir) :
path.normalize(path.join(callingDir, config.srcDir))
return Promise.map(_.keys(defaultConfigFiles), (key) => {
if (config.files && config.files[key] === '') {
return
}
const defaultLoad = !config.files || config.files && !config.files[key]
const value = defaultLoad ? defaultConfigFiles[key] : config.files[key]
let file
if (_.isString(value)) {
let filePath = path.join(config.srcDir, value)
try {
file = fs.readFileSync(filePath, 'utf-8')
} catch (err) {
// Only warn users if they are explicitly trying to load a file
if (!defaultLoad) {
console.error('Couldn\'t load ' + key + ' at ' + filePath + ' - skipping')
}
file = ''
}
return { key, file }
} else if (_.isFunction(value)) {
return value()
.then((file) => {
return { key, file }
})
}
}).then((themeData) => {
let output = {}
themeData.forEach((item) => {
output[item.key] = item.file
})
output.name = config.name
output.enabled = config.enabled
const outputFileName = sanitize(config.name).replace(/ /g, '_').toLowerCase() + '.dcstyle.json'
// Unless the outputDir is absolute, join it to the calling directory
const outputDir = config.outputDir.charAt(0) === '/' ?
path.normalize(config.outputDir) :
path.normalize(path.join(callingDir, config.outputDir))
const finalPath = path.join(outputDir, outputFileName)
mkdirp.sync(outputDir)
const themeJSON = { site_customization: output }
fs.writeFileSync(finalPath, JSON.stringify(themeJSON))
return finalPath
})
}