2023-05-19 22:55:29 +03:00
|
|
|
/** @file This script is for watching the whole IDE and spawning the electron process.
|
2023-03-03 01:00:47 +03:00
|
|
|
*
|
|
|
|
* It sets up watchers for the client and content, and spawns the electron process with the IDE.
|
|
|
|
* The spawned electron process can then use its refresh capability to pull the latest changes
|
|
|
|
* from the watchers.
|
|
|
|
*
|
2023-05-19 22:55:29 +03:00
|
|
|
* If the electron app is closed, the script will restart it, allowing to test the IDE setup.
|
|
|
|
* To stop, use Ctrl+C. */
|
2023-03-03 01:00:47 +03:00
|
|
|
|
2023-03-15 06:42:14 +03:00
|
|
|
import * as childProcess from 'node:child_process'
|
|
|
|
import * as fs from 'node:fs/promises'
|
|
|
|
import * as path from 'node:path'
|
2023-06-19 02:02:08 +03:00
|
|
|
import * as url from 'node:url'
|
2023-03-16 16:09:31 +03:00
|
|
|
import process from 'node:process'
|
2023-03-03 01:00:47 +03:00
|
|
|
|
2023-03-15 06:42:14 +03:00
|
|
|
import * as esbuild from 'esbuild'
|
2023-03-03 01:00:47 +03:00
|
|
|
|
2023-03-31 17:19:07 +03:00
|
|
|
import * as clientBundler from './esbuild-config'
|
|
|
|
import * as contentBundler from '../content/esbuild-config'
|
|
|
|
import * as dashboardBundler from '../dashboard/esbuild-config'
|
|
|
|
import * as paths from './paths'
|
2023-03-03 01:00:47 +03:00
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
// =============
|
|
|
|
// === Types ===
|
|
|
|
// =============
|
|
|
|
|
2023-03-03 01:00:47 +03:00
|
|
|
/** Set of esbuild watches for the client and content. */
|
|
|
|
interface Watches {
|
|
|
|
client: esbuild.BuildResult
|
2023-03-31 17:19:07 +03:00
|
|
|
dashboard: esbuild.BuildResult
|
2023-03-03 01:00:47 +03:00
|
|
|
content: esbuild.BuildResult
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
// =================
|
|
|
|
// === Constants ===
|
|
|
|
// =================
|
|
|
|
|
2023-06-19 02:02:08 +03:00
|
|
|
/** The path of this file. */
|
|
|
|
const THIS_PATH = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)))
|
2023-03-15 06:42:14 +03:00
|
|
|
const IDE_DIR_PATH = paths.getIdeDirectory()
|
|
|
|
const PROJECT_MANAGER_BUNDLE_PATH = paths.getProjectManagerBundlePath()
|
2023-03-03 01:00:47 +03:00
|
|
|
|
2023-05-19 22:55:29 +03:00
|
|
|
// =============
|
|
|
|
// === Watch ===
|
|
|
|
// =============
|
|
|
|
|
2023-03-03 01:00:47 +03:00
|
|
|
console.log('Cleaning IDE dist directory.')
|
2023-03-15 06:42:14 +03:00
|
|
|
await fs.rm(IDE_DIR_PATH, { recursive: true, force: true })
|
|
|
|
await fs.mkdir(IDE_DIR_PATH, { recursive: true })
|
2023-03-03 01:00:47 +03:00
|
|
|
|
2023-03-31 17:19:07 +03:00
|
|
|
const ALL_BUNDLES_READY = new Promise<Watches>((resolve, reject) => {
|
2023-03-15 06:42:14 +03:00
|
|
|
void (async () => {
|
|
|
|
console.log('Bundling client.')
|
2023-03-31 17:19:07 +03:00
|
|
|
const clientBundlerOpts = clientBundler.bundlerOptionsFromEnv()
|
|
|
|
clientBundlerOpts.outdir = path.resolve(IDE_DIR_PATH)
|
|
|
|
// Eslint is wrong here; `clientBundlerOpts.plugins` is actually `undefined`.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
;(clientBundlerOpts.plugins ??= []).push({
|
|
|
|
name: 'enso-on-rebuild',
|
|
|
|
setup: build => {
|
|
|
|
build.onEnd(result => {
|
|
|
|
if (result.errors.length) {
|
2023-05-19 22:55:29 +03:00
|
|
|
// We cannot carry on if the client failed to build, because electron
|
2023-03-15 06:42:14 +03:00
|
|
|
// would immediately exit with an error.
|
2023-03-31 17:19:07 +03:00
|
|
|
console.error('Client watch bundle failed:', result.errors[0])
|
|
|
|
reject(result.errors[0])
|
2023-03-15 06:42:14 +03:00
|
|
|
} else {
|
|
|
|
console.log('Client bundle updated.')
|
|
|
|
}
|
2023-03-31 17:19:07 +03:00
|
|
|
})
|
2023-03-03 01:00:47 +03:00
|
|
|
},
|
2023-03-31 17:19:07 +03:00
|
|
|
})
|
|
|
|
const clientBuilder = await esbuild.context(clientBundlerOpts)
|
|
|
|
const client = await clientBuilder.rebuild()
|
2023-03-15 06:42:14 +03:00
|
|
|
console.log('Result of client bundling: ', client)
|
2023-03-31 17:19:07 +03:00
|
|
|
void clientBuilder.watch()
|
|
|
|
|
|
|
|
console.log('Bundling dashboard.')
|
|
|
|
const dashboardOpts = dashboardBundler.bundleOptions()
|
|
|
|
dashboardOpts.plugins.push({
|
|
|
|
name: 'enso-on-rebuild',
|
|
|
|
setup: build => {
|
|
|
|
build.onEnd(() => {
|
|
|
|
console.log('Dashboard bundle updated.')
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
dashboardOpts.outdir = path.resolve(IDE_DIR_PATH, 'assets')
|
|
|
|
const dashboardBuilder = await esbuild.context(dashboardOpts)
|
|
|
|
const dashboard = await dashboardBuilder.rebuild()
|
|
|
|
console.log('Result of dashboard bundling: ', dashboard)
|
|
|
|
// We do not need to serve the dashboard as it outputs to the same directory.
|
|
|
|
// It will not rebuild on request, but it is not intended to rebuild on request anyway.
|
|
|
|
// This MUST be called before `builder.watch()` as `tailwind.css` must be generated
|
|
|
|
// before the copy plugin runs.
|
|
|
|
void dashboardBuilder.watch()
|
2023-03-03 01:00:47 +03:00
|
|
|
|
2023-03-15 06:42:14 +03:00
|
|
|
console.log('Bundling content.')
|
2023-05-26 13:17:03 +03:00
|
|
|
const contentOpts = contentBundler.bundlerOptionsFromEnv({
|
2023-08-25 16:19:31 +03:00
|
|
|
devMode: process.env.DEV_MODE !== 'false',
|
2023-05-26 13:17:03 +03:00
|
|
|
supportsLocalBackend: true,
|
|
|
|
supportsDeepLinks: false,
|
|
|
|
})
|
2023-03-31 17:19:07 +03:00
|
|
|
contentOpts.plugins.push({
|
|
|
|
name: 'enso-on-rebuild',
|
|
|
|
setup: build => {
|
|
|
|
build.onEnd(() => {
|
|
|
|
console.log('Content bundle updated.')
|
|
|
|
})
|
|
|
|
},
|
2023-03-15 06:42:14 +03:00
|
|
|
})
|
2023-06-19 02:02:08 +03:00
|
|
|
contentOpts.pure.splice(contentOpts.pure.indexOf('assert'), 1)
|
|
|
|
;(contentOpts.inject = contentOpts.inject ?? []).push(
|
|
|
|
path.resolve(THIS_PATH, '..', '..', 'debugGlobals.ts')
|
|
|
|
)
|
2023-03-15 06:42:14 +03:00
|
|
|
contentOpts.outdir = path.resolve(IDE_DIR_PATH, 'assets')
|
2023-05-26 13:17:03 +03:00
|
|
|
contentOpts.define.REDIRECT_OVERRIDE = JSON.stringify('http://localhost:8080')
|
2023-03-31 17:19:07 +03:00
|
|
|
const contentBuilder = await esbuild.context(contentOpts)
|
|
|
|
const content = await contentBuilder.rebuild()
|
2023-03-15 06:42:14 +03:00
|
|
|
console.log('Result of content bundling: ', content)
|
2023-03-31 17:19:07 +03:00
|
|
|
void contentBuilder.watch()
|
|
|
|
|
|
|
|
resolve({ client, dashboard, content })
|
2023-03-15 06:42:14 +03:00
|
|
|
})()
|
2023-03-03 01:00:47 +03:00
|
|
|
})
|
|
|
|
|
2023-03-31 17:19:07 +03:00
|
|
|
await ALL_BUNDLES_READY
|
2023-03-03 01:00:47 +03:00
|
|
|
console.log('Exposing Project Manager bundle.')
|
2023-03-15 06:42:14 +03:00
|
|
|
await fs.symlink(
|
|
|
|
PROJECT_MANAGER_BUNDLE_PATH,
|
|
|
|
path.join(IDE_DIR_PATH, paths.PROJECT_MANAGER_BUNDLE),
|
|
|
|
'dir'
|
|
|
|
)
|
2023-03-03 01:00:47 +03:00
|
|
|
|
2023-04-11 18:18:58 +03:00
|
|
|
const ELECTRON_ARGS = [path.join(IDE_DIR_PATH, 'index.cjs'), '--', ...process.argv.slice(2)]
|
2023-03-03 01:00:47 +03:00
|
|
|
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
console.log('SIGINT received. Exiting.')
|
2023-05-19 22:55:29 +03:00
|
|
|
// The `esbuild` process seems to remain alive at this point and will keep our process
|
|
|
|
// from ending. Thus, we exit manually. It seems to terminate the child `esbuild` process
|
|
|
|
// as well.
|
2023-03-03 01:00:47 +03:00
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
|
2023-07-19 12:48:39 +03:00
|
|
|
while (true) {
|
2023-03-03 01:00:47 +03:00
|
|
|
console.log('Spawning Electron process.')
|
2023-03-15 06:42:14 +03:00
|
|
|
const electronProcess = childProcess.spawn('electron', ELECTRON_ARGS, {
|
2023-03-03 01:00:47 +03:00
|
|
|
stdio: 'inherit',
|
|
|
|
shell: true,
|
|
|
|
})
|
|
|
|
console.log('Waiting for Electron process to finish.')
|
|
|
|
const result = await new Promise((resolve, reject) => {
|
|
|
|
electronProcess.on('close', resolve)
|
|
|
|
electronProcess.on('error', reject)
|
|
|
|
})
|
|
|
|
console.log('Electron process finished. Exit code: ', result)
|
|
|
|
}
|