mirror of
https://github.com/araxiaonline/mpq-tools-osx.git
synced 2026-06-13 03:12:25 -04:00
feat: add addFiles option to createArchive for automatic file population
This commit is contained in:
17
README.md
17
README.md
@@ -82,13 +82,26 @@ Extract all files from an MPQ archive.
|
||||
- `outputDir`: Optional output directory
|
||||
- Returns: true if successful
|
||||
|
||||
### `createArchive(directory, version)`
|
||||
### `createArchive(directory, version, outputPath, options)`
|
||||
|
||||
Create a new MPQ archive from a directory.
|
||||
|
||||
- `directory`: Directory to create MPQ from
|
||||
- `version`: MPQ version (1 or 2, default: 2)
|
||||
- Returns: true if successful
|
||||
- `outputPath`: Optional path for the MPQ file. If not provided, creates it next to the directory
|
||||
- `options`: Additional options object:
|
||||
- `addFiles`: If true, automatically adds all files from the directory to the archive (default: false)
|
||||
- Returns: Path to the created MPQ file
|
||||
|
||||
Example:
|
||||
```javascript
|
||||
// Create MPQ and automatically add all files from the directory
|
||||
const mpqPath = mpq.createArchive('directory/to/archive', 2, 'output.mpq', { addFiles: true });
|
||||
console.log(`Created and populated MPQ at: ${mpqPath}`);
|
||||
|
||||
// Create empty MPQ (traditional way)
|
||||
const emptyMpqPath = mpq.createArchive('directory/to/archive', 2);
|
||||
```
|
||||
|
||||
### Example command line command
|
||||
|
||||
|
||||
34
lib/index.js
34
lib/index.js
@@ -117,9 +117,11 @@ class MPQTool {
|
||||
* @param {string} directory - Directory to create MPQ from
|
||||
* @param {number} version - MPQ version (1 or 2)
|
||||
* @param {string} [outputPath] - Optional path for the MPQ file. If not provided, creates it next to the directory
|
||||
* @param {Object} [options] - Additional options
|
||||
* @param {boolean} [options.addFiles=false] - Whether to automatically add all files from the directory
|
||||
* @returns {string} Path to the created MPQ file
|
||||
*/
|
||||
createArchive(directory, version = 2, outputPath = null) {
|
||||
createArchive(directory, version = 2, outputPath = null, options = { addFiles: false }) {
|
||||
try {
|
||||
const absoluteDir = path.resolve(directory);
|
||||
const dirName = path.basename(absoluteDir);
|
||||
@@ -137,9 +139,9 @@ class MPQTool {
|
||||
const createdMpq = path.join(workingDir, dirName + '.mpq');
|
||||
|
||||
// Move to final destination if specified
|
||||
const finalMpqPath = outputPath ? path.resolve(outputPath) : createdMpq;
|
||||
if (outputPath) {
|
||||
const finalPath = path.resolve(outputPath);
|
||||
const finalDir = path.dirname(finalPath);
|
||||
const finalDir = path.dirname(finalMpqPath);
|
||||
|
||||
// Create output directory if needed
|
||||
if (!require('fs').existsSync(finalDir)) {
|
||||
@@ -147,11 +149,31 @@ class MPQTool {
|
||||
}
|
||||
|
||||
// Move the MPQ file
|
||||
require('fs').renameSync(createdMpq, finalPath);
|
||||
return finalPath;
|
||||
require('fs').renameSync(createdMpq, finalMpqPath);
|
||||
}
|
||||
|
||||
// Automatically add files if requested
|
||||
if (options.addFiles) {
|
||||
const fs = require('fs');
|
||||
const self = this;
|
||||
|
||||
function addFilesRecursively(dir, baseDir) {
|
||||
const files = fs.readdirSync(dir);
|
||||
files.forEach(file => {
|
||||
const fullPath = path.join(dir, file);
|
||||
const relativePath = path.relative(baseDir, fullPath);
|
||||
if (fs.statSync(fullPath).isDirectory()) {
|
||||
addFilesRecursively(fullPath, baseDir);
|
||||
} else {
|
||||
self.addFile(finalMpqPath, fullPath, relativePath);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
addFilesRecursively(absoluteDir, absoluteDir);
|
||||
}
|
||||
|
||||
return createdMpq;
|
||||
return finalMpqPath;
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to create archive: ${error.message}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user