import * as vscode from 'vscode' ; import * as fs from 'fs' ; import { sep } from 'path' ; import WebView from './WebView' ; import { WebviewMsg } from '../util/dataStatement' ; import { setPanelWebview , isProduction , outputLog } from '../util/vscodeUtil' ; import CommitHandle from './commitHandle' ; const hasGit = ( itemSrc ) => { const url = `${ itemSrc }${ sep }.git` ; try { const isDirectory = fs . statSync ( url ) . isDirectory ( ) ; if ( isDirectory ) { return true ; } return false ; } catch ( err ) { return false ; } } ; class ExtensionLogic { public readonly context : vscode . ExtensionContext ; public autoCommitView ! : WebView ; public CommitHandle : any ; public constructor ( context ) { this . context = context ; } public init ( ) { this . autoCommitView = new WebView ( this . context , this . messageCallBack . bind ( this ) ) as WebView ; setPanelWebview ( this . autoCommitView ) ; this . createView ( ) ; } createView ( ) { const option = { type : 'autoCommit' , title : 'autoCommit' , fileName : 'autoCommit' , } ; this . autoCommitView . create ( option ) ; this . autoCommitView . postMessage ( 'isProduction' , isProduction ( ) ) ; const formData = this . context . globalState . get ( 'commit-params' ) ; this . autoCommitView . postMessage ( 'init-formData' , formData ) ; } private messageCallBack ( message ) { if ( message . command === 'commit' ) { this . CommitHandle = new CommitHandle ( message ) ; } else if ( message . command === 'choose-item' ) { this . publishChooseFile ( ) ; } else if ( message . command === 'cancel autoCommit' ) { this . CommitHandle . closeCommit ( ) ; } } async publishChooseFile ( ) { const urlArr = await vscode . window . showOpenDialog ( { canSelectFiles : false , canSelectFolders : true , canSelectMany : false , } ) ; if ( ! urlArr ) return ; let itemSrc = urlArr [ 0 ] . path ; if ( sep === '\\' ) { itemSrc = urlArr [ 0 ] . fsPath ; } if ( hasGit ( itemSrc ) ) { this . autoCommitView . postMessage ( 'choose item success' , itemSrc ) ; } else { this . autoCommitView . postMessage ( 'choose item error' , itemSrc ) ; outputLog ( '项目地址错误' , `${ itemSrc }根目录没有.git文件夹` ) ; } } } export default ExtensionLogic ;