123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import { existsSync, readFileSync } from 'fs-extra'
- import { TauriConfig } from 'types'
- import merge from 'webpack-merge'
- import logger from '../helpers/logger'
- import * as appPaths from './app-paths'
- import nonWebpackRequire from '../helpers/non-webpack-require'
- import chalk from 'chalk'
- import { isTauriConfig, ajv } from '../types/config.validator'
- const error = logger('ERROR:', chalk.red)
- const getTauriConfig = (cfg: Partial<TauriConfig>): TauriConfig => {
- const pkgPath = appPaths.resolve.app('package.json')
- const tauriConfPath = appPaths.resolve.tauri('tauri.conf.json')
- if (!existsSync(pkgPath)) {
- error("Could not find a package.json in your app's directory.")
- process.exit(1)
- }
- if (!existsSync(tauriConfPath)) {
- error(
- "Could not find a tauri config (tauri.conf.json) in your app's directory."
- )
- process.exit(1)
- }
- const tauriConf = JSON.parse(readFileSync(tauriConfPath).toString()) as TauriConfig
- const pkg = nonWebpackRequire(pkgPath) as { productName: string }
- const config = merge(
- {
- build: {},
- ctx: {},
- tauri: {
- embeddedServer: {
- active: true,
- port: "3000"
- },
- bundle: {
- active: true,
- icon: [],
- resources: [],
- externalBin: [],
- deb: {
- depends: []
- },
- osx: {
- frameworks: []
- }
- },
- whitelist: {
- all: false
- },
- window: {
- title: pkg.productName
- },
- security: {
- csp: "default-src blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'"
- },
- edge: {
- active: true
- },
- inliner: {
- active: true
- }
- }
- } as any,
- tauriConf as any,
- cfg as any
- ) as TauriConfig
- if (!isTauriConfig(config)) {
- const messages = ajv.errorsText(
- isTauriConfig.errors?.filter(e => e.keyword !== 'if').map(e => {
- e.dataPath = e.dataPath.replace(/\./g, ' > ')
- if (e.keyword === 'additionalProperties' && typeof e.message === 'string' && 'additionalProperty' in e.params) {
- e.message = `has unknown property ${e.params.additionalProperty}`
- }
- return e
- }), { dataVar: 'tauri.conf.json', separator: '\n' }
- ).split('\n')
- for (const message of messages) {
- error(message)
- }
- process.exit(1)
- }
- const runningDevServer = config.build.devPath?.startsWith('http')
- if (!runningDevServer) {
- config.build.devPath = appPaths.resolve.tauri(config.build.devPath)
- process.env.TAURI_DIST_DIR = config.build.devPath
- }
- if (config.build.distDir) {
- config.build.distDir = appPaths.resolve.tauri(config.build.distDir)
- process.env.TAURI_DIST_DIR = config.build.distDir
- }
- // OSX bundle config
- if (config.tauri.bundle.osx) {
- const license = config.tauri.bundle.osx.license
- if (typeof license === 'string') {
- config.tauri.bundle.osx.license = appPaths.resolve.tauri(license)
- } else if (license !== null) {
- const licensePath = appPaths.resolve.app('LICENSE')
- if (existsSync(licensePath)) {
- config.tauri.bundle.osx.license = licensePath
- }
- }
- }
- // bundle targets
- if (Array.isArray(config.tauri.bundle.targets)) {
- if (process.platform !== 'win32') {
- config.tauri.bundle.targets = config.tauri.bundle.targets.filter(t => t !== 'msi')
- }
- }
- process.env.TAURI_DIR = appPaths.tauriDir
- process.env.TAURI_CONFIG = JSON.stringify(config)
- return config
- }
- export default getTauriConfig
|