123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- var fs = require('fs');
- var path = require('path');
- var crypto = require('crypto');
- var manifest = {
- packageUrl: 'http://localhost/tutorial-hot-update/remote-assets/',
- remoteManifestUrl: 'http://localhost/tutorial-hot-update/remote-assets/project.manifest',
- remoteVersionUrl: 'http://localhost/tutorial-hot-update/remote-assets/version.manifest',
- version: '1.0.0',
- assets: {},
- searchPaths: []
- };
- var dest = './remote-assets/';
- var src = './jsb/';
- // Parse arguments
- var i = 2;
- while (i < process.argv.length) {
- var arg = process.argv[i];
- switch (arg) {
- case '--url' :
- case '-u' :
- var url = process.argv[i + 1];
- manifest.packageUrl = url;
- manifest.remoteManifestUrl = url + 'project.manifest';
- manifest.remoteVersionUrl = url + 'version.manifest';
- i += 2;
- break;
- case '--version' :
- case '-v' :
- manifest.version = process.argv[i + 1];
- i += 2;
- break;
- case '--src' :
- case '-s' :
- src = process.argv[i + 1];
- i += 2;
- break;
- case '--dest' :
- case '-d' :
- dest = process.argv[i + 1];
- i += 2;
- break;
- default :
- i++;
- break;
- }
- }
- function readDir(dir, obj) {
- var stat = fs.statSync(dir);
- if (!stat.isDirectory()) {
- return;
- }
- var subpaths = fs.readdirSync(dir), subpath, size, md5, compressed, relative;
- for (var i = 0; i < subpaths.length; ++i) {
- if (subpaths[i][0] === '.') {
- continue;
- }
- subpath = path.join(dir, subpaths[i]);
- stat = fs.statSync(subpath);
- if (stat.isDirectory()) {
- readDir(subpath, obj);
- } else if (stat.isFile()) {
- // Size in Bytes
- size = stat['size'];
- md5 = crypto.createHash('md5').update(fs.readFileSync(subpath)).digest('hex');
- compressed = path.extname(subpath).toLowerCase() === '.zip';
- relative = path.relative(src, subpath);
- relative = relative.replace(/\\/g, '/');
- relative = encodeURI(relative);
- obj[relative] = {
- 'size': size,
- 'md5': md5
- };
- if (compressed) {
- obj[relative].compressed = true;
- }
- }
- }
- }
- var mkdirSync = function (path) {
- try {
- fs.mkdirSync(path);
- } catch (e) {
- if (e.code != 'EEXIST') throw e;
- }
- }
- var http = require('http');
- //http://192.168.0.190:8080/view/all/job/android/lastBuild/buildNumber
- var options = {
- hostname: '192.168.0.190',
- port: 8080,
- path: '/view/all/job/android/lastBuild/buildNumber',
- method: 'GET',
- headers: {
- 'Content-Type': 'text/plain;charset=US-ASCII'
- }
- };
- var type = ['chinese', 'math', 'pinyin','poetry']
- var replaceTypeFuc = function () {
- type.forEach(value => {
- if (manifest.remoteManifestUrl.includes(value)) {
- options.path = `/view/all/job/android_${value}/lastBuild/buildNumber`
- }
- })
- }
- replaceTypeFuc()
- var req = http.get(options, function (res) {
- console.log('STATUS: ' + res.statusCode);
- console.log('HEADERS: ' + JSON.stringify(res.headers));
- res.setEncoding('utf8');
- res.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- let buildNumber = manifest.version == '0.0.0' ? '0' : process.env.BUILD_NUMBER
- manifest.version = '1.' + chunk + '.' + buildNumber
- // Iterate assets and src folder
- readDir(path.join(src, 'src'), manifest.assets);
- readDir(path.join(src, 'assets'), manifest.assets);
- var destManifest = path.join(dest, 'project.manifest');
- var destVersion = path.join(dest, 'version.manifest');
- mkdirSync(dest);
- fs.writeFile(destManifest, JSON.stringify(manifest), (err) => {
- if (err) throw err;
- console.log('Manifest successfully generated');
- });
- delete manifest.assets;
- delete manifest.searchPaths;
- fs.writeFile(destVersion, JSON.stringify(manifest), (err) => {
- if (err) throw err;
- console.log('Version successfully generated');
- });
- });
- });
- req.on('error', function (e) {
- console.log('problem with request: ' + e.message);
- });
- req.end();
|