From caf774f55cf9be543952bbe1fc908dad9e6907f9 Mon Sep 17 00:00:00 2001 From: "Prashanth.B" Date: Mon, 12 Aug 2024 15:22:28 +0530 Subject: [PATCH] Pass site_config as an optional arg. This change passes a path to a site_config into vite.js's proxyOptions so callers that don't have a standard `sites/` layout (eg frappe press dashboard - potentially any caller using frappe-ui as a lib) can configure the `webserver_port`. Also includes basic node test run via `node --test ./vite.test.js`. --- vite.js | 11 ++++++++-- vite.test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 vite.test.js diff --git a/vite.js b/vite.js index 9f9d689c..57e4e504 100644 --- a/vite.js +++ b/vite.js @@ -4,8 +4,15 @@ const fs = require('fs') module.exports = function proxyOptions({ port = 8080, source = '^/(app|login|api|assets|files)', - } = {}) { - const config = getCommonSiteConfig() + site_config_path = null, +} = {}) { + const config = site_config_path ? + ( + JSON.parse(fs.readFileSync(site_config_path)) + ) : + ( + getCommonSiteConfig() + ) const webserver_port = config ? config.webserver_port : 8000 if (!config) { console.log('No common_site_config.json found, using default port 8000') diff --git a/vite.test.js b/vite.test.js new file mode 100644 index 00000000..33cb9237 --- /dev/null +++ b/vite.test.js @@ -0,0 +1,62 @@ +const { describe, it, afterEach, beforeEach } = require('node:test') +const frappeui = require('./vite.js') +const fs = require('fs/promises') +const path = require('path') +const assert = require('assert') +const os = require('os') + + +describe('Test site_config resolution', () => { + + let tmpDir + let site_config_path + let cwd + const jsonData = { webserver_port: "1001" } + const proxySource = '^/(app|login|api|assets|files)' + const localhostTarget = 'http://127.0.0.1' + + beforeEach(async () => { + dirPath = path.join(os.tmpdir(), ts()) + tmpDir = await fs.mkdtemp(dirPath) + await fs.mkdir(path.join(tmpDir, 'sites')) + await fs.mkdir(path.join(tmpDir, 'apps')) + const sitesDir = path.join(tmpDir, 'sites') + scp = path.join(sitesDir, 'common_site_config.json') + await fs.writeFile(scp, JSON.stringify(jsonData), 'utf8') + + // TODO: avoid chdir in tests. Unfortunately the code under test uses + // relative paths, and changing that will need a bigger change than + // simply doing this. + cwd = process.cwd() + process.chdir(sitesDir) + }) + + afterEach(async () => { + process.chdir(cwd) + await fs.rm(tmpDir, { recursive: true }) + }) + + it('should find commmon_site_config.json', () => { + site_config = frappeui() + got_target = site_config.config().server.proxy[proxySource].target + want_target = localhostTarget + ':' + jsonData.webserver_port + assert.ok(got_target == want_target) + }) + + it('should read the provided commmon_site_config.json', async () => { + let testConfigPath = path.join(tmpDir, 'test.json') + let testConfig = { webserver_port: "1002" } + await fs.writeFile(testConfigPath, JSON.stringify(testConfig), 'utf8') + + site_config = frappeui({ site_config_path: testConfigPath }) + got_target = site_config.config().server.proxy[proxySource].target + want_target = localhostTarget + ':' + testConfig.webserver_port + assert.ok(got_target == want_target) + }) +}) + +function ts() { + const now = new Date(); + const ts = now.toISOString().replace(/[-:]/g, '') + return ts +}