42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
// 插件设置持久化。默认关闭自动生成——Eagle 审核标准要求「批量或不可逆的变更
|
|
// 必须事先明确说明并获得确认」,所以必须由用户主动打开这个开关。
|
|
(function () {
|
|
'use strict';
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const DEFAULTS = { enabled: false };
|
|
|
|
/** 存放在 Eagle 的 userData 目录,不写进素材库,卸载时可直接删掉。 */
|
|
async function resolveFile() {
|
|
const userData = await eagle.app.getPath('userData');
|
|
return path.join(userData, 'SpineThumbnailGenerateServer', 'settings.json');
|
|
}
|
|
|
|
async function read() {
|
|
const file = await resolveFile();
|
|
try {
|
|
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
return { ...DEFAULTS, ...data };
|
|
} catch (error) {
|
|
if (error && error.code === 'ENOENT') return { ...DEFAULTS };
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function write(next) {
|
|
const file = await resolveFile();
|
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
const body = JSON.stringify({ ...DEFAULTS, ...next }, null, 2) + '\n';
|
|
const temp = file + '.' + process.pid + '.tmp';
|
|
fs.writeFileSync(temp, body, 'utf8');
|
|
fs.renameSync(temp, file);
|
|
}
|
|
|
|
const api = { read, write, resolveFile, DEFAULTS };
|
|
if (typeof window !== 'undefined') window.SpineThumbSettings = api;
|
|
else if (typeof module !== 'undefined' && module.exports) module.exports = api;
|
|
|
|
})();
|