Created
July 31, 2017 17:52
-
-
Save moshen/c4414357e304c8344504d0f98591e66c to your computer and use it in GitHub Desktop.
LudumDare web build download and extract
This file contains hidden or 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
{ | |
"name": "ld-pull", | |
"version": "1.0.0", | |
"description": "Pull LudumDare web builds and unpack them", | |
"main": "pull.js", | |
"dependencies": { | |
"request": "^2.72.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Colin Kennedy <[email protected]>", | |
"license": "MIT" | |
} |
This file contains hidden or 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
const request = require('request'), | |
spawn = require('child_process').spawn, | |
buildRegex = /build-([0-9]+)-web/; | |
const ludumDareNum = process.argv[process.argv.length - 1]; | |
if (ludumDareNum !== Number(ludumDareNum).toString()) { | |
console.error('Pass a valid number'); | |
process.exit(1); | |
} | |
const downloadUrlBase = 'https://dl.bintray.com/bploeckelman/LudumDare/', | |
apiUrl = 'https://api.bintray.com/packages/bploeckelman/LudumDare/LudumDare' + ludumDareNum + '/files'; | |
function getBuildNumber(name) { | |
const matches = name.match(buildRegex); | |
if(matches && matches.length > 1) { | |
return +matches[1]; | |
} | |
return 0; | |
} | |
async function getFromApi() { | |
return new Promise((resolve, reject) => { | |
request({ | |
method: 'GET', | |
url: apiUrl, | |
json: true | |
}, function(err, res, body) { | |
if (err) { | |
return reject(err); | |
} | |
if (res.statusCode > 299) { | |
return reject(body); | |
} | |
resolve(body); | |
}); | |
}); | |
} | |
async function doPull() { | |
const body = await getFromApi(); | |
if (!body instanceof Array) { | |
return Promise.reject('Didn\'t get a result back'); | |
} | |
const latest = body.reduce((memo, file) => { | |
file.buildNumber = getBuildNumber(file.name); | |
if (!memo) { | |
return file; | |
} | |
if (file.buildNumber > memo.buildNumber) { | |
return file; | |
} | |
return memo; | |
}, false); | |
console.log('Downloading', latest.name); | |
return new Promise((resolve, reject) => { | |
// The directory needs to exist for this to work, look at `run` | |
const tar = spawn('bsdtar', ['-xf', '-', '-C', ludumDareNum]); | |
tar.on('error', reject) | |
.on('close', code => { | |
if (code !== 0) { | |
return reject('Unzip failed'); | |
} | |
resolve(); | |
}); | |
request({ | |
method: 'GET', | |
url: downloadUrlBase + latest.name, | |
}) | |
.pipe(tar.stdin) | |
.on('error', reject); | |
}); | |
}; | |
doPull() | |
.catch(err => { | |
console.log(err); | |
process.exit(1); | |
}); |
This file contains hidden or 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
#!/bin/bash | |
# Which Ludum Dare number to get | |
LUDUM_DARE_NUM=39 | |
# Stash the current build in case fetching fails | |
if [[ -d $LUDUM_DARE_NUM ]]; then | |
mv $LUDUM_DARE_NUM "${LUDUM_DARE_NUM}-old" | |
fi | |
mkdir $LUDUM_DARE_NUM | |
# Fetch and unpack the new build | |
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh # This loads NVM | |
nvm use 8 | |
node pull.js $LUDUM_DARE_NUM | |
# If we failed to fetch the new build, put the old one back | |
# If we don't have an "old" one, do nothing | |
if [[ $? -gt 0 ]] && [[ -d "${LUDUM_DARE_NUM}-old" ]]; then | |
rm -rf $LUDUM_DARE_NUM | |
mv "${LUDUM_DARE_NUM}-old" $LUDUM_DARE_NUM | |
elif [[ -d "${LUDUM_DARE_NUM}-old" ]]; then | |
rm -rf "${LUDUM_DARE_NUM}-old" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires nvm with node 8 installed