UNPKG

olxupdate

Version:

Running this will update an exported course.tar.gz file with the an olx-structured folder that contains new chapters

96 lines (85 loc) 3.37 kB
#!/usr/bin/env node if (process.argv.length !== 4) { console.log("Usage: olxupdate [course folder] [olx folder]"); console.log("Please make sure that the tar.gz file and the olx folder are in the same directory - the current working directory"); process.exit(0); } var path = require('path'); var fs = require('fs'); var targz = require('tar.gz'); var cwd = process.cwd(); var tarPath = process.argv[2]; var olxPath = process.argv[3]; var xml2js = require('xml2js'); var parse = xml2js.parseString; var builder = new xml2js.Builder(); var dirs = fs.readdirSync(olxPath); var firstNotEmptyFolder = true; var previous = { 'chapter': 'course', 'sequential': 'chapter', 'vertical': 'sequential', 'video': 'vertical', 'problem': 'vertical', 'html': 'vertical', 'discussion': 'vertical' }; var findOneXml = function (dir, folder) { var files = fs.readdirSync(dir); for (var i = 0; i < files.length; i++) { if (files[i].indexOf('.xml' !== -1)) { return path.join(dir, files[i]); } } throw new Error("No " + dir + " to import your " + folder); }; ['chapter', 'sequential', 'vertical', 'video', 'problem', 'html', 'discussion'].forEach(function (folder) { if (dirs.indexOf(folder) !== -1) { var olxFilesPath = path.join(olxPath, folder); var tarFilesPath = path.join(tarPath, folder); var olxFiles = fs.readdirSync(olxFilesPath); if (firstNotEmptyFolder && olxFiles.length > 0) { firstNotEmptyFolder = false; var xmlFile = findOneXml(path.join(tarPath, previous[folder]), folder); parse(fs.readFileSync(xmlFile), function (err, xml) { if (err) { throw err; } else { xml[previous[folder]] for (var i = 0; i < olxFiles.length; i++) { var olxFile = olxFiles[i]; if (olxFile.indexOf('.xml') === -1) { continue; } if (!xml[previous[folder]][folder]) { xml[previous[folder]][folder] = [] } xml[previous[folder]][folder].push({'$': {url_name: olxFile.substring(0, olxFile.indexOf('.xml'))}}); } fs.writeFileSync(xmlFile, builder.buildObject(xml).replace('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n', '')); } }); } for (var i = 0; i < olxFiles.length; i++) { var olxFile = olxFiles[i]; if (olxFile[0] === '.') { continue; } var olxFilePath = path.join(olxFilesPath, olxFile); var tarFilePath = path.join(tarFilesPath, olxFile); if (!fs.existsSync(tarFilePath)) { fs.writeFileSync(tarFilePath, fs.readFileSync(olxFilePath)); } else { throw new Error("File " + olxFile + " already exists in " + folder); } } } }); targz().compress(tarPath, tarPath + '.tar.gz', function (err) { if (err) { throw err; } else { console.log("Warning: " + tarPath + " is modified"); console.log("New course generated: " + tarPath + '.tar.gz'); } });