node-red/scripts/build-custom-theme.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-05-22 13:27:28 +00:00
#!/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
//
// 3. Save the output of the script to a file - either redirect its output,
// or use the --out option.
//
// 4. Edit your settings file to set the theme:
// editorTheme: {
// page: {
2023-10-03 19:42:33 +00:00
// css: '/path/to/file/generated/by/this/script'
2019-05-22 13:27:28 +00:00
// }
// }
//
// 5. Restart Node-RED
//
2023-10-03 19:42:33 +00:00
const os = require('os');
const nopt = require('nopt');
const path = require('path');
const fs = require('fs-extra');
const sass = require('sass');
2019-05-22 13:27:28 +00:00
const knownOpts = {
2023-10-03 19:42:33 +00:00
'help': Boolean,
'long': Boolean,
'in': [path],
'out': [path]
2019-05-22 13:27:28 +00:00
};
const shortHands = {
2023-10-03 19:42:33 +00:00
'?':['--help']
2019-05-22 13:27:28 +00:00
};
nopt.invalidHandler = function(k,v,t) {}
const parsedArgs = nopt(knownOpts,shortHands,process.argv,2)
if (parsedArgs.help) {
2023-10-03 19:42:33 +00:00
showUsageAndExit(0)
2019-05-22 13:27:28 +00:00
}
2023-10-03 19:42:33 +00:00
if (!parsedArgs.in) {
console.warn('Missing argument: in')
showUsageAndExit(1)
2019-05-22 13:27:28 +00:00
}
(async function() {
const tmpDir = os.tmpdir();
const workingDir = await fs.mkdtemp(`${tmpDir}${path.sep}`);
2023-10-03 19:42:33 +00:00
await fs.copy(path.join(__dirname, '../packages/node_modules/@node-red/editor-client/src/sass/'), workingDir);
await fs.copyFile(parsedArgs.in, path.join(workingDir,'colors.scss'));
2023-10-03 19:42:33 +00:00
const output = sass.compile(
path.join(workingDir, 'style-custom-theme.scss'),
{style: parsedArgs.long === true ? 'expanded' : 'compressed'}
);
2023-10-03 19:42:33 +00:00
const nrPkg = require('../package.json');
const now = new Date().toISOString();
2023-10-03 19:42:33 +00:00
const header = `/*\n* Theme generated with Node-RED ${nrPkg.version} on ${now}\n*/`;
if (parsedArgs.out) {
2023-10-03 19:42:33 +00:00
await fs.writeFile(parsedArgs.out, header+'\n'+output.css);
} else {
console.log(header);
console.log(output.css.toString());
}
2023-10-03 19:42:33 +00:00
await fs.remove(workingDir);
})()
2023-10-03 19:42:33 +00:00
function showUsageAndExit (exitCode) {
console.log('');
console.log('Usage: build-custom-theme [-?] [--in FILE] [--out FILE]');
console.log('');
console.log('Options:');
console.log(' --in FILE Custom colors sass file');
console.log(' --out FILE Where you write the result');
console.log(' --long Do not compress the output');
console.log(' -?, --help Show this help');
console.log('');
process.exit(exitCode);
}