Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass site_config as an optional arg. #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}