forked from mbnuqw/sidebery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyles.js
150 lines (138 loc) · 3.82 KB
/
styles.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
140
141
142
143
144
145
146
147
148
149
150
/* eslint no-console: off */
const fs = require('fs')
const path = require('path')
const stylus = require('stylus')
const csso = require('csso')
const { IS_DEV, ADDON_PATH, getTime, watch, log, logOk } = require('./utils')
const OUTPUT_DIR = `${ADDON_PATH}/themes`
const THEMES = {
proton: [
'./src/styles/themes/proton/sidebar/sidebar.styl',
'./src/styles/themes/proton/page.url/url.styl',
'./src/styles/themes/proton/page.group/group.styl',
'./src/styles/themes/proton/page.setup/setup.styl',
'./src/styles/themes/proton/popup.proxy/proxy.styl',
],
compact: [
'./src/styles/themes/compact/sidebar/sidebar.styl',
'./src/styles/themes/compact/page.url/url.styl',
'./src/styles/themes/compact/page.group/group.styl',
'./src/styles/themes/compact/popup.proxy/proxy.styl',
],
plain: [
'./src/styles/themes/plain/sidebar/sidebar.styl',
'./src/styles/themes/plain/page.url/url.styl',
'./src/styles/themes/plain/page.group/group.styl',
'./src/styles/themes/plain/popup.proxy/proxy.styl',
],
}
/**
* Build
*/
async function build() {
const entries = getEntries()
await compileStyles(entries)
}
/**
* Build and watch
*/
async function buildAndWatch() {
const entries = getEntries()
await compileStyles(entries)
const tasks = await Promise.all(
entries.map(async e => {
const srcContent = await fs.promises.readFile(e.srcPath, 'utf-8')
const deps = stylus(srcContent)
.set('paths', [path.dirname(e.srcPath)])
.deps()
e.files = [e.srcPath, ...deps]
return e
})
)
watch(
tasks,
async tasks => {
for (const task of tasks) {
console.log(`${getTime()} Styles: Changed source:`, task.srcPath)
try {
await compile(task.srcPath, task.outputPath)
} catch (err) {
console.log(`${getTime()} Styles: Cannot build ${task.srcPath}:\n`, err)
}
}
},
(task, file) => {
console.log(`${getTime()} Styles: File ${file} was renamed, restart this script`)
tasks.forEach(t => t.watchers.forEach(w => w.close()))
}
)
}
/**
* Compile provided entries
*/
async function compileStyles(entries) {
let lastDir
for (const entry of entries) {
if (lastDir !== entry.outputDir) {
lastDir = entry.outputDir
try {
await fs.promises.mkdir(entry.outputDir, { recursive: true })
} catch (err) {
console.log(`${getTime()} Styles: Cannot create dir ${entry.outputDir}:\n`, err)
}
}
try {
await compile(entry.srcPath, entry.outputPath)
} catch (err) {
console.log(`${getTime()} Styles: Cannot build ${entry.srcPath}:\n`, err)
}
}
}
/**
* Compile stylus
*/
async function compile(srcPath, outputPath, srcContent) {
return new Promise(async (res, rej) => {
try {
if (!srcContent) srcContent = await fs.promises.readFile(srcPath, 'utf-8')
stylus(srcContent)
.set('paths', [path.dirname(srcPath)])
.render(async (err, css) => {
if (!IS_DEV) css = csso.minify(css, { restructure: false }).css
res(fs.promises.writeFile(outputPath, css))
})
} catch (err) {
console.log(`${getTime()} Styles: Cannot build ${srcPath}`, err)
rej(err)
}
})
}
/**
* Get list of styles entries
*/
function getEntries() {
const entries = []
for (const name of Object.keys(THEMES)) {
const styles = THEMES[name]
const outputDir = path.join(OUTPUT_DIR, name)
for (const srcPath of styles) {
const outputPath = path.join(outputDir, path.basename(srcPath, '.styl') + '.css')
entries.push({ srcPath, outputDir, outputPath })
}
}
return entries
}
/**
* Main
*/
async function main() {
log('Styles: Building')
if (IS_DEV) {
await buildAndWatch()
logOk('Styles: Watching')
} else {
await build()
logOk('Styles: Done')
}
}
main()