Repository navigation
Expand file tree
/
Copy pathcreate_build.js
More file actions
180 lines (158 loc) · 6.45 KB
/
Copy pathcreate_build.js
File metadata and controls
180 lines (158 loc) · 6.45 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const fsPromises = require('fs').promises; // Use promises API for async operations
const fs = require('fs'); // Use standard API for streams
const path = require('path');
const archiver = require('archiver');
const os = require('os');
// --- Configuration ---
const includedFilesAndDirs = [
'.modes',
'.processes',
'.roo',
'.templates',
'.workflows',
'build_mode_summary.js',
'build_roomodes.js',
'LICENSE',
'.roomodes',
'create_build.js' // Include the build script itself
];
const emptyDirsToCreate = [
'.archive',
'.context',
'.decisions',
'.docs',
'.ideas',
'.logs',
'.planning',
'.reports',
'.snippets',
'.tasks'
];
const outputDir = '.builds';
// --- End Configuration ---
async function createBuild() {
// --- Argument Parsing ---
const args = process.argv.slice(2);
if (args.length < 4) {
console.error('Usage: node create_build.js <version> <codename> <distReadmePath> <changelogPath>');
console.error('Example: node create_build.js v7.01 Wallaby .templates/build/README.dist.md .tmp/CHANGELOG.v7.01.md');
process.exit(1);
}
const version = args[0]; // e.g., v7.01
const codename = args[1]; // e.g., Wallaby
const distReadmePath = args[2]; // Path to the prepared README for the zip
const changelogPath = args[3]; // Path to the prepared CHANGELOG for the zip
const archiveNameStem = `roo-commander-${version}-${codename}`;
const archiveFileName = `${archiveNameStem}.zip`;
const outputZipPath = path.join(outputDir, archiveFileName);
const stagingDir = await fsPromises.mkdtemp(path.join(os.tmpdir(), `roo-build-${version}-`)); // Use fsPromises
console.log(`Starting build for ${version} (${codename})...`);
console.log(`Staging directory: ${stagingDir}`);
console.log(`Output archive: ${outputZipPath}`);
try {
// --- Ensure Output Directory Exists ---
await fsPromises.mkdir(outputDir, { recursive: true }); // Use fsPromises
// --- Copy Included Files/Dirs ---
console.log('Copying included files and directories...');
for (const item of includedFilesAndDirs) {
const sourcePath = path.resolve(__dirname, item); // Use absolute path for source
const destPath = path.join(stagingDir, item);
try {
const stats = await fsPromises.stat(sourcePath); // Use fsPromises
if (stats.isDirectory()) {
console.log(` Copying directory: ${item}`);
await fsPromises.cp(sourcePath, destPath, { recursive: true }); // Use fsPromises
} else {
console.log(` Copying file: ${item}`);
await fsPromises.copyFile(sourcePath, destPath); // Use fsPromises
}
} catch (err) {
if (err.code === 'ENOENT') {
console.warn(` Warning: Source item not found, skipping: ${item}`);
} else {
throw err; // Re-throw other errors
}
}
}
// --- Create Empty Dirs ---
console.log('Creating empty directories...');
for (const dir of emptyDirsToCreate) {
console.log(` Creating empty dir: ${dir}`);
await fsPromises.mkdir(path.join(stagingDir, dir), { recursive: true }); // Use fsPromises
}
// --- Copy Dist README and CHANGELOG ---
console.log('Copying distribution README and CHANGELOG...');
try {
await fsPromises.copyFile(distReadmePath, path.join(stagingDir, 'README.md')); // Use fsPromises
console.log(` Copied ${distReadmePath} to README.md`);
} catch (err) {
console.error(` Error copying distribution README from ${distReadmePath}: ${err.message}`);
throw err;
}
try {
await fsPromises.copyFile(changelogPath, path.join(stagingDir, 'CHANGELOG.md')); // Use fsPromises
console.log(` Copied ${changelogPath} to CHANGELOG.md`);
} catch (err) {
console.error(` Error copying CHANGELOG from ${changelogPath}: ${err.message}`);
throw err;
}
// --- Create Zip Archive ---
console.log(`Creating zip archive: ${archiveFileName}...`);
const output = fs.createWriteStream(outputZipPath);
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// Listen for all archive data to be written
// 'close' event is fired only when a file descriptor is involved
await new Promise((resolve, reject) => {
output.on('close', () => {
console.log(`Archive created successfully: ${archive.pointer()} total bytes`);
resolve();
});
// This event is fired when the data source is drained no matter what was the data source.
// It is not part of this library but rather from the NodeJS Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
output.on('end', () => {
console.log('Data has been drained');
});
// Good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', (err) => {
if (err.code === 'ENOENT') {
console.warn(`Archiver warning: ${err.message}`);
} else {
// Throw error
reject(err);
}
});
// Good practice to catch this error explicitly
archive.on('error', (err) => {
reject(err);
});
// Pipe archive data to the file
archive.pipe(output);
// Append files from staging directory, putting its contents at the root of archive
archive.directory(stagingDir, false);
// Finalize the archive (ie we are done appending files but streams have to finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand
archive.finalize();
});
console.log(`Build complete: ${outputZipPath}`);
} catch (error) {
console.error('Build failed:', error);
process.exitCode = 1; // Indicate failure
} finally {
// --- Cleanup Staging Directory ---
console.log(`Cleaning up staging directory: ${stagingDir}`);
try {
await fsPromises.rm(stagingDir, { recursive: true, force: true }); // Use fsPromises
console.log('Cleanup complete.');
} catch (cleanupError) {
console.error(`Error during cleanup: ${cleanupError.message}`);
// Don't override process exit code if build already failed
if (!process.exitCode) {
process.exitCode = 1;
}
}
}
}
createBuild();