|
| 1 | +import { |
| 2 | + ArtifactBuilderProviderType, |
| 3 | + VulcanExtensionId, |
| 4 | + VulcanInternalExtension, |
| 5 | +} from '@vulcan-sql/core'; |
| 6 | +import { IBuildOptions } from '../../../models/buildOptions'; |
| 7 | +import { Packager, PackagerType } from '../../../models/extensions'; |
| 8 | +import * as path from 'path'; |
| 9 | +import { promises as fs } from 'fs'; |
| 10 | + |
| 11 | +export interface DockerPackagerConfig { |
| 12 | + folderPath?: string; |
| 13 | +} |
| 14 | + |
| 15 | +@VulcanExtensionId(PackagerType.Docker) |
| 16 | +@VulcanInternalExtension('docker-packager') |
| 17 | +export class DockerPackager extends Packager<DockerPackagerConfig> { |
| 18 | + private logger = this.getLogger(); |
| 19 | + |
| 20 | + public async package(option: IBuildOptions): Promise<void> { |
| 21 | + const { folderPath = 'dist' } = this.getConfig() || {}; |
| 22 | + const distFolder = path.resolve(process.cwd(), folderPath); |
| 23 | + await fs.rm(distFolder, { recursive: true, force: true }); |
| 24 | + await fs.mkdir(distFolder, { recursive: true }); |
| 25 | + // package.json |
| 26 | + await fs.writeFile( |
| 27 | + path.resolve(distFolder, 'package.json'), |
| 28 | + JSON.stringify(await this.getPackageJson(), null, 4), |
| 29 | + 'utf-8' |
| 30 | + ); |
| 31 | + // config.json (vulcan config) |
| 32 | + await fs.writeFile( |
| 33 | + path.resolve(distFolder, 'config.json'), |
| 34 | + JSON.stringify(option), |
| 35 | + 'utf-8' |
| 36 | + ); |
| 37 | + // entrypoint |
| 38 | + await fs.writeFile( |
| 39 | + path.resolve(distFolder, 'index.js'), |
| 40 | + await this.getEntryJS(), |
| 41 | + 'utf-8' |
| 42 | + ); |
| 43 | + // result.json |
| 44 | + if ( |
| 45 | + option.artifact.provider === ArtifactBuilderProviderType.LocalFile && |
| 46 | + option.artifact.filePath |
| 47 | + ) { |
| 48 | + await fs.copyFile( |
| 49 | + path.resolve(process.cwd(), option.artifact.filePath), |
| 50 | + path.resolve(distFolder, option.artifact.filePath) |
| 51 | + ); |
| 52 | + } |
| 53 | + // Dockerfile |
| 54 | + await fs.copyFile( |
| 55 | + path.resolve(__dirname, 'assets', 'Dockerfile'), |
| 56 | + path.resolve(distFolder, 'Dockerfile') |
| 57 | + ); |
| 58 | + this.logger.info( |
| 59 | + `Package successfully, you can go to "${folderPath}" folder and run "docker build ." to build the image.` |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
0 commit comments