-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-script.js
More file actions
92 lines (83 loc) · 2.54 KB
/
Copy pathbuild-script.js
File metadata and controls
92 lines (83 loc) · 2.54 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
88
89
90
91
92
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// Site A
const siteAPath = 'content/site_a';
const siteAMenuPath = 'data/menu/site_a';
const siteAStaticPath = 'static/site_a';
// Site B
const siteBPath = 'content/site_b';
const siteBMenuPath = 'data/menu/site_b';
const siteBStaticPath = 'static/site_b';
// Common directories
const assetsPath = 'assets';
const layoutsPath = 'layouts';
const i18nPath = 'i18n';
const postsPath = 'content/posts';
// Build command function
function runBuildCommand(command) {
try {
console.log(`🚀 Command: ${command}`);
execSync(command, { stdio: 'inherit' });
console.log(`🔥 Success: ${command}`);
} catch (error) {
console.error(`❌ Error: ${error}`);
process.exit(1);
}
}
// Get changed files function
function getChangedFiles() {
console.log('🔍 Get modified files in last commit…');
try {
const output = execSync(
'git diff-tree --no-commit-id --name-only -r HEAD',
{ encoding: 'utf-8' }
);
return output
.split('\n')
.filter(Boolean)
.map((file) => file.trim().replace(/^"|"$/g, ''));
} catch (error) {
console.error(`❌ Error: ${error}`);
process.exit(1);
}
}
// Verify that the public folder is restored from the cache
if (!fs.existsSync('public')) {
console.log(`❌ Public directory dosen't exists`);
} else {
console.log(`✅ Public directory exists .`);
}
// Check for changes in folders
function hasChangesInDirectory(files, directory) {
const normalizedDirectory = path.normalize(directory);
return files.some((file) => file.startsWith(normalizedDirectory));
}
const changedFiles = getChangedFiles();
console.log('✅ Modified files:', changedFiles);
// Build sites
if (
hasChangesInDirectory(changedFiles, i18nPath) ||
hasChangesInDirectory(changedFiles, assetsPath) ||
hasChangesInDirectory(changedFiles, layoutsPath) ||
hasChangesInDirectory(changedFiles, postsPath)
) {
// Build all sites
runBuildCommand('yarn build');
} else if (
hasChangesInDirectory(changedFiles, siteAPath) ||
hasChangesInDirectory(changedFiles, siteAMenuPath) ||
hasChangesInDirectory(changedFiles, siteAStaticPath)
) {
// Build site A
runBuildCommand('yarn build-site_a');
} else if (
hasChangesInDirectory(changedFiles, siteBPath) ||
hasChangesInDirectory(changedFiles, siteBMenuPath) ||
hasChangesInDirectory(changedFiles, siteBStaticPath)
) {
// Build site B
runBuildCommand('yarn build-site_b');
} else {
console.log('❌ No changes detected in monitored folders.');
}