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