1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

create package with hash

This commit is contained in:
tdzl2003 2016-02-23 18:49:53 +08:00
parent 8b887383c6
commit 9ecd7133c5
4 changed files with 84 additions and 49 deletions

View File

@ -47,7 +47,7 @@
"hasValue": true
},
"output": {
"default": "build/output/${platform}.ppk",
"default": "build/output/${platform}.${hash}.ppk",
"hasValue": true
},
"verbose": {

View File

@ -4,10 +4,14 @@
import * as path from 'path';
import { mkdir as mkdirRecurisve } from 'mkdir-recursive';
import { getRNVersion } from './utils';
import {
getRNVersion,
translateOptions,
} from './utils';
import * as fs from 'fs';
import {ZipFile} from 'yazl';
import crypto from 'crypto';
function mkdir(dir){
return new Promise((resolve, reject) => {
@ -21,10 +25,38 @@ function mkdir(dir){
});
}
function pack(dir, output){
return mkdir(path.dirname(output))
.then(()=>{
function calcMd5ForFile(fn) {
return new Promise((resolve, reject) => {
var hash = crypto.createHash('md5'),
stream = fs.createReadStream(fn);
stream.on('data', (data) => hash.update(data, 'utf8'));
stream.on('end', () => resolve(hash.digest('hex')));
stream.on('error', err => reject(err));
})
}
async function calcMd5ForDirectory(dir) {
const childs = fs.readdirSync(dir).sort();
const result = {};
for (const name of childs) {
const fullPath = path.join(dir, name);
const stat = fs.statSync(fullPath);
if (stat.isFile()) {
result[name] = 'file:' + await calcMd5ForFile(fullPath);
} else {
result[name] = 'directory:' + await calcMd5ForDirectory(fullPath);
}
}
var hash = crypto.createHash('md5');
hash.update(JSON.stringify(result), 'md5');
return hash.digest('hex');
}
async function pack(dir, output){
await mkdir(path.dirname(output))
const hash = await calcMd5ForDirectory(dir);
await new Promise((resolve, reject) => {
var zipfile = new ZipFile();
function addDirectory(root, rel){
@ -51,13 +83,13 @@ function pack(dir, output){
addDirectory(dir, '');
zipfile.outputStream.on('error', err => reject(err));
zipfile.outputStream.pipe(fs.createWriteStream(output))
zipfile.outputStream.pipe(fs.createWriteStream(output.replace(/\$\{hash\}/g, hash)))
.on("close", function() {
resolve();
});
zipfile.end();
});
})
console.log('Bundled with hash: ' + hash);
}
export const commands = {
@ -69,7 +101,7 @@ export const commands = {
output,
dev,
verbose
} = options;
} = translateOptions(options);
if (!platform) {
throw new Error('Platform must be specified.');
@ -87,6 +119,7 @@ export const commands = {
/private-cli\/src/,
/local-cli/,
]);
const Config = require(path.resolve('node_modules/react-native/local-cli/util/Config'));
const bundle = require(path.resolve('node_modules/react-native/local-cli/bundle/bundle'));
const defaultConfig = require(path.resolve('node_modules/react-native/local-cli/default.config'));

View File

@ -21,22 +21,9 @@ const commands = {
help: printUsage,
};
function translateOptions(options){
for (let key in options) {
const v = options[key];
if (typeof(v) === 'string') {
options[key] = v.replace(/\$\{(\w+)\}/, function (v, n){
return options[n] || process.env[n] || v;
})
}
}
}
exports.run = function () {
const argv = require('cli-arguments').parse(require('../cli.json'));
translateOptions(argv.options);
loadSession()
.then(()=>commands[argv.command](argv))
.catch(err=>{

View File

@ -15,6 +15,21 @@ export function question(query, password) {
}, (err, result)=> err ? reject(err) : resolve(result)));
}
export function translateOptions(options){
const ret = {};
for (let key in options) {
const v = options[key];
if (typeof(v) === 'string') {
ret[key] = v.replace(/\$\{(\w+)\}/g, function (v, n){
return options[n] || process.env[n] || v;
})
} else {
ret[key] = v;
}
}
return ret;
}
export function getRNVersion() {
const version = JSON.parse(fs.readFileSync(path.resolve('node_modules/react-native/package.json'))).version;