Skip to content

Commit

Permalink
Merge pull request #472 from stevenengland/fix-471
Browse files Browse the repository at this point in the history
fix(json5): fix support for json5 config file
  • Loading branch information
Apollon77 authored Mar 26, 2024
2 parents 9757405 + 8a540ca commit 5f594aa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
28 changes: 20 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ const nodemon_1 = __importDefault(require("nodemon"));
const os_1 = require("os");
const path = __importStar(require("path"));
const ps_tree_1 = __importDefault(require("ps-tree"));
const rimraf_1 = require("rimraf");
const semver_1 = require("semver");
const source_map_1 = require("source-map");
const ws_1 = __importDefault(require("ws"));
const jsonConfig_1 = require("./jsonConfig");
const logger_1 = require("./logger");
const chalk = require("chalk");
const rimraf_1 = require("rimraf");
const acorn = require("acorn");
const EventEmitter = require("events");
const DEFAULT_TEMP_DIR_NAME = '.dev-server';
Expand Down Expand Up @@ -280,6 +280,18 @@ class DevServer {
}
return port;
}
getJsonConfigPath() {
const jsonConfigPath = path.resolve(this.rootDir, 'admin/jsonConfig.json');
if ((0, fs_extra_1.existsSync)(jsonConfigPath)) {
return jsonConfigPath;
}
else if ((0, fs_extra_1.existsSync)(jsonConfigPath + '5')) {
return jsonConfigPath + '5';
}
else {
return '';
}
}
////////////////// Command Handlers //////////////////
async setup(adminPort, dependencies, backupFile, force, useSymlinks = false) {
if (force) {
Expand Down Expand Up @@ -518,7 +530,7 @@ class DevServer {
ws: true,
}));
}
else if ((0, fs_extra_1.existsSync)(path.resolve(this.rootDir, 'admin/jsonConfig.json'))) {
else if (this.getJsonConfigPath()) {
// JSON config
await this.createJsonConfigProxy(app, this.config);
}
Expand Down Expand Up @@ -587,25 +599,25 @@ class DevServer {
async createJsonConfigProxy(app, config) {
const browserSyncPort = this.getPort(config.adminPort, HIDDEN_BROWSER_SYNC_PORT_OFFSET);
const bs = this.startBrowserSync(browserSyncPort, false);
// whenever jsonConfig.json changes, we upload the new file
const jsonConfig = path.resolve(this.rootDir, 'admin/jsonConfig.json');
bs.watch(jsonConfig, undefined, async (e) => {
// whenever jsonConfig.json[5] changes, we upload the new file
const jsonConfigFile = this.getJsonConfigPath();
bs.watch(jsonConfigFile, undefined, async (e) => {
var _a;
if (e === 'change') {
const content = await (0, fs_extra_1.readFile)(jsonConfig);
const content = await (0, fs_extra_1.readFile)(jsonConfigFile);
(_a = this.websocket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify([
3,
46,
'writeFile',
[`${this.adapterName}.admin`, 'jsonConfig.json', Buffer.from(content).toString('base64')],
[`${this.adapterName}.admin`, path.basename(jsonConfigFile), Buffer.from(content).toString('base64')],
]));
}
});
// "proxy" for the main page which injects our script
const adminUrl = `http://127.0.0.1:${this.getPort(config.adminPort, HIDDEN_ADMIN_PORT_OFFSET)}`;
app.get('/', async (_req, res) => {
const { data } = await axios_1.default.get(adminUrl);
res.send((0, jsonConfig_1.injectCode)(data, this.adapterName));
res.send((0, jsonConfig_1.injectCode)(data, this.adapterName, path.basename(jsonConfigFile)));
});
// browser-sync proxy
app.use((0, http_proxy_middleware_1.createProxyMiddleware)(['/browser-sync/**'], {
Expand Down
4 changes: 2 additions & 2 deletions dist/jsonConfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.injectCode = void 0;
function injectCode(html, adapterName) {
function injectCode(html, adapterName, jsonConfigFileName) {
return html.replace('</head>', `
<script type="module">
import { io } from "https://cdn.socket.io/4.4.1/socket.io.esm.min.js";
Expand Down Expand Up @@ -32,7 +32,7 @@ socket.on("browser:reload", async () => {
async function readJsonConfig() {
return new Promise((resolve, reject) => {
window.io.emit("readFile", "${adapterName}.admin", "jsonConfig.json", (err, data, type) => {
window.io.emit("readFile", "${adapterName}.admin", "${jsonConfigFileName}", (err, data, type) => {
if (err) reject(err);
else resolve(data);
});
Expand Down
29 changes: 20 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
existsSync,
mkdir,
mkdirp,
readdir,
readFile,
readJson,
readdir,
rename,
unlinkSync,
writeFile,
Expand All @@ -29,13 +29,13 @@ import nodemon from 'nodemon';
import { EOL, hostname } from 'os';
import * as path from 'path';
import psTree from 'ps-tree';
import { rimraf } from 'rimraf';
import { gt } from 'semver';
import { RawSourceMap, SourceMapGenerator } from 'source-map';
import WebSocket from 'ws';
import { injectCode } from './jsonConfig';
import { Logger } from './logger';
import chalk = require('chalk');
import { rimraf } from 'rimraf';
import acorn = require('acorn');
import EventEmitter = require('events');

Expand Down Expand Up @@ -356,6 +356,17 @@ class DevServer {
return port;
}

private getJsonConfigPath(): string {
const jsonConfigPath = path.resolve(this.rootDir, 'admin/jsonConfig.json');
if (existsSync(jsonConfigPath)) {
return jsonConfigPath;
} else if (existsSync(jsonConfigPath + '5')) {
return jsonConfigPath + '5';
} else {
return '';
}
}

////////////////// Command Handlers //////////////////

async setup(
Expand Down Expand Up @@ -644,7 +655,7 @@ class DevServer {
ws: true,
}),
);
} else if (existsSync(path.resolve(this.rootDir, 'admin/jsonConfig.json'))) {
} else if (this.getJsonConfigPath()) {
// JSON config
await this.createJsonConfigProxy(app, this.config);
} else {
Expand Down Expand Up @@ -716,17 +727,17 @@ class DevServer {
const browserSyncPort = this.getPort(config.adminPort, HIDDEN_BROWSER_SYNC_PORT_OFFSET);
const bs = this.startBrowserSync(browserSyncPort, false);

// whenever jsonConfig.json changes, we upload the new file
const jsonConfig = path.resolve(this.rootDir, 'admin/jsonConfig.json');
bs.watch(jsonConfig, undefined, async (e) => {
// whenever jsonConfig.json[5] changes, we upload the new file
const jsonConfigFile = this.getJsonConfigPath();
bs.watch(jsonConfigFile, undefined, async (e) => {
if (e === 'change') {
const content = await readFile(jsonConfig);
const content = await readFile(jsonConfigFile);
this.websocket?.send(
JSON.stringify([
3,
46,
'writeFile',
[`${this.adapterName}.admin`, 'jsonConfig.json', Buffer.from(content).toString('base64')],
[`${this.adapterName}.admin`, path.basename(jsonConfigFile), Buffer.from(content).toString('base64')],
]),
);
}
Expand All @@ -736,7 +747,7 @@ class DevServer {
const adminUrl = `http://127.0.0.1:${this.getPort(config.adminPort, HIDDEN_ADMIN_PORT_OFFSET)}`;
app.get('/', async (_req, res) => {
const { data } = await axios.get<string>(adminUrl);
res.send(injectCode(data, this.adapterName));
res.send(injectCode(data, this.adapterName, path.basename(jsonConfigFile)));
});

// browser-sync proxy
Expand Down
4 changes: 2 additions & 2 deletions src/jsonConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function injectCode(html: string, adapterName: string): string {
export function injectCode(html: string, adapterName: string, jsonConfigFileName: string): string {
return html.replace(
'</head>',
`
Expand Down Expand Up @@ -31,7 +31,7 @@ socket.on("browser:reload", async () => {
async function readJsonConfig() {
return new Promise((resolve, reject) => {
window.io.emit("readFile", "${adapterName}.admin", "jsonConfig.json", (err, data, type) => {
window.io.emit("readFile", "${adapterName}.admin", "${jsonConfigFileName}", (err, data, type) => {
if (err) reject(err);
else resolve(data);
});
Expand Down

0 comments on commit 5f594aa

Please sign in to comment.