Skip to content

Commit

Permalink
Pass site_config as an optional arg.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Desi Notorious committed Aug 12, 2024
1 parent bb6bce9 commit ef91ce0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
11 changes: 9 additions & 2 deletions vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
62 changes: 62 additions & 0 deletions vite.test.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ef91ce0

Please sign in to comment.