import path from 'path' ; import fs , { readdirSync , unlinkSync } from 'fs' ; import { exec } from 'child_process' ; import inquirer from 'inquirer' ; import QR from 'qrcode-reader' ; import { read as ImageHandler } from 'jimp' ; import QRGenerate from 'qrcode-terminal' ; export const qrEncode = ( content ) : Promise < string > => { return new Promise ( ( resolve , reject ) => { QRGenerate . generate ( content , { small : true } , function ( qrcode ) { resolve ( qrcode ) ; } ) ; } ) ; } ; export const qrDecode = ( imgBuffer ) : Promise < string > => { return new Promise ( ( resolve , reject ) => { const decodeQR = new QR ( ) ; decodeQR . callback = function ( err , result ) { if ( err ) { reject ( err ) ; return ; } if ( ! result ) { resolve ( '' ) ; } else { resolve ( result . result ) ; } } ; ImageHandler ( imgBuffer , ( err , image ) => { if ( err ) { reject ( err ) ; return ; } decodeQR . decode ( image . bitmap ) ; } ) ; } ) ; } ; export const isMacOS = ( ) => { return ! ( /^win/ . test ( process . platform ) ) ; } ; export const cliPrefix = ( ) => { return `${ isMacOS ( ) ? './' : '' }cli${ isMacOS ( ) ? '' : '.bat' } ` ; } ; export const cmdJoin = ( ... cli ) => { return `${ cliPrefix ( ) }${ cli . join ( ' ' ) }` ; } ; export const random = ( ) => { return Math . random ( ) . toString ( 16 ) . substring ( 2 ) ; } ; export const isAsyncFn = ( fn ) => { return Object . prototype . toString . call ( fn ) === '[object AsyncFunction]' ; } ; export const resolvePath = ( ... ps ) => { return path . resolve ( __dirname , ... ps ) ; } ; export const readFileSync = ( path ) => { return fs . readFileSync ( path , 'utf-8' ) ; } ; export const writeFileSync = ( path , data ) => { return fs . writeFileSync ( path , data ) ; } ; export const relativePath = ( p1 , p2 ) => { return path . relative ( p1 , p2 ) ; } ; export const execCmd = ( cmd , path , fn = ( process ) => { } ) : Promise < string > => { return new Promise ( ( resolve , reject ) => { try { const process = exec ( cmd , { cwd : path } , ( err , stdout , stderr ) => { if ( stdout ) { resolve ( stdout ) ; return ; } reject ( err || stderr ) ; } ) ; fn ( process ) ; } catch ( e ) { reject ( e ) ; } } ) ; } ; export const asyncFn = ( fn , ... args ) => { if ( isAsyncFn ( fn ) ) { return new Promise ( async ( resolve , reject ) => { try { resolve ( await fn ( ... args ) ) ; } catch ( e ) { reject ( e ) ; } } ) ; } return new Promise ( ( resolve , reject ) => { try { resolve ( fn ( ... args ) ) ; } catch ( e ) { reject ( e ) ; } } ) ; } ; export const scanf = async ( tip ) => { const { value } = await inquirer . prompt ( [ { name : 'value' , type : 'input' , message : tip . green , } , ] ) ; return value ; } ; export const abort = ( text = '' ) => { console . log ( text ) ; process . exit ( ) ; } ; export const clearDir = ( dir ) => { const files = readdirSync ( dir ) . map ( item => resolvePath ( dir , `./${ item }` ) ) ; files . forEach ( ( file ) => { unlinkSync ( file ) ; } ) ; } ;