2019-08-20 00:09:29 +03:00
|
|
|
const
|
2019-11-16 21:51:46 +03:00
|
|
|
chokidar = require('chokidar')
|
|
|
|
const debounce = require('lodash.debounce')
|
|
|
|
const path = require('path')
|
|
|
|
const { readFileSync, writeFileSync } = require('fs-extra')
|
2019-08-20 00:09:29 +03:00
|
|
|
|
|
|
|
const
|
2019-11-16 21:51:46 +03:00
|
|
|
{ spawn } = require('./helpers/spawn')
|
|
|
|
const onShutdown = require('./helpers/on-shutdown')
|
|
|
|
const generator = require('./generator')
|
|
|
|
const { appDir, tauriDir } = require('./helpers/app-paths')
|
|
|
|
|
|
|
|
const logger = require('./helpers/logger')
|
|
|
|
const log = logger('app:tauri', 'green')
|
|
|
|
const warn = log('app:tauri (template)', 'red')
|
2019-08-20 00:09:29 +03:00
|
|
|
|
2019-10-07 21:36:56 +03:00
|
|
|
class Runner {
|
2019-11-16 21:51:46 +03:00
|
|
|
constructor () {
|
2019-08-20 00:09:29 +03:00
|
|
|
this.pid = 0
|
|
|
|
this.tauriWatcher = null
|
|
|
|
onShutdown(() => {
|
|
|
|
this.stop()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
async run (cfg) {
|
2019-08-22 03:54:49 +03:00
|
|
|
process.env.TAURI_DIST_DIR = cfg.build.distDir
|
2019-10-07 21:36:56 +03:00
|
|
|
process.env.TAURI_CONFIG_DIR = tauriDir
|
2019-08-22 03:54:49 +03:00
|
|
|
const url = cfg.build.APP_URL
|
2019-08-20 00:09:29 +03:00
|
|
|
|
|
|
|
if (this.pid) {
|
|
|
|
if (this.url !== url) {
|
|
|
|
await this.stop()
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.__manipulateToml(toml => {
|
|
|
|
this.__whitelistApi(cfg, toml)
|
|
|
|
})
|
|
|
|
|
2019-08-22 00:35:52 +03:00
|
|
|
generator.generate(cfg.tauri)
|
|
|
|
|
2019-08-20 00:09:29 +03:00
|
|
|
this.url = url
|
|
|
|
|
|
|
|
const args = ['--url', url]
|
|
|
|
|
|
|
|
const startDevTauri = () => {
|
|
|
|
return this.__runCargoCommand({
|
|
|
|
cargoArgs: ['run', '--features', 'dev'],
|
|
|
|
extraArgs: args
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start watching for tauri app changes
|
|
|
|
this.tauriWatcher = chokidar
|
|
|
|
.watch([
|
2019-10-07 21:36:56 +03:00
|
|
|
path.join(tauriDir, 'src'),
|
|
|
|
path.join(tauriDir, 'Cargo.toml'),
|
2019-10-15 15:53:34 +03:00
|
|
|
path.join(tauriDir, 'build.rs'),
|
2019-10-15 18:07:48 +03:00
|
|
|
path.join(appDir, 'tauri.conf.js')
|
2019-08-20 00:09:29 +03:00
|
|
|
], {
|
|
|
|
watchers: {
|
|
|
|
chokidar: {
|
|
|
|
ignoreInitial: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('change', debounce(async () => {
|
|
|
|
await this.__stopCargo()
|
|
|
|
startDevTauri()
|
|
|
|
}, 1000))
|
|
|
|
|
|
|
|
return startDevTauri()
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
async build (cfg) {
|
2019-08-22 03:54:49 +03:00
|
|
|
process.env.TAURI_DIST_DIR = cfg.build.distDir
|
2019-10-07 21:36:56 +03:00
|
|
|
process.env.TAURI_CONFIG_DIR = tauriDir
|
2019-08-22 03:54:49 +03:00
|
|
|
|
2019-08-20 00:09:29 +03:00
|
|
|
this.__manipulateToml(toml => {
|
|
|
|
this.__whitelistApi(cfg, toml)
|
|
|
|
})
|
|
|
|
|
2019-08-22 00:35:52 +03:00
|
|
|
generator.generate(cfg.tauri)
|
|
|
|
|
2019-08-20 00:09:29 +03:00
|
|
|
const features = []
|
|
|
|
if (cfg.tauri.embeddedServer.active) {
|
|
|
|
features.push('embedded-server')
|
|
|
|
}
|
|
|
|
|
|
|
|
const buildFn = target => this.__runCargoCommand({
|
|
|
|
cargoArgs: [cfg.tauri.bundle.active ? 'tauri-bundle' : 'build']
|
|
|
|
.concat(features.length ? ['--features', ...features] : [])
|
|
|
|
.concat(cfg.ctx.debug ? [] : ['--release'])
|
|
|
|
.concat(target ? ['--target', target] : [])
|
|
|
|
})
|
|
|
|
|
|
|
|
if (cfg.ctx.debug || !cfg.ctx.targetName) {
|
2019-10-07 21:36:56 +03:00
|
|
|
// on debug mode or if no target specified,
|
2019-08-20 00:09:29 +03:00
|
|
|
// build only for the current platform
|
|
|
|
return buildFn()
|
|
|
|
}
|
|
|
|
|
|
|
|
const targets = cfg.ctx.target.split(',')
|
|
|
|
|
|
|
|
for (const target of targets) {
|
|
|
|
await buildFn(target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
stop () {
|
2019-08-20 00:09:29 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.tauriWatcher && this.tauriWatcher.close()
|
|
|
|
this.__stopCargo().then(resolve)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
__runCargoCommand ({
|
2019-08-20 00:09:29 +03:00
|
|
|
cargoArgs,
|
|
|
|
extraArgs
|
|
|
|
}) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
this.pid = spawn(
|
|
|
|
'cargo',
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
extraArgs
|
|
|
|
? cargoArgs.concat(['--']).concat(extraArgs)
|
|
|
|
: cargoArgs,
|
2019-08-20 00:09:29 +03:00
|
|
|
|
2019-10-07 21:36:56 +03:00
|
|
|
tauriDir,
|
2019-08-20 00:09:29 +03:00
|
|
|
|
|
|
|
code => {
|
|
|
|
if (code) {
|
|
|
|
warn()
|
2019-11-16 21:51:46 +03:00
|
|
|
warn('⚠️ [FAIL] Cargo CLI has failed')
|
2019-08-20 00:09:29 +03:00
|
|
|
warn()
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.killPromise) {
|
|
|
|
this.killPromise()
|
|
|
|
this.killPromise = null
|
|
|
|
} else { // else it wasn't killed by us
|
|
|
|
warn()
|
|
|
|
warn('Cargo process was killed. Exiting...')
|
|
|
|
warn()
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
__stopCargo () {
|
2019-08-20 00:09:29 +03:00
|
|
|
const pid = this.pid
|
|
|
|
|
|
|
|
if (!pid) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
log('Shutting down tauri process...')
|
|
|
|
this.pid = 0
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.killPromise = resolve
|
|
|
|
process.kill(pid)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
__manipulateToml (callback) {
|
|
|
|
const toml = require('@iarna/toml')
|
|
|
|
const tomlPath = path.join(tauriDir, 'Cargo.toml')
|
|
|
|
const tomlFile = readFileSync(tomlPath)
|
|
|
|
const tomlContents = toml.parse(tomlFile)
|
2019-08-20 00:09:29 +03:00
|
|
|
|
|
|
|
callback(tomlContents)
|
|
|
|
|
|
|
|
const output = toml.stringify(tomlContents)
|
|
|
|
writeFileSync(tomlPath, output)
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:51:46 +03:00
|
|
|
__whitelistApi (cfg, tomlContents) {
|
2019-08-20 00:09:29 +03:00
|
|
|
if (!tomlContents.dependencies.tauri.features) {
|
|
|
|
tomlContents.dependencies.tauri.features = []
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.tauri.whitelist.all) {
|
|
|
|
if (!tomlContents.dependencies.tauri.features.includes('all-api')) {
|
|
|
|
tomlContents.dependencies.tauri.features.push('all-api')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const whitelist = Object.keys(cfg.tauri.whitelist).filter(w => cfg.tauri.whitelist[w] === true)
|
|
|
|
tomlContents.dependencies.tauri.features = whitelist.concat(tomlContents.dependencies.tauri.features.filter(f => f !== 'api' && cfg.tauri.whitelist[f] !== true))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 21:36:56 +03:00
|
|
|
module.exports = Runner
|