-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage-obsidian.js
executable file
·116 lines (100 loc) · 2.79 KB
/
package-obsidian.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const archiver = require('archiver');
// 确保安装 archiver 依赖
try {
require.resolve('archiver');
} catch (e) {
console.log('正在安装打包所需的依赖...');
execSync('npm install --save-dev archiver');
console.log('依赖安装完成!');
}
// 设置相关路径
const PLUGIN_NAME = 'cloudflare-r2-uploader';
const ROOT_DIR = path.resolve(__dirname);
const BUILD_DIR = path.join(ROOT_DIR, 'build');
const PACKAGE_DIR = path.join(BUILD_DIR, PLUGIN_NAME);
// 创建输出目录
console.log('创建输出目录...');
if (fs.existsSync(BUILD_DIR)) {
fs.rmSync(BUILD_DIR, { recursive: true, force: true });
}
fs.mkdirSync(BUILD_DIR);
fs.mkdirSync(PACKAGE_DIR);
// 构建项目
console.log('构建项目...');
try {
execSync('npm run build', { stdio: 'inherit' });
} catch (e) {
console.error('构建失败:', e);
process.exit(1);
}
// 要复制的文件列表
const files = [
'main.js',
'manifest.json',
'README.md'
];
// 可选文件(存在则复制)
const optionalFiles = [
'styles.css'
];
// 复制文件到构建目录
console.log('复制文件到构建目录...');
files.forEach(file => {
if (fs.existsSync(path.join(ROOT_DIR, file))) {
fs.copyFileSync(
path.join(ROOT_DIR, file),
path.join(PACKAGE_DIR, file)
);
console.log(` - 已复制 ${file}`);
} else {
console.warn(` - 警告: ${file} 不存在,跳过`);
}
});
// 复制可选文件(如果存在)
optionalFiles.forEach(file => {
if (fs.existsSync(path.join(ROOT_DIR, file))) {
fs.copyFileSync(
path.join(ROOT_DIR, file),
path.join(PACKAGE_DIR, file)
);
console.log(` - 已复制 ${file}`);
}
});
// 创建 zip 归档
console.log('创建插件归档...');
const output = fs.createWriteStream(path.join(BUILD_DIR, `${PLUGIN_NAME}.zip`));
const archive = archiver('zip', {
zlib: { level: 9 }
});
output.on('close', () => {
console.log(`
✅ 打包完成!
- 输出目录: ${BUILD_DIR}
- 文件夹: ${PLUGIN_NAME}/
- 压缩包: ${PLUGIN_NAME}.zip (${archive.pointer()} 字节)
可以通过以下方式在 Obsidian 中测试此插件:
1. 打开 Obsidian > 设置 > 第三方插件 > 打开插件文件夹
2. 创建文件夹 ${PLUGIN_NAME}
3. 将 build/${PLUGIN_NAME}/ 中的所有文件复制到新创建的文件夹中
4. 重启 Obsidian 并启用插件
或者:
1. 使用 BRAT 插件安装该插件,选择 "从存储库安装" 并使用本地 zip 文件路径
`);
});
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.warn('警告:', err);
} else {
throw err;
}
});
archive.on('error', (err) => {
throw err;
});
archive.pipe(output);
archive.directory(PACKAGE_DIR, PLUGIN_NAME);
archive.finalize();