UNPKG

@adpt/cli

Version:
157 lines 6.77 kB
"use strict"; /* * Copyright 2018-2019 Unbounded Systems, LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const utils_1 = require("@adpt/utils"); const command_1 = require("@oclif/command"); const listr_1 = tslib_1.__importDefault(require("@unboundedsystems/listr")); const fs = tslib_1.__importStar(require("fs-extra")); const path = tslib_1.__importStar(require("path")); const ui_1 = require("../ui"); const adapt_base_1 = require("./adapt_base"); const proj_1 = require("../proj"); exports.cantDeploy = "This project cannot be deployed.\n"; class DeployBase extends adapt_base_1.AdaptBase { get tasks() { if (!this.tasks_) throw new Error(`Internal error: cannot access tasks before init`); return this.tasks_; } async init() { await super.init(); this.parse(); const f = this.flags(DeployBase); this.tasks_ = new listr_1.default(this.outputSettings.listrOptions); let adaptUrl; if (f.serverUrl) { adaptUrl = f.serverUrl; } else { const dbFile = path.join(this.config.dataDir, "local_deploy"); adaptUrl = utils_1.filePathToUrl(dbFile); } const pair = adapt_base_1.createLoggerPair("deploy", this.outputSettings.logging); this.ctx = Object.assign({ adaptUrl, debug: f.debug || "" }, pair); if (f.rootFile) { const projectFile = path.resolve(f.rootFile); if (await fs.pathExists(projectFile)) { this.ctx.projectFile = projectFile; } } } } DeployBase.flags = Object.assign({}, adapt_base_1.AdaptBase.flags, { debug: command_1.flags.string({ char: "d", description: "Enable additional debug output. Should be a comma-separated " + "list of debug flags. Valid debug flags are: build", default: "", helpValue: "debugFlags", }), serverUrl: command_1.flags.string({ description: "URL of Adapt server. Defaults to using local system.", env: "ADAPT_SERVER_URL", }), rootFile: command_1.flags.string({ description: "Project description file to deploy (.ts or .tsx)", default: "index.tsx", }) }); exports.DeployBase = DeployBase; class DeployOpBase extends DeployBase { async init() { await super.init(); const stackName = this.args.stackName; const cacheDir = path.join(this.config.cacheDir, "npmcache"); const { rootFile, dryRun = false, registry } = this.flags(DeployOpBase); if (rootFile == null) throw new Error(`Internal error: rootFile cannot be null`); this.ctx = Object.assign({}, this.ctx, { dryRun, stackName }); this.tasks.add([ { title: "Installing node modules", task: async () => { if (!await fs.pathExists(this.ctx.projectFile)) { this.error(`Project file '${rootFile}' does not exist`); } const projectRoot = path.dirname(this.ctx.projectFile); await fs.ensureDir(cacheDir); const session = { cacheDir, projectDir: projectRoot, }; const projOpts = { session, }; try { if (registry) projOpts.registry = registry; this.ctx.project = await proj_1.load(projectRoot, projOpts); } catch (err) { if (err.code === "ENOPACKAGEJSON") { this.error(exports.cantDeploy + `The directory '${projectRoot}' does not contain a ` + `package.json file`); } throw err; } const ret = this.ctx.project.installModules(); if (!ret) return; return ui_1.taskObservable(ret.stdout, ret); } }, { title: "Validating project", task: async () => { const project = this.ctx.project; if (project == null) { throw new Error(`Internal error: project is null`); } const gen = proj_1.getGen(project); if (!gen.matchInfo.matches) { this.error(exports.cantDeploy + `The following updates must be made:\n` + gen.matchInfo.required.map((ui) => " " + ui.message).join("\n")); } } } ]); } isDeploySuccess(response, options = {}) { return this.isApiSuccess(response, Object.assign({ errorStart: exports.cantDeploy, action: "deploy" }, options)); } deployInformation(deployStatus) { const needsData = []; for (const observerName in deployStatus.needsData) { if (!Object.hasOwnProperty.call(deployStatus.needsData, observerName)) continue; const queries = deployStatus.needsData[observerName]; const queryMsgs = queries.map((q) => ` ${q.query} ${q.variables ? "//" + JSON.stringify(q.variables) : ""}`).join("\n"); needsData.push(`Observer '${observerName}' still needs data for these queries:\n${queryMsgs}`); } if (needsData.length > 0) { this.appendOutput(needsData.join("\n\n")); } } } DeployOpBase.flags = Object.assign({}, DeployBase.flags, { dryRun: command_1.flags.boolean({ description: "Show what would happen during deploy, but do not modify the deployment", }), registry: command_1.flags.string({ description: "URL of alternate NPM registry to use", env: "ADAPT_NPM_REGISTRY", }) }); exports.DeployOpBase = DeployOpBase; //# sourceMappingURL=deploy_base.js.map