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 all 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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"mocha": true
},
"parserOptions": {
"ecmaVersion": 7,
"ecmaVersion": 8,
"sourceType": "module",
"ecmaFeatures": {
"experimentalObjectRestSpread": true
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
"@shoutem/fork-terminal": "1.0.0",
"archiver": "^3.0.0",
"async": "2.6.0",
"babel-eslint": "8.2.3",
"babel-plugin-transform-object-rest-spread": "6.26.0",
"babel-preset-env": "^1.7.0",
"babel-register": "6.26.0",
"bluebird": "3.5.1",
"child-process-promise": "2.2.1",
Expand Down Expand Up @@ -89,7 +86,11 @@
},
"devDependencies": {
"babel-cli": "6.26.0",
"babel-eslint": "8.2.3",
"babel-plugin-rewire": "1.1.0",
"babel-plugin-transform-object-rest-spread": "6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "6.24.1",
"chai": "4.1.2",
"dos2unix-cli": "1.0.1",
"eslint-config-airbnb": "16.1.0",
Expand Down
11 changes: 6 additions & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import yargs from 'yargs';

import getHomeDir from './home-dir';
import autoUpdate from './commands/update-cli';
import * as analytics from './services/analytics';
import { isAscii, containsSpace } from './services/validation';
import { authorizeRequests, getRefreshToken } from './clients/auth-service';
import analytics from './services/analytics';
import authService from './clients/auth-service';
import apiUrls from '../config/services';

require('yargonaut')
Expand All @@ -28,10 +28,11 @@ analytics.setArgv(process.argv);

(async () => {
if (await autoUpdate(cliArgs)) {
return null;
return;
}
const refreshToken = await getRefreshToken();
authorizeRequests(refreshToken);

const refreshToken = await authService.getRefreshToken();
authService.authorizeRequests(refreshToken);

const cli = yargs.usage('Usage: shoutem [command] [-h]')
.option('version', {
Expand Down
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);
65 changes: 36 additions & 29 deletions src/cli/extension/publish.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
import fs from 'fs-extra';
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 {uploadExtension} from "../../commands/push";
import {publishExtension} from "../../commands/publish";
import {updateExtension, getInstallation, installExtension} from "../../clients/app-manager";
import confirmer from "../../services/confirmer";
import {getExtension} from "../../clients/extension-manager";

import confirmer from '../../services/confirmer';
import { executeAndHandleError } from '../../services/error-handler';
import { getPlatformConfig, getPlatformExtensionsDir } from '../../services/platform';
import { ensureUserIsLoggedIn } from '../../commands/login';
import { uploadExtension } from '../../commands/push';
import { publishExtension } from '../../commands/publish';
import { updateExtension, getInstallation, installExtension } from '../../clients/app-manager';
import { getExtension } from '../../clients/extension-manager';

export const description = 'Publish an extension from the app in the working directory';
export const command = 'publish <name>';
export const builder = yargs => {
return 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 const builder = yargs => yargs.usage(`shoutem ${command}\n\n${description}`);

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 {
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);
Loading