cli: switch to axios

This commit is contained in:
Thomas Hobson 2021-03-13 16:44:33 +13:00
parent a1536ffe56
commit 5a05537a7f
No known key found for this signature in database
GPG Key ID: 9F1FD9D87950DB6F
6 changed files with 48 additions and 46 deletions

View File

@ -1,4 +1,4 @@
const fetch = require('node-fetch');
//const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
@ -59,14 +59,8 @@ exports.handler = async function(argv){
run_timeout: argv.rt
};
const response = await fetch(argv['piston-url'] + '/jobs', {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(request)
})
.then(res=>res.json())
.then(res=>{if(res.data)return res.data; throw new Error(res.message)})
.catch(x=>x);
let response = await argv.axios.post('/jobs', request);
response = response.data
function step(name, ctx){
console.log(chalk.bold(`== ${name} ==`))

View File

@ -1,4 +1,3 @@
const fetch = require('node-fetch');
const chalk = require('chalk');
exports.command = ['install <language> <language-version>']
@ -13,15 +12,12 @@ const msg_format = {
}
exports.handler = async function(argv){
const install = await fetch(
`${argv['piston-url']}/packages/${argv['language']}/${argv['language-version']}`,
{method: 'post'}
)
.then(res=>res.json())
.then(res=>{if(res.data)return res.data; throw new Error(res.message)})
.catch(x=>x);
console.log(msg_format.color(install));
exports.handler = async function({axios, language, languageVersion}){
try{
const install = await axios.post(`/packages/${language}/${languageVersion}`)
console.log(msg_format.color(install.data));
}catch({response}){
console.error(response.data.message)
}
}

View File

@ -1,4 +1,4 @@
const fetch = require('node-fetch');
//const fetch = require('node-fetch');
const chalk = require('chalk');
exports.command = ['list']
@ -13,19 +13,12 @@ const msg_format = {
}
exports.handler = async function(argv){
const api = new PistonEngine(argv['piston-url']);
exports.handler = async function({axios}){
const packages = await api.list_packages();
const packages = await axios.get('/packages');
const packages = await fetch(argv['piston-url'] + '/packages')
.then(res=>res.json())
.then(res=>{if(res.data)return res.data; throw new Error(res.message)});
.then(res=>res.packages)
.catch(x=>x)
const pkg_msg = packages
const pkg_msg = packages.data.data.packages
.map(msg_format.color)
.join('\n');

View File

@ -1,4 +1,15 @@
#!/usr/bin/env node
const axios = require('axios').default;
const axios_instance = function(argv){
argv.axios = axios.create({
baseURL: argv['piston-url']
});
return argv;
};
require('yargs')(process.argv.slice(2))
.option('piston-url', {
alias: ['u'],
@ -6,9 +17,10 @@ require('yargs')(process.argv.slice(2))
desc: 'Piston API URL',
string: true
})
.middleware(axios_instance)
.scriptName("piston")
.commandDir('commands')
.demandCommand()
.help()
.wrap(72)
.argv
.argv;

View File

@ -6,8 +6,8 @@
"author": "Thomas Hobson <thomas@hexf.me>",
"license": "MIT",
"dependencies": {
"axios": "^0.21.1",
"chalk": "^4.1.0",
"node-fetch": "^2.6.1",
"yargs": "^16.2.0"
}
}

View File

@ -14,6 +14,13 @@ ansi-styles@^4.0.0, ansi-styles@^4.1.0:
dependencies:
color-convert "^2.0.1"
axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
dependencies:
follow-redirects "^1.10.0"
chalk@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
@ -53,6 +60,11 @@ escalade@^3.1.1:
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
integrity sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==
follow-redirects@^1.10.0:
version "1.13.3"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.3.tgz#e5598ad50174c1bc4e872301e82ac2cd97f90267"
integrity sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==
get-caller-file@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
@ -68,20 +80,15 @@ is-fullwidth-code-point@^3.0.0:
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d"
integrity sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==
node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
require-directory@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
integrity sha1-jGStX9MNqxyXbiNE/+f3kqam30I=
string-width@^4.1.0, string-width@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.0.tgz#952182c46cc7b2c313d1596e623992bd163b72b5"
integrity sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==
version "4.2.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.2.tgz#dafd4f9559a7585cfba529c6a0a4f73488ebd4c5"
integrity sha512-XBJbT3N4JhVumXE0eoLU9DCjcaF92KLNqTmFCnG1pf8duUxFGwtP6AD6nkjw9a3IdiRtL3E2w3JDiE/xi3vOeA==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
@ -116,9 +123,9 @@ y18n@^5.0.5:
integrity sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==
yargs-parser@^20.2.2:
version "20.2.6"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.6.tgz#69f920addf61aafc0b8b89002f5d66e28f2d8b20"
integrity sha512-AP1+fQIWSM/sMiET8fyayjx/J+JmTPt2Mr0FkrgqB4todtfa53sOsrSAcIrJRD5XS20bKUwaDIuMkWKCEiQLKA==
version "20.2.7"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.7.tgz#61df85c113edfb5a7a4e36eb8aa60ef423cbc90a"
integrity sha512-FiNkvbeHzB/syOjIUxFDCnhSfzAL8R5vs40MgLFBorXACCOAEaWu0gRZl14vG8MR9AOJIZbmkjhusqBYZ3HTHw==
yargs@^16.2.0:
version "16.2.0"