修复win环境下运行bug

This commit is contained in:
可达鸭 2025-02-20 13:29:32 +08:00
parent 30b337c711
commit cabea23a7a
3 changed files with 48 additions and 37 deletions

54
main.js
View File

@ -1,9 +1,6 @@
// import {Template} from "./template.js"
// console.log(process )
// let dirname = process.mainModule.path
import fs from 'fs' import fs from 'fs'
import path from 'path'; import path from 'path';
import {fileURLToPath} from 'url'; import {fileURLToPath, pathToFileURL} from 'url';
import ini from 'ini' import ini from 'ini'
let filename; let filename;
@ -28,33 +25,40 @@ if (length>2) {
if (filename) { if (filename) {
let type = filename.match(/(^[A-Za-z0-9]+)\_/)[1] let type = filename.match(/(^[A-Za-z0-9]+)\_/)[1]
try { try {
let psyDuck = await import( `./parse/${type}/${filename}.js`) let jsPath = pathToFileURL(`./parse/${type}/${filename}.js`).href
let psyDuck = await import(jsPath)
let main = new psyDuck.Main() let main = new psyDuck.Main()
await main.init(params) await main.init(params)
} catch (e) { } catch (e) {
// console.log(e) // console.log(e)
let iniText = fs.readFileSync(`config/config.ini`, 'UTF-8') if (e == 'End') {
let config = ini.parse(iniText) console.log("End")
let iniPath = `config/${type}.ini`
if (config.env && config.env.iniPath) {
iniPath = `${config.env.iniPath}/${type}.ini`
}
let scText = fs.readFileSync(iniPath, 'UTF-8')
let script = ini.parse(scText)
let map = ''
for (let i in script) {
if (i == filename && script[i].map) {
map = script[i].map
}
}
if (map) {
params.mapping = map
let psyDuck = await import( `./parse/${type}/${map}.js`)
let main = new psyDuck.Main()
await main.init(params)
} }
else { else {
console.log("没有可执行的脚本") let iniText = fs.readFileSync(`config/config.ini`, 'UTF-8')
let config = ini.parse(iniText)
let iniPath = `config/${type}.ini`
if (config.env && config.env.iniPath) {
iniPath = `${config.env.iniPath}/${type}.ini`
}
let scText = fs.readFileSync(iniPath, 'UTF-8')
let script = ini.parse(scText)
let map = ''
for (let i in script) {
if (i == filename && script[i].map) {
map = script[i].map
}
}
if (map) {
params.mapping = map
let jsPath = pathToFileURL(`./parse/${type}/${map}.js`).href
let psyDuck = await import(jsPath)
let main = new psyDuck.Main()
await main.init(params)
}
else {
console.log("没有可执行的脚本")
}
} }
} }
} }

View File

@ -181,7 +181,7 @@ class Ql {
async create() { async create() {
if (!this.config) { if (!this.config) {
consoel.log("请先初始化config.ini,并设置青龙ClientID和ClientSecret") console.log("请先初始化config.ini,并设置青龙ClientID和ClientSecret")
return return
} }
await this.getToken() await this.getToken()
@ -221,9 +221,7 @@ class Ql {
let title = obj[filename].title || `${psyDuck.profile.title}分身` let title = obj[filename].title || `${psyDuck.profile.title}分身`
let code = ` let code = `
import path from 'path'; import path from 'path';
import { import {fileURLToPath, pathToFileURL} from 'url';
fileURLToPath
} from 'url';
!(async () => { !(async () => {
let dirpath = fileURLToPath(import.meta.url).replace('.swap',''); let dirpath = fileURLToPath(import.meta.url).replace('.swap','');
let abspath = path.dirname(dirpath) let abspath = path.dirname(dirpath)
@ -246,11 +244,16 @@ import {
} }
} }
} }
let psyDuck = await import (\`\${abspath}/parse/\${type}/${map}.js\`) let jsPath = pathToFileURL(\`\${abspath}/parse/\${type}/${map}.js\`).href
let psyDuck = await import (jsPath)
let main = new psyDuck.Main() let main = new psyDuck.Main()
await main.init(params) await main.init(params)
})().catch((e) => { })().catch((e) => {
console.log(e) if (e == 'End') {
console.log("End")
}else{
console.log(e)
}
})` })`
fs.writeFile(`${abspath}/${filename}.js`, code, function(err, data) { fs.writeFile(`${abspath}/${filename}.js`, code, function(err, data) {
if (err) { if (err) {
@ -281,9 +284,7 @@ import {
let crontab = psyDuck.crontab() let crontab = psyDuck.crontab()
let code = ` let code = `
import path from 'path'; import path from 'path';
import { import {fileURLToPath, pathToFileURL} from 'url';
fileURLToPath
} from 'url';
!(async () => { !(async () => {
let dirpath = fileURLToPath(import.meta.url).replace('.swap',''); let dirpath = fileURLToPath(import.meta.url).replace('.swap','');
let abspath = path.dirname(dirpath) let abspath = path.dirname(dirpath)
@ -304,11 +305,17 @@ import {
} }
} }
} }
let psyDuck = await import (\`\${abspath}/parse/\${type}/\${filename}.js\`) let jsPath = pathToFileURL(\`\${abspath}/parse/\${type}/\${filename}.js\`).href
let psyDuck = await import (jsPath)
let main = new psyDuck.Main() let main = new psyDuck.Main()
await main.init(params) await main.init(params)
})().catch((e) => { })().catch((e) => {
console.log(e) if (e == 'End') {
console.log("End")
}else{
console.log(e)
}
})` })`
fs.writeFile(`${abspath}/${script}`, code, function(err, data) { fs.writeFile(`${abspath}/${script}`, code, function(err, data) {
if (err) { if (err) {

View File

@ -34,7 +34,7 @@ export class Cache {
await this._cache.connect(); await this._cache.connect();
} }
else { else {
let dirpath = fileURLToPath(import.meta.url).split('/util')[0]; let dirpath = fileURLToPath(import.meta.url).split('/util')[0].split("\\util")[0];
this._cache = new Tfc({dir: `${dirpath}/temp`}) this._cache = new Tfc({dir: `${dirpath}/temp`})
} }
} }