-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
761 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
node-linker=hoisted | ||
node-linker=hoisted | ||
hoist-pattern[]=*!node-pty* | ||
hoist-pattern[]=*!nan* | ||
hoist-pattern[]=* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# customize | ||
|
||
*.local.test.js | ||
|
||
pnpm-lock.yaml | ||
package-lock.json | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// @ts-check | ||
|
||
const ocs = require("@ocsjs/scripts"); | ||
const { loggerPrefix } = require("@ocsjs/scripts"); | ||
const { Instance: Chalk } = require("chalk"); | ||
const { Logger } = require("./src/logger.core"); | ||
const { redBright, bgRedBright, bgBlueBright, bgYellowBright, bgGray } = new Chalk({ level: 2 }); | ||
|
||
/** @type { Logger} */ | ||
let logger; | ||
|
||
/** @type {import("playwright").BrowserContext | import("playwright").Browser} */ | ||
let browser; | ||
/** @type {import("playwright").Page} */ | ||
let page; | ||
|
||
process.on("message", async (message) => { | ||
// @ts-ignore | ||
const { action, data, uid, logsPath } = JSON.parse(message); | ||
if (logger === undefined) { | ||
logger = new Logger(logsPath, false, "script", uid); | ||
logger.info("日志 : " + logger.dest); | ||
} | ||
|
||
try { | ||
if (action === "open") { | ||
if (browser === undefined) { | ||
logger.info("任务启动 : ", action, data, uid, logsPath); | ||
|
||
/** @type {import("@ocsjs/scripts").LaunchScriptsOptions} */ | ||
const options = JSON.parse(data); | ||
const { userDataDir, launchOptions, scripts } = options; | ||
console.log("\n"); | ||
debug("任务启动", uid); | ||
debug("隐身模式 ", launchOptions.headless === true ? bgBlueBright("开启") : bgRedBright("关闭")); | ||
debug("无痕浏览 ", userDataDir === "" ? bgBlueBright("开启") : bgRedBright("关闭")); | ||
|
||
function debug(...msg) { | ||
console.log(bgGray(loggerPrefix("debug")), ...msg); | ||
} | ||
|
||
launchOptions.logger = { | ||
isEnabled: () => true, | ||
log(name, severity, message, args) { | ||
const str = [severity, new Date().toLocaleTimeString(), name, message, args].join(" "); | ||
console.log(str); | ||
logger.info(str); | ||
}, | ||
}; | ||
|
||
const { browser: _browser, page: _page } = await ocs.launchScripts({ | ||
userDataDir, | ||
launchOptions, | ||
scripts, | ||
}); | ||
browser = _browser; | ||
page = _page; | ||
} else { | ||
console.log(bgYellowBright(loggerPrefix("warn")), "任务已开启,请勿重复开启。", uid); | ||
logger.info("任务已开启,请勿重复开启。"); | ||
} | ||
} else if (action === "close") { | ||
console.log(bgBlueBright(loggerPrefix("info")), "任务已关闭!"); | ||
logger.info("任务已关闭!"); | ||
await browser.close(); | ||
browser = undefined; | ||
page = undefined; | ||
} else if (action === "call") { | ||
const { name, args } = JSON.parse(data); | ||
await page[name](args); | ||
} | ||
} catch (e) { | ||
console.log(bgRedBright(loggerPrefix("error")), "任务发生未知错误 : ", e); | ||
logger.info("任务发生未知错误!", e); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// @ts-check | ||
const path = require("path"); | ||
const util = require("util"); | ||
const fs = require("fs"); | ||
|
||
function formatDate() { | ||
const date = new Date(); | ||
return [ | ||
date.getFullYear(), | ||
String(date.getMonth() + 1).padStart(2, "0"), | ||
date.getDate().toString().padStart(2, "0"), | ||
].join("-"); | ||
} | ||
|
||
/** | ||
* 日志对象 | ||
* ```js | ||
* const l = new logger(app.getPath("logs"), true, 'test') // create `${logs}/YYYY-MM-DD/test.log` | ||
* const l2 = new logger(app.getPath("logs"), true,'project','error','1') // create `${logs}/YYYY-MM-DD/project/error/1.log` | ||
* ``` | ||
* @param {string} basePath 根路径 | ||
* @param {boolean} withConsole 是否同时使用 console 输出 | ||
* @param {string[]} name 日志名,每个代表一个路径 | ||
*/ | ||
exports.Logger = class Logger { | ||
basePath; | ||
withConsole; | ||
dest; | ||
constructor(basePath, withConsole = true, ...name) { | ||
this.basePath = basePath; | ||
this.withConsole = withConsole; | ||
this.dest = path.join(this.basePath, "/", formatDate(), "/", name.join("/") + ".log"); | ||
} | ||
|
||
log = (...msg) => this._log(this.dest, "信息", ...msg); | ||
info = (...msg) => this._log(this.dest, "信息", ...msg); | ||
error = (...msg) => this._log(this.dest, "错误", ...msg); | ||
debug = (...msg) => this._log(this.dest, "调试", ...msg); | ||
warn = (...msg) => this._log(this.dest, "警告", ...msg); | ||
|
||
_log(dest, level, ...msg) { | ||
const data = msg | ||
.map((s) => { | ||
if (typeof s === "object" || typeof s === "function") { | ||
s = util.inspect(s); | ||
} | ||
return s; | ||
}) | ||
.join(""); | ||
const txt = `[${level}] ${new Date().toLocaleString()} \t ` + data; | ||
|
||
if (this.withConsole) { | ||
console.log(txt); | ||
} | ||
|
||
if (!fs.existsSync(path.dirname(dest))) { | ||
fs.mkdirSync(path.dirname(dest), { recursive: true }); | ||
} | ||
fs.appendFileSync(dest, txt + "\n"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,7 @@ | ||
//@ts-check | ||
|
||
const { Logger } = require("./logger.core"); | ||
const { app } = require("electron"); | ||
const path = require("path"); | ||
const dayjs = require("dayjs"); | ||
const util = require("util"); | ||
const fs = require("fs"); | ||
|
||
module.exports = function logger(name) { | ||
const dest = path.join(app.getPath("logs"), "/", dayjs().format("YYYY-MM-DD"), "/", name + ".log"); | ||
|
||
function log(level, ...msg) { | ||
const data = msg | ||
.map((s) => { | ||
if (typeof s === "object" || typeof s === "function") { | ||
s = util.inspect(s); | ||
} | ||
return s; | ||
}) | ||
.join(""); | ||
const txt = `[${level}] ${new Date().toLocaleString()} \t ` + data; | ||
console.log(txt); | ||
if (!fs.existsSync(path.dirname(dest))) { | ||
fs.mkdirSync(path.dirname(dest), { recursive: true }); | ||
} | ||
fs.appendFileSync(dest, txt + "\n"); | ||
} | ||
|
||
return { | ||
log: (...msg) => log("信息", ...msg), | ||
info: (...msg) => log("信息", ...msg), | ||
error: (...msg) => log("错误", ...msg), | ||
debug: (...msg) => log("调试", ...msg), | ||
warn: (...msg) => log("警告", ...msg), | ||
}; | ||
module.exports = function logger(...name) { | ||
return new Logger(app.getPath("logs"), true, ...name); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./nodejs"; | ||
export { loggerPrefix } from "./logger"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.