import { spawnSync } from 'child_process' ; import * as fs from 'fs' ; import { join , parse } from 'path' ; import { resolvePath } from './utils' ; export abstract class StorageWriter { abstract write ( _data , _dataPath ) ; } export class LocalStorageWriter extends StorageWriter { constructor ( readonly root ) { super ( ) ; } async write ( data , dataPath ) { return this . writeToFs ( data , dataPath ) ; } protected async writeToFs ( data , dataPath ) { const directory = parse ( dataPath ) . dir ; await fs . promises . mkdir ( join ( this . root , directory ) , { recursive : true } ) ; await fs . promises . writeFile ( join ( this . root , dataPath ) , data ) ; } } export class GitStorageWriter extends LocalStorageWriter { async write ( data , dataPath ) { await this . writeToFs ( data , dataPath ) ; spawnSync ( 'git' , [ 'add' , dataPath ] , { cwd : this . root } ) ; spawnSync ( 'git' , [ 'commit' , '--message' , `"Upload ${ dataPath }"` ] , { cwd : this . root } ) ; spawnSync ( 'git' , [ 'push' , 'origin' , 'master' ] , { cwd : this . root } ) ; return ; } } export class GoogleStorageWriter extends LocalStorageWriter { private readonly bucket : string ; constructor ( readonly local , bucket ) { super ( local ) ; this . bucket = bucket ; } async write ( data , dataPath ) { await this . writeToFs ( data , dataPath ) ; spawnSync ( 'gsutil' , [ 'cp' , join ( this . root , dataPath ) , `gs://${ this . bucket }${ dataPath }` ] , { cwd : this . root } ) ; } } export class AwsStorageWriter extends LocalStorageWriter { private readonly bucket : string ; constructor ( readonly local , bucket ) { super ( local ) ; this . bucket = bucket ; } async write ( data , dataPath ) { await this . writeToFs ( data , dataPath ) ; spawnSync ( 'aws' , [ 's3' , 'cp' , join ( this . root , dataPath ) , `s3://${ this . bucket }${ dataPath }` ] , { cwd : this . root } ) ; } } export class MockStorageWriter extends LocalStorageWriter { constructor ( readonly root , readonly mockedStorageRoot , readonly fetchMock ) { super ( root ) ; } async write ( data , dataPath ) { await this . writeToFs ( data , dataPath ) ; this . fetchMock . mock ( resolvePath ( this . mockedStorageRoot , dataPath ) , data , { sendAsJson : false , overwriteRoutes : true } ) ; } }