var path = require('path');
var fs = require('fs');
var compressing = require('compressing');
const process_args = require('minimist')(process.argv);
var is_development = !process_args.docker;
//========================Theme=============================
global.nv_theme_list = ()=>{
/*返回:
存在 theme.json 才处理进去
[
{
"folder": "xxxxx",
"error": Boolean, //不存在 function.js
"name": "客户端渲染主题示例",
"url": "",
"description": "nvPress自带默认主题",
"version": "0.0.1",
"author": "PANDA Studio",
"author_url": "",
},
...
]
*/
const themeDir = path.join(process.cwd(),'./nv-themes')
const all_themes = [];
try{
fs.readdirSync(themeDir).forEach(folderName=>{
const filePath = path.join(themeDir,folderName);
if ( fs.statSync(filePath).isDirectory() ) {
const themeInfo = path.join(filePath,'./theme.json');
const themeEntrance = path.join(filePath,'./function.js');
try{
if ( fs.statSync(themeInfo).isFile() ) {
try {
var themeJson = require(themeInfo);
} catch(e) {
themeJson = {error:true};
// console.log("主题theme.json格式错误:"+folderName)
}
all_themes.push({
folder: folderName,
error: !fs.statSync(themeEntrance).isFile(),
...themeJson,
})
}
} catch(e) {
all_themes.push({
folder: folderName,
error: true,
})
}
}
})
} catch(e) {
console.log("读取主题信息异常:"+e)
}
return all_themes;
}
global.get_theme_path = sub_path=> path.join(
process.cwd(),
'./nv-themes',
global.nv_current_theme_folder || "",
sub_path || ""
)
//========================Plugin=============================
global.nv_plugin_list = ()=>{
/*返回:
存在 plugin.json 才处理进去
[
{
"folder": "xxxxx",
"error": Boolean, //不存在 function.js
"name": "客户端渲染主题示例",
"url": "",
"description": "nvPress自带默认主题",
"version": "0.0.1",
"author": "PANDA Studio",
"author_url": "",
},
...
]
*/
const pluginDir = path.join(process.cwd(),'./nv-plugins')
const all_plugins = [];
try{
fs.readdirSync(pluginDir).forEach(folderName=>{
const filePath = path.join(pluginDir,folderName);
if ( fs.statSync(filePath).isDirectory() ) {
const pluginInfo = path.join(filePath,'./plugin.json');
const pluginEntrance = path.join(filePath,'./function.js');
try{
if ( fs.statSync(pluginInfo).isFile() ) {
try {
var pluginJson = require(pluginInfo);
} catch(e) {
pluginJson = {error:true};
// console.log("插件plugin.json格式错误:"+folderName)
}
all_plugins.push({
folder: folderName,
error: !fs.statSync(pluginEntrance).isFile(),
...pluginJson,
})
}
} catch(e) {
all_plugins.push({
folder: folderName,
error: true,
})
}
}
})
} catch(e) {
console.log("读取插件信息异常:"+e)
}
return all_plugins;
}
//======================Package==============================
global.nv_get_package_status = folder_name => {
// 根据foldername找,会找到theme或者plugin
if (!folder_name || typeof folder_name !== 'string') {return new NV_Error('参数错误!')}
var found_theme = nv_theme_list().filter(r=>r.folder.toLowerCase() == folder_name)[0];
var found_plugin = nv_plugin_list().filter(r=>r.folder.toLowerCase() == folder_name)[0];
if (found_theme) {
found_theme.is_active = nv_current_theme_folder.toLowerCase() == folder_name;
}
if (found_plugin) {
found_plugin.is_active = nv_enabled_plugin_folders.map(i=>i.toLowerCase()).includes(folder_name);
}
return {
theme: found_theme,
plugin: found_plugin
}
}
// 两个参数分别是:上传文件夹位置、文件本身
global.async_nv_install_package = (uploadDir, file) => new Promise((resolve,reject)=>{
// 解压文件
var parsed = path.parse(file.path);
compressing.zip.uncompress(file.path, parsed.dir)
.then(()=>{
// 解压成功,获取解压的文件夹
fs.readdir(parsed.dir,{},(err,files)=>{
if (err || files.length !== 2) {
nv_remove_folder(uploadDir);
reject(new NV_Error(`套件安装失败:${file.originalFilename}`));
return;
}
var dirnames = files.filter(string=> string !== parsed.name + parsed.ext);
if (dirnames.length !== 1) {
nv_remove_folder(uploadDir);
reject(new NV_Error(`套件包异常:${file.originalFilename}`));
return;
}
var package_slug = dirnames[0];
var package_path = `${uploadDir}/${package_slug}`;
// 检查是否有 function.js plugin.json 或 theme.json
fs.readdir(package_path,{},(err,files)=>{
if (err) {
nv_remove_folder(uploadDir);
reject(new NV_Error(`检查套件包失败:${file.originalFilename}`));
return;
}
var include_function_js = files.includes('function.js');
var include_theme_json = files.includes('theme.json');
var include_plugin_json = files.includes('plugin.json');
if (!include_function_js) {
nv_remove_folder(uploadDir);
reject(new NV_Error(`套件包未包含 function.js 文件:${file.originalFilename}`));
return;
}
if (include_theme_json && include_plugin_json) {
nv_remove_folder(uploadDir);
reject(new NV_Error(`套件包不能同时为主题和插件:${file.originalFilename}`));
return;
}
if (!(include_theme_json || include_plugin_json)) {
nv_remove_folder(uploadDir);
reject(new NV_Error(`套件包无法识别主题或插件:${file.originalFilename}`));
return;
}
var package_type = include_theme_json ? 'theme' : 'plugin';
var package_json = null;
try {
package_json = require(path.join(process.cwd(),`${package_path}/${package_type}.json`));
} catch(e) {}
if (!package_json) {
nv_remove_folder(uploadDir);
reject(new NV_Error(`${package_type}.json 文件异常:${file.originalFilename}`));
return;
}
var package_target_path = path.join(process.cwd(),`./nv-${package_type}s/${package_slug}`);
try {
var stat = fs.lstatSync(package_target_path);
if (stat.isDirectory()) {
var is_new_package = false;
} else {
var is_new_package = true;
fs.unlinkSync(package_target_path);
fs.mkdirSync(package_target_path);
}
} catch(e) {
// 路径不存在
var is_new_package = true;
fs.mkdirSync(package_target_path);
}
if (is_development && !is_new_package) {
nv_remove_folder(uploadDir);
reject(new NV_Error('开发模式禁止覆盖安装!请手动在套件文件夹操作'))
return;
}
// 排除 update_excludes,删除目标路径下的文件、文件夹
if (package_json.update_excludes) {
var subnames = fs.readdirSync(package_target_path);
subnames.forEach(file_name=>{
if (package_json.update_excludes.includes(file_name)) {
return;
}
var currentPath = path.join(package_target_path,`/${file_name}`);
if (fs.statSync(currentPath).isDirectory()) {
try{
fs.rmSync( currentPath, {recursive:true} );
} catch(e) {}
} else {
try{
fs.unlinkSync( currentPath );
} catch(e) {}
}
})
} else {
// 没有update_excludes,删除全部
try{
fs.rmSync( package_target_path, {recursive:true} );
fs.mkdirSync(package_target_path);
} catch(e) {}
}
// 复制新的进去,不覆盖
try {
fs.cpSync(package_path, package_target_path, {
force: false, // 不覆盖现有的文件或目录
recursive: true, //递归复制目录
})
nv_remove_folder(uploadDir);
// 判断当前是否是Active
var active = false;
if (!is_new_package && package_type == 'theme') {
active = nv_current_theme_folder == package_slug;
}
if (!is_new_package && package_type == 'plugin') {
active = nv_enabled_plugin_folders.includes(package_slug);
}
resolve({
type: package_type,
slug: package_slug,
name: package_json.name,
is_new_package,
active,
});
} catch(e) {
nv_remove_folder(uploadDir);
reject(new NV_Error(`套件安装失败:${package_json.name}`));
return;
}
})
})
})
.catch(()=>{
nv_remove_folder(uploadDir);
reject(new NV_Error(`套件解压失败:${file.originalFilename}`));
return;
})
})