Skip to content

Commit

Permalink
Added version and support for unpublish
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBomholtz committed Mar 3, 2023
1 parent 3260eef commit c4e5784
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 14 deletions.
16 changes: 12 additions & 4 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
Expand Down Expand Up @@ -77,15 +77,15 @@ 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();
}
}

getVersion(): Version {
const idleVersion = this.findIdleVersion();
const idleVersion = this.findVersionByStatus('idle');
if (idleVersion) {
return idleVersion;
}
Expand All @@ -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) => {
Expand Down Expand Up @@ -244,6 +248,10 @@ export default class Application extends Model implements Application21 {
}

async publish(newVersion: string, change: string): Promise<boolean> {
const pending = this.getPendingVersion();
if (pending) {
await pending.unpublish();
}
const version = this.getVersion();
version.version_app = newVersion;
if (version.description) {
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 },
],
},
];

Expand Down
5 changes: 5 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 25 additions & 4 deletions src/util/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ type Request = {
class Questions {
private async ask(questions: any[]): Promise<any | false> {
Spinner.stop();
let done = false;
return new Promise<any | false>((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);
}
});
});
}
Expand Down Expand Up @@ -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',
Expand Down
22 changes: 20 additions & 2 deletions src/util/setup_cli.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.',
Expand Down Expand Up @@ -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 },
],
},
]);

Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/util/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const VERSION = '2.0.12';
const VERSION = '2.0.13';
export { VERSION };
16 changes: 16 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,20 @@ export default class Version extends Model implements Version21 {
}
return false;
}

async unpublish(): Promise<boolean> {
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;
}
}
4 changes: 3 additions & 1 deletion src/wapp.publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c4e5784

Please sign in to comment.