'use strict'; const config = require('./config'); const cdblib = require('./cdblib'); async function checkalready() { const uri = config.root + '/packages/' + config.user + '/' + config.pkg + '/'; console.log('fetching release list...'); let $ = await cdblib.fetch(uri); let already; $('a') .each(function() { if(already) return; const a = $(this); const h = a.attr('href'); if(!h || !/\/releases\/\d+\/download\//.test(h)) return; if(a.text() .trim() === config.version.trim()) already = true; }); return already; } async function makerelease() { const uri = config.root + '/packages/' + config.user + '/' + config.pkg + '/releases/new/'; console.log('fetching new release page...'); let $ = await cdblib.fetch(uri); const fields = cdblib.getfields($); fields.min_rel = cdblib.findopt($, 'select#min_rel option', config.min); fields.max_rel = cdblib.findopt($, 'select#max_rel option', config.max); fields.vcsLabel = config.branch; fields.uploadOpt = 'vcs'; fields.title = config.version; console.log('submitting new release...'); $ = await cdblib.fetch(uri, 'post', fields, { follow_max: 10 }); } const delay = n => new Promise(r => setTimeout(r, n * 1000)); module.exports = async () => { console.log('checking if new version is already released...'); if(await checkalready()) { console.log('version ' + config.version + ' already released'); return; } await cdblib.login(); console.log('rechecking if new version is already pending...'); if(await checkalready()) { console.log('version ' + config.version + ' already released'); return; } await makerelease(); console.log('waiting for new submission to be listed...'); for(let i = 0; i < config.timeout; i += config.poll) { if(await checkalready()) { console.log('version ' + config.version + ' release successful'); return true; } await delay(config.poll); } throw Error('timed out awaiting new release listing'); };