mirror of
https://gitcode.com/github-mirrors/react-native-update-cli.git
synced 2025-11-24 01:03:37 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b76440d018 | ||
|
|
e3c951bc1b | ||
|
|
f6ed8872bd | ||
|
|
fd46bafb98 |
1
.swcrc
1
.swcrc
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"jsc": {
|
"jsc": {
|
||||||
|
"baseUrl": "./src",
|
||||||
"loose": true,
|
"loose": true,
|
||||||
"target": "es2018",
|
"target": "es2018",
|
||||||
"parser": {
|
"parser": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update-cli",
|
"name": "react-native-update-cli",
|
||||||
"version": "2.1.2",
|
"version": "2.2.1",
|
||||||
"description": "command line tool for react-native-update (remote updates for react native)",
|
"description": "command line tool for react-native-update (remote updates for react native)",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
|||||||
13
src/api.ts
13
src/api.ts
@@ -10,18 +10,16 @@ import packageJson from '../package.json';
|
|||||||
import type { Package, Session } from './types';
|
import type { Package, Session } from './types';
|
||||||
import {
|
import {
|
||||||
credentialFile,
|
credentialFile,
|
||||||
defaultEndpoint,
|
|
||||||
pricingPageUrl,
|
pricingPageUrl,
|
||||||
} from './utils/constants';
|
} from './utils/constants';
|
||||||
import { t } from './utils/i18n';
|
import { t } from './utils/i18n';
|
||||||
|
import { getBaseUrl } from 'utils/http-helper';
|
||||||
|
|
||||||
const tcpPing = util.promisify(tcpp.ping);
|
const tcpPing = util.promisify(tcpp.ping);
|
||||||
|
|
||||||
let session: Session | undefined;
|
let session: Session | undefined;
|
||||||
let savedSession: Session | undefined;
|
let savedSession: Session | undefined;
|
||||||
|
|
||||||
const host =
|
|
||||||
process.env.PUSHY_REGISTRY || process.env.RNU_API || defaultEndpoint;
|
|
||||||
|
|
||||||
const userAgent = `react-native-update-cli/${packageJson.version}`;
|
const userAgent = `react-native-update-cli/${packageJson.version}`;
|
||||||
|
|
||||||
@@ -64,7 +62,9 @@ export const closeSession = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function query(url: string, options: fetch.RequestInit) {
|
async function query(url: string, options: fetch.RequestInit) {
|
||||||
const resp = await fetch(url, options);
|
const baseUrl = await getBaseUrl;
|
||||||
|
const fullUrl = `${baseUrl}${url}`;
|
||||||
|
const resp = await fetch(fullUrl, options);
|
||||||
const text = await resp.text();
|
const text = await resp.text();
|
||||||
let json: any;
|
let json: any;
|
||||||
try {
|
try {
|
||||||
@@ -83,7 +83,7 @@ async function query(url: string, options: fetch.RequestInit) {
|
|||||||
|
|
||||||
function queryWithoutBody(method: string) {
|
function queryWithoutBody(method: string) {
|
||||||
return (api: string) =>
|
return (api: string) =>
|
||||||
query(host + api, {
|
query(api, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': userAgent,
|
'User-Agent': userAgent,
|
||||||
@@ -94,7 +94,7 @@ function queryWithoutBody(method: string) {
|
|||||||
|
|
||||||
function queryWithBody(method: string) {
|
function queryWithBody(method: string) {
|
||||||
return (api: string, body?: Record<string, any>) =>
|
return (api: string, body?: Record<string, any>) =>
|
||||||
query(host + api, {
|
query(api, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': userAgent,
|
'User-Agent': userAgent,
|
||||||
@@ -116,6 +116,7 @@ export async function uploadFile(fn: string, key?: string) {
|
|||||||
});
|
});
|
||||||
let realUrl = url;
|
let realUrl = url;
|
||||||
if (backupUrl) {
|
if (backupUrl) {
|
||||||
|
// @ts-ignore
|
||||||
if (global.USE_ACC_OSS) {
|
if (global.USE_ACC_OSS) {
|
||||||
realUrl = backupUrl;
|
realUrl = backupUrl;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -75,15 +75,12 @@ async function runReactNativeBundleCommand({
|
|||||||
const envArgs = process.env.PUSHY_ENV_ARGS;
|
const envArgs = process.env.PUSHY_ENV_ARGS;
|
||||||
|
|
||||||
if (envArgs) {
|
if (envArgs) {
|
||||||
Array.prototype.push.apply(
|
reactNativeBundleArgs.push(...envArgs.trim().split(/\s+/));
|
||||||
reactNativeBundleArgs,
|
|
||||||
envArgs.trim().split(/\s+/),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.emptyDirSync(outputFolder);
|
fs.emptyDirSync(outputFolder);
|
||||||
|
|
||||||
let cliPath: string | undefined;
|
let cliPath = '';
|
||||||
let usingExpo = false;
|
let usingExpo = false;
|
||||||
|
|
||||||
const getExpoCli = () => {
|
const getExpoCli = () => {
|
||||||
@@ -104,7 +101,7 @@ async function runReactNativeBundleCommand({
|
|||||||
if (satisfies(expoCliVersion, '>= 0.10.17')) {
|
if (satisfies(expoCliVersion, '>= 0.10.17')) {
|
||||||
usingExpo = true;
|
usingExpo = true;
|
||||||
} else {
|
} else {
|
||||||
cliPath = undefined;
|
cliPath = '';
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
};
|
};
|
||||||
@@ -166,49 +163,34 @@ async function runReactNativeBundleCommand({
|
|||||||
bundleCommand = 'build';
|
bundleCommand = 'build';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (platform === 'harmony') {
|
reactNativeBundleArgs.push(
|
||||||
Array.prototype.push.apply(reactNativeBundleArgs, [
|
cliPath,
|
||||||
cliPath,
|
bundleCommand,
|
||||||
bundleCommand,
|
'--assets-dest',
|
||||||
'--dev',
|
outputFolder,
|
||||||
dev,
|
'--bundle-output',
|
||||||
'--entry-file',
|
path.join(
|
||||||
entryFile,
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (sourcemapOutput) {
|
|
||||||
reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config) {
|
|
||||||
reactNativeBundleArgs.push('--config', config);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Array.prototype.push.apply(reactNativeBundleArgs, [
|
|
||||||
cliPath,
|
|
||||||
bundleCommand,
|
|
||||||
'--assets-dest',
|
|
||||||
outputFolder,
|
outputFolder,
|
||||||
'--bundle-output',
|
platform === 'harmony' ? 'bundle.harmony.js' : bundleName,
|
||||||
path.join(outputFolder, bundleName),
|
),
|
||||||
'--platform',
|
);
|
||||||
platform,
|
|
||||||
'--reset-cache',
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (cli.taro) {
|
if (platform !== 'harmony') {
|
||||||
reactNativeBundleArgs.push(...['--type', 'rn']);
|
reactNativeBundleArgs.push('--platform', platform, '--reset-cache');
|
||||||
} else {
|
}
|
||||||
reactNativeBundleArgs.push(...['--dev', dev, '--entry-file', entryFile]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sourcemapOutput) {
|
if (cli.taro) {
|
||||||
reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput);
|
reactNativeBundleArgs.push('--type', 'rn');
|
||||||
}
|
} else {
|
||||||
|
reactNativeBundleArgs.push('--dev', dev, '--entry-file', entryFile);
|
||||||
|
}
|
||||||
|
|
||||||
if (config) {
|
if (sourcemapOutput) {
|
||||||
reactNativeBundleArgs.push('--config', config);
|
reactNativeBundleArgs.push('--sourcemap-output', sourcemapOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config) {
|
||||||
|
reactNativeBundleArgs.push('--config', config);
|
||||||
}
|
}
|
||||||
|
|
||||||
const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs);
|
const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs);
|
||||||
@@ -597,6 +579,7 @@ async function diffFromPPK(origin: string, next: string, output: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const copies = {};
|
const copies = {};
|
||||||
|
const copiesv2 = {};
|
||||||
|
|
||||||
const zipfile = new YazlZipFile();
|
const zipfile = new YazlZipFile();
|
||||||
|
|
||||||
@@ -668,6 +651,7 @@ async function diffFromPPK(origin: string, next: string, output: string) {
|
|||||||
addEntry(base);
|
addEntry(base);
|
||||||
}
|
}
|
||||||
copies[entry.fileName] = originMap[entry.crc32];
|
copies[entry.fileName] = originMap[entry.crc32];
|
||||||
|
copiesv2[entry.crc32] = entry.fileName;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -700,7 +684,7 @@ async function diffFromPPK(origin: string, next: string, output: string) {
|
|||||||
|
|
||||||
//console.log({copies, deletes});
|
//console.log({copies, deletes});
|
||||||
zipfile.addBuffer(
|
zipfile.addBuffer(
|
||||||
Buffer.from(JSON.stringify({ copies, deletes })),
|
Buffer.from(JSON.stringify({ copies, copiesv2, deletes })),
|
||||||
'__diff.json',
|
'__diff.json',
|
||||||
);
|
);
|
||||||
zipfile.end();
|
zipfile.end();
|
||||||
@@ -747,6 +731,7 @@ async function diffFromPackage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const copies = {};
|
const copies = {};
|
||||||
|
const copiesv2 = {};
|
||||||
|
|
||||||
const zipfile = new YazlZipFile();
|
const zipfile = new YazlZipFile();
|
||||||
|
|
||||||
@@ -792,6 +777,7 @@ async function diffFromPackage(
|
|||||||
// If moved from other place
|
// If moved from other place
|
||||||
if (originMap[entry.crc32]) {
|
if (originMap[entry.crc32]) {
|
||||||
copies[entry.fileName] = originMap[entry.crc32];
|
copies[entry.fileName] = originMap[entry.crc32];
|
||||||
|
copiesv2[entry.crc32] = entry.fileName;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -810,7 +796,10 @@ async function diffFromPackage(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
zipfile.addBuffer(Buffer.from(JSON.stringify({ copies })), '__diff.json');
|
zipfile.addBuffer(
|
||||||
|
Buffer.from(JSON.stringify({ copies, copiesv2 })),
|
||||||
|
'__diff.json',
|
||||||
|
);
|
||||||
zipfile.end();
|
zipfile.end();
|
||||||
await writePromise;
|
await writePromise;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ export const pricingPageUrl = IS_CRESC
|
|||||||
? 'https://cresc.dev/pricing'
|
? 'https://cresc.dev/pricing'
|
||||||
: 'https://pushy.reactnative.cn/pricing.html';
|
: 'https://pushy.reactnative.cn/pricing.html';
|
||||||
|
|
||||||
export const defaultEndpoint = IS_CRESC
|
export const defaultEndpoints = IS_CRESC
|
||||||
? 'https://api.cresc.dev'
|
? ['https://api.cresc.dev', 'https://api.cresc.app']
|
||||||
: 'https://update.reactnative.cn/api';
|
: ['https://update.reactnative.cn/api', 'https://update.react-native.cn/api'];
|
||||||
|
|||||||
83
src/utils/http-helper.ts
Normal file
83
src/utils/http-helper.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import { defaultEndpoints } from './constants';
|
||||||
|
|
||||||
|
// const baseUrl = `http://localhost:9000`;
|
||||||
|
// let baseUrl = SERVER.main[0];
|
||||||
|
// const baseUrl = `https://p.reactnative.cn/api`;
|
||||||
|
|
||||||
|
export function promiseAny<T>(promises: Promise<T>[]) {
|
||||||
|
return new Promise<T>((resolve, reject) => {
|
||||||
|
let count = 0;
|
||||||
|
|
||||||
|
for (const promise of promises) {
|
||||||
|
Promise.resolve(promise)
|
||||||
|
.then(resolve)
|
||||||
|
.catch(() => {
|
||||||
|
count++;
|
||||||
|
if (count === promises.length) {
|
||||||
|
reject(new Error('All promises were rejected'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ping = async (url: string) => {
|
||||||
|
let pingFinished = false;
|
||||||
|
return Promise.race([
|
||||||
|
fetch(url, {
|
||||||
|
method: 'HEAD',
|
||||||
|
})
|
||||||
|
.then(({ status, statusText }) => {
|
||||||
|
pingFinished = true;
|
||||||
|
if (status === 200) {
|
||||||
|
// console.log('ping success', url);
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
// console.log('ping failed', url, status, statusText);
|
||||||
|
throw new Error('ping failed');
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
pingFinished = true;
|
||||||
|
// console.log('ping error', url, e);
|
||||||
|
throw new Error('ping error');
|
||||||
|
}),
|
||||||
|
new Promise((_, reject) =>
|
||||||
|
setTimeout(() => {
|
||||||
|
reject(new Error('ping timeout'));
|
||||||
|
if (!pingFinished) {
|
||||||
|
// console.log('ping timeout', url);
|
||||||
|
}
|
||||||
|
}, 2000),
|
||||||
|
),
|
||||||
|
]) as Promise<string | null>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const testUrls = async (urls?: string[]) => {
|
||||||
|
if (!urls?.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const ret = await promiseAny(urls.map(ping));
|
||||||
|
if (ret) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
// console.log('all ping failed, use first url:', urls[0]);
|
||||||
|
return urls[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getBaseUrl = (async () => {
|
||||||
|
const testEndpoint = process.env.PUSHY_REGISTRY || process.env.RNU_API;
|
||||||
|
if (testEndpoint) {
|
||||||
|
return testEndpoint;
|
||||||
|
}
|
||||||
|
return testUrls(defaultEndpoints.map((url) => `${url}/status`)).then(
|
||||||
|
(ret) => {
|
||||||
|
let baseUrl = defaultEndpoints[0];
|
||||||
|
if (ret) {
|
||||||
|
// remove /status
|
||||||
|
baseUrl = ret.replace('/status', '');
|
||||||
|
}
|
||||||
|
// console.log('baseUrl', baseUrl);
|
||||||
|
return baseUrl;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user