You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
4.1 KiB
158 lines
4.1 KiB
// @ts-nocheck
|
|
const electron = require("electron")
|
|
const execa = require("execa")
|
|
const vite = require("vite")
|
|
const path = require("path")
|
|
const fs = require("fs")
|
|
const os = require("os")
|
|
const esbuild = require("esbuild")
|
|
const { say } = require("cfonts")
|
|
const { spawn } = require("child_process")
|
|
const chalk = require("chalk")
|
|
|
|
function getEnvScript () {
|
|
let script = `process.env={...process.env};`
|
|
script += `process.env.RES_DIR = require("path").join(require("path").dirname(process.execPath),"resources/resource/release")`
|
|
return script
|
|
}
|
|
|
|
async function buildRender() {
|
|
const subprocess = execa("vite",['-c','vite.config.ts']).stdout.pipe(process.stdout);;
|
|
setTimeout(() => {
|
|
subprocess.killed();
|
|
}, 1000);
|
|
|
|
try {
|
|
await subprocess;
|
|
} catch (error) {
|
|
console.log(subprocess.killed); // true
|
|
console.log(error.isCanceled); // true
|
|
}
|
|
return
|
|
let options = {
|
|
root: path.join(__dirname, "../src/render"),
|
|
enableEsbuild: true,
|
|
minify: false,
|
|
resolve:{
|
|
alias: [
|
|
{ find: "@", replacement: path.resolve(__dirname, "src/render") },
|
|
{ find: "@render", replacement: path.resolve(__dirname, "src/render") },
|
|
{ find: "@main", replacement: path.resolve(__dirname, "src/main") },
|
|
{ find: "@src", replacement: path.resolve(__dirname, "src/src") },
|
|
{ find: "@root", replacement: __dirname },
|
|
],
|
|
},
|
|
optimizeDeps: {
|
|
exclude: ["process"],
|
|
},
|
|
}
|
|
await vite.build(options)
|
|
// let htmlPath = path.join(__dirname, "../dist/electron", "index.html")
|
|
// let html = fs.readFileSync(htmlPath, { encoding: "utf8" })
|
|
// html = html.replace("<head>", `<head><script>${getEnvScript()};</script>`)
|
|
// fs.writeFileSync(htmlPath, html)
|
|
}
|
|
|
|
function buildMain() {
|
|
let outfile = path.join(process.cwd(), "dist/electron/entry.js")
|
|
let entryFilePath = path.join(process.cwd(), "src/main/index.js")
|
|
esbuild.buildSync({
|
|
entryPoints: [entryFilePath],
|
|
outfile,
|
|
minify: false,
|
|
bundle: true,
|
|
platform: "node",
|
|
sourcemap: false,
|
|
external: ["electron"],
|
|
})
|
|
let js = `${getEnvScript()}${os.EOL}${fs.readFileSync(outfile)}`
|
|
fs.writeFileSync(outfile, js)
|
|
}
|
|
|
|
function startElectron() {
|
|
var args = ["--inspect=5858", path.join(__dirname, "../dist/electron/main.js")]
|
|
|
|
// detect yarn or npm and process commandline args accordingly
|
|
if (process.env.npm_execpath.endsWith("yarn.js")) {
|
|
args = args.concat(process.argv.slice(3))
|
|
} else if (process.env.npm_execpath.endsWith("npm-cli.js")) {
|
|
args = args.concat(process.argv.slice(2))
|
|
}
|
|
|
|
// electronProcess = spawn(electron, args)
|
|
|
|
// electronProcess.stdout.on("data", data => {
|
|
// electronLog(data, "blue")
|
|
// })
|
|
// electronProcess.stderr.on("data", data => {
|
|
// electronLog(data, "red")
|
|
// })
|
|
|
|
// electronProcess.on("close", () => {
|
|
// if (!manualRestart) process.exit()
|
|
// })
|
|
}
|
|
|
|
function logStats(proc, data) {
|
|
let log = ""
|
|
|
|
log += chalk.yellow.bold(`┏ ${proc} Process ${new Array(19 - proc.length + 1).join("-")}`)
|
|
log += "\n\n"
|
|
|
|
if (typeof data === "object") {
|
|
data
|
|
.toString({
|
|
colors: true,
|
|
chunks: false,
|
|
})
|
|
.split(/\r?\n/)
|
|
.forEach(line => {
|
|
log += " " + line + "\n"
|
|
})
|
|
} else {
|
|
log += ` ${data}\n`
|
|
}
|
|
|
|
log += "\n" + chalk.yellow.bold(`┗ ${new Array(28 + 1).join("-")}`) + "\n"
|
|
|
|
console.log(log)
|
|
}
|
|
|
|
function electronLog(data, color) {
|
|
let log = ""
|
|
data = data.toString().split(/\r?\n/)
|
|
data.forEach(line => {
|
|
log += ` ${line}\n`
|
|
})
|
|
if (/[0-9A-z]+/.test(log)) {
|
|
console.log(
|
|
chalk[color].bold("┏ Electron -------------------") + "\n\n" + log + chalk[color].bold("┗ ----------------------------") + "\n"
|
|
)
|
|
}
|
|
}
|
|
|
|
function greeting() {
|
|
const cols = process.stdout.columns
|
|
let text = ""
|
|
|
|
if (cols > 104) text = "electron-vue"
|
|
else if (cols > 76) text = "electron-|vue"
|
|
else text = ''
|
|
|
|
if (text) {
|
|
say(text, {
|
|
colors: ["yellow"],
|
|
font: "simple3d",
|
|
space: false,
|
|
})
|
|
} else console.log(chalk.yellow.bold("\n electron-vue"))
|
|
console.log(chalk.blue(" getting ready...") + "\n")
|
|
}
|
|
|
|
function init() {
|
|
greeting()
|
|
// buildMain()
|
|
buildRender()
|
|
}
|
|
|
|
init()
|
|
|