Files
zulip-desktop/tools/run-dev
Priyank Patel dd4f3047c3 tools: Add tools for running typescript compiler and electron app.
This tool handles edge cases where all the typescript files might not
be compiled when the electron process starts this defer that; it runs
tsc compiler first.

Next, we want to only run the compiler if the files are not
compiled.
2019-07-17 00:22:17 +05:30

90 lines
2.7 KiB
JavaScript

#!/usr/bin/env node
const path = require('path');
const glob = require('glob');
const chalk = require('chalk');
const { spawn } = require('child_process');
async function run(task, commandToRun, opts = {}) {
const args = commandToRun.split(' ');
let cmd = args[0];
args.splice(0, 1);
if (process.platform === 'win32' && /np(m|x)/.test(cmd)) {
cmd = cmd + '.cmd';
}
const defaults = {
cwd: path.resolve(__dirname, '..'),
env: process.env,
stdio: 'pipe'
};
opts = { ...defaults, ...opts };
task = ' ' + task + ' ';
const colors = [
{ bg: 'bold.bgGreen', color: 'bold.green' },
{ bg: 'bold.bgMagenta', color: 'bold.magenta' },
{ bg: 'bold.bgBlue', color: 'bold.blue' },
{ bg: 'bold.bgYellow', color: 'bold.yellow' },
{ bg: 'bold.bgCyan', color: 'bold.cyan' },
{ bg: 'bold.bgKeyword("rebeccapurple")', color: 'bold.keyword("rebeccapurple")' },
{ bg: 'bold.bgKeyword("darkslategray")', color: 'bold.keyword("darkslategray")' },
];
const randomColorIndex = Math.floor((Math.random() * (colors.length * 1000)) / 1000);
const randomColor = colors[randomColorIndex];
console.log(chalk`{${randomColor.bg} ${ task }} {${randomColor.color} ${commandToRun}}`);
const proc = spawn(cmd, args, opts);
proc.stderr.on('data', data => console.log(data.toString()));
proc.stdout.on('data', data => console.log(data.toString()));
return new Promise((resolve, reject) => {
function check(code) {
if (code !== 0) {
reject(chalk`{bgRed ERROR!} Running {red ${commandToRun}} failed with exit code: ${code}`);
} else {
resolve();
}
// we don't want to exit after compiling typescript files
// but instead want to do it if the tsc -w or electron app died.
if (commandToRun !== 'npx tsc') {
process.exit(0);
}
}
proc.on('exit', check);
proc.on('close', check);
proc.on('error', () => {
reject();
process.exit(0);
});
});
}
async function main() {
const baseFilePattern = 'app/+(main|renderer)/**/*';
const globOptions = { cwd: path.resolve(__dirname, '..') };
const jsFiles = glob.sync(baseFilePattern + '.js', globOptions);
const tsFiles = glob.sync(baseFilePattern + '.ts', globOptions);
// if the are missing compiled js file for typescript files
// run the typescript compiler before starting the app
if (jsFiles.length !== tsFiles.length) {
console.log('Compiling typescript files...');
await run('TypeScript', 'npx tsc');
}
await Promise.all([
run('Electron app', 'npx electron app --disable-http-cache --no-electron-connect'),
run('TypeScript watch mode', 'npx tsc --watch --pretty')
]);
}
main()
.catch(err => console.error(err));