diff --git a/src/application.ts b/src/application.ts index 0280f27..14842b3 100644 --- a/src/application.ts +++ b/src/application.ts @@ -42,13 +42,13 @@ export default class Application extends Model implements Application21 { return data; } - private findIdleVersion(): Version | null { + private findVersionByStatus(status: string): Version | null { if (this.version.length > 0) { const findIdle = () => { return this.version .filter((ver: Version | string) => { if (typeof ver !== 'string') { - return ver.status === 'idle'; + return ver.status === status; } return false; }) @@ -77,7 +77,7 @@ export default class Application extends Model implements Application21 { } async validate() { - if (this.id && this.findIdleVersion() === null) { + if (this.id && this.findVersionByStatus('idle') === null) { tui.showWarning('Failed to find idle version, refresing application'); await this.fetch(); this.save(); @@ -85,7 +85,7 @@ export default class Application extends Model implements Application21 { } getVersion(): Version { - const idleVersion = this.findIdleVersion(); + const idleVersion = this.findVersionByStatus('idle'); if (idleVersion) { return idleVersion; } @@ -94,6 +94,10 @@ export default class Application extends Model implements Application21 { return new Version({}, this); } + getPendingVersion() { + return this.findVersionByStatus('pending'); + } + getOAuthExternal(): OauthExternal21[] { const oauth: OauthExternal21[] = []; this.oauth_external.forEach((o) => { @@ -244,6 +248,10 @@ export default class Application extends Model implements Application21 { } async publish(newVersion: string, change: string): Promise { + const pending = this.getPendingVersion(); + if (pending) { + await pending.unpublish(); + } const version = this.getVersion(); version.version_app = newVersion; if (version.description) { diff --git a/src/index.ts b/src/index.ts index 6ccee91..2529756 100755 --- a/src/index.ts +++ b/src/index.ts @@ -16,13 +16,15 @@ import { startTrace } from './util/trace'; import tui from './util/tui'; import Config from './config'; import Wapp from './wapp'; +import { VERSION } from './util/version'; const mainDefinitions = [{ name: 'command', defaultOption: true }]; const sections = [ { header: 'wappsto-cli', - content: 'Script to create and maintain wapps on {underline wappsto.com}', + content: + 'Script to create and maintain wapps on {underline https://wappsto.com}', }, { header: 'Synopsis', @@ -52,7 +54,14 @@ const sections = [ ], }, { - content: 'Project home: {underline https://github.com/wappsto/wappsto-cli}', + header: 'Information', + content: [ + { + name: 'Project', + summary: `{underline https://github.com/wappsto/wappsto-cli}`, + }, + { name: 'Version', summary: VERSION }, + ], }, ]; diff --git a/src/model.ts b/src/model.ts index 6605f78..9e7610b 100644 --- a/src/model.ts +++ b/src/model.ts @@ -173,6 +173,11 @@ export default class Model { `Please visit ${Config.host()}/pricing for more information` ); break; + case 500089: + tui.showError( + `${msg} because you cannot publish a new version, before you old version have been aproved.` + ); + break; case 9900147: tui.showError(`${msg} because it was not found on Wappsto`); break; diff --git a/src/util/questions.ts b/src/util/questions.ts index 563bc56..4ce23f9 100644 --- a/src/util/questions.ts +++ b/src/util/questions.ts @@ -14,15 +14,20 @@ type Request = { class Questions { private async ask(questions: any[]): Promise { Spinner.stop(); + let done = false; return new Promise((resolve) => { const onCancel = () => { Spinner.start(); + done = true; resolve(false); return false; }; prompts(questions, { onCancel }).then((answers) => { - Spinner.start(); - resolve(answers); + if (!done) { + done = true; + Spinner.start(); + resolve(answers); + } }); }); } @@ -649,9 +654,25 @@ class Questions { ]); } - askPublishWapp( - oldVersion: string + async askPublishWapp( + oldVersion: string, + pendingVersion: boolean ): Promise<{ version: string; change: string } | false> { + if (pendingVersion) { + const override = await this.ask([ + { + name: 'override', + type: 'confirm', + initial: () => true, + message: + 'You already have a published version pending for review, do you want to unpublish this version?', + }, + ]); + if (override === false || !override.override) { + return false; + } + } + return this.ask([ { name: 'version', diff --git a/src/util/setup_cli.ts b/src/util/setup_cli.ts index 0fe205a..7a50c4d 100644 --- a/src/util/setup_cli.ts +++ b/src/util/setup_cli.ts @@ -1,6 +1,7 @@ import commandLineArgs from 'command-line-args'; import commandLineUsage from 'command-line-usage'; import tui from '../util/tui'; +import { VERSION } from './version'; export default function setupCLI( name: string, @@ -16,6 +17,12 @@ export default function setupCLI( alias: 'h', type: Boolean, }, + { + name: 'version', + description: 'Display this current verison.', + alias: 'V', + type: Boolean, + }, { name: 'verbose', description: 'Enable verbose output.', @@ -43,8 +50,14 @@ export default function setupCLI( optionList: definitions, }, { - content: - 'Project home: {underline https://github.com/wappsto/wappsto-cli}', + header: 'Information', + content: [ + { + name: 'Project', + summary: `{underline https://github.com/wappsto/wappsto-cli}`, + }, + { name: 'Version', summary: VERSION }, + ], }, ]); @@ -61,6 +74,11 @@ export default function setupCLI( return false; } + if (options.version) { + console.log(`Wappsto CLI by Seluxit A/S - Version: ${VERSION}`); + return false; + } + tui.debug = options.debug; tui.verbose = options.verbose; diff --git a/src/util/version.ts b/src/util/version.ts index 162550e..8e04ce4 100644 --- a/src/util/version.ts +++ b/src/util/version.ts @@ -1,2 +1,2 @@ -const VERSION = '2.0.12'; +const VERSION = '2.0.13'; export { VERSION }; diff --git a/src/version.ts b/src/version.ts index 42a0eb9..244291c 100644 --- a/src/version.ts +++ b/src/version.ts @@ -172,4 +172,20 @@ export default class Version extends Model implements Version21 { } return false; } + + async unpublish(): Promise { + try { + const response = await HTTP.patch(`${this.url}`, { + status: 'uncommit', + }); + this.parse(response.data); + return true; + } catch (err) { + this.handleException( + `Failed to update ${this.meta.type}: ${this.id}`, + err + ); + } + return false; + } } diff --git a/src/wapp.publish.ts b/src/wapp.publish.ts index da80161..c981476 100644 --- a/src/wapp.publish.ts +++ b/src/wapp.publish.ts @@ -18,8 +18,10 @@ export default class PublishWapp extends Wapp { return; } + const pendig = this.application.getPendingVersion(); + const answers = await section('Wait for user input', () => { - return questions.askPublishWapp(this.manifest.version_app); + return questions.askPublishWapp(this.manifest.version_app, !!pendig); }); if (answers === false) {