mirror of
https://gitcode.com/github-mirrors/react-native-update-cli.git
synced 2025-09-16 09:41:38 +08:00
fix error message
This commit is contained in:
43
src/api.ts
43
src/api.ts
@@ -7,7 +7,7 @@ import packageJson from '../package.json';
|
|||||||
import tcpp from 'tcp-ping';
|
import tcpp from 'tcp-ping';
|
||||||
import filesizeParser from 'filesize-parser';
|
import filesizeParser from 'filesize-parser';
|
||||||
import { pricingPageUrl } from './utils';
|
import { pricingPageUrl } from './utils';
|
||||||
import { Session } from 'types';
|
import type { Session } from 'types';
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
|
|
||||||
const tcpPing = util.promisify(tcpp.ping);
|
const tcpPing = util.promisify(tcpp.ping);
|
||||||
@@ -20,15 +20,13 @@ let host = process.env.PUSHY_REGISTRY || defaultEndpoint;
|
|||||||
|
|
||||||
const userAgent = `react-native-update-cli/${packageJson.version}`;
|
const userAgent = `react-native-update-cli/${packageJson.version}`;
|
||||||
|
|
||||||
export const getSession = function () {
|
export const getSession = () => session;
|
||||||
return session;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const replaceSession = function (newSession: { token: string }) {
|
export const replaceSession = (newSession: { token: string }) => {
|
||||||
session = newSession;
|
session = newSession;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const loadSession = async function () {
|
export const loadSession = async () => {
|
||||||
if (fs.existsSync('.update')) {
|
if (fs.existsSync('.update')) {
|
||||||
try {
|
try {
|
||||||
replaceSession(JSON.parse(fs.readFileSync('.update', 'utf8')));
|
replaceSession(JSON.parse(fs.readFileSync('.update', 'utf8')));
|
||||||
@@ -42,7 +40,7 @@ export const loadSession = async function () {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const saveSession = function () {
|
export const saveSession = () => {
|
||||||
// Only save on change.
|
// Only save on change.
|
||||||
if (session !== savedSession) {
|
if (session !== savedSession) {
|
||||||
const current = session;
|
const current = session;
|
||||||
@@ -52,7 +50,7 @@ export const saveSession = function () {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const closeSession = function () {
|
export const closeSession = () => {
|
||||||
if (fs.existsSync('.update')) {
|
if (fs.existsSync('.update')) {
|
||||||
fs.unlinkSync('.update');
|
fs.unlinkSync('.update');
|
||||||
savedSession = undefined;
|
savedSession = undefined;
|
||||||
@@ -64,38 +62,35 @@ export const closeSession = function () {
|
|||||||
async function query(url: string, options: fetch.RequestInit) {
|
async function query(url: string, options: fetch.RequestInit) {
|
||||||
const resp = await fetch(url, options);
|
const resp = await fetch(url, options);
|
||||||
const text = await resp.text();
|
const text = await resp.text();
|
||||||
let json;
|
let json: any;
|
||||||
try {
|
try {
|
||||||
json = JSON.parse(text);
|
json = JSON.parse(text);
|
||||||
} catch (e) {
|
} catch (e) {}
|
||||||
if (resp.statusText.includes('Unauthorized')) {
|
|
||||||
throw new Error('登录信息已过期,请使用 pushy login 命令重新登录');
|
|
||||||
} else {
|
|
||||||
throw new Error(`Server error: ${resp.statusText}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (resp.status !== 200) {
|
if (resp.status !== 200) {
|
||||||
throw new Error(`${resp.status}: ${resp.statusText}`);
|
const message = json?.message || resp.statusText;
|
||||||
|
if (resp.status === 401) {
|
||||||
|
throw new Error('登录信息已过期,请使用 pushy login 命令重新登录');
|
||||||
|
}
|
||||||
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryWithoutBody(method: string) {
|
function queryWithoutBody(method: string) {
|
||||||
return function (api: string) {
|
return (api: string) =>
|
||||||
return query(host + api, {
|
query(host + api, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': userAgent,
|
'User-Agent': userAgent,
|
||||||
'X-AccessToken': session ? session.token : '',
|
'X-AccessToken': session ? session.token : '',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function queryWithBody(method: string) {
|
function queryWithBody(method: string) {
|
||||||
return function (api: string, body: Record<string, any>) {
|
return (api: string, body: Record<string, any>) =>
|
||||||
return query(host + api, {
|
query(host + api, {
|
||||||
method,
|
method,
|
||||||
headers: {
|
headers: {
|
||||||
'User-Agent': userAgent,
|
'User-Agent': userAgent,
|
||||||
@@ -104,10 +99,8 @@ function queryWithBody(method: string) {
|
|||||||
},
|
},
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
});
|
});
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const get = queryWithoutBody('GET');
|
|
||||||
export const post = queryWithBody('POST');
|
export const post = queryWithBody('POST');
|
||||||
export const put = queryWithBody('PUT');
|
export const put = queryWithBody('PUT');
|
||||||
export const doDelete = queryWithBody('DELETE');
|
export const doDelete = queryWithBody('DELETE');
|
||||||
@@ -155,7 +148,7 @@ export async function uploadFile(fn: string, key?: string) {
|
|||||||
form.append(k, v);
|
form.append(k, v);
|
||||||
});
|
});
|
||||||
const fileStream = fs.createReadStream(fn);
|
const fileStream = fs.createReadStream(fn);
|
||||||
fileStream.on('data', function (data) {
|
fileStream.on('data', (data) => {
|
||||||
bar.tick(data.length);
|
bar.tick(data.length);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
12
src/app.js
12
src/app.js
@@ -1,5 +1,5 @@
|
|||||||
import { question } from './utils';
|
import { question } from './utils';
|
||||||
import fs from 'fs';
|
import fs from 'node:fs';
|
||||||
import Table from 'tty-table';
|
import Table from 'tty-table';
|
||||||
|
|
||||||
import { post, get, doDelete } from './api';
|
import { post, get, doDelete } from './api';
|
||||||
@@ -84,7 +84,7 @@ export const commands = {
|
|||||||
options: { platform, downloadUrl },
|
options: { platform, downloadUrl },
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
deleteApp: async function ({ args, options }) {
|
deleteApp: async ({ args, options }) => {
|
||||||
const { platform } = options;
|
const { platform } = options;
|
||||||
const id = args[0] || chooseApp(platform);
|
const id = args[0] || chooseApp(platform);
|
||||||
if (!id) {
|
if (!id) {
|
||||||
@@ -93,15 +93,17 @@ export const commands = {
|
|||||||
await doDelete(`/app/${id}`);
|
await doDelete(`/app/${id}`);
|
||||||
console.log('操作成功');
|
console.log('操作成功');
|
||||||
},
|
},
|
||||||
apps: async function ({ options }) {
|
apps: async ({ options }) => {
|
||||||
const { platform } = options;
|
const { platform } = options;
|
||||||
listApp(platform);
|
listApp(platform);
|
||||||
},
|
},
|
||||||
selectApp: async function ({ args, options }) {
|
selectApp: async ({ args, options }) => {
|
||||||
const platform = checkPlatform(
|
const platform = checkPlatform(
|
||||||
options.platform || (await question('平台(ios/android/harmony):')),
|
options.platform || (await question('平台(ios/android/harmony):')),
|
||||||
);
|
);
|
||||||
const id = args[0] ? parseInt(args[0]) : (await chooseApp(platform)).id;
|
const id = args[0]
|
||||||
|
? Number.parseInt(args[0])
|
||||||
|
: (await chooseApp(platform)).id;
|
||||||
|
|
||||||
let updateInfo = {};
|
let updateInfo = {};
|
||||||
if (fs.existsSync('update.json')) {
|
if (fs.existsSync('update.json')) {
|
||||||
|
@@ -1,13 +1,13 @@
|
|||||||
import { question } from './utils';
|
import { question } from './utils';
|
||||||
import { post, get, replaceSession, saveSession, closeSession } from './api';
|
import { post, get, replaceSession, saveSession, closeSession } from './api';
|
||||||
import crypto from 'crypto';
|
import crypto from 'node:crypto';
|
||||||
|
|
||||||
function md5(str) {
|
function md5(str) {
|
||||||
return crypto.createHash('md5').update(str).digest('hex');
|
return crypto.createHash('md5').update(str).digest('hex');
|
||||||
}
|
}
|
||||||
|
|
||||||
export const commands = {
|
export const commands = {
|
||||||
login: async function ({ args }) {
|
login: async ({ args }) => {
|
||||||
const email = args[0] || (await question('email:'));
|
const email = args[0] || (await question('email:'));
|
||||||
const pwd = args[1] || (await question('password:', true));
|
const pwd = args[1] || (await question('password:', true));
|
||||||
const { token, info } = await post('/user/login', {
|
const { token, info } = await post('/user/login', {
|
||||||
@@ -18,11 +18,11 @@ export const commands = {
|
|||||||
await saveSession();
|
await saveSession();
|
||||||
console.log(`欢迎使用 pushy 热更新服务, ${info.name}.`);
|
console.log(`欢迎使用 pushy 热更新服务, ${info.name}.`);
|
||||||
},
|
},
|
||||||
logout: async function () {
|
logout: async () => {
|
||||||
await closeSession();
|
await closeSession();
|
||||||
console.log('已退出登录');
|
console.log('已退出登录');
|
||||||
},
|
},
|
||||||
me: async function () {
|
me: async () => {
|
||||||
const me = await get('/user/me');
|
const me = await get('/user/me');
|
||||||
for (const k in me) {
|
for (const k in me) {
|
||||||
if (k !== 'ok') {
|
if (k !== 'ok') {
|
||||||
|
Reference in New Issue
Block a user