Last active
November 15, 2018 08:57
-
-
Save ilyaigpetrov/369dd9a8b1c22b594258cfd7e37faf92 to your computer and use it in GitHub Desktop.
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
const https = require('https') | |
/* | |
const argv = require('yargs') | |
.option('token', { | |
alias: 't', | |
describe: 'api token', | |
demand: true, | |
type: 'string', | |
}) | |
.option('name', { | |
alias: 'n', | |
describe: 'deployment name', | |
demand: true, | |
type: 'string', | |
}) | |
.parse(process.argv); | |
*/ | |
function getDeploymentKey(token, name) { | |
return new Promise((resolve, reject) => { | |
let data = ""; | |
const req = https.request({ | |
method: 'GET', | |
headers: { | |
accept: 'application/json', | |
authorization: `Bearer ${token}`, | |
}, | |
hostname: 'api.netlify.com', | |
path: `/api/v1/sites?name=${name}&filter=all`, | |
}, (res) => { | |
res.on('data', (chunk) => { | |
data += chunk; | |
}); | |
res.on('end', () => { | |
resolve(JSON.parse(data)); | |
}) | |
res.on('error', (err) => { | |
reject(err); | |
}) | |
}); | |
req.end(); | |
}).then((data) => { | |
data = data.filter((item) => item.name === name)[0]; | |
if (!data) { | |
return Promise.reject(new Error(`Unable to find site ${name}`)); | |
} | |
const deploymentKey = data.build_settings.deploy_key_id; | |
// console.log(`Found deployment key id ${deploymentKey}`); | |
return new Promise((resolve, reject) => { | |
let data = ""; | |
const req = https.request({ | |
method: 'GET', | |
headers: { | |
accept: 'application/json', | |
authorization: `Bearer ${token}`, | |
}, | |
hostname: 'api.netlify.com', | |
path: `/api/v1/deploy_keys/${deploymentKey}`, | |
}, (res) => { | |
res.on('data', (chunk) => { | |
data += chunk; | |
}); | |
res.on('end', () => { | |
resolve(JSON.parse(data).public_key); | |
}) | |
res.on('error', (err) => { | |
reject(err); | |
}) | |
}); | |
req.end(); | |
}); | |
}); | |
} | |
getDeploymentKey('<NETLIFY_API_TOKEN>', '<SITE_NAME>') | |
.then((key) => { | |
console.log(key); | |
process.exit(0); | |
}) | |
.catch((err) => { | |
console.error(err); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment