Skip to content

Commit a1231f6

Browse files
committed
add deps
1 parent f32b201 commit a1231f6

File tree

5 files changed

+35
-80
lines changed

5 files changed

+35
-80
lines changed

src/bundle.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path';
2-
import { getRNVersion, translateOptions } from './utils';
2+
import { translateOptions } from './utils';
33
import * as fs from 'fs-extra';
44
import { ZipFile } from 'yazl';
55
import { open as openZipFile } from 'yauzl';
@@ -10,6 +10,7 @@ import semverSatisfies from 'semver/functions/satisfies';
1010
const g2js = require('gradle-to-js/lib/parser');
1111
import os from 'node:os';
1212
const properties = require('properties');
13+
import { depVersions } from './utils/dep-versions';
1314

1415
let bsdiff;
1516
let hdiff;
@@ -82,11 +83,13 @@ async function runReactNativeBundleCommand({
8283
paths: [process.cwd()],
8384
});
8485
const expoCliVersion = JSON.parse(
85-
fs.readFileSync(
86-
require.resolve('@expo/cli/package.json', {
87-
paths: [process.cwd()],
88-
}),
89-
).toString(),
86+
fs
87+
.readFileSync(
88+
require.resolve('@expo/cli/package.json', {
89+
paths: [process.cwd()],
90+
}),
91+
)
92+
.toString(),
9093
).version;
9194
// expo cli 0.10.17 (expo 49) 开始支持 bundle:embed
9295
if (semverSatisfies(expoCliVersion, '>= 0.10.17')) {
@@ -175,19 +178,11 @@ async function runReactNativeBundleCommand({
175178
platform,
176179
'--reset-cache',
177180
]);
178-
181+
179182
if (cli.taro) {
180-
reactNativeBundleArgs.push(...[
181-
'--type',
182-
'rn',
183-
])
183+
reactNativeBundleArgs.push(...['--type', 'rn']);
184184
} else {
185-
reactNativeBundleArgs.push(...[
186-
'--dev',
187-
dev,
188-
'--entry-file',
189-
entryFile,
190-
])
185+
reactNativeBundleArgs.push(...['--dev', dev, '--entry-file', entryFile]);
191186
}
192187

193188
if (sourcemapOutput) {
@@ -927,9 +922,7 @@ export const commands = {
927922
throw new Error('Platform must be specified.');
928923
}
929924

930-
const { version, major, minor } = getRNVersion();
931-
932-
console.log(`Bundling with react-native: ${version}`);
925+
console.log(`Bundling with react-native: ${depVersions['react-native']}`);
933926

934927
await runReactNativeBundleCommand({
935928
bundleName,

src/package.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { checkPlatform, getSelectedApp } from './app';
55

66
import { getApkInfo, getIpaInfo, getAppInfo } from './utils';
77
import Table from 'tty-table';
8+
import { depVersions } from 'utils/dep-versions';
89

910
export async function listPackage(appId: string) {
1011
const { data } = await get(`/app/${appId}/package/list?limit=1000`);
@@ -79,6 +80,7 @@ export const commands = {
7980
name: versionName,
8081
hash,
8182
buildTime,
83+
deps: depVersions,
8284
});
8385
saveToLocal(fn, `${appId}/package/${id}.ipa`);
8486
console.log(
@@ -116,6 +118,7 @@ export const commands = {
116118
name: versionName,
117119
hash,
118120
buildTime,
121+
deps: depVersions,
119122
});
120123
saveToLocal(fn, `${appId}/package/${id}.apk`);
121124
console.log(
@@ -153,6 +156,7 @@ export const commands = {
153156
name: versionName,
154157
hash,
155158
buildTime,
159+
deps: depVersions,
156160
});
157161
saveToLocal(fn, `${appId}/package/${id}.app`);
158162
console.log(

src/utils/dep-versions.ts

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
1-
import fs from 'node:fs';
2-
import path from 'node:path';
31
import currentPackage from '../../package.json';
42

5-
const packages = fs.readdirSync(path.join(__dirname, 'node_modules'));
6-
const exclude = ['.bin', '.cache'];
7-
83
const depKeys = Object.keys(currentPackage.dependencies);
94
const devDepKeys = Object.keys(currentPackage.devDependencies);
105
const dedupedDeps = [...new Set([...depKeys, ...devDepKeys])];
116

12-
const versions = {};
7+
export const depVersions: Record<string, string> = {};
138

14-
for (const package of dedupedDeps) {
9+
for (const dep of dedupedDeps) {
1510
try {
16-
const packageDir = path.resolve(__dirname, 'node_modules', current);
17-
const { name, version } = require(`${packageDir}/package.json`);
18-
if (depKeys.includes(name)) {
19-
return Object.assign(acc, {
20-
dependencies: Object.assign(acc.dependencies, { [name]: version }),
21-
});
22-
} else {
23-
return Object.assign(acc, {
24-
devDependencies: Object.assign(acc.devDependencies, {
25-
[name]: version,
26-
}),
27-
});
28-
}
29-
} catch (e) {
30-
// noop
31-
console.log(e);
32-
return acc;
33-
}
11+
const packageJsonPath = require.resolve(`${dep}/package.json`, {
12+
paths: [process.cwd()],
13+
});
14+
const version = require(packageJsonPath).version;
15+
depVersions[dep] = version;
16+
} catch (e) {}
3417
}

src/utils/index.ts

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { checkPlugins } from './check-plugin';
1010

1111
import { read } from 'read';
1212
import { tempDir } from './constants';
13+
import { depVersions } from './dep-versions';
1314

1415
export async function question(query: string, password?: boolean) {
1516
if (NO_INTERACTIVE) {
@@ -38,26 +39,6 @@ export function translateOptions(options: Record<string, string>) {
3839
return ret;
3940
}
4041

41-
export function getRNVersion() {
42-
const version = JSON.parse(
43-
fs
44-
.readFileSync(
45-
require.resolve('react-native/package.json', {
46-
paths: [process.cwd()],
47-
}),
48-
)
49-
.toString(),
50-
).version;
51-
52-
const [, major, minor] = /^(\d+)\.(\d+)\./.exec(version) || [];
53-
54-
return {
55-
version,
56-
major: Number(major),
57-
minor: Number(minor),
58-
};
59-
}
60-
6142
export async function getApkInfo(fn: string) {
6243
const appInfoParser = new AppInfoParser(fn);
6344
const bundleFile = await appInfoParser.parser.getEntry(
@@ -198,21 +179,11 @@ export async function printVersionCommand() {
198179
`react-native-update-cli: ${pkg.version}${latestPushyCliVersion}`,
199180
);
200181
let pushyVersion = '';
201-
try {
202-
const PACKAGE_JSON_PATH = require.resolve(
203-
'react-native-update/package.json',
204-
{
205-
paths: [process.cwd()],
206-
},
207-
);
208-
pushyVersion = require(PACKAGE_JSON_PATH).version;
209-
latestPushyVersion = latestPushyVersion
210-
? ` (最新:${chalk.green(latestPushyVersion)})`
211-
: '';
212-
console.log(`react-native-update: ${pushyVersion}${latestPushyVersion}`);
213-
} catch (e) {
214-
console.log('react-native-update: 无法获取版本号,请在项目目录中运行命令');
215-
}
182+
pushyVersion = depVersions['react-native-update'];
183+
latestPushyVersion = latestPushyVersion
184+
? ` (最新:${chalk.green(latestPushyVersion)})`
185+
: '';
186+
console.log(`react-native-update: ${pushyVersion}${latestPushyVersion}`);
216187
if (pushyVersion) {
217188
if (semverSatisfies(pushyVersion, '<8.5.2')) {
218189
console.warn(
@@ -229,6 +200,8 @@ export async function printVersionCommand() {
229200
'当前版本已不再支持,请升级到 v10 的最新小版本(代码无需改动,可直接热更): npm i react-native-update@10',
230201
);
231202
}
203+
} else {
204+
console.log('react-native-update: 无法获取版本号,请在项目目录中运行命令');
232205
}
233206
}
234207

src/versions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { question, saveToLocal } from './utils';
44
import { checkPlatform, getSelectedApp } from './app';
55
import { choosePackage } from './package';
66
import { compare } from 'compare-versions';
7+
import { depVersions } from 'utils/dep-versions';
78

89
async function showVersion(appId: string, offset: number) {
910
const { data, count } = await get(`/app/${appId}/version/list`);
@@ -103,6 +104,7 @@ export const commands = {
103104
hash,
104105
description: description || (await question('输入版本描述:')),
105106
metaInfo: metaInfo || (await question('输入自定义的 meta info:')),
107+
deps: depVersions,
106108
});
107109
// TODO local diff
108110
saveToLocal(fn, `${appId}/ppk/${id}.ppk`);

0 commit comments

Comments
 (0)