|
1 | 1 | import { setWorldConstructor, World, IWorldOptions } from '@cucumber/cucumber'
|
| 2 | +import { RemoteRepo } from '../classes/remote' |
2 | 3 | import * as utils from './utils'
|
| 4 | +import * as fs from 'fs' |
| 5 | +import * as path from 'path' |
| 6 | +import { ClonedRepository } from '../classes/repository' |
3 | 7 | import { BundleServer } from '../classes/bundleServer'
|
4 | 8 |
|
| 9 | +export enum User { |
| 10 | + Me = 1, |
| 11 | +} |
| 12 | + |
5 | 13 | interface BundleServerParameters {
|
| 14 | + bundleServerCommand: string |
6 | 15 | bundleWebServerCommand: string
|
| 16 | + trashDirectoryBase: string |
7 | 17 | }
|
8 | 18 |
|
9 | 19 | export class BundleServerWorld extends World<BundleServerParameters> {
|
| 20 | + // Internal variables |
| 21 | + trashDirectory: string |
| 22 | + |
10 | 23 | // Bundle server
|
11 | 24 | bundleServer: BundleServer
|
| 25 | + remote: RemoteRepo | undefined |
| 26 | + |
| 27 | + // Users |
| 28 | + repoMap: Map<User, ClonedRepository> |
12 | 29 |
|
13 | 30 | constructor(options: IWorldOptions<BundleServerParameters>) {
|
14 | 31 | super(options)
|
15 | 32 |
|
| 33 | + this.repoMap = new Map<User, ClonedRepository>() |
| 34 | + |
| 35 | + // Set up the trash directory |
| 36 | + this.trashDirectory = path.join(utils.absPath(this.parameters.trashDirectoryBase), randomUUID()) |
| 37 | + fs.mkdirSync(this.trashDirectory, { recursive: true }); |
| 38 | + |
16 | 39 | // Set up the bundle server
|
17 |
| - this.bundleServer = new BundleServer(utils.absPath(this.parameters.bundleWebServerCommand)) |
| 40 | + this.bundleServer = new BundleServer(utils.absPath(this.parameters.bundleServerCommand), |
| 41 | + utils.absPath(this.parameters.bundleWebServerCommand)) |
| 42 | + } |
| 43 | + |
| 44 | + cloneRepositoryFor(user: User, bundleUri?: string): void { |
| 45 | + if (!this.remote) { |
| 46 | + throw new Error("Remote repository is not initialized") |
| 47 | + } |
| 48 | + |
| 49 | + const repoRoot = `${this.trashDirectory}/${User[user]}` |
| 50 | + this.repoMap.set(user, new ClonedRepository(this.remote, repoRoot, bundleUri)) |
| 51 | + } |
| 52 | + |
| 53 | + getRepo(user: User): ClonedRepository { |
| 54 | + const repo = this.repoMap.get(user) |
| 55 | + if (!repo) { |
| 56 | + throw new Error("Cloned repository has not been initialized") |
| 57 | + } |
| 58 | + |
| 59 | + return repo |
| 60 | + } |
| 61 | + |
18 | 62 | }
|
19 | 63 |
|
20 | 64 | cleanup(): void {
|
21 | 65 | this.bundleServer.cleanup()
|
| 66 | + |
| 67 | + // Delete the trash directory |
| 68 | + fs.rmSync(this.trashDirectory, { recursive: true }) |
22 | 69 | }
|
23 | 70 | }
|
24 | 71 |
|
|
0 commit comments