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

兼容0.59的bundle命令

This commit is contained in:
sunnylqm 2019-04-14 13:31:26 +08:00
parent a17ce21718
commit 453a861302

View File

@ -5,10 +5,7 @@
import * as path from 'path';
import { mkdir as mkdirRecurisve } from 'mkdir-recursive';
import rmdirRecursive from 'rimraf';
import {
getRNVersion,
translateOptions,
} from './utils';
import { getRNVersion, translateOptions } from './utils';
import * as fs from 'fs';
import { ZipFile } from 'yazl';
import { open as openZipFile } from 'yauzl';
@ -16,6 +13,7 @@ import {open as openZipFile} from 'yauzl';
import { question } from './utils';
import { checkPlatform } from './app';
import crypto from 'crypto';
import minimist from 'minimist';
var diff;
try {
@ -23,9 +21,11 @@ try {
diff = typeof bsdiff != 'function' ? bsdiff.diff : bsdiff;
} catch (e) {
diff = function() {
console.warn('This function needs "node-bsdiff". Please run "npm i node-bsdiff -S" from your project directory first!');
throw new Error('This function needs module "node-bsdiff". Please install it first.')
}
console.warn(
'This function needs "node-bsdiff". Please run "npm i node-bsdiff -S" from your project directory first!',
);
throw new Error('This function needs module "node-bsdiff". Please install it first.');
};
}
function mkdir(dir) {
@ -81,8 +81,7 @@ async function pack(dir, output){
addDirectory(dir, '');
zipfile.outputStream.on('error', err => reject(err));
zipfile.outputStream.pipe(fs.createWriteStream(output))
.on("close", function() {
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', function() {
resolve();
});
zipfile.end();
@ -101,18 +100,13 @@ function readEntire(entry, zipFile) {
end() {
resolve(Buffer.concat(buffers));
},
prependListener() {
},
on() {
},
once() {
},
emit() {
},
})
prependListener() {},
on() {},
once() {},
emit() {},
});
});
});
})
}
function basename(fn) {
@ -136,7 +130,7 @@ async function diffFromPPK(origin, next, output) {
if (entry.fileName === 'index.bundlejs') {
// This is source.
return readEntire(entry, zipFile).then(v=>originSource = v);
return readEntire(entry, zipFile).then(v => (originSource = v));
}
}
});
@ -148,9 +142,10 @@ async function diffFromPPK(origin, next, output) {
var zipfile = new ZipFile();
const writePromise = new Promise((resolve, reject) => {
zipfile.outputStream.on('error', err => {throw err;});
zipfile.outputStream.pipe(fs.createWriteStream(output))
.on("close", function() {
zipfile.outputStream.on('error', err => {
throw err;
});
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', function() {
resolve();
});
});
@ -218,7 +213,7 @@ async function diffFromPPK(origin, next, output) {
resolve();
});
});
})
});
}
});
@ -259,7 +254,7 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
if (fn === originBundleName) {
// This is source.
return readEntire(entry, zipFile).then(v=>originSource = v);
return readEntire(entry, zipFile).then(v => (originSource = v));
}
}
});
@ -271,9 +266,10 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
var zipfile = new ZipFile();
const writePromise = new Promise((resolve, reject) => {
zipfile.outputStream.on('error', err => {throw err;});
zipfile.outputStream.pipe(fs.createWriteStream(output))
.on("close", function() {
zipfile.outputStream.on('error', err => {
throw err;
});
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', function() {
resolve();
});
});
@ -312,7 +308,7 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
resolve();
});
});
})
});
}
});
@ -331,7 +327,7 @@ function enumZipEntries(zipFn, callback) {
zipfile.on('error', reject);
zipfile.on('entry', entry => {
const result = callback(entry, zipfile);
if (result && typeof(result.then) === 'function') {
if (result && typeof result.then === 'function') {
result.then(() => zipfile.readEntry());
} else {
zipfile.readEntry();
@ -344,15 +340,12 @@ function enumZipEntries(zipFn, callback) {
export const commands = {
bundle: async function({ options }) {
const platform = checkPlatform(options.platform || await question('Platform(ios/android):'));
const platform = checkPlatform(options.platform || (await question('Platform(ios/android):')));
let {
entryFile,
intermediaDir,
output,
dev,
verbose
} = translateOptions({...options, platform});
let { entryFile, intermediaDir, output, dev, verbose } = translateOptions({
...options,
platform,
});
const realIntermedia = path.resolve(intermediaDir);
@ -390,9 +383,7 @@ export const commands = {
// This line fix issue #11
require(path.resolve('node_modules/react-native/local-cli/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'));
let defaultConfig;
let Config, defaultConfig, bundle;
if (major === 0) {
if (minor >= 49) {
@ -403,7 +394,38 @@ export const commands = {
}
if (major === 0) {
if (minor >= 57) {
if (minor >= 0.59) {
Config = require(path.resolve('node_modules/@react-native-community/cli/build/tools/loadMetroConfig'));
bundle = require(path.resolve('node_modules/@react-native-community/cli/build/commands/bundle/bundle')).default;
} else {
Config = require(path.resolve('node_modules/react-native/local-cli/util/Config'));
bundle = require(path.resolve('node_modules/react-native/local-cli/bundle/bundle'));
}
if (minor >= 59) {
// https://github.com/react-native-community/cli/blob/1.x/packages/cli/src/cliEntry.js#L170-L202
const options = minimist(process.argv.slice(2));
const root = options.projectRoot ? path.resolve(options.projectRoot) : process.cwd();
const reactNativePath = options.reactNativePath
? path.resolve(options.reactNativePath)
: (() => {
try {
return path.dirname(
require.resolve('react-native/package.json', {
paths: [root],
}),
);
} catch (_ignored) {
throw new Error('Unable to find React Native files. Please use --reactNativePath to specify the path.');
}
})();
defaultConfig = {
reactNativePath,
root,
};
} else if (minor >= 57) {
// https://github.com/facebook/react-native/commit/a32620dc3b7a0ebd53feeaf7794051705d80f49e#diff-75692fe55c8b1a7c05f4264301342167L101
// defaultConfig = Config.load();
const { configPromise } = require(path.resolve('node_modules/react-native/local-cli/core'));
@ -414,21 +436,27 @@ export const commands = {
defaultConfig = Config.get(
path.resolve('node_modules/react-native/local-cli'),
require(path.resolve('node_modules/react-native/local-cli/core/default.config')),
path.resolve('node_modules/react-native/packager/rn-cli.config.js'));
path.resolve('node_modules/react-native/packager/rn-cli.config.js'),
);
} else if (minor >= 33) {
defaultConfig = Config.get(
path.resolve('node_modules/react-native/local-cli'),
require(path.resolve('node_modules/react-native/local-cli/default.config')),
path.resolve('node_modules/react-native/packager/rn-cli.config.js'));
path.resolve('node_modules/react-native/packager/rn-cli.config.js'),
);
} else {
defaultConfig= Config.get(path.resolve('node_modules/react-native/local-cli'), require(path.resolve('node_modules/react-native/local-cli/default.config')));
defaultConfig = Config.get(
path.resolve('node_modules/react-native/local-cli'),
require(path.resolve('node_modules/react-native/local-cli/default.config')),
);
}
} else {
defaultConfig = Config.findOptional(path.resolve('.'));
}
console;
if (bundle.func) {
// React native after 0.31.0
// React native >= 0.31.0
await bundle.func([], defaultConfig, {
entryFile: entryFile,
platform: platform,
@ -439,8 +467,9 @@ export const commands = {
bundleEncoding: 'utf8',
});
} else {
// React native before 0.30.0
await bundle([
// React native < 0.31.0
await bundle(
[
'--entry-file',
entryFile,
'--platform',
@ -453,7 +482,9 @@ export const commands = {
`${realIntermedia}`,
'--verbose',
'' + !!verbose,
], defaultConfig);
],
defaultConfig,
);
}
console.log('Packing');
@ -462,9 +493,12 @@ export const commands = {
const v = await question('Would you like to publish it?(Y/N)');
if (v.toLowerCase() === 'y') {
await this.publish({args: [realOutput], options: {
await this.publish({
args: [realOutput],
options: {
platform,
}})
},
});
}
},