This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
149 lines (134 loc) · 4.81 KB
/
build.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
#!/usr/bin/env node
// This script can be used to build custom colour-scheme css files.
//
// 1. Create a copy of packages/node_modules/@node-red/editor-client/src/sass/colors.scss
// and change the values to the desired colours.
//
// 2. Run this script, providing the path to the custom file using the --in option
// and the output will be written to the location specified by the --out option
//
const os = require('os')
const nopt = require('nopt')
const path = require('path')
const fs = require('fs-extra')
const sass = require('sass')
const sassDir = '/packages/node_modules/@node-red/editor-client/src/sass/'
const knownOpts = {
help: Boolean,
long: Boolean,
in: [path],
out: [path],
src: [path]
}
const shortHands = {
'?': ['--help']
}
nopt.invalidHandler = function (k, v, t) {}
const parsedArgs = nopt(knownOpts, shortHands, process.argv, 2)
const argSrc = process.env.npm_config_src || parsedArgs.src
const argIn = process.env.npm_config_in || parsedArgs.in
const argOut = process.env.npm_config_out || parsedArgs.out
const argLong = !!(process.env.npm_config_long || parsedArgs.long)
const ruleRegex = /(\$.*?) *: *(\S[\S\s]*?);/g
const customColors = {}
let match
console.log('')
if (parsedArgs.help) {
showUsageAndExit(0)
}
if (!argOut) {
console.warn('Missing variable: out')
showUsageAndExit(1)
}
if (!argSrc) {
console.warn('Missing variable: src')
showUsageAndExit(1)
}
if (!fs.existsSync(argSrc)) {
console.warn(`Node-RED directory '${argSrc}' not found`)
showUsageAndExit(1)
}
if (!fs.existsSync(path.join(argSrc, '/package.json'))) {
console.warn(`Node-RED path is not valid. Could not find '${path.join(argSrc, '/package.json')}'`)
showUsageAndExit(2)
}
if (!fs.existsSync(path.join(argSrc, sassDir))) {
console.warn(`Node-RED path is not valid. Could not find '${path.join(argSrc, sassDir)}'`)
showUsageAndExit(3)
}
// load custom colours
if (argIn && fs.existsSync(argIn)) {
const customColorsFile = fs.readFileSync(argIn, 'utf-8')
while ((match = ruleRegex.exec(customColorsFile)) !== null) {
customColors[match[1]] = match[2]
}
}
// Load base colours
const colorsFile = fs.readFileSync(path.join(argSrc, sassDir, 'colors.scss'), 'utf-8')
const updatedColors = []
while ((match = ruleRegex.exec(colorsFile)) !== null) {
updatedColors.push(match[1] + ': ' + (customColors[match[1]] || match[2]) + ';')
}
(async function () {
const tmpDir = os.tmpdir()
const workingDir = await fs.mkdtemp(`${tmpDir}${path.sep}`)
await fs.copy(path.join(argSrc, sassDir), workingDir)
await fs.writeFile(path.join(workingDir, 'colors.scss'), updatedColors.join('\n'))
const result = sass.compile(path.join(workingDir, 'style.scss'), { outputStyle: 'expanded' })
const css = result.css.toString()
const lines = css.split('\n')
const colorCSS = []
const nonColorCSS = []
let inKeyFrameBlock = false
lines.forEach(l => {
if (inKeyFrameBlock) {
nonColorCSS.push(l)
if (/^}/.test(l)) {
inKeyFrameBlock = false
}
} else if (/^@keyframes/.test(l)) {
nonColorCSS.push(l)
inKeyFrameBlock = true
} else if (!/^ {2}/.test(l)) {
colorCSS.push(l)
nonColorCSS.push(l)
} else if (/color|border|background|fill|stroke|outline|box-shadow/.test(l)) {
colorCSS.push(l)
} else {
nonColorCSS.push(l)
}
})
let nrPkg
if (path.isAbsolute(argSrc)) {
nrPkg = require(path.join(argSrc, 'package.json'))
} else {
nrPkg = require(path.join(__dirname, argSrc, 'package.json'))
}
const now = new Date().toISOString()
const header = `/*
* Theme generated with Node-RED ${nrPkg.version} on ${now}
*/`
const output = sass.compileString(colorCSS.join('\n'), { style: argLong ? 'expanded' : 'compressed' })
if (argOut) {
await fs.writeFile(argOut, header + '\n' + output.css)
} else {
console.log(header)
console.log(output.css.toString())
}
await fs.remove(workingDir)
})()
function showUsageAndExit (exitCode) {
console.log('')
console.log('Usage: build [-?] [--in=FILE] [--out=FILE] [--src=PATH]')
console.log('Example: npm run build --src=../src/node-red')
console.log('Example: node build.js --in=colors.scss --out=compiled.css --src=../src/node-red')
console.log('')
console.log('Options:')
console.log(' --in FILE Custom colors sass file')
console.log(' --out FILE Where you write the result')
console.log(' --src PATH Path to src of node-red')
console.log(' --long Do not compress the colors.scss file')
console.log(' -?, --help Show this help')
console.log('')
process.exit(exitCode == null ? (parsedArgs.help ? 0 : 1) : exitCode)
}