瀏覽代碼

Setup Wizard - Git check

NGPixel 8 年之前
父節點
當前提交
6698ca094d
共有 5 個文件被更改,包括 73 次插入23 次删除
  1. 0 1
      agent.js
  2. 0 0
      assets/js/configure.js
  3. 3 1
      client/js/configure.js
  4. 66 17
      configure.js
  5. 4 4
      views/configure/index.pug

+ 0 - 1
agent.js

@@ -183,7 +183,6 @@ db.onReady.then(() => {
     timeZone: 'UTC',
     timeZone: 'UTC',
     runOnInit: true
     runOnInit: true
   })
   })
-
 })
 })
 
 
 // ----------------------------------------
 // ----------------------------------------

文件差異過大導致無法顯示
+ 0 - 0
assets/js/configure.js


+ 3 - 1
client/js/configure.js

@@ -205,8 +205,9 @@ jQuery(document).ready(function ($) {
         let self = this
         let self = this
         this.state = 'gitcheck'
         this.state = 'gitcheck'
         this.loading = true
         this.loading = true
-        self.dbcheck = {
+        self.gitcheck = {
           ok: false,
           ok: false,
+          results: [],
           error: ''
           error: ''
         }
         }
 
 
@@ -214,6 +215,7 @@ jQuery(document).ready(function ($) {
           axios.post('/gitcheck', self.conf).then(resp => {
           axios.post('/gitcheck', self.conf).then(resp => {
             if (resp.data.ok === true) {
             if (resp.data.ok === true) {
               self.gitcheck.ok = true
               self.gitcheck.ok = true
+              self.gitcheck.results = resp.data.results
             } else {
             } else {
               self.gitcheck.ok = false
               self.gitcheck.ok = false
               self.gitcheck.error = resp.data.error
               self.gitcheck.error = resp.data.error

+ 66 - 17
configure.js

@@ -15,11 +15,10 @@ module.exports = (port, spinner) => {
   const http = require('http')
   const http = require('http')
   const path = require('path')
   const path = require('path')
   const Promise = require('bluebird')
   const Promise = require('bluebird')
-  const fs = require('fs-extra')
+  const fs = Promise.promisifyAll(require('fs-extra'))
   const yaml = require('js-yaml')
   const yaml = require('js-yaml')
   const _ = require('lodash')
   const _ = require('lodash')
 
 
-
   // ----------------------------------------
   // ----------------------------------------
   // Define Express App
   // Define Express App
   // ----------------------------------------
   // ----------------------------------------
@@ -160,26 +159,76 @@ module.exports = (port, spinner) => {
    */
    */
   app.post('/gitcheck', (req, res) => {
   app.post('/gitcheck', (req, res) => {
     const exec = require('execa')
     const exec = require('execa')
+    const url = require('url')
 
 
     const dataDir = path.resolve(ROOTPATH, req.body.pathData)
     const dataDir = path.resolve(ROOTPATH, req.body.pathData)
     const gitDir = path.resolve(ROOTPATH, req.body.pathRepo)
     const gitDir = path.resolve(ROOTPATH, req.body.pathRepo)
-    let results = []
 
 
-    fs.ensureDir(dataDir).then(() => {
-      results.push('Data directory path is valid.')
-      return fs.ensureDir(gitDir).then(() => {
-        results.push('Git directory path is valid.')
-        return true
-      })
-    }).then(() => {
-      return exec.stdout('git', ['init'], { cwd: gitDir }).then(result => {
-        results.push('Git local repository initialized.')
-        return true
-      })
-    }).then(() => {
-      return res.json({ ok: true, results })
+    let urlObj = url.parse(req.body.gitUrl)
+    if (req.body.gitAuthType === 'basic') {
+      urlObj.auth = req.body.gitAuthUser + ':' + req.body.gitAuthPass
+    }
+    const gitRemoteUrl = url.format(urlObj)
+
+    Promise.mapSeries([
+      () => {
+        return fs.ensureDirAsync(dataDir).return('Data directory path is valid.')
+      },
+      () => {
+        return fs.ensureDirAsync(gitDir).return('Git directory path is valid.')
+      },
+      () => {
+        return exec.stdout('git', ['init'], { cwd: gitDir }).then(result => {
+          return 'Local git repository has been initialized.'
+        })
+      },
+      () => {
+        return exec.stdout('git', ['config', '--local', 'user.name', req.body.gitSignatureName], { cwd: gitDir }).then(result => {
+          return 'Git Signature Name has been set successfully.'
+        })
+      },
+      () => {
+        return exec.stdout('git', ['config', '--local', 'user.email', req.body.gitSignatureEmail], { cwd: gitDir }).then(result => {
+          return 'Git Signature Name has been set successfully.'
+        })
+      },
+      () => {
+        return exec.stdout('git', ['config', '--local', '--bool', 'http.sslVerify', req.body.gitAuthSSL], { cwd: gitDir }).then(result => {
+          return 'Git SSL Verify flag has been set successfully.'
+        })
+      },
+      () => {
+        if (req.body.gitAuthType === 'ssh') {
+          return exec.stdout('git', ['config', '--local', 'core.sshCommand', 'ssh -i "' + req.body.gitAuthSSHKey + '" -o StrictHostKeyChecking=no'], { cwd: gitDir }).then(result => {
+            return 'Git SSH Private Key path has been set successfully.'
+          })
+        } else {
+          return false
+        }
+      },
+      () => {
+        return exec.stdout('git', ['remote', 'remove', 'origin'], { cwd: gitDir }).catch(err => {
+          if (_.includes(err.message, 'No such remote')) {
+            return true
+          } else {
+            throw err
+          }
+        }).then(() => {
+          return exec.stdout('git', ['remote', 'add', 'origin', gitRemoteUrl], { cwd: gitDir }).then(result => {
+            return 'Git Remote was added successfully.'
+          })
+        })
+      },
+      () => {
+        return exec.stdout('git', ['pull', 'origin', req.body.gitBranch], { cwd: gitDir }).then(result => {
+          return 'Git Pull operation successful.'
+        })
+      }
+    ], step => { return step() }).then(results => {
+      return res.json({ ok: true, results: _.without(results, false) })
     }).catch(err => {
     }).catch(err => {
-      res.json({ ok: false, error: err.message })
+      let errMsg = (err.stderr) ? err.stderr.replace(/(error:|warning:|fatal:)/gi, '').replace(/ \s+/g, ' ') : err.message
+      res.json({ ok: false, error: errMsg })
     })
     })
   })
   })
 
 

+ 4 - 4
views/configure/index.pug

@@ -252,17 +252,17 @@ html
                     .column(v-show='conf.gitAuthType === "basic"')
                     .column(v-show='conf.gitAuthType === "basic"')
                       p.control.is-fullwidth
                       p.control.is-fullwidth
                         label.label Username
                         label.label Username
-                        input(type='text', v-model='conf.gitUrl')
+                        input(type='text', v-model='conf.gitAuthUser')
                         span.desc The username for the remote git connection.
                         span.desc The username for the remote git connection.
                     .column(v-show='conf.gitAuthType === "basic"')
                     .column(v-show='conf.gitAuthType === "basic"')
                       p.control.is-fullwidth
                       p.control.is-fullwidth
                         label.label Password
                         label.label Password
-                        input(type='password', v-model='conf.gitUrl')
+                        input(type='password', v-model='conf.gitAuthPass')
                         span.desc The password for the remote git connection.
                         span.desc The password for the remote git connection.
                     .column(v-show='conf.gitAuthType === "ssh"')
                     .column(v-show='conf.gitAuthType === "ssh"')
                       p.control.is-fullwidth
                       p.control.is-fullwidth
                         label.label Private Key location
                         label.label Private Key location
-                        input(type='text', placeholder='e.g. /etc/wiki/keys/git.pem')
+                        input(type='text', placeholder='e.g. /etc/wiki/keys/git.pem', v-model='conf.gitAuthSSHKey')
                         span.desc The full path to the private key on disk.
                         span.desc The full path to the private key on disk.
                   section.columns
                   section.columns
                     .column
                     .column
@@ -277,7 +277,7 @@ html
                         span.desc The email to use when pushing commits to the git repository.
                         span.desc The email to use when pushing commits to the git repository.
                 .panel-footer
                 .panel-footer
                   .progress-bar: div(v-bind:style='{width: currentProgress}')
                   .progress-bar: div(v-bind:style='{width: currentProgress}')
-                  button.button.is-indigo.is-outlined(v-on:click='proceedToDb', v-bind:disabled='loading') Back
+                  button.button.is-indigo.is-outlined(v-on:click='proceedToPaths', v-bind:disabled='loading') Back
                   button.button.is-indigo.is-outlined(v-on:click='proceedToAdmin', v-bind:disabled='loading') Skip this step
                   button.button.is-indigo.is-outlined(v-on:click='proceedToAdmin', v-bind:disabled='loading') Skip this step
                   button.button.is-indigo(v-on:click='proceedToGitCheck', v-bind:disabled='loading || errors.any("git")') Continue
                   button.button.is-indigo(v-on:click='proceedToGitCheck', v-bind:disabled='loading || errors.any("git")') Continue
             
             

部分文件因文件數量過多而無法顯示