forked from phillipclapham/microchaos-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·123 lines (107 loc) · 4.88 KB
/
Copy pathbuild.js
File metadata and controls
executable file
·123 lines (107 loc) · 4.88 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
117
118
119
120
121
122
123
#!/usr/bin/env node
/**
* MicroChaos Build Script
*
* This script compiles the modular version of MicroChaos into a single file for distribution.
*
* Usage: node build.js
*/
const fs = require("fs");
const path = require("path");
// Configuration
const outputDir = path.join(__dirname, "dist");
const outputFile = path.join(outputDir, "microchaos-cli.php");
const sources = {
header: path.join(__dirname, "microchaos-cli.php"),
components: [
// Load constants first
path.join(__dirname, "microchaos/core/constants.php"),
// Load interfaces (logger first - used by all components)
path.join(__dirname, "microchaos/core/interfaces/logger.php"),
path.join(__dirname, "microchaos/core/interfaces/baseline-storage.php"),
// Load logging infrastructure (before any component that logs)
path.join(__dirname, "microchaos/core/log.php"),
path.join(__dirname, "microchaos/core/logging/wp-cli-logger.php"),
// Note: null-logger.php excluded from production build (test-only)
// Load storage implementations
path.join(__dirname, "microchaos/core/storage/transient-baseline-storage.php"),
// Load authentication manager (before components that use it)
path.join(__dirname, "microchaos/core/authentication-manager.php"),
// Load core components
path.join(__dirname, "microchaos/core/thresholds.php"),
path.join(__dirname, "microchaos/core/integration-logger.php"),
path.join(__dirname, "microchaos/core/request-generator.php"),
path.join(__dirname, "microchaos/core/resource-monitor.php"),
path.join(__dirname, "microchaos/core/cache-analyzer.php"),
path.join(__dirname, "microchaos/core/reporting-engine.php"),
// Load orchestrators (before commands which depends on them)
path.join(__dirname, "microchaos/core/orchestrators/loadtest-orchestrator.php"),
// Load commands last (depends on orchestrators)
path.join(__dirname, "microchaos/core/commands.php"),
],
};
console.log("Building MicroChaos single-file distribution...");
// Create output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
console.log(`Creating output directory: ${outputDir}`);
fs.mkdirSync(outputDir, { recursive: true });
}
// Extract the header part of the main file (up to but not including the bootstrap loader)
const headerContents = fs.readFileSync(sources.header, "utf8");
const headerPattern = /^(.+?\/\/ Bootstrap MicroChaos components)/ms;
const headerMatches = headerContents.match(headerPattern);
const header = headerMatches ? headerMatches[1] : "";
if (!header) {
console.error("Error: Could not extract header from main file.");
process.exit(1);
}
// Start with the plugin header
let compiledCode = header + "\n\n";
compiledCode += `/**\n * COMPILED SINGLE-FILE VERSION\n * Generated on: ${new Date().toISOString()}\n * \n * This is an automatically generated file - DO NOT EDIT DIRECTLY\n * Make changes to the modular version and rebuild.\n */\n\n`;
// Collect all component classes
const classContents = [];
for (const componentFile of sources.components) {
console.log(`Processing component: ${path.basename(componentFile)}`);
const content = fs.readFileSync(componentFile, "utf8");
// Remove PHP opening tags, prevent direct access blocks, etc.
const cleanedContent = content
.replace(/^<\?php/, "")
.replace(/\/\/ Prevent direct access[\s\S]+?exit;\s*\}/m, "")
.replace(/if \(!defined\('ABSPATH'\)[\s\S]+?exit;\s*\}/m, "")
.replace(/if \(!defined\('WP_CLI'\)[\s\S]+?exit;\s*\}/m, "");
// Extract class or interface declaration and all its content
const classPattern = /(class|interface)\s+([A-Za-z0-9_]+)[\s\S]+?^}/ms;
const classMatches = cleanedContent.match(classPattern);
if (classMatches && classMatches[0]) {
classContents.push(classMatches[0]);
} else {
console.warn(
`Warning: Could not extract class/interface from ${path.basename(
componentFile
)}`
);
}
}
// Add component classes to compiled code
compiledCode += "if (defined('WP_CLI') && WP_CLI) {\n\n";
compiledCode += classContents.join("\n\n");
compiledCode += "\n\n";
// Add logger initialization and WP-CLI command registration
compiledCode += " // Initialize the WP-CLI logger\n";
compiledCode += " MicroChaos_Log::set_logger(new MicroChaos_WP_CLI_Logger());\n\n";
compiledCode += " // Register the MicroChaos WP-CLI command\n";
compiledCode += " WP_CLI::add_command('microchaos', 'MicroChaos_Commands');\n";
compiledCode += "}\n";
// Write the compiled code to the output file
console.log(`Writing compiled code to: ${outputFile}`);
fs.writeFileSync(outputFile, compiledCode);
// Verify the file was created successfully
if (fs.existsSync(outputFile)) {
const stats = fs.statSync(outputFile);
console.log(
`Build successful! Single-file version created at: ${outputFile}`
);
console.log(`File size: ${(stats.size / 1024).toFixed(2)} KB`);
} else {
console.error("Error: Failed to create output file.");
}