diff --git a/src/api.ts b/src/api.ts index 4652d22..1259c67 100644 --- a/src/api.ts +++ b/src/api.ts @@ -6,16 +6,19 @@ import ProgressBar from 'progress'; import packageJson from '../package.json'; import tcpp from 'tcp-ping'; import filesizeParser from 'filesize-parser'; -import { pricingPageUrl } from './utils'; +import { pricingPageUrl } from './utils/constants'; import type { Session } from 'types'; import FormData from 'form-data'; +import { credentialFile } from 'utils/constants'; const tcpPing = util.promisify(tcpp.ping); let session: Session | undefined; let savedSession: Session | undefined; -const defaultEndpoint = 'https://update.reactnative.cn/api'; +const defaultEndpoint = global.IS_CRESC + ? 'https://api.cresc.dev' + : 'https://update.reactnative.cn/api'; let host = process.env.PUSHY_REGISTRY || defaultEndpoint; const userAgent = `react-native-update-cli/${packageJson.version}`; @@ -26,14 +29,16 @@ export const replaceSession = (newSession: { token: string }) => { session = newSession; }; + + export const loadSession = async () => { - if (fs.existsSync('.update')) { + if (fs.existsSync(credentialFile)) { try { - replaceSession(JSON.parse(fs.readFileSync('.update', 'utf8'))); + replaceSession(JSON.parse(fs.readFileSync(credentialFile, 'utf8'))); savedSession = session; } catch (e) { console.error( - 'Failed to parse file `.update`. Try to remove it manually.', + `Failed to parse file ${credentialFile}. Try to remove it manually.`, ); throw e; } @@ -45,14 +50,14 @@ export const saveSession = () => { if (session !== savedSession) { const current = session; const data = JSON.stringify(current, null, 4); - fs.writeFileSync('.update', data, 'utf8'); + fs.writeFileSync(credentialFile, data, 'utf8'); savedSession = current; } }; export const closeSession = () => { - if (fs.existsSync('.update')) { - fs.unlinkSync('.update'); + if (fs.existsSync(credentialFile)) { + fs.unlinkSync(credentialFile); savedSession = undefined; } session = undefined; @@ -137,7 +142,7 @@ export async function uploadFile(fn: string, key?: string) { ); } - const bar = new ProgressBar(' 上传中 [:bar] :percent :etas', { + const bar = new ProgressBar(' Uploading [:bar] :percent :etas', { complete: '=', incomplete: ' ', total: fileSize, @@ -145,9 +150,9 @@ export async function uploadFile(fn: string, key?: string) { const form = new FormData(); - Object.entries(formData).forEach(([k, v]) => { + for (const [k, v] of Object.entries(formData)) { form.append(k, v); - }); + } const fileStream = fs.createReadStream(fn); fileStream.on('data', (data) => { bar.tick(data.length); diff --git a/src/utils/constants.ts b/src/utils/constants.ts new file mode 100644 index 0000000..c64749d --- /dev/null +++ b/src/utils/constants.ts @@ -0,0 +1,6 @@ +export const credentialFile = global.IS_CRESC ? '.cresc.token' : '.update'; +export const updateJson = global.IS_CRESC ? 'cresc.config.json' : 'update.json'; +export const tempDir = global.IS_CRESC ? '.cresc.temp' : '.pushy'; +export const pricingPageUrl = global.IS_CRESC + ? 'https://cresc.dev/pricing' + : 'https://pushy.reactnative.cn/pricing.html'; diff --git a/src/utils/index.ts b/src/utils/index.ts index 2548a89..0648c7e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -9,8 +9,9 @@ import latestVersion from '@badisi/latest-version'; import { checkPlugins } from './check-plugin'; import { read } from 'read'; +import { tempDir } from './constants'; -export async function question(query, password) { +export async function question(query: string, password: boolean) { if (NO_INTERACTIVE) { return ''; } @@ -21,7 +22,7 @@ export async function question(query, password) { }); } -export function translateOptions(options) { +export function translateOptions(options: Record) { const ret = {}; for (const key in options) { const v = options[key]; @@ -163,16 +164,16 @@ export async function getIpaInfo(fn: string) { return { versionName, buildTime, ...appCredential }; } -const localDir = path.resolve(os.homedir(), '.pushy'); +const localDir = path.resolve(os.homedir(), tempDir); fs.ensureDirSync(localDir); -export function saveToLocal(originPath, destName) { +export function saveToLocal(originPath: string, destName: string) { // TODO // const destPath = path.join(localDir, destName); // fs.ensureDirSync(path.dirname(destPath)); // fs.copyFileSync(originPath, destPath); } -async function getLatestVersion(pkgName) { +async function getLatestVersion(pkgName: string) { return Promise.race([ latestVersion(pkgName) .then((p) => p.latest) @@ -225,6 +226,6 @@ export async function printVersionCommand() { } } -export const pricingPageUrl = 'https://pushy.reactnative.cn/pricing.html'; + export { checkPlugins };