Skip to content

Commit

Permalink
Configure for railway support and fix icon resolution during builds
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmflynn committed Sep 28, 2023
1 parent e7ad4f0 commit fe8eb59
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/introduction/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ To use a service, you should (1) check for the existence of the service, then (2
}
```

If your service will be accessible from a particular URL, you'll want to make sure that the `port` used is derived from the first optional command-line argument. In Node.js, this looks like:
If your service will be accessible from a particular URL, you'll want to make sure that the `port` used is derived from the `PORT` environment variable:

```js
const port = process.argv[2]
const port = process.env.PORT || 3000
```

### Using Services in Production
Expand Down
2 changes: 1 addition & 1 deletion packages/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default async function build ({ target, platform }: BuildOptions, config?

if (toBuild.frontend) {
if (target === 'mobile') await mobile.prebuild(resolvedConfig) // Run mobile prebuild command
await ViteBuild(resolveViteConfig(config, { build: true })) // Build the standard output files using Vite. Force recognition as build
await ViteBuild(resolveViteConfig(resolvedConfig, { build: true })) // Build the standard output files using Vite. Force recognition as build
}

// Build services if not specifically the frontend
Expand Down
7 changes: 6 additions & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ export async function resolveConfig(o: UserConfig = {}) {
// Remove services that are not specified
const selectedServices = cliArgs.service
if (selectedServices) {
const selected = !Array.isArray(selectedServices) ? [ selectedServices ] : selectedServices
const isSingleService = !Array.isArray(selectedServices)
const selected = isSingleService ? [ selectedServices ] : selectedServices
for (let name in copy.services) {
if (!selected.includes(name)) delete copy.services[name]
else if (isSingleService) {
const customPort = cliArgs.port || process.env.PORT
if (customPort) copy.services[name].port = customPort
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions packages/core/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import electron from 'vite-plugin-electron'
import renderer from 'vite-plugin-electron-renderer'
import { VitePWA } from 'vite-plugin-pwa'

import { join, normalize } from 'node:path'
import { join, normalize, extname } from 'node:path'

import { rootDir, userPkg, scopedOutDir, target, command, cliArgs, outDir } from "./globals";
import { getIcon } from './utils/index'
Expand All @@ -18,23 +18,25 @@ export const resolveViteConfig = (commonersConfig = {}, opts = {}) => {

const config = { ... commonersConfig }

// Add extra global state variables

// Sanitize the global configuration object
const icon = getIcon(config.icon)

function assetPath(path) {
return `./${normalize(`${build ? '' : 'dist/'}.commoners/assets/${path}`)}`
}

const plugins = [
{
name: 'commoners',
transformIndexHtml(html) {
return `
${
icon ? `<link rel="icon" type="image/x-icon" href="./${normalize(icon)}">` : '' // Add favicon
icon ? `<link rel="shortcut icon" type="image/${extname(icon).slice(1)}" href="${assetPath(icon)}">` : '' // Add favicon
}
<script type="module">
// Directly import the plugins from the transpiled configuration object
import COMMONERS_CONFIG from "./${build ? '' : 'dist/'}.commoners/assets/commoners.config.mjs"
import COMMONERS_CONFIG from "${assetPath('commoners.config.mjs')}"
const { plugins } = COMMONERS_CONFIG
// Set global variable
Expand Down
2 changes: 1 addition & 1 deletion template/src/main/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export async function start (config, id, roots) {

if (ext === '.js') process = node(config)
else if (ext === '.py') process = python(config)
else if (!ext || ext === '.exe') process = spawn(config.abspath, [config.port]) // Run executables as extra resources
else if (!ext || ext === '.exe') process = spawn(config.abspath, [], { env: { ...process.env, PORT: config.port } }) // Run executables as extra resources

if (process) {
const label = id ?? 'commoners-service'
Expand Down
2 changes: 1 addition & 1 deletion template/src/main/services/node/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fork } from "node:child_process"


export default ({ abspath, port }) => fork(abspath, [ port ], { silent: true })
export default ({ abspath, port }) => fork(abspath, [ ], { silent: true, env: { ...process.env, PORT: port } })
2 changes: 1 addition & 1 deletion template/src/main/services/python/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { spawn } from 'node:child_process';

export default ({ src, port }) => spawn("python", [src, port])
export default ({ src, port }) => spawn("python", [src], { env: { ...process.env, PORT: port } })

0 comments on commit fe8eb59

Please sign in to comment.