Skip to content

Commit

Permalink
Merge pull request #113 from shoutem/hc/SEEXT-9440-Retries_every_time
Browse files Browse the repository at this point in the history
Add proper exit to ext download retry loop
  • Loading branch information
Definitely-Not-Vlad authored Mar 8, 2021
2 parents 71a0532 + 39e6194 commit ba7ceeb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 60 additions & 37 deletions src/commands/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import { getApp } from '../clients/legacy-service';
import { pathExists, copy } from 'fs-extra';
import selectApp from '../services/app-selector';
import {
downloadApp, fixPlatform, configurePlatform, createPlatformConfig,
setPlatformConfig
downloadApp,
fixPlatform,
configurePlatform,
createPlatformConfig,
setPlatformConfig,
} from '../services/platform';
import { ensureUserIsLoggedIn } from './login';
import { createProgressHandler } from '../services/progress-bar';
Expand All @@ -29,48 +32,59 @@ export async function pullExtensions(appId, destinationDir) {
const installations = await appManager.getInstallations(appId);
const n = installations.length;
let i = 0;
for(const inst of installations) {
for (const inst of installations) {
i++;
await spinify(pullExtension(destinationDir, inst), `Downloading extension ${i}/${n}: ${inst.canonicalName}...`);
await spinify(
pullExtension(destinationDir, inst),
`Downloading extension ${i}/${n}: ${inst.canonicalName}...`
);
}
}

async function pullExtension(destinationDir, { extension, canonicalName }) {
let pullError = {};
let pullSuccessful = false;
for (let i = 0; !pullSuccessful || i < 4; i++) {
let pullError = null;
for (let i = 0; i < 4; i++) {
try {
const url = await getExtensionUrl(extension);
const tgzDir = (await tmp.dir()).path;
await downloadFile(url, { directory: tgzDir, filename: 'extension.tgz' });
await shoutemUnpack(path.join(tgzDir, 'extension.tgz'), path.join(destinationDir, canonicalName));
pullSuccessful = true;
await shoutemUnpack(
path.join(tgzDir, 'extension.tgz'),
path.join(destinationDir, canonicalName)
);

pullError = null;
return;
} catch (error) {
pullError = error;
console.log(`Failed to download extension ${canonicalName}, try ${i + 1}/5`);
if (error.code !== 'ENOTEMPTY') {
pullError = error;
}
}
}

if (!pullSuccessful) {
if (!_.isEmpty(pullError)) {
pullError.message = `Could not fetch extension ${canonicalName}.`;
throw pullError;
}
}

async function getExtensionUrl(extId) {
const resp = await getExtension(extId);
const { location: { extension } } = resp;
const {
location: { extension },
} = resp;

return `${removeTrailingSlash(extension.package)}/extension.tgz`;
return `${removeTrailingSlash(extension.package)}/extension.tgz`;
}

function removeTrailingSlash(str) {
return str.replace(/\/$/, "");
return str.replace(/\/$/, '');
}

function ensurePlatformCompatibility(platform) {
const msg = `Your app is using Shoutem Platform ${platform.version}`+
`, but cloning is supported only on Shoutem Platform 1.1.2 or later.\n`+
const msg =
`Your app is using Shoutem Platform ${platform.version}` +
`, but cloning is supported only on Shoutem Platform 1.1.2 or later.\n` +
`Please, update the Platform through Settings -> Shoutem Platform -> Install page on the Builder or install older (and unsupported) version of ` +
`the Shoutem CLI by running 'npm install -g @shoutem/[email protected]'`;

Expand All @@ -84,16 +98,20 @@ async function queryPathExistsAction(destinationDir, oldDirectoryName) {
type: 'list',
name: 'action',
message: `Directory ${oldDirectoryName} already exists.`,
choices: [{
name: 'Overwrite',
value: 'overwrite'
}, {
name: 'Abort',
value: 'abort',
}, {
name: 'Different app directory name',
value: 'rename'
}]
choices: [
{
name: 'Overwrite',
value: 'overwrite',
},
{
name: 'Abort',
value: 'abort',
},
{
name: 'Different app directory name',
value: 'rename',
},
],
});

if (action === 'overwrite') {
Expand All @@ -111,26 +129,26 @@ async function queryPathExistsAction(destinationDir, oldDirectoryName) {
return 'No spaces are allowed.';
}
if (await pathExists(path.join(destinationDir, dirName))) {
return `Directory ${dirName} already exists.`
return `Directory ${dirName} already exists.`;
}
return true;
}
},
});

return {
type: 'rename',
newDirectoryName,
newAppDir: path.join(destinationDir, newDirectoryName)
newAppDir: path.join(destinationDir, newDirectoryName),
};
}

export async function clone(opts, destinationDir) {
if (!await commandExists('git')) {
if (!(await commandExists('git'))) {
throw new Error('Missing `git` command');
}
await ensureUserIsLoggedIn();

opts.appId = opts.appId || await selectApp();
opts.appId = opts.appId || (await selectApp());

const { name } = await getApp(opts.appId);

Expand Down Expand Up @@ -174,9 +192,11 @@ export async function clone(opts, destinationDir) {

versionCheck: mobileAppVersion => {
if (!semver.gte(mobileAppVersion, '0.58.9')) {
throw new Error('This version of CLI only supports platforms containing mobile app 0.58.9 or higher.');
throw new Error(
'This version of CLI only supports platforms containing mobile app 0.58.9 or higher.'
);
}
}
},
});
}

Expand All @@ -185,7 +205,7 @@ export async function clone(opts, destinationDir) {
await fixPlatform(appDir, opts.appId);

const config = await createPlatformConfig(appDir, {
appId: opts.appId
appId: opts.appId,
});
await setPlatformConfig(appDir, config);

Expand All @@ -205,7 +225,10 @@ export async function clone(opts, destinationDir) {
console.log(' Have an Android simulator running or a device connected');
console.log(' react-native run-android');

if (!/^win/.test(process.platform) && !await commandExists('watchman')) {
console.log('HINT: You should probably install Facebook\'s `watchman` before running react-native commands'.bold.yellow);
if (!/^win/.test(process.platform) && !(await commandExists('watchman'))) {
console.log(
"HINT: You should probably install Facebook's `watchman` before running react-native commands"
.bold.yellow
);
}
}

0 comments on commit ba7ceeb

Please sign in to comment.