From 3c7e7f6acc42aa27476614811c6b2ef0b68bf7a4 Mon Sep 17 00:00:00 2001 From: jeasonnow Date: Fri, 14 Jul 2023 22:14:01 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E5=AE=8C=E5=96=84=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + bin/helpers/merge.ts | 22 +++++++++++-------- bin/utils/dir.ts | 4 +++- dist/cli.js | 24 +++++++++++---------- package.json | 3 ++- plugins/pakeCliDevPlugin.js | 43 +++++++++++++++++++++++++++++++++++++ rollup.config.js | 21 +++++++++++++++--- src-tauri/Cargo.toml | 2 ++ src-tauri/src/util.rs | 9 +++++++- 9 files changed, 103 insertions(+), 26 deletions(-) create mode 100644 plugins/pakeCliDevPlugin.js diff --git a/.gitignore b/.gitignore index 43ac11e..f8267c8 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ dist src-tauri/.cargo/config src-tauri/.cargo/ .next +src-tauri/.pake/ \ No newline at end of file diff --git a/bin/helpers/merge.ts b/bin/helpers/merge.ts index d5fcb25..4f38e20 100644 --- a/bin/helpers/merge.ts +++ b/bin/helpers/merge.ts @@ -5,6 +5,7 @@ import { npmDirectory } from '@/utils/dir'; import combineFiles from '@/utils/combine'; import logger from '@/options/logger'; import { PakeAppOptions, PlatformMap } from '@/types'; +import { tauriConfigDirectory } from '../utils/dir'; export async function mergeConfig(url: string, options: PakeAppOptions, tauriConf: any) { const { @@ -200,20 +201,23 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon // Save config file. const platformConfigPaths: PlatformMap = { - win32: 'src-tauri/tauri.windows.conf.json', - darwin: 'src-tauri/tauri.macos.conf.json', - linux: 'src-tauri/tauri.linux.conf.json', + win32: 'tauri.windows.conf.json', + darwin: 'tauri.macos.conf.json', + linux: 'tauri.linux.conf.json', }; - const configPath = path.join(npmDirectory, platformConfigPaths[platform]); + const configPath = path.join(tauriConfigDirectory, platformConfigPaths[platform]); const bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } }; - await fsExtra.writeJson(configPath, bundleConf, { spaces: 4 }); - const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json'); - await fsExtra.writeJson(pakeConfigPath, tauriConf.pake, { spaces: 4 }); + await fsExtra.outputJSON(configPath, bundleConf, { spaces: 4 }); + const pakeConfigPath = path.join(tauriConfigDirectory, 'pake.json'); + await fsExtra.outputJSON(pakeConfigPath, tauriConf.pake, { spaces: 4 }); let tauriConf2 = JSON.parse(JSON.stringify(tauriConf)); delete tauriConf2.pake; delete tauriConf2.tauri.bundle; - const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json'); - await fsExtra.writeJson(configJsonPath, tauriConf2, { spaces: 4 }); + if (process.env.NODE_ENV === 'development') { + tauriConf2.tauri.bundle = bundleConf.tauri.bundle; + } + const configJsonPath = path.join(tauriConfigDirectory, 'tauri.conf.json'); + await fsExtra.outputJSON(configJsonPath, tauriConf2, { spaces: 4 }); } diff --git a/bin/utils/dir.ts b/bin/utils/dir.ts index c46d9fd..8e19caf 100644 --- a/bin/utils/dir.ts +++ b/bin/utils/dir.ts @@ -5,4 +5,6 @@ import { fileURLToPath } from 'url'; const currentModulePath = fileURLToPath(import.meta.url); // Resolve the parent directory of the current module -export const npmDirectory = process.env.DEV === 'true' ? process.cwd() : path.join(path.dirname(currentModulePath), '..'); +export const npmDirectory = path.join(path.dirname(currentModulePath), '..'); + +export const tauriConfigDirectory = process.env.NODE_ENV === 'development' ? path.join(npmDirectory, 'src-tauri', '.pake') : path.join(npmDirectory, 'src-tauri'); \ No newline at end of file diff --git a/dist/cli.js b/dist/cli.js index d7421d6..aa4585b 100644 --- a/dist/cli.js +++ b/dist/cli.js @@ -60,7 +60,7 @@ var scripts = { analyze: "cd src-tauri && cargo bloat --release --crates", tauri: "tauri", cli: "rollup -c rollup.config.js --watch", - "cli:dev": "cross-env DEV=true tsx watch ./bin/dev.ts & npm run dev", + "cli:dev": "cross-env NODE_ENV=development rollup -c rollup.config.js -w", "cli:build": "cross-env NODE_ENV=production rollup -c rollup.config.js", prepublishOnly: "npm run cli:build" }; @@ -88,6 +88,7 @@ var devDependencies = { "@rollup/plugin-alias": "^4.0.2", "@rollup/plugin-commonjs": "^23.0.2", "@rollup/plugin-json": "^5.0.2", + "@rollup/plugin-replace": "^5.0.2", "@rollup/plugin-terser": "^0.1.0", "@types/fs-extra": "^9.0.13", "@types/is-url": "^1.2.30", @@ -362,7 +363,8 @@ const IS_LINUX = platform$1 === 'linux'; // Convert the current module URL to a file path const currentModulePath = fileURLToPath(import.meta.url); // Resolve the parent directory of the current module -const npmDirectory = process.env.DEV === 'true' ? process.cwd() : path.join(path.dirname(currentModulePath), '..'); +const npmDirectory = path.join(path.dirname(currentModulePath), '..'); +const tauriConfigDirectory = path.join(npmDirectory, 'src-tauri'); function shellExec(command) { return new Promise((resolve, reject) => { @@ -639,20 +641,20 @@ async function mergeConfig(url, options, tauriConf) { } // Save config file. const platformConfigPaths = { - win32: 'src-tauri/tauri.windows.conf.json', - darwin: 'src-tauri/tauri.macos.conf.json', - linux: 'src-tauri/tauri.linux.conf.json', + win32: 'tauri.windows.conf.json', + darwin: 'tauri.macos.conf.json', + linux: 'tauri.linux.conf.json', }; - const configPath = path.join(npmDirectory, platformConfigPaths[platform]); + const configPath = path.join(tauriConfigDirectory, platformConfigPaths[platform]); const bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } }; - await fsExtra.writeJson(configPath, bundleConf, { spaces: 4 }); - const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json'); - await fsExtra.writeJson(pakeConfigPath, tauriConf.pake, { spaces: 4 }); + await fsExtra.outputJSON(configPath, bundleConf, { spaces: 4 }); + const pakeConfigPath = path.join(tauriConfigDirectory, 'pake.json'); + await fsExtra.outputJSON(pakeConfigPath, tauriConf.pake, { spaces: 4 }); let tauriConf2 = JSON.parse(JSON.stringify(tauriConf)); delete tauriConf2.pake; delete tauriConf2.tauri.bundle; - const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json'); - await fsExtra.writeJson(configJsonPath, tauriConf2, { spaces: 4 }); + const configJsonPath = path.join(tauriConfigDirectory, 'tauri.conf.json'); + await fsExtra.outputJSON(configJsonPath, tauriConf2, { spaces: 4 }); } class BaseBuilder { diff --git a/package.json b/package.json index 0d89175..698dd36 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "analyze": "cd src-tauri && cargo bloat --release --crates", "tauri": "tauri", "cli": "rollup -c rollup.config.js --watch", - "cli:dev": "cross-env DEV=true tsx watch ./bin/dev.ts & npm run dev", + "cli:dev": "cross-env NODE_ENV=development rollup -c rollup.config.js -w", "cli:build": "cross-env NODE_ENV=production rollup -c rollup.config.js", "prepublishOnly": "npm run cli:build" }, @@ -68,6 +68,7 @@ "@rollup/plugin-alias": "^4.0.2", "@rollup/plugin-commonjs": "^23.0.2", "@rollup/plugin-json": "^5.0.2", + "@rollup/plugin-replace": "^5.0.2", "@rollup/plugin-terser": "^0.1.0", "@types/fs-extra": "^9.0.13", "@types/is-url": "^1.2.30", diff --git a/plugins/pakeCliDevPlugin.js b/plugins/pakeCliDevPlugin.js new file mode 100644 index 0000000..2932c51 --- /dev/null +++ b/plugins/pakeCliDevPlugin.js @@ -0,0 +1,43 @@ +import chalk from 'chalk'; +import {spawn, exec} from 'child_process'; + + +// just run in development mode +export default function pakeCliDevPlugin() { + return { + name: 'pake-cli-dev-plugin', + buildEnd() { + + const command = 'node'; + const cliCmdArgs = ['./dist/dev.js']; + const cliChildProcess = spawn(command, cliCmdArgs); + + cliChildProcess.stdout.on('data', (data) => { + console.log(chalk.green(data.toString())); + }); + + cliChildProcess.stderr.on('data', (data) => { + console.error(chalk.yellow(data.toString())); + }); + + cliChildProcess.on('close', async (code) => { + console.log(chalk.yellow(`cli running end with code: ${code}`)); + cliChildProcess.kill(); + const dev = await exec('npm run tauri dev -- --config ./src-tauri/.pake/tauri.conf.json --features cli-build'); + + dev.stdout.on('data', (data) => { + console.error(chalk.green(data.toString())); + }); + + dev.stderr.on('data', (data) => { + console.error(chalk.yellow(data.toString())); + }); + + dev.on('close', () => { + dev.kill(); + console.log(chalk.green('rebuild start')); + }); + }); + } + } +} \ No newline at end of file diff --git a/rollup.config.js b/rollup.config.js index 0c21614..1aaf44b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -4,12 +4,23 @@ import typescript from 'rollup-plugin-typescript2'; import alias from '@rollup/plugin-alias'; import commonjs from '@rollup/plugin-commonjs'; import json from '@rollup/plugin-json'; +import replace from '@rollup/plugin-replace'; + +import pakeCliDevPlugin from './plugins/pakeCliDevPlugin.js'; + +const isProduction = process.env.NODE_ENV === 'production'; +const devPlugins = !isProduction ? [pakeCliDevPlugin()] : []; export default { - input: 'bin/cli.ts', + input: isProduction ? 'bin/cli.ts' : 'bin/dev.ts', output: { - file: 'dist/cli.js', - format: 'es' + file: isProduction ? 'dist/cli.js' : 'dist/dev.js', + format: 'es', + sourcemap: !isProduction, + }, + watch: { + include: 'bin/**', + exclude: 'node_modules/**', }, plugins: [ json(), @@ -18,8 +29,12 @@ export default { clean: true, // 清理缓存 }), commonjs(), + replace({ + 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV), + }), alias({ entries: [{ find: '@', replacement: path.join(appRootPath.path, 'bin') }], }), + ...devPlugins, ], }; diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index c7719d1..b55995a 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -25,6 +25,8 @@ tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-works cargo-bloat = "0.11.1" [features] +# this feature is used for development builds from development cli +cli-build = [] # by default Tauri runs in production mode # when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL default = ["custom-protocol"] diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index dfb38a1..8fc31a8 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -4,9 +4,16 @@ use std::path::PathBuf; use tauri::{api, Config, Window}; pub fn get_pake_config() -> (PakeConfig, Config) { + #[cfg(feature = "cli-build")] + let pake_config: PakeConfig = + serde_json::from_str(include_str!("../.pake/pake.json")).expect("Failed to parse pake config"); + #[cfg(not(feature = "cli-build"))] let pake_config: PakeConfig = serde_json::from_str(include_str!("../pake.json")).expect("Failed to parse pake config"); - + #[cfg(feature = "cli-build")] + let tauri_config: Config = serde_json::from_str(include_str!("../.pake/tauri.conf.json")) + .expect("Failed to parse tauri config"); + #[cfg(not(feature = "cli-build"))] let tauri_config: Config = serde_json::from_str(include_str!("../tauri.conf.json")) .expect("Failed to parse tauri config");