-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
58 lines (48 loc) · 1.69 KB
/
deploy.js
File metadata and controls
58 lines (48 loc) · 1.69 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
const chokidar = require('chokidar');
const fs = require('fs-extra');
const path = require('path');
const simpleGit = require('simple-git');
const PADDING = 80
const repo = simpleGit('.');
async function isIgnored(sourcePath) {
return (await repo.checkIgnore([sourcePath])).length !== 0;
}
async function copyFileOnChange(sourcePath, targetDirectory) {
if (sourcePath.includes('.git') || targetDirectory.includes('.git'))
return;
try {
let targetPath = path.join(targetDirectory, sourcePath);
if (await isIgnored(sourcePath)) {
console.error(`${new Date().toISOString()} ${sourcePath.padEnd(PADDING)} -> ignored`);
} else {
await fs.copy(sourcePath, targetPath);
console.log(`${new Date().toISOString()} ${sourcePath.padEnd(PADDING)} -> ${targetPath}`);
}
} catch (error) {
console.error(`Error copying file: ${error.message}`);
}
}
function watchDirectory(sourceDir, targetDir) {
let source = path.join(__dirname, sourceDir)
console.log(`Watching for changes in: ${source}`);
const watcher = chokidar.watch(source, {
persistent: true,
ignoreInitial: true
});
watcher.on('change', (filePath) => copyFileOnChange(filePath.replace(__dirname + '\\', ''), targetDir.replace(sourceDir, '')));
watcher.on('error', (error) => console.error(`Watcher error: ${error.message}`));
}
let sourceDirectory = '.'
let targetDirectory
if (process.argv.length === 4) {
sourceDirectory = process.argv[2];
targetDirectory = process.argv[3];
} else {
targetDirectory = process.argv[2];
}
if (!targetDirectory) {
console.log("Usage: node deploy.js [sourceDirectory] <targetDirectory>");
process.exit(1);
}
// Start watching the source directory
watchDirectory(sourceDirectory, targetDirectory);