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

Compare commits

..

6 Commits

Author SHA1 Message Date
sunnylqm
c08c5c0b07 fix taro cli path 2025-02-13 16:13:48 +08:00
sunnylqm
dc8c134ff0 v1.40.0-beta.0 2025-02-13 16:07:25 +08:00
sunnylqm
1d1e6cde0f support taro 2025-02-13 16:02:04 +08:00
sunny.luo
f16aff5674 Improve file filtering during bundle packing
# Conflicts:
#	package.json
#	src/bundle.js
2025-02-10 17:22:00 +08:00
Sunny Luo
d7da311c5e Update package.json 2025-02-09 16:15:04 +08:00
波仔糕
abef760f43 add logic to remove soucemap and merge sourcemap params (#9)
* add logic to remove soucemap and merge sourcemap params

* Update bundle.js

---------

Co-authored-by: Sunny Luo <sunnylqm@gmail.com>
2025-02-09 16:14:45 +08:00
5 changed files with 230 additions and 131 deletions

View File

@@ -145,6 +145,15 @@
}, },
"sourcemap": { "sourcemap": {
"default": false "default": false
},
"taro": {
"default": false
},
"expo": {
"default": false
},
"rncli": {
"default": false
} }
} }
}, },

View File

@@ -1,6 +1,6 @@
{ {
"name": "react-native-update-cli", "name": "react-native-update-cli",
"version": "1.39.0", "version": "1.40.0-beta.1",
"description": "Command tools for javaScript updater with `pushy` service for react native apps.", "description": "Command tools for javaScript updater with `pushy` service for react native apps.",
"main": "index.js", "main": "index.js",
"bin": { "bin": {

View File

@@ -22,16 +22,33 @@ try {
hdiff = require('node-hdiffpatch').diff; hdiff = require('node-hdiffpatch').diff;
} catch (e) {} } catch (e) {}
async function runReactNativeBundleCommand( async function runReactNativeBundleCommand({
bundleName, bundleName,
development, dev,
entryFile, entryFile,
outputFolder, outputFolder,
platform, platform,
sourcemapOutput, sourcemapOutput,
config, config,
) { cli,
let gradleConfig = {}; }: {
bundleName: string;
dev: string;
entryFile: string;
outputFolder: string;
platform: string;
sourcemapOutput: string;
config?: string;
cli: {
taro?: boolean;
expo?: boolean;
rncli?: boolean;
};
}) {
let gradleConfig: {
crunchPngs?: boolean;
enableHermes?: boolean;
} = {};
if (platform === 'android') { if (platform === 'android') {
gradleConfig = await checkGradleConfig(); gradleConfig = await checkGradleConfig();
if (gradleConfig.crunchPngs !== false) { if (gradleConfig.crunchPngs !== false) {
@@ -41,7 +58,7 @@ async function runReactNativeBundleCommand(
} }
} }
const reactNativeBundleArgs = []; const reactNativeBundleArgs: string[] = [];
const envArgs = process.env.PUSHY_ENV_ARGS; const envArgs = process.env.PUSHY_ENV_ARGS;
@@ -54,26 +71,31 @@ async function runReactNativeBundleCommand(
fs.emptyDirSync(outputFolder); fs.emptyDirSync(outputFolder);
let cliPath; let cliPath: string | undefined;
let usingExpo = false; let usingExpo = false;
try {
cliPath = require.resolve('@expo/cli', { const getExpoCli = () => {
paths: [process.cwd()], try {
}); cliPath = require.resolve('@expo/cli', {
const expoCliVersion = JSON.parse( paths: [process.cwd()],
fs.readFileSync( });
require.resolve('@expo/cli/package.json', { const expoCliVersion = JSON.parse(
paths: [process.cwd()], fs.readFileSync(
}), require.resolve('@expo/cli/package.json', {
), paths: [process.cwd()],
).version; }),
// expo cli 0.10.17 (expo 49) 开始支持 bundle:embed ),
if (semverSatisfies(expoCliVersion, '>= 0.10.17')) { ).version;
usingExpo = true; // expo cli 0.10.17 (expo 49) 开始支持 bundle:embed
} if (semverSatisfies(expoCliVersion, '>= 0.10.17')) {
} catch (e) {} usingExpo = true;
if (!usingExpo) { } else {
cliPath = undefined;
}
} catch (e) {}
};
const getRnCli = () => {
try { try {
// rn >= 0.75 // rn >= 0.75
cliPath = require.resolve('@react-native-community/cli/build/bin.js', { cliPath = require.resolve('@react-native-community/cli/build/bin.js', {
@@ -85,21 +107,49 @@ async function runReactNativeBundleCommand(
paths: [process.cwd()], paths: [process.cwd()],
}); });
} }
};
const getTaroCli = () => {
try {
cliPath = require.resolve('@tarojs/cli/bin/taro', {
paths: [process.cwd()],
});
} catch (e) {}
};
if (cli.expo) {
getExpoCli();
} else if (cli.taro) {
getTaroCli();
} else if (cli.rncli) {
getRnCli();
} }
if (!cliPath) {
getExpoCli();
if (!usingExpo) {
getRnCli();
}
}
const bundleParams = await checkPlugins(); const bundleParams = await checkPlugins();
const minifyOption = bundleParams.minify;
const isSentry = bundleParams.sentry; const isSentry = bundleParams.sentry;
const bundleCommand = usingExpo
? 'export:embed' let bundleCommand = 'bundle';
: platform === 'harmony' if (usingExpo) {
? 'bundle-harmony' bundleCommand = 'export:embed';
: 'bundle'; } else if (platform === 'harmony') {
bundleCommand = 'bundle-harmony';
} else if (cli.taro) {
bundleCommand = 'build';
}
if (platform === 'harmony') { if (platform === 'harmony') {
Array.prototype.push.apply(reactNativeBundleArgs, [ Array.prototype.push.apply(reactNativeBundleArgs, [
cliPath, cliPath,
bundleCommand, bundleCommand,
'--dev', '--dev',
development, dev,
'--entry-file', '--entry-file',
entryFile, entryFile,
]); ]);
@@ -119,17 +169,25 @@ async function runReactNativeBundleCommand(
outputFolder, outputFolder,
'--bundle-output', '--bundle-output',
path.join(outputFolder, bundleName), path.join(outputFolder, bundleName),
'--dev',
development,
'--entry-file',
entryFile,
'--platform', '--platform',
platform, platform,
'--reset-cache', '--reset-cache',
'--minify',
minifyOption,
]); ]);
if (cli.taro) {
reactNativeBundleArgs.push(...[
'--type',
'rn',
])
} else {
reactNativeBundleArgs.push(...[
'--dev',
dev,
'--entry-file',
entryFile,
])
}
if (sourcemapOutput) { if (sourcemapOutput) {
reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput); reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput);
} }
@@ -161,17 +219,19 @@ async function runReactNativeBundleCommand(
), ),
); );
} else { } else {
let hermesEnabled = false; let hermesEnabled: boolean | undefined = false;
if (platform === 'android') { if (platform === 'android') {
const gradlePropeties = await new Promise((resolve) => { const gradlePropeties = await new Promise<{
hermesEnabled?: boolean;
}>((resolve) => {
properties.parse( properties.parse(
'./android/gradle.properties', './android/gradle.properties',
{ path: true }, { path: true },
(error, props) => { (error: any, props: { hermesEnabled?: boolean }) => {
if (error) { if (error) {
console.error(error); console.error(error);
resolve(null); resolve({});
} }
resolve(props); resolve(props);
@@ -204,7 +264,7 @@ async function runReactNativeBundleCommand(
}); });
} }
async function copyHarmonyBundle(outputFolder) { async function copyHarmonyBundle(outputFolder: string) {
const harmonyRawPath = 'harmony/entry/src/main/resources/rawfile'; const harmonyRawPath = 'harmony/entry/src/main/resources/rawfile';
try { try {
await fs.ensureDir(harmonyRawPath); await fs.ensureDir(harmonyRawPath);
@@ -218,7 +278,7 @@ async function copyHarmonyBundle(outputFolder) {
await fs.ensureDir(outputFolder); await fs.ensureDir(outputFolder);
await fs.copy(harmonyRawPath, outputFolder); await fs.copy(harmonyRawPath, outputFolder);
} catch (error) { } catch (error: any) {
console.error('copyHarmonyBundle 错误:', error); console.error('copyHarmonyBundle 错误:', error);
throw new Error(`复制文件失败: ${error.message}`); throw new Error(`复制文件失败: ${error.message}`);
} }
@@ -256,10 +316,10 @@ async function checkGradleConfig() {
} }
async function compileHermesByteCode( async function compileHermesByteCode(
bundleName, bundleName: string,
outputFolder, outputFolder: string,
sourcemapOutput, sourcemapOutput: string,
shouldCleanSourcemap, shouldCleanSourcemap: boolean,
) { ) {
console.log('Hermes enabled, now compiling to hermes bytecode:\n'); console.log('Hermes enabled, now compiling to hermes bytecode:\n');
// >= rn 0.69 // >= rn 0.69
@@ -321,7 +381,11 @@ async function compileHermesByteCode(
} }
} }
async function copyDebugidForSentry(bundleName, outputFolder, sourcemapOutput) { async function copyDebugidForSentry(
bundleName: string,
outputFolder: string,
sourcemapOutput: string,
) {
if (sourcemapOutput) { if (sourcemapOutput) {
let copyDebugidPath; let copyDebugidPath;
try { try {
@@ -358,10 +422,10 @@ async function copyDebugidForSentry(bundleName, outputFolder, sourcemapOutput) {
} }
async function uploadSourcemapForSentry( async function uploadSourcemapForSentry(
bundleName, bundleName: string,
outputFolder, outputFolder: string,
sourcemapOutput, sourcemapOutput: string,
version, version: string,
) { ) {
if (sourcemapOutput) { if (sourcemapOutput) {
let sentryCliPath; let sentryCliPath;
@@ -408,19 +472,24 @@ async function uploadSourcemapForSentry(
} }
} }
async function pack(dir, output) { const ignorePackingFileNames = ['.', '..', 'index.bundlejs.map'];
const ignorePackingExtensions = ['DS_Store'];
async function pack(dir: string, output: string) {
console.log('Packing'); console.log('Packing');
fs.ensureDirSync(path.dirname(output)); fs.ensureDirSync(path.dirname(output));
await new Promise((resolve, reject) => { await new Promise<void>((resolve, reject) => {
const zipfile = new ZipFile(); const zipfile = new ZipFile();
function addDirectory(root, rel) { function addDirectory(root: string, rel: string) {
if (rel) { if (rel) {
zipfile.addEmptyDirectory(rel); zipfile.addEmptyDirectory(rel);
} }
const childs = fs.readdirSync(root); const childs = fs.readdirSync(root);
for (const name of childs) { for (const name of childs) {
if (name === '.' || name === '..' || name === 'index.bundlejs.map') { if (
ignorePackingFileNames.includes(name) ||
ignorePackingExtensions.some((ext) => name.endsWith(`.${ext}`))
) {
continue; continue;
} }
const fullPath = path.join(root, name); const fullPath = path.join(root, name);
@@ -437,7 +506,7 @@ async function pack(dir, output) {
addDirectory(dir, ''); addDirectory(dir, '');
zipfile.outputStream.on('error', (err) => reject(err)); zipfile.outputStream.on('error', (err: any) => reject(err));
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', () => { zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', () => {
resolve(); resolve();
}); });
@@ -446,12 +515,12 @@ async function pack(dir, output) {
console.log(`ppk热更包已生成并保存到: ${output}`); console.log(`ppk热更包已生成并保存到: ${output}`);
} }
export function readEntire(entry, zipFile) { export function readEntire(entry: string, zipFile: ZipFile) {
const buffers = []; const buffers: Buffer[] = [];
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
zipFile.openReadStream(entry, (err, stream) => { zipFile.openReadStream(entry, (err: any, stream: any) => {
stream.pipe({ stream.pipe({
write(chunk) { write(chunk: Buffer) {
buffers.push(chunk); buffers.push(chunk);
}, },
end() { end() {
@@ -466,12 +535,12 @@ export function readEntire(entry, zipFile) {
}); });
} }
function basename(fn) { function basename(fn: string) {
const m = /^(.+\/)[^\/]+\/?$/.exec(fn); const m = /^(.+\/)[^\/]+\/?$/.exec(fn);
return m?.[1]; return m?.[1];
} }
async function diffFromPPK(origin, next, output) { async function diffFromPPK(origin: string, next: string, output: string) {
fs.ensureDirSync(path.dirname(output)); fs.ensureDirSync(path.dirname(output));
const originEntries = {}; const originEntries = {};
@@ -516,7 +585,7 @@ async function diffFromPPK(origin, next, output) {
const addedEntry = {}; const addedEntry = {};
function addEntry(fn) { function addEntry(fn: string) {
//console.log(fn); //console.log(fn);
if (!fn || addedEntry[fn]) { if (!fn || addedEntry[fn]) {
return; return;
@@ -613,11 +682,11 @@ async function diffFromPPK(origin, next, output) {
} }
async function diffFromPackage( async function diffFromPackage(
origin, origin: string,
next, next: string,
output, output: string,
originBundleName, originBundleName: string,
transformPackagePath = (v) => v, transformPackagePath = (v: string) => v,
) { ) {
fs.ensureDirSync(path.dirname(output)); fs.ensureDirSync(path.dirname(output));
@@ -626,7 +695,7 @@ async function diffFromPackage(
let originSource; let originSource;
await enumZipEntries(origin, (entry, zipFile) => { await enumZipEntries(origin, (entry: any, zipFile: any) => {
if (!/\/$/.test(entry.fileName)) { if (!/\/$/.test(entry.fileName)) {
const fn = transformPackagePath(entry.fileName); const fn = transformPackagePath(entry.fileName);
if (!fn) { if (!fn) {
@@ -720,55 +789,66 @@ async function diffFromPackage(
await writePromise; await writePromise;
} }
export async function enumZipEntries(zipFn, callback, nestedPath = '') { export async function enumZipEntries(
zipFn: string,
callback: (entry: any, zipFile: any) => void,
nestedPath = '',
) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
openZipFile(zipFn, { lazyEntries: true }, async (err, zipfile) => { openZipFile(
if (err) { zipFn,
return reject(err); { lazyEntries: true },
} async (err: any, zipfile: ZipFile) => {
if (err) {
zipfile.on('end', resolve); return reject(err);
zipfile.on('error', reject);
zipfile.on('entry', async (entry) => {
const fullPath = nestedPath + entry.fileName;
try {
if (
!entry.fileName.endsWith('/') &&
entry.fileName.toLowerCase().endsWith('.hap')
) {
const tempDir = path.join(os.tmpdir(), `nested_zip_${Date.now()}`);
await fs.ensureDir(tempDir);
const tempZipPath = path.join(tempDir, 'temp.zip');
await new Promise((res, rej) => {
zipfile.openReadStream(entry, async (err, readStream) => {
if (err) return rej(err);
const writeStream = fs.createWriteStream(tempZipPath);
readStream.pipe(writeStream);
writeStream.on('finish', res);
writeStream.on('error', rej);
});
});
await enumZipEntries(tempZipPath, callback, `${fullPath}/`);
await fs.remove(tempDir);
}
const result = callback(entry, zipfile, fullPath);
if (result && typeof result.then === 'function') {
await result;
}
} catch (error) {
console.error('处理文件时出错:', error);
} }
zipfile.readEntry(); zipfile.on('end', resolve);
}); zipfile.on('error', reject);
zipfile.on('entry', async (entry) => {
const fullPath = nestedPath + entry.fileName;
zipfile.readEntry(); try {
}); if (
!entry.fileName.endsWith('/') &&
entry.fileName.toLowerCase().endsWith('.hap')
) {
const tempDir = path.join(
os.tmpdir(),
`nested_zip_${Date.now()}`,
);
await fs.ensureDir(tempDir);
const tempZipPath = path.join(tempDir, 'temp.zip');
await new Promise((res, rej) => {
zipfile.openReadStream(entry, async (err, readStream) => {
if (err) return rej(err);
const writeStream = fs.createWriteStream(tempZipPath);
readStream.pipe(writeStream);
writeStream.on('finish', res);
writeStream.on('error', rej);
});
});
await enumZipEntries(tempZipPath, callback, `${fullPath}/`);
await fs.remove(tempDir);
}
const result = callback(entry, zipfile, fullPath);
if (result && typeof result.then === 'function') {
await result;
}
} catch (error) {
console.error('处理文件时出错:', error);
}
zipfile.readEntry();
});
zipfile.readEntry();
},
);
}); });
} }
@@ -814,14 +894,23 @@ export const commands = {
options.platform || (await question('平台(ios/android/harmony):')), options.platform || (await question('平台(ios/android/harmony):')),
); );
const { bundleName, entryFile, intermediaDir, output, dev } = const {
translateOptions({ bundleName,
...options, entryFile,
platform, intermediaDir,
}); output,
dev,
sourcemap,
taro,
expo,
rncli,
} = translateOptions({
...options,
platform,
});
const bundleParams = await checkPlugins(); const bundleParams = await checkPlugins();
const sourcemap = bundleParams.sourcemap; const sourcemapPlugin = bundleParams.sourcemap;
const isSentry = bundleParams.sentry; const isSentry = bundleParams.sentry;
const sourcemapOutput = path.join(intermediaDir, `${bundleName}.map`); const sourcemapOutput = path.join(intermediaDir, `${bundleName}.map`);
@@ -836,20 +925,25 @@ export const commands = {
console.log(`Bundling with react-native: ${version}`); console.log(`Bundling with react-native: ${version}`);
await runReactNativeBundleCommand( await runReactNativeBundleCommand({
bundleName, bundleName,
dev, dev,
entryFile, entryFile,
intermediaDir, outputFolder: intermediaDir,
platform, platform,
sourcemap ? sourcemapOutput : '', sourcemapOutput: sourcemap || sourcemapPlugin ? sourcemapOutput : '',
); cli: {
taro,
expo,
rncli,
},
});
await pack(path.resolve(intermediaDir), realOutput); await pack(path.resolve(intermediaDir), realOutput);
const v = await question('是否现在上传此热更包?(Y/N)'); const v = await question('是否现在上传此热更包?(Y/N)');
if (v.toLowerCase() === 'y') { if (v.toLowerCase() === 'y') {
const versionName = await this.publish({ const versionName = await this.publish({
args: [realOutput], args: [realOutput],
options: { options: {
platform, platform,

View File

@@ -2,7 +2,6 @@ import { plugins } from './plugin-config';
interface BundleParams { interface BundleParams {
sentry: boolean; sentry: boolean;
minify: boolean;
sourcemap: boolean; sourcemap: boolean;
[key: string]: any; [key: string]: any;
} }
@@ -10,7 +9,6 @@ interface BundleParams {
export async function checkPlugins(): Promise<BundleParams> { export async function checkPlugins(): Promise<BundleParams> {
const params: BundleParams = { const params: BundleParams = {
sentry: false, sentry: false,
minify: true,
sourcemap: false, sourcemap: false,
}; };

View File

@@ -3,7 +3,6 @@ import fs from 'fs-extra';
interface PluginConfig { interface PluginConfig {
name: string; name: string;
bundleParams?: { bundleParams?: {
minify?: boolean;
[key: string]: any; [key: string]: any;
}; };
detect: () => Promise<boolean>; detect: () => Promise<boolean>;
@@ -14,7 +13,6 @@ export const plugins: PluginConfig[] = [
name: 'sentry', name: 'sentry',
bundleParams: { bundleParams: {
sentry: true, sentry: true,
minify: false,
sourcemap: true, sourcemap: true,
}, },
detect: async () => { detect: async () => {