Skip to content
This repository has been archived by the owner on Oct 26, 2019. It is now read-only.

Commit

Permalink
Have a github fallback if git isn't setup locally.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaine Schmeisser committed Dec 27, 2016
1 parent 6772c8a commit 8983a0a
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 27 deletions.
4 changes: 4 additions & 0 deletions app/lib/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const git = require('./git')
const github = require('./github')

module.exports = git.isInstalled() ? git : github
25 changes: 17 additions & 8 deletions app/lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ const which = require('which')

const git = (args, options) => {
return new Promise((resolve, reject) => {
which('git', (err, resolvedPath) => {
if (err) return reject('GIT was not found on your system')
exec(resolvedPath, args, options || {}, (err) => {
err && reject(err) || resolve()
})
exec(gitPath(), args, options || {}, (err) => {
err && reject(err) || resolve()
})
})
}

const pull = (packagePath) => {
const pull = (name, packagePath) => {
return git(['pull'], { cwd: packagePath }).catch((err) => {
if (err.message.match(/ENOENT/i)) {
throw new Error('Package "' + name + '" is not cloneed')
throw new Error('Package "' + name + '" is not cloned')
} else {
throw err
}
Expand All @@ -35,4 +32,16 @@ const clone = (name, packagePath) => {
})
}

module.exports = { clone, pull, git }
const gitPath = () => {
return which.sync('git')
}

const isInstalled = () => {
try {
return gitPath()
} catch (e) {
return false
}
}

module.exports = { clone, pull, git, isInstalled }
69 changes: 69 additions & 0 deletions app/lib/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const request = require('request')
const path = require('path')
const fs = require('fs')
const json = require('./json')
const jetpack = require('fs-jetpack')
const decompress = require('decompress')

const currentRemoteVersion = (name) => {
return json({ host: 'api.github.com',
path: '/repos/' + name + '/commits',
}).then((response) => {
return response[0] && response[0].sha || ''
})
}

const currentLocalVersion = (packagePath) => {
const versionPath = path.join(packagePath, '.head.zazu.')
return new Promise((resolve, reject) => {
fs.readFile(versionPath, (_, data) => {
resolve(data || '')
})
})
}

const pull = (name, packagePath) => {
const versions = [currentRemoteVersion(name), currentLocalVersion(packagePath)]
return Promise.all(versions).then(([remoteVersion, localVersion]) => {
if (remoteVersion === localVersion) return
return jetpack.removeAsync(packagePath).then(() => {
return clone(name, packagePath)
})
})
}

const download = (remote, local) => {
return new Promise((resolve, reject) => {
request(remote)
.pipe(fs.createWriteStream(local))
.on('close', () => {
resolve()
})
})
}

const clone = (name, packagePath) => {
return currentRemoteVersion(name).then((version) => {
const zipUrl = `https://github.com/${name}/archive/${version}.zip`
const packageName = name.split('/')[1]
const extractDir = path.join(packagePath, '..')
const extractPath = path.join(extractDir, `${packageName}-${version}`)
const tempPath = path.join(extractDir, `${packageName}-${version}.zip`)
const versionPath = path.join(packagePath, '.head.zazu.')
return download(zipUrl, tempPath).then(() => {
return decompress(tempPath, extractDir)
}).then(() => {
return jetpack.removeAsync(tempPath)
}).then(() => {
return jetpack.renameAsync(extractPath, packageName)
}).then(() => {
return jetpack.writeAsync(versionPath, version)
})
})
}

const isInstalled = () => {
return true
}

module.exports = { clone, pull, isInstalled }
25 changes: 25 additions & 0 deletions app/lib/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const electron = require('electron')
const https = require('https')
const app = electron.app || electron.remote.app

module.exports = (opts) => {
const options = Object.assign({}, opts, {
headers: {
'User-Agent': `ZazuApp v${app.getVersion()}`,
},
})
return new Promise((resolve) => {
https.get(options, (res) => {
var chunks = []
res.on('data', (chunk) => {
chunks.push(chunk.toString())
})
res.on('end', () => {
resolve(JSON.parse(chunks.join('')))
})
res.on('error', (e) => {
throw e
})
})
})
}
20 changes: 5 additions & 15 deletions app/lib/update.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const semver = require('semver')
const https = require('https')
const json = require('./json')
const { shell, app, dialog } = require('electron')

const globalEmitter = require('./globalEmitter')
Expand All @@ -21,22 +21,12 @@ var self = {
if (self._latestVersion) {
return resolve(self._latestVersion)
}
https.get({
json({
host: 'api.github.com',
path: '/repos/tinytacoteam/zazu/releases/latest',
headers: {
'User-Agent': `ZazuApp Updater v${app.getVersion()}`,
},
}, (res) => {
var chunks = []
res.on('data', (chunk) => {
chunks.push(chunk.toString())
})
res.on('end', () => {
self._latestVersion = JSON.parse(chunks.join()).tag_name
resolve(self._latestVersion || app.getVersion())
})
}).on('error', (e) => {
}).then((body) => {
resolve(body.tag_name || app.getVersion())
}).catch((e) => {
reject(`Got error: ${e.message}`)
})
})
Expand Down
2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"dependencies": {
"auto-launch": "^2.1.0",
"cuid": "^1.3.8",
"decompress": "^4.0.0",
"fs-jetpack": "^0.7.0",
"mousetrap": "^1.5.3",
"nedb": "^1.8.0",
"node-notifier": "^4.5.0",
"npm": "^3.10.5",
"react": "^15.0.1",
"react-dom": "^15.0.1",
"request": "^2.79.0",
"semver": "^5.0.3",
"winston": "^2.2.0",
"winston-daily-rotate-file": "^1.2.0"
Expand Down
4 changes: 2 additions & 2 deletions app/packages/package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const jetpack = require('fs-jetpack')

const { clone, pull } = require('../lib/git')
const { clone, pull } = require('../lib/download')
const freshRequire = require('../lib/freshRequire')
const configuration = require('../lib/configuration')
const logger = require('../lib/logger')
Expand Down Expand Up @@ -32,7 +32,7 @@ class Package {
return Promise.reject('Package' + this.url + ' does not exist')
}
this.logger.log('info', 'pull package')
return this.pull(this.path)
return this.pull(this.url, this.path)
}

download () {
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/home/.fallback.zazurc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"tinytacoteam/zazu-fallback"
],
"comments": [
"zazu-fixtures is installed",
"zazu-fallback is not"
"zazu-fixtures and zazu-fallback is installed"
]
}

0 comments on commit 8983a0a

Please sign in to comment.