import { Wetland } from './Wetland' ; import { SnapshotManager } from './SnapshotManager' ; import * as rm from 'del' ; import * as path from 'path' ; import { SchemaBuilder } from './SchemaBuilder' ; export class Cleaner { wetland : Wetland ; param { Wetland } wetland constructor ( wetland ) { this . wetland = wetland ; } return { Promise < any > } public clean ( ) { return this . dropTables ( ) . then ( ( ) => this . cleanDataDirectory ( ) ) ; } return { Promise < any > } private cleanDataDirectory ( ) { return rm ( path . join ( this . wetland . getConfig ( ) . fetch ( 'dataDirectory' ) , SnapshotManager . DEV_SNAPSHOTS_PATH , '*' ) ) . catch ( error => { if ( error . code === 'ENOENT' ) { return Promise . resolve ( ) ; } return Promise . reject ( error ) ; } ) ; } return { Promise < any > } private dropTables ( ) { const manager = this . wetland . getManager ( ) ; const snapshotManager = this . wetland . getSnapshotManager ( ) ; const schemaBuilder = new SchemaBuilder ( manager ) ; return snapshotManager . fetch ( ) . then ( previous => { const instructions = snapshotManager . diff ( previous , { } ) ; return schemaBuilder . process ( instructions ) . apply ( ) ; } ) ; } }