2016-07-18 18:48:34 +00:00
"use strict" ;
2017-03-28 23:01:37 +00:00
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function step ( result ) { result . done ? resolve ( result . value ) : new P ( function ( resolve ) { resolve ( result . value ) ; } ) . then ( fulfilled , rejected ) ; }
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2016-07-18 18:48:34 +00:00
const plugins = require ( "./npmdocker.plugins" ) ;
2016-07-19 17:21:06 +00:00
const paths = require ( "./npmdocker.paths" ) ;
const snippets = require ( "./npmdocker.snippets" ) ;
let config ;
2017-02-11 19:23:10 +00:00
/ * *
* the docker data used to build the internal testing container
* /
2016-07-19 17:21:06 +00:00
let dockerData = {
2017-02-11 19:23:10 +00:00
imageTag : 'npmdocker-temp-image:latest' ,
containerName : 'npmdocker-temp-container' ,
dockerProjectMountString : '' ,
dockerSockString : '' ,
dockerEnvString : ''
2016-07-19 17:21:06 +00:00
} ;
2016-07-18 18:48:34 +00:00
/ * *
* check if docker is available
* /
2016-07-19 17:21:06 +00:00
let checkDocker = ( ) => {
2016-07-18 18:48:34 +00:00
let done = plugins . q . defer ( ) ;
2017-02-11 19:23:10 +00:00
plugins . beautylog . ora . text ( 'checking docker...' ) ;
2017-03-28 23:01:37 +00:00
if ( plugins . smartshell . which ( 'docker' ) ) {
2017-02-11 19:23:10 +00:00
plugins . beautylog . ok ( 'Docker found!' ) ;
2016-07-18 18:48:34 +00:00
done . resolve ( ) ;
}
else {
2017-02-11 19:23:10 +00:00
done . reject ( new Error ( 'docker not found on this machine' ) ) ;
2016-07-18 18:48:34 +00:00
}
return done . promise ;
} ;
2016-07-19 17:21:06 +00:00
/ * *
* builds the Dockerfile according to the config in the project
* /
let buildDockerFile = ( ) => {
let done = plugins . q . defer ( ) ;
2017-02-11 19:23:10 +00:00
plugins . beautylog . ora . text ( 'building Dockerfile...' ) ;
2016-07-19 17:21:06 +00:00
let dockerfile = snippets . dockerfileSnippet ( {
baseImage : config . baseImage ,
command : config . command
} ) ;
plugins . beautylog . info ( ` Base image is: ${ config . baseImage } ` ) ;
plugins . beautylog . info ( ` Command is: ${ config . command } ` ) ;
2017-10-13 14:44:25 +00:00
plugins . smartfile . memory . toFsSync ( dockerfile , plugins . path . join ( paths . cwd , 'npmdocker' ) ) ;
2017-02-11 19:23:10 +00:00
plugins . beautylog . ok ( 'Dockerfile created!' ) ;
2017-10-13 14:44:25 +00:00
plugins . beautylog . ora . stop ( ) ;
2016-07-19 17:21:06 +00:00
done . resolve ( ) ;
return done . promise ;
} ;
/ * *
* builds the Dockerimage from the built Dockerfile
* /
2017-03-28 23:01:37 +00:00
let buildDockerImage = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2017-10-13 14:44:25 +00:00
plugins . beautylog . info ( 'pulling latest base image from registry...' ) ;
yield plugins . smartshell . exec ( ` docker pull ${ config . baseImage } ` ) ;
plugins . beautylog . ora . text ( 'building Dockerimage...' ) ;
let execResult = yield plugins . smartshell . execSilent ( ` docker build -f npmdocker -t ${ dockerData . imageTag } ${ paths . cwd } ` ) ;
if ( execResult . exitCode !== 0 ) {
console . log ( execResult . stdout ) ;
process . exit ( 1 ) ;
}
plugins . beautylog . ok ( 'Dockerimage built!' ) ;
2017-03-28 23:01:37 +00:00
} ) ;
let buildDockerProjectMountString = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2017-02-11 19:23:10 +00:00
if ( process . env . CI !== 'true' ) {
2016-08-04 20:25:15 +00:00
dockerData . dockerProjectMountString = ` -v ${ paths . cwd } :/workspace ` ;
2016-07-28 22:52:30 +00:00
}
2017-03-28 23:01:37 +00:00
} ) ;
2017-02-11 22:37:01 +00:00
/ * *
* builds an environment string that docker cli understands
* /
2017-03-28 23:01:37 +00:00
let buildDockerEnvString = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2016-08-04 20:25:15 +00:00
for ( let keyValueObjectArg of config . keyValueObjectArray ) {
2018-05-18 11:09:14 +00:00
let envString = ( dockerData . dockerEnvString =
dockerData . dockerEnvString + ` -e ${ keyValueObjectArg . key } = ${ keyValueObjectArg . value } ` ) ;
2016-08-04 20:25:15 +00:00
}
2017-03-28 23:01:37 +00:00
} ) ;
2017-02-11 22:37:01 +00:00
/ * *
* creates string to mount the docker . sock inside the testcontainer
* /
2017-03-28 23:01:37 +00:00
let buildDockerSockString = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2016-07-28 16:01:02 +00:00
if ( config . dockerSock ) {
2016-08-04 20:25:15 +00:00
dockerData . dockerSockString = ` -v /var/run/docker.sock:/var/run/docker.sock ` ;
2016-07-28 16:01:02 +00:00
}
2017-03-28 23:01:37 +00:00
} ) ;
2016-08-04 20:25:15 +00:00
/ * *
* creates a container by running the built Dockerimage
* /
2017-03-28 23:01:37 +00:00
let runDockerImage = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2016-08-04 20:25:15 +00:00
let done = plugins . q . defer ( ) ;
2017-02-11 19:23:10 +00:00
plugins . beautylog . ora . text ( 'starting Container...' ) ;
plugins . beautylog . ora . end ( ) ;
plugins . beautylog . log ( 'now running Dockerimage' ) ;
2017-03-28 23:01:37 +00:00
config . exitCode = ( yield plugins . smartshell . exec ( ` docker run ${ dockerData . dockerProjectMountString } ${ dockerData . dockerSockString } ${ dockerData . dockerEnvString } --name ${ dockerData . containerName } ${ dockerData . imageTag } ` ) ) . exitCode ;
} ) ;
2017-02-11 22:37:01 +00:00
/ * *
* cleans up : deletes the test container
* /
2017-03-28 23:01:37 +00:00
let deleteDockerContainer = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield plugins . smartshell . execSilent ( ` docker rm -f ${ dockerData . containerName } ` ) ;
} ) ;
2017-02-11 22:37:01 +00:00
/ * *
* cleans up deletes the test image
* /
2017-03-28 23:01:37 +00:00
let deleteDockerImage = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2017-04-02 12:48:23 +00:00
yield plugins . smartshell . execSilent ( ` docker rmi ${ dockerData . imageTag } ` ) . then ( ( response ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
if ( response . exitCode !== 0 ) {
console . log ( response . stdout ) ;
}
} ) ) ;
2017-03-28 23:01:37 +00:00
} ) ;
let preClean = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield deleteDockerImage ( )
2016-07-29 20:00:22 +00:00
. then ( deleteDockerContainer )
2017-03-28 23:01:37 +00:00
. then ( ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2017-02-11 19:23:10 +00:00
plugins . beautylog . ok ( 'ensured clean Docker environment!' ) ;
2017-03-28 23:01:37 +00:00
} ) ) ;
} ) ;
let postClean = ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield deleteDockerContainer ( )
2016-07-29 20:00:22 +00:00
. then ( deleteDockerImage )
2017-03-28 23:01:37 +00:00
. then ( ( ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
2017-02-11 19:23:10 +00:00
plugins . beautylog . ok ( 'cleaned up!' ) ;
2017-03-28 23:01:37 +00:00
} ) ) ;
2017-10-13 16:06:12 +00:00
plugins . smartfile . fs . removeSync ( paths . npmdockerFile ) ;
2017-03-28 23:01:37 +00:00
} ) ;
exports . run = ( configArg ) => _ _awaiter ( this , void 0 , void 0 , function * ( ) {
plugins . beautylog . ora . start ( ) ;
2016-07-19 17:21:06 +00:00
config = configArg ;
2017-03-28 23:01:37 +00:00
let resultConfig = yield checkDocker ( )
2016-07-29 20:00:22 +00:00
. then ( preClean )
2016-07-19 17:21:06 +00:00
. then ( buildDockerFile )
. then ( buildDockerImage )
2016-08-04 20:25:15 +00:00
. then ( buildDockerProjectMountString )
. then ( buildDockerEnvString )
. then ( buildDockerSockString )
2016-07-19 17:21:06 +00:00
. then ( runDockerImage )
2016-07-29 20:00:22 +00:00
. then ( postClean )
2018-05-18 11:09:14 +00:00
. catch ( err => {
console . log ( err ) ;
} ) ;
2017-03-28 23:01:37 +00:00
return config ;
} ) ;
2018-05-18 11:09:14 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibnBtZG9ja2VyLmRvY2tlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25wbWRvY2tlci5kb2NrZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7OztBQUFBLCtDQUErQztBQUMvQywyQ0FBMkM7QUFDM0MsaURBQWlEO0FBS2pELElBQUksTUFBZSxDQUFDO0FBRXBCOztHQUVHO0FBQ0gsSUFBSSxVQUFVLEdBQUc7SUFDZixRQUFRLEVBQUUsNkJBQTZCO0lBQ3ZDLGFBQWEsRUFBRSwwQkFBMEI7SUFDekMsd0JBQXdCLEVBQUUsRUFBRTtJQUM1QixnQkFBZ0IsRUFBRSxFQUFFO0lBQ3BCLGVBQWUsRUFBRSxFQUFFO0NBQ3BCLENBQUM7QUFFRjs7R0FFRztBQUNILElBQUksV0FBVyxHQUFHLEdBQUcsRUFBRTtJQUNyQixJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQzdCLE9BQU8sQ0FBQyxTQUFTLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxvQkFBb0IsQ0FBQyxDQUFDO0lBQ2pELElBQUksT0FBTyxDQUFDLFVBQVUsQ0FBQyxLQUFLLENBQUMsUUFBUSxDQUFDLEVBQUU7UUFDdEMsT0FBTyxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsZUFBZSxDQUFDLEN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