2022-07-04 16:15:01 +03:00
|
|
|
const handler = require('serve-handler');
|
|
|
|
const http = require('http');
|
|
|
|
const chalk = require('chalk');
|
|
|
|
const {spawn} = require('child_process');
|
|
|
|
const minimist = require('minimist');
|
|
|
|
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
const log = console.log;
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
|
|
|
|
let yarnStartProcess;
|
2022-07-05 12:53:27 +03:00
|
|
|
let tailwindServerProcess;
|
2022-07-04 16:15:01 +03:00
|
|
|
let stdOutChunks = [];
|
|
|
|
let stdErrChunks = [];
|
|
|
|
let startYarnOutput = false;
|
|
|
|
|
2022-07-04 16:19:49 +03:00
|
|
|
const {v, verbose, port = 5369} = minimist(process.argv.slice(2));
|
2022-07-04 16:15:01 +03:00
|
|
|
const showVerbose = !!(v || verbose);
|
|
|
|
|
|
|
|
function clearConsole({withHistory = true} = {}) {
|
|
|
|
if (!withHistory) {
|
|
|
|
process.stdout.write('\x1Bc');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
process.stdout.write(
|
|
|
|
process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function printConfigInstruction() {
|
|
|
|
const data = {
|
2022-07-04 16:32:35 +03:00
|
|
|
comments: {
|
2022-07-04 16:22:10 +03:00
|
|
|
url: `http://localhost:${port}/comments`
|
2022-07-04 16:15:01 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const stringifedData = JSON.stringify(data, null, 2);
|
|
|
|
const splitData = stringifedData.split('\n');
|
|
|
|
log();
|
|
|
|
splitData.forEach((data, idx, arr) => {
|
|
|
|
if (idx === 0 || idx === arr.length - 1) {
|
|
|
|
log(chalk.grey(data));
|
|
|
|
} else {
|
|
|
|
log(chalk.bold.whiteBright(data));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
log();
|
|
|
|
}
|
|
|
|
|
|
|
|
function printInstructions() {
|
|
|
|
log();
|
2022-07-04 16:33:51 +03:00
|
|
|
log(chalk.yellowBright.underline(`Add comments to your local Ghost config`));
|
2022-07-04 16:15:01 +03:00
|
|
|
printConfigInstruction();
|
|
|
|
log(chalk.cyanBright('='.repeat(50)));
|
|
|
|
log();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onProcessClose(code) {
|
|
|
|
yarnStartProcess = null;
|
2022-07-05 12:53:27 +03:00
|
|
|
tailwindServerProcess = null;
|
2022-07-04 16:15:01 +03:00
|
|
|
stdErrChunks = [];
|
|
|
|
stdOutChunks = [];
|
|
|
|
log(chalk.redBright.bold.underline(`Please restart the script...\n`));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBuildOptions() {
|
|
|
|
process.env.FORCE_COLOR = 'true';
|
|
|
|
const options = {
|
|
|
|
shell: true,
|
|
|
|
env: process.env
|
|
|
|
};
|
|
|
|
if (showVerbose) {
|
|
|
|
options.stdio = 'inherit';
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
function doYarnStart() {
|
|
|
|
if (yarnStartProcess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const options = getBuildOptions();
|
|
|
|
yarnStartProcess = spawn('yarn start:combined', options);
|
|
|
|
|
|
|
|
['SIGINT', 'SIGTERM'].forEach(function (sig) {
|
|
|
|
yarnStartProcess.on(sig, function () {
|
|
|
|
yarnStartProcess && yarnStartProcess.exit();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
yarnStartProcess.on('close', onProcessClose);
|
|
|
|
|
|
|
|
if (!showVerbose) {
|
|
|
|
yarnStartProcess.stdout.on('data', (data) => {
|
|
|
|
stdOutChunks.push(data);
|
|
|
|
printYarnProcessOutput(data);
|
|
|
|
});
|
|
|
|
yarnStartProcess.stderr.on('data', (data) => {
|
|
|
|
log(Buffer.from(data).toString());
|
|
|
|
stdErrChunks.push(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 12:53:27 +03:00
|
|
|
function doTailwindServerStart() {
|
|
|
|
if (tailwindServerProcess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const options = getBuildOptions();
|
|
|
|
tailwindServerProcess = spawn('yarn tailwind', options);
|
|
|
|
|
|
|
|
['SIGINT', 'SIGTERM'].forEach(function (sig) {
|
|
|
|
tailwindServerProcess.on(sig, function () {
|
|
|
|
tailwindServerProcess && tailwindServerProcess.exit();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tailwindServerProcess.on('close', onProcessClose);
|
|
|
|
|
|
|
|
if (!showVerbose) {
|
|
|
|
tailwindServerProcess.stdout.on('data', (data) => {
|
|
|
|
stdOutChunks.push(data);
|
|
|
|
printYarnProcessOutput(data);
|
|
|
|
});
|
|
|
|
tailwindServerProcess.stderr.on('data', (data) => {
|
|
|
|
log(Buffer.from(data).toString());
|
|
|
|
stdErrChunks.push(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-04 16:15:01 +03:00
|
|
|
function printYarnProcessOutput(data) {
|
|
|
|
const dataStr = Buffer.from(data).toString();
|
|
|
|
const dataArr = dataStr.split('\n').filter((d) => {
|
|
|
|
return /\S/.test(d.trim());
|
|
|
|
});
|
|
|
|
if (dataArr.find(d => d.includes('Starting the development'))) {
|
|
|
|
startYarnOutput = true;
|
|
|
|
log(chalk.yellowBright('Starting the development server...\n'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dataArr.forEach((dataOut) => {
|
|
|
|
if (startYarnOutput) {
|
|
|
|
log(dataOut);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (startYarnOutput) {
|
|
|
|
log();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function startDevServer() {
|
|
|
|
const server = http.createServer((request, response) => {
|
|
|
|
return handler(request, response, {
|
|
|
|
rewrites: [
|
2022-07-04 16:22:10 +03:00
|
|
|
{source: '/comments', destination: 'scripts/load-portal.js'}
|
2022-07-04 16:15:01 +03:00
|
|
|
],
|
|
|
|
headers: [
|
|
|
|
{
|
|
|
|
source: '**',
|
|
|
|
headers: [{
|
|
|
|
key: 'Cache-Control',
|
|
|
|
value: 'no-cache'
|
|
|
|
},{
|
|
|
|
key: 'Access-Control-Allow-Origin',
|
|
|
|
value: '*'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(port, () => {
|
2022-07-04 16:33:51 +03:00
|
|
|
log(chalk.whiteBright(`Comments dev server is running on http://localhost:${port}`));
|
2022-07-04 16:15:01 +03:00
|
|
|
printInstructions();
|
|
|
|
doYarnStart();
|
2022-07-05 12:53:27 +03:00
|
|
|
doTailwindServerStart();
|
2022-07-04 16:15:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
clearConsole({withHistory: false});
|
|
|
|
startDevServer();
|