|
| 1 | +import chalk from 'chalk' |
| 2 | +import StoryblokClient from 'storyblok-js-client' |
| 3 | + |
| 4 | +// Throttling |
| 5 | +export default class Migration { |
| 6 | + constructor(oauth, target_space_id, simultaneous_uploads, region) { |
| 7 | + this.target_space_id = target_space_id |
| 8 | + this.oauth = oauth |
| 9 | + this.simultaneous_uploads = simultaneous_uploads |
| 10 | + this.region = region |
| 11 | + this.assets_retries = {} |
| 12 | + this.retries_limit = 4 |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Migration error callback |
| 17 | + */ |
| 18 | + migrationError(err) { |
| 19 | + throw new Error(err) |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Print a message of the current step |
| 24 | + */ |
| 25 | + stepMessage(index, text, append_text) { |
| 26 | + process.stdout.clearLine() |
| 27 | + process.stdout.cursorTo(0) |
| 28 | + process.stdout.write(`${chalk.white.bgBlue(` ${index}/3 `)} ${text} ${append_text ? chalk.black.bgYellow(` ${append_text} `) : ''}`) |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Print a message of the completed step |
| 33 | + */ |
| 34 | + stepMessageEnd(index, text) { |
| 35 | + process.stdout.clearLine() |
| 36 | + process.stdout.cursorTo(0) |
| 37 | + process.stdout.write(`${chalk.black.bgGreen(` ${index}/3 `)} ${text}\n`) |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Start the migration |
| 42 | + */ |
| 43 | + async start() { |
| 44 | + try { |
| 45 | + await this.getTargetSpaceToken() |
| 46 | + await this.getAssets() |
| 47 | + await this.logUnusedAssets() |
| 48 | + } catch (err) { |
| 49 | + console.log(`${chalk.white.bgRed(` ⚠ Migration Error `)} ${chalk.red(err.toString().replace('Error: ', ''))}`) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get the target space token and setup the Storyblok js client |
| 55 | + */ |
| 56 | + async getTargetSpaceToken() { |
| 57 | + try { |
| 58 | + this.storyblok = new StoryblokClient({ |
| 59 | + oauthToken: this.oauth, |
| 60 | + region: this.region |
| 61 | + }) |
| 62 | + const space_request = await this.storyblok.get(`spaces/${this.target_space_id}`) |
| 63 | + this.target_space_token = space_request.data.space.first_token |
| 64 | + this.storyblok = new StoryblokClient({ |
| 65 | + accessToken: this.target_space_token, |
| 66 | + region: this.region, |
| 67 | + oauthToken: this.oauth, |
| 68 | + rateLimit: 3 |
| 69 | + }) |
| 70 | + this.stepMessageEnd('1', `Personal access token is valid. New StoryblokClient is created.`) |
| 71 | + |
| 72 | + } catch (err) { |
| 73 | + this.migrationError('Error trying to retrieve the space token. Please double check the target space id and the OAUTH token.') |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /** |
| 79 | + * Get the Assets list from the target space |
| 80 | + */ |
| 81 | + async getAssets() { |
| 82 | + this.stepMessage('2', `Fetching assets from target space.`) |
| 83 | + try { |
| 84 | + const assets_page_request = await this.storyblok.get(`spaces/${this.target_space_id}/assets`, { |
| 85 | + per_page: 100, |
| 86 | + page: 1 |
| 87 | + }) |
| 88 | + const pages_total = Math.ceil(assets_page_request.headers.total / 100) |
| 89 | + const assets_requests = [] |
| 90 | + for (let i = 1; i <= pages_total; i++) { |
| 91 | + assets_requests.push( |
| 92 | + this.storyblok.get(`spaces/${this.target_space_id}/assets`, { |
| 93 | + per_page: 100, |
| 94 | + page: i |
| 95 | + }) |
| 96 | + ) |
| 97 | + } |
| 98 | + const assets_responses = await Promise.all(assets_requests) |
| 99 | + |
| 100 | + this.assets_list = assets_responses.map(r => r.data.assets).flat().map((asset) => asset.filename) |
| 101 | + this.stepMessageEnd('2', `Fetched assets from target space.`) |
| 102 | + } catch (err) { |
| 103 | + this.migrationError('Error fetching the assets. Please double check the target space id.') |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Log unused assets |
| 109 | + */ |
| 110 | + |
| 111 | + async logUnusedAssets() { |
| 112 | + this.stepMessage('3', `Log assets, which are not used in any story.`) |
| 113 | + |
| 114 | + try { |
| 115 | + const unusedAssets = this.assets_list.map(async (assetFileName, i) => { |
| 116 | + // get end of URL starting with space ID |
| 117 | + //eg. "245445/901x593/ec5855f2b5/aff-lp-hero-img.png" |
| 118 | + const refSearchStart = assetFileName.indexOf(this.target_space_id) |
| 119 | + |
| 120 | + // get full search string |
| 121 | + // eg. "/f/245445/901x593/ec5855f2b5/aff-lp-hero-img.png" |
| 122 | + const refSearch = assetFileName.substring(refSearchStart - 3) |
| 123 | + |
| 124 | + const assetReferencesRequest = this.storyblok.get(`spaces/${this.target_space_id}/stories`, { |
| 125 | + page: 1, |
| 126 | + per_page: 25, |
| 127 | + reference_search: refSearch, |
| 128 | + }) |
| 129 | + |
| 130 | + return await Promise.resolve(assetReferencesRequest).then((res) => { |
| 131 | + this.stepMessage('3', ``, `${i} of ${this.assets_list.length} assets checked`) |
| 132 | + if (res.total === 0) { |
| 133 | + const lastSlashIndex = assetFileName.lastIndexOf("/") |
| 134 | + const assetClearName = assetFileName.substring(lastSlashIndex + 1) |
| 135 | + return assetClearName |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | + |
| 140 | + }) |
| 141 | + |
| 142 | + const unusedAssetsNames = await Promise.all(unusedAssets) |
| 143 | + let number = 1 |
| 144 | + unusedAssetsNames.map((unusedAssetName, i) => { |
| 145 | + if (unusedAssetName) { |
| 146 | + process.stdout.clearLine() |
| 147 | + process.stdout.cursorTo(0) |
| 148 | + process.stdout.write(`${chalk.dim(` ${number}.`)} ${unusedAssetName}\n`) |
| 149 | + number++ |
| 150 | + } |
| 151 | + }) |
| 152 | + |
| 153 | + this.stepMessageEnd('3', `${chalk.blueBright(unusedAssetsNames.length)} assets are checked for references in space ${chalk.dim("#" + this.target_space_id)}. You can see the list of unused assets filenames above.`) |
| 154 | + |
| 155 | + |
| 156 | + } catch (err) { |
| 157 | + this.migrationError('Error fetching the assets references. Please double check the target space id.') |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments