Source: storage/file/restore.js

import { StorageInstance as storage } from '../shared.js';

/**
 * ### Restore non-current file version as current
 * @method module:cpb-storage/file/restore
 * @async
 * @param {?string} [bucket=process.env.BUCKET || 'custom-product-builder'] - source storage bucket
 * @param {!string} path - source file path
 * @param {!number} generation - source file generation to restore
 * @param {?string} [destinationBucket=bucket] - destination storage bucket
 * @param {?string} [destinationPath=path] - destination file path for the restored version
 * @returns {Promise<void>}
 */
export default async function RestoreFile(
  { bucket = process.env.BUCKET || 'custom-product-builder', path, generation } = {},
  { destinationPath = path, destinationBucket = bucket } = {},
) {
  if (!path) throw new TypeError('!path');
  if (!generation) throw new TypeError('!generation');
  if (!destinationPath) throw new TypeError('!destinationPath');
  if (!destinationBucket) throw new TypeError('!destinationBucket');

  async function restore() {
    console.debug(`[storage/file/restore][${bucket}/${path}/${generation}] RESTORED AS CURRENT`);
    return await storage.bucket(bucket).file(path, { generation }).copy(storage.bucket(destinationBucket).file(destinationPath)).catch(console.error);
  }

  return await restore().catch(console.error);
}