UNPKG

mdev-pre

Version:

mdev is a compiler for module development.

256 lines (214 loc) 7.98 kB
var fnGetUserDirectory = function () { var child = require('child_process'); var platform = process.platform + '-' + process.arch; var ec; if (platform === 'win32-x64') { ec = 'echo %HOMEDRIVE%%HOMEPATH%'; } else { ec = 'cd ~ && pwd'; } var ecl = child.execSync(ec); return ecl.toString().replace(/\\/g, '/').replace(/\r/g, '').replace(/\n/g, ''); }; var fnUpdate = function (fn) { require('shelljs/global'); var child = require('child_process'); var fs = require('fs'); var url = require('url'); var base = __dirname.replace(/\\/g, '/'); var user = fnGetUserDirectory(); var fnDownload = function (downLoadUrl, filePath) { var http = require('http'); var url = require('url'); return new Promise (function (resolve, reject) { var options = { host: url.parse(downLoadUrl).hostname, port: url.parse(downLoadUrl).port || 80, path: url.parse(downLoadUrl).pathname }; fs.writeFileSync(filePath, '', 'utf-8'); http.get(options, function(res) { res.on('data', function(data) { fs.appendFileSync(filePath, data, 'utf-8'); }); res.on('end', function() { if (res.statusCode !== 200) { console.log(`download error. ["${res.statusCode}"]["${downLoadUrl}"]`); return; } resolve(); }); res.on('error', function (e) { console.log(`download error. ["${e.message}"]["${downLoadUrl}"]`); reject(e); }); }) .on('error', (e) => { console.log(`download error. ["${e.message}"]["${downLoadUrl}"]`); reject(e); }); }); }; var fnDeCompress = function (work, gzip, dist) { var platform = process.platform + '-' + process.arch; var fnGetTool = function () { var tar, gzip; if (platform === 'win32-x64') { tar = `${base}/tool-tar/${platform}/tar`; gzip = `${base}/tool-tar/${platform}/gzip`; } else { tar = 'tar'; gzip = 'gzip'; } return { tar: tar, gzip: gzip }; }; var tool = fnGetTool(); var ec = platform === 'win32-x64' ? `${work.split(':')[0]}: && ` : ''; ec += `cd ${work} && ${tool.gzip} -d ${gzip} && ${tool.tar} -xvf ` + `${gzip}`.replace(/.gz$/, ''); child.execSync(ec); var arr = fs.readdirSync(work); var key = +new Date(); mkdir('-p', `${dist}/${key}`); arr.forEach(function (item) { if (fs.existsSync(`${dist}/${item}`)) { mv('-f', `${dist}/${item}`, `${dist}/${key}/${item}`); } }); mv('-f', `${work}/\*`, dist); rm('-rf', work); }; var fnUpdate = function (arr) { if (!arr.length) { console.log('update complete.'); fn && fn(); return; } var update = arr.shift(); var workPath = `${user}/mdev/__work/tar-${Math.random()}`; var distPath = `${user}/mdev/${update.dist}`; var fileName = url.parse(update.patch).pathname.split('/').pop(); mkdir('-p', workPath); mkdir('-p', distPath); console.log(`download files... ["${update.patch}"]`); fnDownload(update.patch, `${workPath}/${fileName}`).then( (code) => { console.log(`download files success. ["${update.patch}"]`); console.log(`unpack files... ["${update.patch}"]`); fnDeCompress(workPath, fileName, distPath); var updatedPath = `${user}/mdev/update.json`; var updatedJSON = {}; if (fs.existsSync(updatedPath)) { updatedJSON = JSON.parse(fs.readFileSync(updatedPath, 'utf-8')); } updatedJSON[update.key] = true; fs.writeFileSync(updatedPath, JSON.stringify(updatedJSON, null, 4), 'utf-8'); console.log(`unpack files success. ["${update.patch}"]`); fnUpdate(arr); }, (e) => { fnUpdate(arr); } ); }; var fnCheckUpdate = function (fn) { var platform = process.platform + '-' + process.arch; var fnGetUpdateList = function (fn) { var ec = ''; if (platform === 'win32-x64') { ec = base.split(':')[0] + ': && '; } else { ec = ''; } ec += 'cd ' + user + '/mdev && npm install mdev-update'; mkdir('-p', `${user}/mdev`); var m = `${user}/mdev/update.js`; if (!fs.existsSync(m)) { fs.writeFileSync(m, 'module.exports = require("mdev-update")', 'utf8') } child.exec(ec, function () { var ulist = require(m); var updatedPath = `${user}/mdev/update.json`; var updatedJSON = {}; if (fs.existsSync(updatedPath)) { updatedJSON = JSON.parse(fs.readFileSync(updatedPath, 'utf-8')); } var updatingList = []; ulist.forEach(function (update, i) { if (typeof update.patch !== 'string' || !update.patch.match(/^http/)) { return; } if (typeof update.dist !== 'string' || !update.dist.match(/^\/.*\/$/)) { return; } if (typeof update.key !== 'string') { return; } if (typeof update.platform !== 'string') { return; } if (updatedJSON[update.key] === true) { return; } if (update.platform === '*' || update.platform === platform) { updatingList.push(update); } }); console.log(`${updatingList.length} modules need to be updated.`); fn(updatingList); }); }; console.log('check update list ...'); fnGetUpdateList(fn); }; fnCheckUpdate(function (updatingList) { fnUpdate(updatingList); }); }; var fnAutoUpdate = function (fn) { require('shelljs/global'); var fs = require('fs'); var delay = 1000 * 60 * 60 * 24; var user = fnGetUserDirectory(); var recordPath = `${user}/mdev/record`; mkdir('-p', `${user}/mdev`); if (!fs.existsSync(recordPath)) { fs.writeFileSync(recordPath, new Date('2000-01-01'), 'utf8'); } var last = fs.readFileSync(recordPath, 'utf8'); try { last = new Date(last); } catch(e) { last = new Date(); } var diff = (+new Date()) - last; if (diff > delay) { console.log('auto update...'); fnUpdate(function () { fs.writeFileSync(recordPath, new Date(), 'utf8'); fn && fn(); }); } else { fn && fn(); } }; var fnInit = function () { var argv = process.argv; var command = argv[2]; if (command === 'update') { fnUpdate(); return; } fnAutoUpdate(function () { var fs = require('fs'); var module = fnGetUserDirectory() + '/mdev/src/app.js'; if (!fs.existsSync(module)) { console.log(`Can\'t find the module, try command "mdev update". ["${module}"]`); } else { require(module); } }); }; fnInit();