Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/async await cleanup 1 #53

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/cli/builder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import opn from 'opn';
import services from '../../config/services';
import {getPlatformConfig} from "../services/platform";
import {executeAndHandleError} from "../services/error-handler";
import { getPlatformConfig } from '../services/platform';
import { executeAndHandleError } from '../services/error-handler';

export const description = 'Opens the app in the shoutem builder dashboard using default browser';
export const command = 'builder [appId]';
Expand All @@ -10,9 +10,11 @@ export const builder = yargs => {
.usage(`shoutem ${command} \n\n${description}`);
};

export const handler = args => executeAndHandleError(async () => {
const appId = args.appId || (await getPlatformConfig()).appId;
function openAppInBuilder(args) {
const appId = args.appId || (getPlatformConfig()).appId;
const url = `${services.appBuilder}/app/${appId}`;
console.log(url);
opn(url, { wait: false });
});
}

export const handler = args => executeAndHandleError(openAppInBuilder, args);
4 changes: 1 addition & 3 deletions src/cli/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@ export const builder = yargs => {
.usage(`shoutem ${command} \n\n${description}`);
};

export async function handler(args) {
await executeAndHandleError(() => clone(args, process.cwd()));
}
export const handler = args => executeAndHandleError(clone, args, process.cwd());
28 changes: 18 additions & 10 deletions src/cli/configure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {
configurePlatform,
getPlatformConfig,
getPlatformRootDir,
setPlatformConfig,
} from '../services/platform';
import { executeAndHandleError } from '../services/error-handler';
import {configurePlatform, getPlatformConfig, getPlatformRootDir, setPlatformConfig} from '../services/platform';

export const description = 'Runs platform\'s configure script to sync with native changes to local extensions';
export const command = 'configure';
Expand All @@ -21,16 +26,19 @@ export const builder = yargs => {
})
.usage(`shoutem ${command} \n\n${description}`);
};
export async function handler(args) {
await executeAndHandleError(async () => {
const appDir = await getPlatformRootDir();

await setPlatformConfig(appDir, {
...await getPlatformConfig(appDir),
release: !!args.release,
production: !!args.production
});

await configurePlatform(appDir);
export function configure(args) {
const appDir = getPlatformRootDir();
const config = getPlatformConfig(appDir);

setPlatformConfig(appDir, {
...config,
release: !!args.release,
production: !!args.production,
});

return configurePlatform(appDir);
}

export const handler = args => executeAndHandleError(configure, args);
34 changes: 18 additions & 16 deletions src/cli/extension/add.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import path from 'path';
import 'colors';

import {
addToExtensionsJs,
getPlatformConfig,
getPlatformExtensionsDir,
getPlatformRootDir,
linkLocalExtension
} from "../../services/platform";
import {executeAndHandleError} from "../../services/error-handler";
import {initExtension} from "../../commands/init";
import {uploadExtension} from "../../commands/push";
import {installLocalExtension} from "../../commands/install";
import 'colors';
import {spinify} from "../../services/spinner";
import {publishExtension} from "../../commands/publish";
linkLocalExtension,
} from '../../services/platform';
import { spinify } from '../../services/spinner';
import { executeAndHandleError } from '../../services/error-handler';

import { initExtension } from '../../commands/init';
import { installLocalExtension } from '../../commands/install';
import { uploadExtension, publishExtension } from '../../commands/publish';

export const description = 'Create a new extension for the current app';
export const command = 'add <name>';
Expand Down Expand Up @@ -43,31 +44,32 @@ const postRunMessage =
add a new settings page
`;

export const handler = args => executeAndHandleError(() => addExtension(args));

export async function addExtension({ name, local, externalDestination }) {
const platformDir = externalDestination || await getPlatformRootDir();
const extensionPath = await initExtension(name, externalDestination || await getPlatformExtensionsDir(platformDir));
const platformDir = externalDestination || getPlatformRootDir();
const extensionDir = externalDestination || getPlatformExtensionsDir(platformDir);
const extensionPath = await initExtension(name, extensionDir);

if (!local && !externalDestination) {
await uploadExtension({ publish: true }, extensionPath);
await publishExtension(extensionPath);

const { appId } = await getPlatformConfig(platformDir);
const { appId } = getPlatformConfig(platformDir);
await spinify(installLocalExtension(appId, extensionPath), 'Installing it in your app...', 'OK');
}

if (!externalDestination) {
console.log('\nRunning npm install script:');
await linkLocalExtension(platformDir, extensionPath);
await addToExtensionsJs(platformDir, extensionPath);
addToExtensionsJs(platformDir, extensionPath);
console.log(`npm install [${'OK'.bold.green}]`);
}

const cdCommand = 'cd ' + path.relative(process.cwd(), extensionPath);
const cdCommand = `cd ${path.relative(process.cwd(), extensionPath)}`;
console.log('\nCongratulations, your new extension is ready!'.green.bold);
console.log(`You might try doing ${cdCommand.cyan} where you can:`);
console.log(postRunMessage);
console.log('Success!'.green.bold);
console.log('Happy coding!');
}

export const handler = args => executeAndHandleError(addExtension, args);
38 changes: 22 additions & 16 deletions src/cli/extension/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path';
import {executeAndHandleError} from "../../services/error-handler";
import {ensureUserIsLoggedIn} from "../../commands/login";
import {getPlatformConfig, getPlatformExtensionsDir} from "../../services/platform";
import { pathExists } from 'fs-extra';
import fs from 'fs-extra';
import {uploadExtension} from "../../commands/push";
import {publishExtension} from "../../commands/publish";
import {updateExtension, getInstallation, installExtension} from "../../clients/app-manager";
Expand All @@ -16,39 +16,45 @@ export const builder = yargs => {
.usage(`shoutem ${command}\n\n${description}`);
};

export const handler = ({ name }) => executeAndHandleError(async () => {
const dev = await ensureUserIsLoggedIn();
const extensionPath = path.join(await getPlatformExtensionsDir(), `${dev.name}.${name}`);

if (!await pathExists(extensionPath)) {
throw new Error(`Path ${path.relative(process.cwd(), extensionPath)} does not exist`);
}

await uploadExtension({ publish: true }, extensionPath);
const { id: extensionId, version } = await publishExtension(extensionPath);
await offerInstallationUpdate(extensionId, name, version);
console.log('Success'.green.bold);
});

export async function offerInstallationUpdate(extensionId, extensionName, newVersion) {
const { appId } = await getPlatformConfig();
const { appId } = getPlatformConfig();

const dev = await ensureUserIsLoggedIn();
const canonical = `${dev.name}.${extensionName}`;

try {
const { id: installationId, extension: oldExtensionId } = await getInstallation(appId, canonical);
const { version: oldVersion } = await getExtension(oldExtensionId);

const versionMsg = `${canonical}@${oldVersion} => @${newVersion}`;
const msg = `Update the version used in the current app (${versionMsg})?`;

if (await confirmer(msg)) {
await updateExtension(appId, installationId, extensionId);
}
} catch (e) {
if (e.statusCode !== 404) {
throw e;
}

if (await confirmer(`Do you want to install ${canonical} extension into app ${appId}?`)) {
await installExtension(appId, extensionId);
}
}
}

async function publish (name) {
const dev = await ensureUserIsLoggedIn();
const extensionPath = path.join(getPlatformExtensionsDir(), `${dev.name}.${name}`);

if (!fs.existsSync(extensionPath)) {
throw new Error(`Path ${path.relative(process.cwd(), extensionPath)} does not exist`);
}

await uploadExtension({ publish: true }, extensionPath);
const { id: extensionId, version } = await publishExtension(extensionPath);
await offerInstallationUpdate(extensionId, name, version);
console.log('Success'.green.bold);
}

export const handler = ({ name }) => executeAndHandleError(publish, name);
7 changes: 4 additions & 3 deletions src/cli/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const builder = yargs => {
return yargs.usage(`shoutem ${command}\n\n${description}`).strict();
};

export const handler = ({ name }) => executeAndHandleError(async () => {
await addExtension({ name, externalDestination: process.cwd() });
});
export const handler = ({ name }) => {
const args = { name, externalDestination: process.cwd() };
executeAndHandleError(addExtension, args);
};
14 changes: 9 additions & 5 deletions src/cli/last-error.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import 'colors';
import prettyJson from 'prettyjson';
import * as cache from '../services/cache-env';

import cache from '../services/cache-env';

export const description = null;
export const command = 'last-error';
export async function handler() {
const lastError = await cache.getValue('last-error');

export function handler() {
const lastError = cache.getValue('last-error');

if (lastError) {
console.log(prettyJson.render(lastError, {
keysColor: 'cyan',
numberColor: 'white'
numberColor: 'white',
}));
console.log(`\nIf you think this error is caused by bug in the shoutem command, you can report the issue here: ${"https://github.com/shoutem/cli/issues".bold}`.yellow);

console.log(`\nIf you think this error is caused by a bug in the Shoutem CLI, please report the issue here: ${'https://github.com/shoutem/cli/issues'.bold}`.yellow);
} else {
console.log('No error'.green);
}
Expand Down
7 changes: 2 additions & 5 deletions src/cli/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import { executeAndHandleError } from '../services/error-handler';
export const description = 'Log in and register as a Shoutem developer.\nProvide credentials in username:password format.';
export const command = 'login [credentials]';

export function handler(args) {

return executeAndHandleError(() => loginUser(args));
}

export function builder(yargs) {
return yargs
.usage(`shoutem ${command}\n\n${description}`);
}

export const handler = args => executeAndHandleError(loginUser, args);
6 changes: 3 additions & 3 deletions src/cli/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { executeAndHandleError } from '../services/error-handler';

export const description = 'Erase all locally stored credentials.';
export const command = 'logout';
export async function handler() {
await executeAndHandleError(logout);
}

export function builder(yargs) {
return yargs.usage(`shoutem ${command}\n\n${description}`);
}

export const handler = () => executeAndHandleError(logout);
15 changes: 9 additions & 6 deletions src/cli/page/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import {offerChanges} from "../../services/diff";
export const description = 'Add a settings page to current extension';
export const command = 'add [name]';

export const handler = args => executeAndHandleError(async () => {
const extJson = await loadExtensionJson();
const answers = await askPageCreationQuestions({ ...extJson, defaultName: args.name });
await createPage(answers, ensureInExtensionDir());
});

export async function createPage(opts, extensionPath) {
const changes = await instantiateExtensionTemplate('settings-page', { ...opts, extensionPath });
await offerChanges(changes);
console.log('Success'.green.bold);
}

async function addPage(args) {
const extJson = loadExtensionJson();
const answers = await askPageCreationQuestions({ ...extJson, defaultName: args.name });

return createPage(answers, ensureInExtensionDir());
}

export const handler = args => executeAndHandleError(addPage, args);
7 changes: 4 additions & 3 deletions src/cli/platform/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ const postRunPublish = platformId => `
To publish this platform for everyone to use
`;

export const handler = args => executeAndHandleError(() => createPlatform(args));

export async function createPlatform({ url }) {
const developer = await ensureUserIsLoggedIn();

Expand All @@ -54,7 +52,8 @@ export async function createPlatform({ url }) {
published = true;
}

const { appId } = await getPlatformConfig();
const { appId } = getPlatformConfig();

if (!_.isNil(appId)) {
if (await confirmer(`Do you want to install the new platform to this app (${appId})?`)) {
await spinify(installPlatform({ app: appId, platform: platformResponse.id }));
Expand All @@ -76,3 +75,5 @@ export async function createPlatform({ url }) {
}
}
}

export const handler = args => executeAndHandleError(createPlatform, args);
8 changes: 4 additions & 4 deletions src/cli/platform/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ export const builder = yargs => yargs
})
.usage(`shoutem ${command} [options]\n\n${description}`);

export const handler = args => executeAndHandleError(() => installPlatform(args));

export async function installPlatform({ app, platform }) {
const developer = await ensureUserIsLoggedIn();

let appConfig;
if (await getPlatformRootDir(process.cwd(), { shouldThrow: false })) {
appConfig = await getPlatformConfig();
if (getPlatformRootDir(process.cwd(), { shouldThrow: false })) {
appConfig = getPlatformConfig();
}

// if app ID is not explicitly passed, then try to get the ID from current directory, otherwise ask the user
Expand All @@ -46,3 +44,5 @@ export async function installPlatform({ app, platform }) {
console.log('Your platform is now installed on your app');
console.log('Success!'.green.bold);
}

export const handler = args => executeAndHandleError(installPlatform, args);
4 changes: 2 additions & 2 deletions src/cli/platform/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export const builder = yargs => yargs
})
.usage(`shoutem ${command} [options]\n\n${description}`);

export const handler = args => executeAndHandleError(() => listPlatforms(args));

export async function listPlatforms({ all }) {
const developer = await ensureUserIsLoggedIn();
const platforms = await spinify(getAvailablePlatforms(all ? null : 20));
Expand All @@ -32,3 +30,5 @@ export async function listPlatforms({ all }) {
console.log(`${id}\t${published}\t\t${author}@${version}`);
});
}

export const handler = args => executeAndHandleError(listPlatforms, args);
4 changes: 2 additions & 2 deletions src/cli/platform/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export const builder = yargs => yargs
})
.usage(`shoutem ${command} [options]\n\n${description}`);

export const handler = args => executeAndHandleError(() => publishOwnPlatform(args));

export async function publishOwnPlatform({ platform }) {
const developer = await ensureUserIsLoggedIn();

Expand All @@ -31,3 +29,5 @@ export async function publishOwnPlatform({ platform }) {
console.log('Success!'.green.bold);
console.log('Your platform is now public!');
}

export const handler = args => executeAndHandleError(publishOwnPlatform, args);
Loading