-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_handler.js
More file actions
116 lines (96 loc) · 3.64 KB
/
content_handler.js
File metadata and controls
116 lines (96 loc) · 3.64 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const RP_STACK_API_KEY = process.env.RP_STACK_API_KEY
/**
* Updates the content directory by deleting it and running fresh export
*/
async function updateContentDirectory() {
try {
const contentDir = path.join(__dirname, 'content');
console.log('🗑️ Deleting existing content directory...');
// Check if content directory exists
if (fs.existsSync(contentDir)) {
// Remove the content directory recursively
fs.rmSync(contentDir, { recursive: true, force: true });
console.log('✅ Content directory deleted successfully');
} else {
console.log('ℹ️ Content directory does not exist');
}
console.log('📥 Starting fresh content export...');
// Run the Contentstack export command
const exportCommand = `csdx cm:stacks:export -k ${RP_STACK_API_KEY} -d content`;
console.log(`Running: ${exportCommand}`);
execSync(exportCommand, {
stdio: 'inherit',
cwd: __dirname
});
console.log('✅ Content export completed successfully!');
// Verify the content directory was created
if (fs.existsSync(contentDir)) {
console.log('✅ New content directory created');
// Show some basic stats about the exported content
const stats = getContentStats(contentDir);
console.log('📊 Export Summary:');
console.log(` - Directories: ${stats.directories}`);
console.log(` - Files: ${stats.files}`);
} else {
console.log('⚠️ Warning: Content directory was not created');
}
} catch (error) {
console.error('❌ Error updating content directory:', error.message);
process.exit(1);
}
}
/**
* Get basic statistics about the content directory
* @param {string} dirPath - Path to the content directory
* @returns {object} Stats object with file and directory counts
*/
function getContentStats(dirPath) {
let files = 0;
let directories = 0;
function countItems(currentPath) {
try {
const items = fs.readdirSync(currentPath);
for (const item of items) {
const itemPath = path.join(currentPath, item);
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
directories++;
countItems(itemPath); // Recursively count subdirectories
} else {
files++;
}
}
} catch (error) {
// Skip inaccessible directories
}
}
countItems(dirPath);
return { files, directories };
}
// CLI interface
if (require.main === module) {
if (!RP_STACK_API_KEY) {
console.error('❌ RP_STACK_API_KEY is not set in environment variables.');
process.exit(1);
}
const args = process.argv.slice(2);
const command = args[0];
if (!command) {
updateContentDirectory(); // Default action
}
command === 'help' && console.log(`
📁 Content Directory Updater
Usage:
npm run content:update - Delete content directory and export fresh content
Export Command Used: csdx cm:export -k ${RP_STACK_API_KEY} -d content
- Export stack used: Red Panda Main Stack
`);
}
module.exports = {
updateContentDirectory,
getContentStats
};