1+ const yaml = require ( 'js-yaml' ) ;
2+ const fs = require ( 'fs' ) ;
3+
4+ const secrets = require ( './lib/secrets' ) ;
5+ const volumes = require ( './lib/volumes' ) ;
6+ const configs = require ( './lib/configs' ) ;
7+ const networks = require ( './lib/networks' ) ;
8+ const services = require ( './lib/services' ) ;
9+ const tools = require ( './lib/tools' ) ;
10+
11+ class Compose {
12+ constructor ( dockerode , file , projectName ) {
13+ this . docker = dockerode ;
14+
15+ if ( file === undefined || projectName === undefined ) {
16+ throw new Error ( 'please specify a file and a project name' ) ;
17+ }
18+
19+ this . file = file ;
20+ this . projectName = projectName ;
21+
22+ try {
23+ this . recipe = yaml . load ( fs . readFileSync ( file , 'utf8' ) ) ;
24+ } catch ( e ) {
25+ throw e ;
26+ }
27+ }
28+
29+ async up ( ) {
30+ var output = { } ;
31+ try {
32+ output . secrets = await secrets ( this . docker , this . projectName , this . recipe , output ) ;
33+ output . volumes = await volumes ( this . docker , this . projectName , this . recipe , output ) ;
34+ output . configs = await configs ( this . docker , this . projectName , this . recipe , output ) ;
35+ output . networks = await networks ( this . docker , this . projectName , this . recipe , output ) ;
36+ output . services = await services ( this . docker , this . projectName , this . recipe , output ) ;
37+ return output ;
38+ } catch ( e ) {
39+ throw e ;
40+ }
41+ }
42+
43+ //ToDo: create a version with followprogress with onFinished
44+ async pull ( serviceN ) {
45+ var streams = [ ] ;
46+ var serviceNames = ( serviceN !== undefined ) ? [ serviceN ] : tools . sortServices ( this . recipe ) ;
47+ for ( var serviceName of serviceNames ) {
48+ var service = this . recipe . services [ serviceName ] ;
49+ try {
50+ streams . push ( await this . docker . pull ( service . image ) ) ;
51+ } catch ( e ) {
52+ throw e ;
53+ }
54+ }
55+ return streams ;
56+ }
57+ }
58+
59+ module . exports = Compose ;
0 commit comments