-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from shoutem/hc/SEEXT-9440-Retries_every_time
Add proper exit to ext download retry loop
- Loading branch information
Showing
2 changed files
with
61 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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]'`; | ||
|
||
|
@@ -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') { | ||
|
@@ -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); | ||
|
||
|
@@ -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.' | ||
); | ||
} | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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 | ||
); | ||
} | ||
} |