-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #583 from desktop/break-all-the-things
Remove jest in favor of Node's built-in test runner
- Loading branch information
Showing
31 changed files
with
842 additions
and
2,602 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
20.17.0 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,132 +1,117 @@ | ||
const fs = require('fs') | ||
|
||
const ProgressBar = require('progress') | ||
const tar = require('tar') | ||
const https = require('https') | ||
const { createHash } = require('crypto') | ||
const { rm, rmSync, mkdir, createReadStream, createWriteStream, existsSync } = require('fs') | ||
const { createReadStream, createWriteStream } = require('fs') | ||
|
||
const { rm, mkdir, access } = require('fs/promises') | ||
|
||
/** | ||
* Returns a value indicating whether or not the provided path exists (as in | ||
* whether it's visible to the current process or not). | ||
*/ | ||
const pathExists = path => | ||
access(path).then( | ||
() => true, | ||
() => false | ||
) | ||
|
||
const { Readable } = require('stream') | ||
|
||
const config = require('./config')() | ||
|
||
const verifyFile = function(file, callback) { | ||
const h = createHash('sha256').on('finish', () => { | ||
const hash = h.digest('hex') | ||
const match = hash === config.checksum | ||
if (!match) { | ||
console.log(`Validation failed. Expected '${config.checksum}' but got '${hash}'`) | ||
} | ||
callback(match) | ||
}) | ||
const verifyFile = function (file, callback) { | ||
return new Promise((resolve, reject) => { | ||
const h = createHash('sha256') | ||
.on('error', reject) | ||
.on('finish', () => { | ||
const hash = h.digest('hex') | ||
if (hash !== config.checksum) { | ||
reject( | ||
new Error( | ||
`Validation failed. Expected '${config.checksum}' but got '${hash}'` | ||
) | ||
) | ||
} else { | ||
resolve() | ||
} | ||
}) | ||
|
||
createReadStream(file).pipe(h) | ||
createReadStream(file).pipe(h).on('error', reject) | ||
}) | ||
} | ||
|
||
const unpackFile = function(file) { | ||
const unpackFile = file => | ||
tar.x({ cwd: config.outputPath, file }).catch(e => { | ||
console.log('Unable to extract archive, aborting...', error) | ||
console.log('Unable to extract archive, aborting...', e) | ||
process.exit(1) | ||
}) | ||
} | ||
|
||
const downloadAndUnpack = (url, isFollowingRedirect) => { | ||
if (!isFollowingRedirect) { | ||
console.log(`Downloading Git from: ${url}`) | ||
} | ||
|
||
const options = { | ||
const downloadAndUnpack = async url => { | ||
const res = await fetch(url, { | ||
headers: { | ||
Accept: 'application/octet-stream', | ||
'User-Agent': 'dugite' | ||
'User-Agent': 'dugite', | ||
}, | ||
secureProtocol: 'TLSv1_2_method' | ||
} | ||
|
||
const req = https.get(url, options) | ||
|
||
req.on('error', function(error) { | ||
if (error.code === 'ETIMEDOUT') { | ||
console.log( | ||
`A timeout has occurred while downloading '${url}' - check ` + | ||
`your internet connection and try again. If you are using a proxy, ` + | ||
`make sure that the HTTP_PROXY and HTTPS_PROXY environment variables are set.`, | ||
error | ||
) | ||
} else { | ||
console.log(`Error raised while downloading ${url}`, error) | ||
} | ||
}).catch(e => { | ||
console.log('Unable to download archive, aborting...', e) | ||
process.exit(1) | ||
}) | ||
|
||
req.on('response', function(res) { | ||
if ([301, 302].includes(res.statusCode) && res.headers['location']) { | ||
downloadAndUnpack(res.headers.location, true) | ||
return | ||
} | ||
|
||
if (res.statusCode !== 200) { | ||
console.log(`Non-200 response returned from ${url} - (${res.statusCode})`) | ||
process.exit(1) | ||
} | ||
|
||
const len = parseInt(res.headers['content-length'], 10) | ||
if (!res.ok) { | ||
console.log(`Got ${res.status} trying to download archive, aborting...`) | ||
process.exit(1) | ||
} | ||
|
||
const bar = new ProgressBar('Downloading Git [:bar] :percent :etas', { | ||
complete: '=', | ||
incomplete: ' ', | ||
width: 50, | ||
total: len | ||
}) | ||
const len = parseInt(res.headers.get('content-length'), 10) | ||
|
||
res.pipe(createWriteStream(config.tempFile)) | ||
const bar = new ProgressBar('Downloading Git [:bar] :percent :etas', { | ||
complete: '=', | ||
incomplete: ' ', | ||
width: 50, | ||
total: len, | ||
}) | ||
|
||
res.on('data', c => bar.tick(c.length)) | ||
res.on('end', function() { | ||
verifyFile(config.tempFile, valid => { | ||
if (valid) { | ||
unpackFile(config.tempFile) | ||
} else { | ||
console.log(`checksum verification failed, refusing to unpack...`) | ||
process.exit(1) | ||
} | ||
}) | ||
}) | ||
await new Promise((resolve, reject) => { | ||
Readable.fromWeb(res.body) | ||
.on('data', c => bar.tick(c.length)) | ||
.pipe(createWriteStream(config.tempFile)) | ||
.on('error', reject) | ||
.on('finish', resolve) | ||
}) | ||
await verifyFile(config.tempFile) | ||
await unpackFile(config.tempFile) | ||
} | ||
|
||
if (config.source === '') { | ||
console.log( | ||
`Skipping downloading embedded Git as platform '${process.platform}' is not supported.` | ||
) | ||
console.log(`To learn more about using dugite with a system Git: https://git.io/vF5oj`) | ||
process.exit(0) | ||
} | ||
;(async function run() { | ||
if (config.source === '') { | ||
console.log( | ||
`Skipping downloading embedded Git as platform '${process.platform}' is not supported.` | ||
) | ||
console.log( | ||
`To learn more about using dugite with a system Git: https://git.io/vF5oj` | ||
) | ||
process.exit(0) | ||
} | ||
|
||
rm(config.outputPath, { recursive: true, force: true }, error => { | ||
if (error) { | ||
await rm(config.outputPath, { recursive: true, force: true }).catch(error => { | ||
console.log(`Unable to clean directory at ${config.outputPath}`, error) | ||
process.exit(1) | ||
} | ||
|
||
mkdir(config.outputPath, { recursive: true }, function(error) { | ||
if (error) { | ||
console.log(`Unable to create directory at ${config.outputPath}`, error) | ||
process.exit(1) | ||
} | ||
}) | ||
|
||
const tempFile = config.tempFile | ||
await mkdir(config.outputPath, { recursive: true }).catch(error => { | ||
console.log(`Unable to create directory at ${config.outputPath}`, error) | ||
process.exit(1) | ||
}) | ||
|
||
if (existsSync(tempFile)) { | ||
verifyFile(tempFile, valid => { | ||
if (valid) { | ||
unpackFile(tempFile) | ||
} else { | ||
rmSync(tempFile) | ||
downloadAndUnpack(config.source) | ||
} | ||
}) | ||
return | ||
} | ||
const tempFile = config.tempFile | ||
|
||
downloadAndUnpack(config.source) | ||
}) | ||
}) | ||
if (await pathExists(tempFile)) { | ||
await verifyFile(tempFile).catch(e => { | ||
console.log('Unable to verify cached archive, removing...', e) | ||
return rm(tempFile) | ||
}) | ||
await unpackFile(tempFile) | ||
} else { | ||
await downloadAndUnpack(config.source) | ||
} | ||
})() |
Oops, something went wrong.