|
1 | 1 | const chalk = require('chalk') |
2 | | - |
3 | 2 | /** |
4 | 3 | * @method listSpaces |
5 | 4 | * @param api - Pass the api instance as a parameter |
6 | 5 | * @return {Promise} |
7 | 6 | */ |
8 | 7 |
|
9 | | -const listSpaces = async (api) => { |
| 8 | +const listSpaces = async (api, currentRegion) => { |
| 9 | + const isChinaEnv = currentRegion === 'cn' |
| 10 | + const regionOptions = { |
| 11 | + eu: 'Europe', |
| 12 | + us: 'United States' |
| 13 | + } |
10 | 14 | console.log() |
11 | 15 | console.log(chalk.green('✓') + ' Loading spaces...') |
12 | | - console.log() |
13 | 16 |
|
14 | 17 | if (!api) { |
15 | 18 | console.log(chalk.red('X') + 'Api instance is required to make the request') |
16 | 19 | return [] |
17 | 20 | } |
18 | 21 |
|
19 | | - const spaces = await api.getAllSpaces() |
20 | | - .then(res => res) |
21 | | - .catch(err => Promise.reject(err)) |
| 22 | + if (isChinaEnv) { |
| 23 | + const spaces = await api.getAllSpacesByRegion(currentRegion) |
| 24 | + .then(res => res) |
| 25 | + .catch(err => Promise.reject(err)) |
22 | 26 |
|
23 | | - if (!spaces) { |
24 | | - console.log(chalk.red('X') + ' No spaces were found for this user ') |
25 | | - return [] |
26 | | - } |
27 | | - |
28 | | - spaces.map(space => { |
29 | | - console.log(`${space.name} (id: ${space.id})`) |
30 | | - }) |
| 27 | + if (!spaces) { |
| 28 | + console.log(chalk.red('X') + ' No spaces were found for this user ') |
| 29 | + return [] |
| 30 | + } |
| 31 | + console.log(chalk.blue(' -') + ' Spaces From China region:') |
31 | 32 |
|
32 | | - return spaces |
| 33 | + spaces.map(space => { |
| 34 | + console.log(` ${space.name} (id: ${space.id})`) |
| 35 | + }) |
| 36 | + return spaces |
| 37 | + } else { |
| 38 | + const spacesList = [] |
| 39 | + for (const key in regionOptions) { |
| 40 | + spacesList.push(await api.getAllSpacesByRegion(key) |
| 41 | + .then((res) => { |
| 42 | + return { |
| 43 | + key, |
| 44 | + res |
| 45 | + } |
| 46 | + }) |
| 47 | + .catch(err => Promise.reject(err))) |
| 48 | + } |
| 49 | + if (!spacesList) { |
| 50 | + console.log(chalk.red('X') + ' No spaces were found for this user ') |
| 51 | + return [] |
| 52 | + } |
| 53 | + spacesList.forEach(region => { |
| 54 | + console.log() |
| 55 | + console.log(`${chalk.blue(' -')} Spaces From ${regionOptions[region.key]} region:`) |
| 56 | + region.res.forEach((space) => { |
| 57 | + console.log(` ${space.name} (id: ${space.id})`) |
| 58 | + }) |
| 59 | + }) |
| 60 | + return spacesList |
| 61 | + } |
33 | 62 | } |
34 | 63 |
|
35 | 64 | module.exports = listSpaces |
0 commit comments