Skip to content

Instantly share code, notes, and snippets.

@nextend
Created July 30, 2015 06:55
Show Gist options
  • Save nextend/f41781b0aa348031be3f to your computer and use it in GitHub Desktop.
Save nextend/f41781b0aa348031be3f to your computer and use it in GitHub Desktop.
Create Magento extension xml with nodejs as a build process
var fs = require('fs'),
path = require('path'),
builder = require('xmlbuilder'),
crypto = require('crypto');
var targetDir = process.argv[2] + '/',
magentoVersion = process.argv[3];
function xmlAddSyncRecursive(xml, cpath, failSilent) {
var files;
try {
files = fs.readdirSync(cpath);
} catch (err) {
if (failSilent) return;
throw new Error(err.message);
}
for (var i = 0; i < files.length; i++) {
var currFile = fs.lstatSync(path.join(cpath, files[i]));
if (currFile.isDirectory()) {
var dir = xml.ele('dir', {name: files[i]});
xmlAddSyncRecursive(dir, path.join(cpath, files[i]));
} else {
xml.ele('file', {
name: files[i],
hash: crypto.createHash('md5').update(fs.readFileSync(path.join(cpath, files[i]), 'utf8')).digest('hex')
});
}
}
try {
files = fs.readdirSync(cpath);
} catch (err) {
if (failSilent) return;
throw new Error(err.message);
}
if (files.length == 0) {
xml.remove();
fs.rmdirSync(cpath);
}
return true;
};
var xml = builder.create('package');
xml.ele('name', null, 'Nextend');
xml.ele('version', null, magentoVersion);
xml.ele('stability', null, 'stable');
xml.ele('license', {uri: 'http://www.gnu.org/licenses/gpl-3.0.txt'}, 'GNU GENERAL PUBLIC LICENSE V3');
xml.ele('channel', null, 'community');
xml.ele('extends');
xml.ele('summary', null, '');
xml.ele('description', null, '');
xml.ele('notes', null, 'No notes');
var author = xml.ele('authors', null).ele('author', null);
author.ele('name', null, 'Nextendweb.com');
author.ele('user', null, 'nextendroland');
author.ele('email', null, '[email protected]');
xml.ele('date', null, '2015-08-01');
xml.ele('time', null, '00:00:00');
xml.ele('compatible');
var dependencies = xml.ele('dependencies');
var required = dependencies.ele('required');
var php = required.ele('php');
php.ele('min', null, '5.0.0');
php.ele('max', null, '8.0.0');
var contents = xml.ele('contents', null);
var mageetc = contents.ele('target', {name: 'mageetc'});
xmlAddSyncRecursive(mageetc, targetDir + 'app/etc/');
var magecommunity = contents.ele('target', {name: 'magecommunity'});
xmlAddSyncRecursive(magecommunity, targetDir + 'app/code/community/');
var magedesign = contents.ele('target', {name: 'magedesign'});
xmlAddSyncRecursive(magedesign, targetDir + 'app/design/');
var magemedia = contents.ele('target', {name: 'magemedia'});
xmlAddSyncRecursive(magemedia, targetDir + 'media/');
fs.writeFileSync(targetDir + 'package.xml', xml.end({pretty: true}), 'utf8');
@nextend
Copy link
Author

nextend commented Jul 30, 2015

Usage:
node magento-packager.js /path/to/extension 1.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment