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 "hasValue": true
}, },
"output": { "output": {
"default": "build/output/${platform}.ppk", "default": "build/output/${platform}.${hash}.ppk",
"hasValue": true "hasValue": true
}, },
"verbose": { "verbose": {

View File

@ -4,10 +4,14 @@
import * as path from 'path'; import * as path from 'path';
import { mkdir as mkdirRecurisve } from 'mkdir-recursive'; import { mkdir as mkdirRecurisve } from 'mkdir-recursive';
import { getRNVersion } from './utils'; import {
getRNVersion,
translateOptions,
} from './utils';
import * as fs from 'fs'; import * as fs from 'fs';
import {ZipFile} from 'yazl'; import {ZipFile} from 'yazl';
import crypto from 'crypto';
function mkdir(dir){ function mkdir(dir){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -21,43 +25,71 @@ function mkdir(dir){
}); });
} }
function pack(dir, output){ function calcMd5ForFile(fn) {
return mkdir(path.dirname(output)) return new Promise((resolve, reject) => {
.then(()=>{ var hash = crypto.createHash('md5'),
return new Promise((resolve, reject) => { stream = fs.createReadStream(fn);
var zipfile = new ZipFile();
function addDirectory(root, rel){ stream.on('data', (data) => hash.update(data, 'utf8'));
if (rel) { stream.on('end', () => resolve(hash.digest('hex')));
zipfile.addEmptyDirectory(rel); stream.on('error', err => reject(err));
} })
const childs = fs.readdirSync(root); }
for (const name of childs) {
if (name === '.' || name === '..'){ async function calcMd5ForDirectory(dir) {
continue; const childs = fs.readdirSync(dir).sort();
} const result = {};
const fullPath = path.join(root, name); for (const name of childs) {
const stat = fs.statSync(fullPath); const fullPath = path.join(dir, name);
if (stat.isFile()) { const stat = fs.statSync(fullPath);
console.log('adding: ' + rel+name); if (stat.isFile()) {
zipfile.addFile(fullPath, rel+name); result[name] = 'file:' + await calcMd5ForFile(fullPath);
} else if (stat.isDirectory()) { } else {
console.log('adding: ' + rel+name+'/'); result[name] = 'directory:' + await calcMd5ForDirectory(fullPath);
addDirectory(fullPath, rel+name+'/'); }
} }
} 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){
if (rel) {
zipfile.addEmptyDirectory(rel);
}
const childs = fs.readdirSync(root);
for (const name of childs) {
if (name === '.' || name === '..'){
continue;
} }
const fullPath = path.join(root, name);
const stat = fs.statSync(fullPath);
if (stat.isFile()) {
console.log('adding: ' + rel+name);
zipfile.addFile(fullPath, rel+name);
} else if (stat.isDirectory()) {
console.log('adding: ' + rel+name+'/');
addDirectory(fullPath, rel+name+'/');
}
}
}
addDirectory(dir, ''); addDirectory(dir, '');
zipfile.outputStream.on('error', err => reject(err)); 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() { .on("close", function() {
resolve(); resolve();
});
zipfile.end();
}); });
}) zipfile.end();
});
console.log('Bundled with hash: ' + hash);
} }
export const commands = { export const commands = {
@ -69,7 +101,7 @@ export const commands = {
output, output,
dev, dev,
verbose verbose
} = options; } = translateOptions(options);
if (!platform) { if (!platform) {
throw new Error('Platform must be specified.'); throw new Error('Platform must be specified.');
@ -77,7 +109,7 @@ export const commands = {
await mkdir(intermediaDir); await mkdir(intermediaDir);
const { version, major, minor} = getRNVersion(); const { version, major, minor } = getRNVersion();
console.log('Bundling with React Native version: ', version); console.log('Bundling with React Native version: ', version);
@ -87,6 +119,7 @@ export const commands = {
/private-cli\/src/, /private-cli\/src/,
/local-cli/, /local-cli/,
]); ]);
const Config = require(path.resolve('node_modules/react-native/local-cli/util/Config')); 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 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')); const defaultConfig = require(path.resolve('node_modules/react-native/local-cli/default.config'));

View File

@ -21,22 +21,9 @@ const commands = {
help: printUsage, 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 () { exports.run = function () {
const argv = require('cli-arguments').parse(require('../cli.json')); const argv = require('cli-arguments').parse(require('../cli.json'));
translateOptions(argv.options);
loadSession() loadSession()
.then(()=>commands[argv.command](argv)) .then(()=>commands[argv.command](argv))
.catch(err=>{ .catch(err=>{

View File

@ -15,6 +15,21 @@ export function question(query, password) {
}, (err, result)=> err ? reject(err) : resolve(result))); }, (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() { export function getRNVersion() {
const version = JSON.parse(fs.readFileSync(path.resolve('node_modules/react-native/package.json'))).version; const version = JSON.parse(fs.readFileSync(path.resolve('node_modules/react-native/package.json'))).version;