forked from callumalpass/tasknotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-files.mjs
More file actions
66 lines (58 loc) · 2.55 KB
/
copy-files.mjs
File metadata and controls
66 lines (58 loc) · 2.55 KB
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
#!/usr/bin/env node
import { copyFile, mkdir, access, constants, readFile } from 'fs/promises';
import { join, resolve } from 'path';
import os from 'os';
// Default copy destination - can be overridden with OBSIDIAN_PLUGIN_PATH environment variable
// Use os.homedir() to avoid literal "$HOME" not being expanded by Node
const defaultPath = join(os.homedir(), 'testvault', 'test', '.obsidian', 'plugins', 'tasknotes');
const LOCAL_OVERRIDE_FILE = '.copy-files.local';
let copyPath = process.env.OBSIDIAN_PLUGIN_PATH || defaultPath;
try {
const local = await readFile(LOCAL_OVERRIDE_FILE, 'utf8');
const trimmed = local.trim();
if (trimmed) copyPath = trimmed;
} catch (_) {
// no local override
}
// Files to copy after build
const files = ['main.js', 'styles.css', 'manifest.json'];
async function copyFiles() {
try {
// Resolve the destination path
const destPath = resolve(copyPath);
// Ensure the directory exists (including nested)
await mkdir(destPath, { recursive: true });
// Check each file exists before copying
const copyPromises = files.map(async (file) => {
try {
await access(file, constants.F_OK);
const destFile = join(destPath, file);
await copyFile(file, destFile);
console.log(`✅ Copied ${file}`);
} catch (err) {
if (err && err.code === 'ENOENT') {
// Differentiate between missing source and missing destination path
try {
await access(file, constants.F_OK);
} catch {
console.warn(`⚠️ Warning: source file ${file} not found, skipping`);
return;
}
console.warn(`⚠️ Warning: destination path missing for ${file}. Attempting to create…`);
await mkdir(destPath, { recursive: true });
const destFileRetry = join(destPath, file);
await copyFile(file, destFileRetry);
console.log(`✅ Copied ${file} (after creating destination)`);
} else {
throw new Error(`Failed to copy ${file}: ${err?.message || err}`);
}
}
});
await Promise.all(copyPromises);
console.log(`✅ Files copied to: ${destPath}`);
} catch (error) {
console.error('❌ Failed to copy files:', error.message);
process.exit(1);
}
}
copyFiles();