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
87 lines (74 loc) · 3.04 KB
/
copy-files.mjs
File metadata and controls
87 lines (74 loc) · 3.04 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env node
import { copyFile, mkdir, access, constants, readFile } from 'fs/promises';
import { join, resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { homedir } from 'os';
// Get current script directory
const __dirname = dirname(fileURLToPath(import.meta.url));
// Default copy destination - the e2e-vault in this repo
const defaultPaths = [
join(__dirname, 'tasknotes-e2e-vault', '.obsidian', 'plugins', 'tasknotes'),
];
// Can be overridden with OBSIDIAN_PLUGIN_PATH environment variable (single path)
// or .copy-files.local file (one path per line for multiple destinations)
const LOCAL_OVERRIDE_FILE = '.copy-files.local';
let copyPaths = defaultPaths;
// Expand ~ to home directory
const expandTilde = (p) => p.startsWith('~/') ? join(homedir(), p.slice(2)) : p;
if (process.env.OBSIDIAN_PLUGIN_PATH) {
copyPaths = [process.env.OBSIDIAN_PLUGIN_PATH];
} else {
try {
const local = await readFile(LOCAL_OVERRIDE_FILE, 'utf8');
const paths = local.split('\n').map(p => p.trim()).filter(p => p && !p.startsWith('#')).map(expandTilde);
if (paths.length > 0) copyPaths = paths;
} catch (_) {
// no local override, use defaults
}
}
// Files to copy after build
const files = ['main.js', 'styles.css', 'manifest.json'];
async function copyToDestination(destPath) {
// Resolve the destination path
const resolvedPath = resolve(destPath);
// Ensure the directory exists (including nested)
await mkdir(resolvedPath, { recursive: true });
// Check each file exists before copying
const copyPromises = files.map(async (file) => {
try {
await access(file, constants.F_OK);
const destFile = join(resolvedPath, file);
await copyFile(file, destFile);
} 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(resolvedPath, { recursive: true });
const destFileRetry = join(resolvedPath, file);
await copyFile(file, destFileRetry);
} else {
throw new Error(`Failed to copy ${file}: ${err?.message || err}`);
}
}
});
await Promise.all(copyPromises);
console.log(`✅ Files copied to: ${resolvedPath}`);
}
async function copyFiles() {
try {
for (const destPath of copyPaths) {
await copyToDestination(destPath);
}
console.log(`✅ Copied ${files.length} files to ${copyPaths.length} destination(s)`);
} catch (error) {
console.error('❌ Failed to copy files:', error.message);
process.exit(1);
}
}
copyFiles();