UNPKG

pcs-cli

Version:

Baidu Personal Cloud Storage's Command Line Interface.

2 lines (1 loc) 3.23 kB
import{_ as e}from"../_tslib-NbJOzQvB.js";import{existsSync as t}from"fs";import i from"chalk";import o from"prompts";import s from"dayjs";import n from"open";import{f as r,l as a,J as c,E as p}from"../index-7szJY7Td.js";import d from"../services/pcs.js";import"os";import"path";import"crypto";import"https";import"progress";import"../utils/axios.js";import"axios";var l=l=>{l.command("init").description("initialize baidu pcs").option("-n, --name <string>","app name").option("-k, --key <string>","app key").option("-s, --secret <string>","app secret").action((l=>e(void 0,void 0,void 0,(function*(){const m=r("TOKEN");if(m&&m.access_token){a(`Your access token has not expired (expiration date: ${s(m.expires_time).format("YYYY-MM-DD HH:mm:ss")}).`);const{value:e}=yield o({type:"confirm",name:"value",message:"Do you want to continue initializing?",initial:!1});if(!e)return}const y=r("DEVICE");if(y)return void(yield u(y));const f=[];["name","key","secret"].forEach((e=>{l[e]||f.push({type:"text",name:e,message:`Please enter baidu app ${e}`})}));const h=yield o(f),k=Object.assign(Object.assign({},l),h);if(!["name","key"].every((e=>k[e])))return;let g=!0;if(t(c.APP)){const{value:e}=yield o({type:"confirm",name:"value",message:"Baidu pcs initialization will be begin. If you have already configured before, your old settings will be overwritten. Can you confirm?",initial:!1});g=e}g&&(r("APP",k),yield function(){return e(this,void 0,void 0,(function*(){const e=r("APP"),{key:t="client_id",secret:s}=e;if(s)try{const e=yield d.oauthDevice(t),i=Object.assign(Object.assign({},e),{key:t,secret:s});r("DEVICE",i),yield u(i)}catch(e){const{response:{data:t}}=e;console.log(`OAuth error ${t.error} : ${t.error_description}`)}else{const e=`https://openapi.baidu.com/oauth/2.0/authorize?response_type=token&client_id=${t}&redirect_uri=oob&scope=netdisk`;yield n(e);const c=Date.now();a("You'll have to grab the access_token generated by Baidu.",i.hex("#FFA500")),a(""),a(`1. Visit ${i.blue.underline(e)}`),a("2. After the page is being redirected (it should show something like OAuth 2.0), copy the current URL to your favorite text editor"),a('3. Grab the access_token part, take only the part between "access_token=" and the next "&" symbol (without quotes).'),a("4. Copy it and paste here, then press Enter.");const{access_token:d}=yield o({type:"text",name:"access_token",message:"access_token"});if(d){const e=(Date.now()-c)/1e3;r("TOKEN",{access_token:d,expires_in:p-e,key:t,secret:s})}}}))}())}))))};function u(t){return e(this,void 0,void 0,(function*(){const{verification_url:e,user_code:s,device_code:c,key:p,secret:l}=t;yield n(`${e}?code=${s}`),a("Launch your favorite web browser and visit: "),a(e,i.blue.underline),a(`Input ${i.bold.red(s)} as the user code if asked.`),a("After granting access to the application, come back here and ");const{confirm:u}=yield o({type:"confirm",name:"confirm",message:"Press Enter to continue",initial:!0});if(u){const e=yield d.oauthToken(p,l,c);r("TOKEN",Object.assign(Object.assign({},e),{key:p,secret:l})),a("Successfully initialized",i.green),a(`access_token: ${i.yellowBright(e.access_token)}`),a(`refresh_token: ${i.yellowBright(e.refresh_token)}`)}}))}export{l as default};