UNPKG

@modyo/cli

Version:

Modyo CLI Command line to expose local development tools

41 lines (40 loc) 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const debug_1 = tslib_1.__importDefault(require("debug")); const api_client_1 = tslib_1.__importDefault(require("../utils/api-client")); const modyo_api_paths_1 = require("./modyo-api-paths"); const debug = (0, debug_1.default)('get/get-modyo-site-id.ts'); const debugError = (0, debug_1.default)('error::get/get-modyo-site-id.ts'); async function fetchSite(url, token) { const response = await api_client_1.default.get(url, { headers: { Authorization: `Bearer ${token}` } }); if (response.status === 401) { throw new Error('Unauthorized, please check your Modyo Api Access Token'); } const { data } = response; return data.sites; } async function getModyoSiteId(accountUrl, siteHost, token, version) { const query = version === '9' || version === '10' ? `?query=${siteHost}` : '?paginate=false'; const url = `${accountUrl}/${modyo_api_paths_1.MODYO_API_PATH[version]}/sites${query}`; core_1.ux.action.start('Getting Site Id'); try { debug(`fetching from ${url} with ${token}`); let sites = await fetchSite(url, token); if (sites.length === 0) { sites = await fetchSite(`${url}&status=staging`, token); } const site = sites.find((currentSite) => currentSite.host === siteHost); if (!site) { throw new Error('Check whether the site host or the page status (e.g. Development - Published)'); } core_1.ux.action.stop(`${site.id}`); return site.id; } catch (error) { debugError(error); throw error; } } exports.default = getModyoSiteId;