1
0
mirror of https://gitcode.com/github-mirrors/react-native-update-cli.git synced 2025-09-16 09:41:38 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

print version when bundling

This commit is contained in:
sunnylqm
2020-03-27 13:53:17 +08:00
parent 6bc7ee7091
commit 755485b4cb
3 changed files with 109 additions and 48 deletions

View File

@@ -7,7 +7,7 @@ import { getRNVersion, translateOptions } from './utils';
import * as fs from 'fs-extra'; import * as fs from 'fs-extra';
import { ZipFile } from 'yazl'; import { ZipFile } from 'yazl';
import { open as openZipFile } from 'yauzl'; import { open as openZipFile } from 'yauzl';
import { question } from './utils'; import { question, printVersionCommand } from './utils';
import { checkPlatform } from './app'; import { checkPlatform } from './app';
const { spawn, spawnSync, execSync } = require('child_process'); const { spawn, spawnSync, execSync } = require('child_process');
const g2js = require('gradle-to-js/lib/parser'); const g2js = require('gradle-to-js/lib/parser');
@@ -22,7 +22,9 @@ try {
console.warn( console.warn(
'This function needs "node-bsdiff". Please run "npm i node-bsdiff" from your project directory first!', 'This function needs "node-bsdiff". Please run "npm i node-bsdiff" from your project directory first!',
); );
throw new Error('This function needs module "node-bsdiff". Please install it first.'); throw new Error(
'This function needs module "node-bsdiff". Please install it first.',
);
}; };
} }
@@ -50,14 +52,17 @@ async function runReactNativeBundleCommand(
let envArgs = process.env.PUSHY_ENV_ARGS; let envArgs = process.env.PUSHY_ENV_ARGS;
if (envArgs) { if (envArgs) {
Array.prototype.push.apply(reactNativeBundleArgs, envArgs.trim().split(/\s+/)); Array.prototype.push.apply(
reactNativeBundleArgs,
envArgs.trim().split(/\s+/),
);
} }
fs.emptyDirSync(outputFolder); fs.emptyDirSync(outputFolder);
Array.prototype.push.apply(reactNativeBundleArgs, [ Array.prototype.push.apply(reactNativeBundleArgs, [
path.join("node_modules", "react-native", "local-cli", "cli.js"), path.join('node_modules', 'react-native', 'local-cli', 'cli.js'),
"bundle", 'bundle',
'--assets-dest', '--assets-dest',
outputFolder, outputFolder,
'--bundle-output', '--bundle-output',
@@ -80,7 +85,9 @@ async function runReactNativeBundleCommand(
} }
const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs); const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs);
console.log(`Running bundle command: node ${reactNativeBundleArgs.join(' ')}`); console.log(
`Running bundle command: node ${reactNativeBundleArgs.join(' ')}`,
);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
reactNativeBundleProcess.stdout.on('data', data => { reactNativeBundleProcess.stdout.on('data', data => {
@@ -93,7 +100,11 @@ async function runReactNativeBundleCommand(
reactNativeBundleProcess.on('close', async exitCode => { reactNativeBundleProcess.on('close', async exitCode => {
if (exitCode) { if (exitCode) {
reject(new Error(`"react-native bundle" command exited with code ${exitCode}.`)); reject(
new Error(
`"react-native bundle" command exited with code ${exitCode}.`,
),
);
} else { } else {
if (platform === 'android') { if (platform === 'android') {
await compileHermesByteCode(bundleName, outputFolder); await compileHermesByteCode(bundleName, outputFolder);
@@ -116,7 +127,10 @@ async function compileHermesByteCode(bundleName, outputFolder) {
const gradleConfig = await g2js.parseFile('android/app/build.gradle'); const gradleConfig = await g2js.parseFile('android/app/build.gradle');
const projectConfig = gradleConfig['project.ext.react']; const projectConfig = gradleConfig['project.ext.react'];
for (const packagerConfig of projectConfig) { for (const packagerConfig of projectConfig) {
if (packagerConfig.includes('enableHermes') && packagerConfig.includes('true')) { if (
packagerConfig.includes('enableHermes') &&
packagerConfig.includes('true')
) {
enableHermes = true; enableHermes = true;
break; break;
} }
@@ -164,9 +178,11 @@ 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)).on('close', function() { zipfile.outputStream
resolve(); .pipe(fs.createWriteStream(output))
}); .on('close', function() {
resolve();
});
zipfile.end(); zipfile.end();
}); });
console.log('Bundled saved to: ' + output); console.log('Bundled saved to: ' + output);
@@ -219,7 +235,9 @@ async function diffFromPPK(origin, next, output) {
}); });
if (!originSource) { if (!originSource) {
throw new Error(`Bundle file not found! Please use default bundle file name and path.`); throw new Error(
`Bundle file not found! Please use default bundle file name and path.`,
);
} }
const copies = {}; const copies = {};
@@ -230,9 +248,11 @@ async function diffFromPPK(origin, next, output) {
zipfile.outputStream.on('error', err => { zipfile.outputStream.on('error', err => {
throw err; throw err;
}); });
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', function() { zipfile.outputStream
resolve(); .pipe(fs.createWriteStream(output))
}); .on('close', function() {
resolve();
});
}); });
const addedEntry = {}; const addedEntry = {};
@@ -263,7 +283,10 @@ async function diffFromPPK(origin, next, output) {
//console.log('Found bundle'); //console.log('Found bundle');
return readEntire(entry, nextZipfile).then(newSource => { return readEntire(entry, nextZipfile).then(newSource => {
//console.log('Begin diff'); //console.log('Begin diff');
zipfile.addBuffer(diff(originSource, newSource), 'index.bundlejs.patch'); zipfile.addBuffer(
diff(originSource, newSource),
'index.bundlejs.patch',
);
//console.log('End diff'); //console.log('End diff');
}); });
} else { } else {
@@ -312,12 +335,21 @@ async function diffFromPPK(origin, next, output) {
} }
//console.log({copies, deletes}); //console.log({copies, deletes});
zipfile.addBuffer(Buffer.from(JSON.stringify({ copies, deletes })), '__diff.json'); zipfile.addBuffer(
Buffer.from(JSON.stringify({ copies, deletes })),
'__diff.json',
);
zipfile.end(); zipfile.end();
await writePromise; await writePromise;
} }
async function diffFromPackage(origin, next, output, originBundleName, transformPackagePath = v => v) { async function diffFromPackage(
origin,
next,
output,
originBundleName,
transformPackagePath = v => v,
) {
fs.ensureDirSync(path.dirname(output)); fs.ensureDirSync(path.dirname(output));
const originEntries = {}; const originEntries = {};
@@ -345,7 +377,9 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
}); });
if (!originSource) { if (!originSource) {
throw new Error(`Bundle file not found! Please use default bundle file name and path.`); throw new Error(
`Bundle file not found! Please use default bundle file name and path.`,
);
} }
const copies = {}; const copies = {};
@@ -356,9 +390,11 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
zipfile.outputStream.on('error', err => { zipfile.outputStream.on('error', err => {
throw err; throw err;
}); });
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', function() { zipfile.outputStream
resolve(); .pipe(fs.createWriteStream(output))
}); .on('close', function() {
resolve();
});
}); });
await enumZipEntries(next, (entry, nextZipfile) => { await enumZipEntries(next, (entry, nextZipfile) => {
@@ -369,7 +405,10 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
//console.log('Found bundle'); //console.log('Found bundle');
return readEntire(entry, nextZipfile).then(newSource => { return readEntire(entry, nextZipfile).then(newSource => {
//console.log('Begin diff'); //console.log('Begin diff');
zipfile.addBuffer(diff(originSource, newSource), 'index.bundlejs.patch'); zipfile.addBuffer(
diff(originSource, newSource),
'index.bundlejs.patch',
);
//console.log('End diff'); //console.log('End diff');
}); });
} else { } else {
@@ -427,9 +466,18 @@ 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 { bundleName, entryFile, intermediaDir, output, dev, verbose } = translateOptions({ let {
bundleName,
entryFile,
intermediaDir,
output,
dev,
verbose,
} = translateOptions({
...options, ...options,
platform, platform,
}); });
@@ -444,9 +492,16 @@ export const commands = {
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);
printVersionCommand();
await runReactNativeBundleCommand(bundleName, dev, entryFile, intermediaDir, platform); await runReactNativeBundleCommand(
bundleName,
dev,
entryFile,
intermediaDir,
platform,
);
await pack(path.resolve(intermediaDir), realOutput); await pack(path.resolve(intermediaDir), realOutput);
@@ -487,7 +542,12 @@ export const commands = {
process.exit(1); process.exit(1);
} }
await diffFromPackage(origin, next, realOutput, 'assets/index.android.bundle'); await diffFromPackage(
origin,
next,
realOutput,
'assets/index.android.bundle',
);
console.log(`${realOutput} generated.`); console.log(`${realOutput} generated.`);
}, },

View File

@@ -5,6 +5,7 @@
const {loadSession} = require('./api'); const {loadSession} = require('./api');
const updateNotifier = require('update-notifier'); const updateNotifier = require('update-notifier');
import { printVersionCommand } from './utils/index.js';
const pkg = require('../package.json'); const pkg = require('../package.json');
updateNotifier({pkg}).notify({isGlobal: true}); updateNotifier({pkg}).notify({isGlobal: true});
@@ -18,25 +19,6 @@ function printUsage({args}) {
process.exit(1); process.exit(1);
} }
function printVersionCommand() {
if (process.argv.indexOf('-v') >= 0 || process.argv[2] === 'version') {
console.log('react-native-update-cli: ' + pkg.version);
try {
const PACKAGE_JSON_PATH = path.resolve(
process.cwd(),
'node_modules',
'react-native-update',
'package.json'
);
console.log('react-native-update: ' + require(PACKAGE_JSON_PATH).version);
} catch (e) {
console.log('react-native-update: n/a - not inside a React Native project directory')
}
process.exit();
}
}
const commands = { const commands = {
...require('./user').commands, ...require('./user').commands,
...require('./bundle').commands, ...require('./bundle').commands,
@@ -47,7 +29,10 @@ const commands = {
}; };
function run() { function run() {
printVersionCommand(); if (process.argv.indexOf('-v') >= 0 || process.argv[2] === 'version') {
printVersionCommand();
process.exit();
}
const argv = require('cli-arguments').parse(require('../cli.json')); const argv = require('cli-arguments').parse(require('../cli.json'));
global.NO_INTERACTIVE = argv.options['no-interactive']; global.NO_INTERACTIVE = argv.options['no-interactive'];

View File

@@ -5,6 +5,7 @@
import * as fs from 'fs-extra'; import * as fs from 'fs-extra';
import os from 'os'; import os from 'os';
import path from 'path'; import path from 'path';
const pkg = require('../../package.json');
const AppInfoParser = require('app-info-parser'); const AppInfoParser = require('app-info-parser');
var read = require('read'); var read = require('read');
@@ -112,3 +113,18 @@ export function saveToLocal(originPath, destName) {
// fs.ensureDirSync(path.dirname(destPath)); // fs.ensureDirSync(path.dirname(destPath));
// fs.copyFileSync(originPath, destPath); // fs.copyFileSync(originPath, destPath);
} }
export function printVersionCommand() {
console.log('react-native-update-cli: ' + pkg.version);
try {
const PACKAGE_JSON_PATH = path.resolve(
process.cwd(),
'node_modules',
'react-native-update',
'package.json'
);
console.log('react-native-update: ' + require(PACKAGE_JSON_PATH).version);
} catch (e) {
console.log('react-native-update: 无法获取版本号,请在项目目录中运行命令')
}
}