diff --git a/CHANGELOG b/CHANGELOG.md similarity index 58% rename from CHANGELOG rename to CHANGELOG.md index f97b452..9418940 100644 --- a/CHANGELOG +++ b/CHANGELOG.md @@ -1,3 +1,22 @@ +# NEXT: +## Breaking changes +- +## Added features +- +## Fixes and improvements +- + +# 1.8.0 + +## Added features +- Add `post` command +## Fixes and improvements +- Don't print command when selecting default +- `public` excluded from service groups +- Fix bug where fetch would not initialize the git repos +- Make `clone` command slightly faster +- Add `.md` to `CHANGELOG` for better syntax highlighting + # 1.7.0 - Add $broadcast and $join diff --git a/config.js b/config.js index 0ed5290..ec2b386 100644 --- a/config.js +++ b/config.js @@ -1,7 +1,9 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.GIT_HOST = exports.SSH_USER = exports.SSH_HOST = exports.HTTP_HOST = exports.API_URL = void 0; -exports.API_URL = `api.merrymake.io`; +exports.GIT_HOST = exports.SSH_USER = exports.SSH_HOST = exports.HTTP_HOST = exports.RAPIDS_HOST = exports.API_URL = exports.MERRYMAKE_IO = void 0; +exports.MERRYMAKE_IO = `merrymake.io`; +exports.API_URL = `api.${exports.MERRYMAKE_IO}`; +exports.RAPIDS_HOST = `https://rapids.${exports.MERRYMAKE_IO}`; exports.HTTP_HOST = `https://${exports.API_URL}`; exports.SSH_HOST = `${exports.API_URL}`; exports.SSH_USER = `mist`; diff --git a/config.ts b/config.ts index df10c4e..6ca6a1f 100644 --- a/config.ts +++ b/config.ts @@ -1,4 +1,6 @@ -export const API_URL = `api.merrymake.io`; +export const MERRYMAKE_IO = `merrymake.io`; +export const API_URL = `api.${MERRYMAKE_IO}`; +export const RAPIDS_HOST = `https://rapids.${MERRYMAKE_IO}`; export const HTTP_HOST = `https://${API_URL}`; export const SSH_HOST = `${API_URL}`; diff --git a/dist/windows.zip b/dist/windows.zip index fd4f3b8..524afa6 100644 Binary files a/dist/windows.zip and b/dist/windows.zip differ diff --git a/executors.js b/executors.js index 2b2bd94..73f5042 100644 --- a/executors.js +++ b/executors.js @@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.do_help = exports.do_queue_time = exports.printTableHeader = exports.alignLeft = exports.alignRight = exports.do_cron = exports.do_event = exports.do_envvar = exports.do_key = exports.do_inspect = exports.do_build = exports.do_redeploy = exports.do_deploy = exports.generateNewKey = exports.useExistingKey = exports.do_register = exports.addKnownHost = exports.do_duplicate = exports.fetch_template = exports.createService = exports.createServiceGroup = exports.createOrganization = exports.do_clone = exports.do_fetch = void 0; +exports.do_post = exports.do_help = exports.do_queue_time = exports.printTableHeader = exports.alignLeft = exports.alignRight = exports.do_cron = exports.do_event = exports.do_envvar = exports.do_key = exports.do_inspect = exports.do_build = exports.do_redeploy = exports.do_deploy = exports.generateNewKey = exports.useExistingKey = exports.do_register = exports.addKnownHost = exports.do_duplicate = exports.fetch_template = exports.createService = exports.createServiceGroup = exports.createOrganization = exports.do_clone = exports.do_fetch = void 0; const fs_1 = __importDefault(require("fs")); const os_1 = __importDefault(require("os")); const utils_1 = require("./utils"); @@ -20,7 +20,6 @@ const config_1 = require("./config"); const detect_project_type_1 = require("@merrymake/detect-project-type"); const child_process_1 = require("child_process"); const prompt_1 = require("./prompt"); -const path_1 = __importDefault(require("path")); const args_1 = require("./args"); function clone(struct, name) { return __awaiter(this, void 0, void 0, function* () { @@ -35,7 +34,7 @@ function clone(struct, name) { yield (0, utils_1.execPromise)(`git init --initial-branch=main`, dir); yield (0, utils_1.execPromise)(`git remote add origin "${config_1.GIT_HOST}/${name}/public"`, dir); // await execPromise(`git fetch`, dir); - fetch(".", name, struct); + fetch(`./${name}`, name, struct); } catch (e) { throw e; @@ -45,10 +44,12 @@ function clone(struct, name) { function fetch(prefix, org, struct) { return __awaiter(this, void 0, void 0, function* () { try { - Object.keys(struct).forEach((team) => { - fs_1.default.mkdirSync(`${prefix}/${org}/${team}`, { recursive: true }); - createFolderStructure(struct[team], `${prefix}/${org}/${team}`, org, team); - }); + let keys = Object.keys(struct); + for (let i = 0; i < keys.length; i++) { + let group = keys[i]; + fs_1.default.mkdirSync(`${prefix}/${group}`, { recursive: true }); + yield createFolderStructure(struct[group], `${prefix}/${group}`, org, group); + } } catch (e) { throw e; @@ -66,7 +67,7 @@ function do_fetch() { } (0, utils_1.output2)(`Fetching...`); let structure = JSON.parse(reply); - yield fetch(path_1.default.join(org.pathToRoot, ".."), org.org.name, structure); + yield fetch(org.pathToRoot, org.org.name, structure); } catch (e) { throw e; @@ -75,34 +76,51 @@ function do_fetch() { } exports.do_fetch = do_fetch; function createFolderStructure(struct, prefix, org, team) { - Object.keys(struct).forEach((k) => __awaiter(this, void 0, void 0, function* () { - if (struct[k] instanceof Object) - createFolderStructure(struct[k], prefix + "/" + k, org, team); - else { - // output(`git clone "${HOST}/${org}/${team}/${k}" "${prefix}/${k}"`); - let repo = `"${config_1.GIT_HOST}/${org}/${team}/${k}"`; - let dir = `${prefix}/${k}`; - try { - if (!fs_1.default.existsSync(dir)) { - fs_1.default.mkdirSync(dir, { recursive: true }); - yield (0, utils_1.execPromise)(`git init --initial-branch=main`, dir); - yield (0, utils_1.execPromise)(`git remote add origin ${repo}`, dir); - yield fs_1.default.writeFile(dir + "/fetch.bat", `@echo off + return __awaiter(this, void 0, void 0, function* () { + try { + let keys = Object.keys(struct); + for (let i = 0; i < keys.length; i++) { + let k = keys[i]; + if (struct[k] instanceof Object) + yield createFolderStructure(struct[k], prefix + "/" + k, org, team); + else { + // output(`git clone "${HOST}/${org}/${team}/${k}" "${prefix}/${k}"`); + let repo = `"${config_1.GIT_HOST}/${org}/${team}/${k}"`; + let dir = `${prefix}/${k}`; + try { + if (!fs_1.default.existsSync(dir)) { + fs_1.default.mkdirSync(dir, { recursive: true }); + } + if (!fs_1.default.existsSync(dir + "/.git")) { + (0, utils_1.output2)("Here1 " + dir); + yield (0, utils_1.execPromise)(`git init --initial-branch=main`, dir); + (0, utils_1.output2)("Here2 " + dir); + yield (0, utils_1.execPromise)(`git remote add origin ${repo}`, dir); + (0, utils_1.output2)("Here3 " + dir); + fs_1.default.writeFileSync(dir + "/fetch.bat", `@echo off git fetch git reset --hard origin/main del fetch.sh -(goto) 2>nul & del fetch.bat`, () => { }); - yield fs_1.default.writeFile(dir + "/fetch.sh", `#!/bin/sh +(goto) 2>nul & del fetch.bat`); + fs_1.default.writeFileSync(dir + "/fetch.sh", `#!/bin/sh git fetch git reset --hard origin/main -rm fetch.bat fetch.sh`, () => { }); +rm fetch.bat fetch.sh`); + } + else { + yield (0, utils_1.execPromise)(`git remote set-url origin ${repo}`, dir); + } + } + catch (e) { + console.log(e); + } } } - catch (e) { - console.log(e); - } } - })); + catch (e) { + throw e; + } + }); } function do_clone(name) { return __awaiter(this, void 0, void 0, function* () { @@ -227,10 +245,10 @@ function do_register(keyAction, email) { let key = yield keyAction(); console.log("Registering..."); addKnownHost(); - let result = yield (0, utils_1.urlReq)(`${config_1.HTTP_HOST}/admin/user`, "POST", { + let result = yield (0, utils_1.urlReq)(`${config_1.HTTP_HOST}/admin/user`, "POST", JSON.stringify({ email, key, - }); + })); if (/^\d+$/.test(result.body)) { (0, utils_1.saveCache)({ registered: true, hasOrgs: +result.body > 0 }); (0, utils_1.output2)("Registered user."); @@ -549,3 +567,15 @@ function do_help() { }); } exports.do_help = do_help; +function do_post(eventType, key, contentType, payload) { + return __awaiter(this, void 0, void 0, function* () { + try { + let resp = yield (0, utils_1.urlReq)(`${config_1.RAPIDS_HOST}/${key}/${eventType}`, "POST", payload, contentType); + (0, utils_1.output2)(resp.body); + } + catch (e) { + throw e; + } + }); +} +exports.do_post = do_post; diff --git a/executors.ts b/executors.ts index 9c1c2c6..aedc237 100644 --- a/executors.ts +++ b/executors.ts @@ -14,7 +14,7 @@ import { getCache, fetchOrgRaw, } from "./utils"; -import { API_URL, GIT_HOST, HTTP_HOST, SSH_USER } from "./config"; +import { API_URL, GIT_HOST, HTTP_HOST, RAPIDS_HOST, SSH_USER } from "./config"; import { detectProjectType, BUILD_SCRIPT_MAKERS, @@ -42,7 +42,7 @@ async function clone(struct: any, name: string) { dir ); // await execPromise(`git fetch`, dir); - fetch(".", name, struct); + fetch(`./${name}`, name, struct); } catch (e) { throw e; } @@ -50,15 +50,17 @@ async function clone(struct: any, name: string) { async function fetch(prefix: string, org: string, struct: any) { try { - Object.keys(struct).forEach((team) => { - fs.mkdirSync(`${prefix}/${org}/${team}`, { recursive: true }); - createFolderStructure( - struct[team], - `${prefix}/${org}/${team}`, + let keys = Object.keys(struct); + for (let i = 0; i < keys.length; i++) { + let group = keys[i]; + fs.mkdirSync(`${prefix}/${group}`, { recursive: true }); + await createFolderStructure( + struct[group], + `${prefix}/${group}`, org, - team + group ); - }); + } } catch (e) { throw e; } @@ -74,53 +76,64 @@ export async function do_fetch() { } output2(`Fetching...`); let structure = JSON.parse(reply); - await fetch(path.join(org.pathToRoot, ".."), org.org.name, structure); + await fetch(org.pathToRoot, org.org.name, structure); } catch (e) { throw e; } } -function createFolderStructure( +async function createFolderStructure( struct: any, prefix: string, org: string, team: string ) { - Object.keys(struct).forEach(async (k) => { - if (struct[k] instanceof Object) - createFolderStructure(struct[k], prefix + "/" + k, org, team); - else { - // output(`git clone "${HOST}/${org}/${team}/${k}" "${prefix}/${k}"`); - let repo = `"${GIT_HOST}/${org}/${team}/${k}"`; - let dir = `${prefix}/${k}`; - try { - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir, { recursive: true }); - await execPromise(`git init --initial-branch=main`, dir); - await execPromise(`git remote add origin ${repo}`, dir); - await fs.writeFile( - dir + "/fetch.bat", - `@echo off + try { + let keys = Object.keys(struct); + for (let i = 0; i < keys.length; i++) { + let k = keys[i]; + if (struct[k] instanceof Object) + await createFolderStructure(struct[k], prefix + "/" + k, org, team); + else { + // output(`git clone "${HOST}/${org}/${team}/${k}" "${prefix}/${k}"`); + let repo = `"${GIT_HOST}/${org}/${team}/${k}"`; + let dir = `${prefix}/${k}`; + try { + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + if (!fs.existsSync(dir + "/.git")) { + output2("Here1 " + dir); + await execPromise(`git init --initial-branch=main`, dir); + output2("Here2 " + dir); + await execPromise(`git remote add origin ${repo}`, dir); + output2("Here3 " + dir); + fs.writeFileSync( + dir + "/fetch.bat", + `@echo off git fetch git reset --hard origin/main del fetch.sh -(goto) 2>nul & del fetch.bat`, - () => {} - ); - await fs.writeFile( - dir + "/fetch.sh", - `#!/bin/sh +(goto) 2>nul & del fetch.bat` + ); + fs.writeFileSync( + dir + "/fetch.sh", + `#!/bin/sh git fetch git reset --hard origin/main -rm fetch.bat fetch.sh`, - () => {} - ); +rm fetch.bat fetch.sh` + ); + } else { + await execPromise(`git remote set-url origin ${repo}`, dir); + } + } catch (e) { + console.log(e); } - } catch (e) { - console.log(e); } } - }); + } catch (e) { + throw e; + } } export async function do_clone(name: string) { @@ -262,10 +275,14 @@ export async function do_register( let key = await keyAction(); console.log("Registering..."); addKnownHost(); - let result = await urlReq(`${HTTP_HOST}/admin/user`, "POST", { - email, - key, - }); + let result = await urlReq( + `${HTTP_HOST}/admin/user`, + "POST", + JSON.stringify({ + email, + key, + }) + ); if (/^\d+$/.test(result.body)) { saveCache({ registered: true, hasOrgs: +result.body > 0 }); output2("Registered user."); @@ -637,3 +654,22 @@ export async function do_help() { output2(`${GREEN}Inside service repo.${NORMAL_COLOR}`); } } + +export async function do_post( + eventType: string, + key: string, + contentType: string, + payload: string +) { + try { + let resp = await urlReq( + `${RAPIDS_HOST}/${key}/${eventType}`, + "POST", + payload, + contentType + ); + output2(resp.body); + } catch (e) { + throw e; + } +} diff --git a/package.json b/package.json index 9a4cea6..9d84fa8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@merrymake/cli", - "version": "1.7.0", + "version": "1.8.0", "description": "", "main": "index.js", "scripts": { diff --git a/prompt.js b/prompt.js index 2ce40e1..f8d12e3 100644 --- a/prompt.js +++ b/prompt.js @@ -106,7 +106,9 @@ function choice(options, invertedQuiet = { cmd: false, select: true }, def = 0) if (options.length === 1) { if ((0, args_1.getArgs)().length > 0) (0, args_1.getArgs)().splice(0, 1); - resolve(makeSelection(options[0])); + resolve(invertedQuiet.cmd + ? makeSelection(options[0]) + : makeSelectionQuietly(options[0])); return; } options.push({ diff --git a/prompt.ts b/prompt.ts index 08f940b..c594222 100644 --- a/prompt.ts +++ b/prompt.ts @@ -129,7 +129,11 @@ export function choice( let str: string[] = []; if (options.length === 1) { if (getArgs().length > 0) getArgs().splice(0, 1); - resolve(makeSelection(options[0])); + resolve( + invertedQuiet.cmd + ? makeSelection(options[0]) + : makeSelectionQuietly(options[0]) + ); return; } options.push({ diff --git a/questions.js b/questions.js index 8c0442f..97506ae 100644 --- a/questions.js +++ b/questions.js @@ -248,6 +248,10 @@ function queue_event(org, id, river) { function checkout() { return __awaiter(this, void 0, void 0, function* () { try { + if ((0, args_1.getArgs)().length > 0 && (0, args_1.getArgs)()[0] !== "_") { + let org = (0, args_1.getArgs)().splice(0, 1)[0]; + return yield checkout_org(org); + } let resp = yield (0, utils_1.sshReq)(`list-organizations`); let orgs = JSON.parse(resp); return yield (0, prompt_1.choice)(orgs.map((x) => ({ @@ -460,6 +464,78 @@ function keys(org) { } }); } +function post_event_key_payload(eventType, key, contentType, payload) { + (0, utils_1.addToExecuteQueue)(() => (0, executors_1.do_post)(eventType, key, contentType, payload)); + return (0, utils_1.finish)(); +} +function post_event_key_payloadType(eventType, key, contentType) { + return __awaiter(this, void 0, void 0, function* () { + try { + let payload = yield (0, prompt_1.shortText)("Payload", "The data to be attached to the request", ""); + return post_event_key_payload(eventType, key, contentType, payload); + } + catch (e) { + throw e; + } + }); +} +function post_event_key(eventType, key) { + return (0, prompt_1.choice)([ + { + long: "empty", + short: "e", + text: "empty message, ie. no payload", + action: () => post_event_key_payload(eventType, key, `plain/text`, ``), + }, + { + long: "text", + short: "t", + text: "attach plain text payload", + action: () => post_event_key_payloadType(eventType, key, `plain/text`), + }, + { + long: "json", + short: "j", + text: "attach json payload", + action: () => post_event_key_payloadType(eventType, key, `application/json`), + }, + ]); +} +function post_event(org, eventType) { + return __awaiter(this, void 0, void 0, function* () { + try { + if ((0, args_1.getArgs)().length > 0 && (0, args_1.getArgs)()[0] !== "_") { + let key = (0, args_1.getArgs)().splice(0, 1)[0]; + return yield post_event_key(eventType, key); + } + let resp = yield (0, utils_1.sshReq)(`list-keys`, `--org`, org, `--active`); + let keys = JSON.parse(resp); + let options = keys.map((x) => { + let n = x.name ? ` (${x.name})` : ""; + return { + long: x.key, + text: `${x.key}${n}`, + action: () => post_event_key(eventType, x.key), + }; + }); + return yield (0, prompt_1.choice)(options).then((x) => x); + } + catch (e) { + throw e; + } + }); +} +function post(org) { + return __awaiter(this, void 0, void 0, function* () { + try { + let eventType = yield (0, prompt_1.shortText)("Event type", "The type of event to post", "hello"); + return post_event(org, eventType); + } + catch (e) { + throw e; + } + }); +} function event_key_event(org, key, event, create) { (0, utils_1.addToExecuteQueue)(() => (0, executors_1.do_event)(org, key, event, create)); return (0, utils_1.finish)(); @@ -665,6 +741,7 @@ function sim() { (0, utils_1.addToExecuteQueue)(() => new simulator_1.Run(3000).execute()); return (0, utils_1.finish)(); } +const SPECIAL_FOLDERS = ["event-catalogue", "public"]; function start() { return __awaiter(this, void 0, void 0, function* () { try { @@ -676,7 +753,8 @@ function start() { let selectedGroup = null; let struct = { org: rawStruct.org, - serviceGroup: rawStruct.serviceGroup !== "event-catalogue" + serviceGroup: rawStruct.serviceGroup !== null && + !SPECIAL_FOLDERS.includes(rawStruct.serviceGroup) ? rawStruct.serviceGroup : null, inEventCatalogue: rawStruct.serviceGroup === "event-catalogue", @@ -689,7 +767,10 @@ function start() { }; } else { - let serviceGroups = (0, utils_1.directoryNames)(new utils_2.Path(), ["event-catalogue"]); + let serviceGroups = (0, utils_1.directoryNames)(new utils_2.Path(), [ + "event-catalogue", + "public", + ]); if (serviceGroups.length === 1) { selectedGroup = { name: serviceGroups[0].name, @@ -784,6 +865,12 @@ function start() { text: "add or edit api-keys for the organization", action: () => keys(orgName), }); + options.push({ + long: "post", + short: "p", + text: "post message to Rapids using an api-key", + action: () => post(orgName), + }); options.push({ long: "event", short: "v", diff --git a/questions.ts b/questions.ts index 69b624b..27a2d24 100644 --- a/questions.ts +++ b/questions.ts @@ -45,6 +45,7 @@ import { alignLeft, do_event, do_help, + do_post, } from "./executors"; import { VERSION_CMD, type ProjectType } from "@merrymake/detect-project-type"; import { execSync } from "child_process"; @@ -324,6 +325,10 @@ function queue_event(org: string, id: string, river: string) { async function checkout() { try { + if (getArgs().length > 0 && getArgs()[0] !== "_") { + let org = getArgs().splice(0, 1)[0]; + return await checkout_org(org); + } let resp = await sshReq(`list-organizations`); let orgs: string[] = JSON.parse(resp); return await choice( @@ -637,6 +642,92 @@ async function keys(org: string) { } } +function post_event_key_payload( + eventType: string, + key: string, + contentType: string, + payload: string +) { + addToExecuteQueue(() => do_post(eventType, key, contentType, payload)); + return finish(); +} + +async function post_event_key_payloadType( + eventType: string, + key: string, + contentType: string +) { + try { + let payload = await shortText( + "Payload", + "The data to be attached to the request", + "" + ); + return post_event_key_payload(eventType, key, contentType, payload); + } catch (e) { + throw e; + } +} + +function post_event_key(eventType: string, key: string) { + return choice([ + { + long: "empty", + short: "e", + text: "empty message, ie. no payload", + action: () => post_event_key_payload(eventType, key, `plain/text`, ``), + }, + { + long: "text", + short: "t", + text: "attach plain text payload", + action: () => post_event_key_payloadType(eventType, key, `plain/text`), + }, + { + long: "json", + short: "j", + text: "attach json payload", + action: () => + post_event_key_payloadType(eventType, key, `application/json`), + }, + ]); +} + +async function post_event(org: string, eventType: string) { + try { + if (getArgs().length > 0 && getArgs()[0] !== "_") { + let key = getArgs().splice(0, 1)[0]; + return await post_event_key(eventType, key); + } + let resp = await sshReq(`list-keys`, `--org`, org, `--active`); + let keys: { name: string; key: string }[] = JSON.parse(resp); + let options: Option[] = keys.map((x) => { + let n = x.name ? ` (${x.name})` : ""; + return { + long: x.key, + text: `${x.key}${n}`, + action: () => post_event_key(eventType, x.key), + }; + }); + return await choice(options).then((x) => x); + } catch (e) { + throw e; + } +} + +async function post(org: string) { + try { + let eventType = await shortText( + "Event type", + "The type of event to post", + "hello" + ); + return post_event(org, eventType); + } catch (e) { + throw e; + } +} + function event_key_event( org: string, key: string, @@ -878,6 +969,7 @@ function sim() { return finish(); } +const SPECIAL_FOLDERS = ["event-catalogue", "public"]; export async function start() { try { let rawStruct = fetchOrgRaw(); @@ -889,7 +981,8 @@ export async function start() { let struct = { org: rawStruct.org, serviceGroup: - rawStruct.serviceGroup !== "event-catalogue" + rawStruct.serviceGroup !== null && + !SPECIAL_FOLDERS.includes(rawStruct.serviceGroup) ? rawStruct.serviceGroup : null, inEventCatalogue: rawStruct.serviceGroup === "event-catalogue", @@ -901,7 +994,10 @@ export async function start() { path: new Path(struct.pathToRoot).withoutLastUp(), }; } else { - let serviceGroups = directoryNames(new Path(), ["event-catalogue"]); + let serviceGroups = directoryNames(new Path(), [ + "event-catalogue", + "public", + ]); if (serviceGroups.length === 1) { selectedGroup = { name: serviceGroups[0].name, @@ -1006,6 +1102,12 @@ export async function start() { text: "add or edit api-keys for the organization", action: () => keys(orgName), }); + options.push({ + long: "post", + short: "p", + text: "post message to Rapids using an api-key", + action: () => post(orgName), + }); options.push({ long: "event", short: "v", diff --git a/utils.js b/utils.js index 90fed14..1ea03ba 100644 --- a/utils.js +++ b/utils.js @@ -194,7 +194,6 @@ function versionIsOlder(old, new_) { return false; } function execPromise(cmd, cwd) { - // output("Executing", cmd); return new Promise((resolve, reject) => { (0, child_process_1.exec)(cmd, { cwd }, (error, stdout, stderr) => { let err = (error === null || error === void 0 ? void 0 : error.message) || stderr; @@ -282,16 +281,15 @@ function partition(str, radix) { return [str.substring(0, index), str.substring(index + radix.length)]; } exports.partition = partition; -function urlReq(url, method = "GET", body) { +function urlReq(url, method = "GET", data, contentType = "application/json") { return new Promise((resolve, reject) => { let [protocol, fullPath] = url.indexOf("://") >= 0 ? partition(url, "://") : ["http", url]; let [base, path] = partition(fullPath, "/"); let [host, port] = partition(base, ":"); - let data = JSON.stringify(body); let headers; - if (body !== undefined) + if (data !== undefined) headers = { - "Content-Type": "application/json", + "Content-Type": contentType, "Content-Length": data.length, }; let sender = protocol === "http" ? http_1.default : https_1.default; diff --git a/utils.ts b/utils.ts index 5bce793..b1d4472 100644 --- a/utils.ts +++ b/utils.ts @@ -161,7 +161,6 @@ function versionIsOlder(old: string, new_: string) { } export function execPromise(cmd: string, cwd?: string) { - // output("Executing", cmd); return new Promise((resolve, reject) => { exec(cmd, { cwd }, (error, stdout, stderr) => { let err = error?.message || stderr; @@ -247,7 +246,8 @@ export function partition(str: string, radix: string) { export function urlReq( url: string, method: "POST" | "GET" = "GET", - body?: any + data?: string, + contentType = "application/json" ) { return new Promise<{ body: string; code: number | undefined }>( (resolve, reject) => { @@ -255,11 +255,10 @@ export function urlReq( url.indexOf("://") >= 0 ? partition(url, "://") : ["http", url]; let [base, path] = partition(fullPath, "/"); let [host, port] = partition(base, ":"); - let data = JSON.stringify(body); let headers; - if (body !== undefined) + if (data !== undefined) headers = { - "Content-Type": "application/json", + "Content-Type": contentType, "Content-Length": data.length, }; let sender = protocol === "http" ? http : https;