11'use strict' ;
22
33const fs = require ( 'fs-extra' ) ;
4+ const path = require ( 'path' ) ;
45const mktemp = require ( 'mktemp' ) ;
56const execa = require ( 'execa' ) ;
67const EventEmitter = require ( 'events' ) . EventEmitter ;
78
89module . exports = class SkeletonApp {
910 constructor ( ) {
1011 this . _watched = null ;
11- this . appDir = mktemp . createDirSync ( 'test-skeleton-app-XXXXXX' ) ;
12- fs . copySync ( `${ __dirname } /../fixtures/skeleton-app` , this . appDir ) ;
12+ this . root = mktemp . createDirSync ( 'test-skeleton-app-XXXXXX' ) ;
13+ fs . copySync ( `${ __dirname } /../fixtures/skeleton-app` , this . root ) ;
1314 }
1415
1516 build ( ) {
@@ -24,40 +25,52 @@ module.exports = class SkeletonApp {
2425 return this . _watched = new WatchedBuild ( this . _ember ( [ 'serve' ] ) ) ;
2526 }
2627
27- writeFile ( path , contents ) {
28- fs . writeFileSync ( `${ this . appDir } /${ path } ` , contents , 'utf-8' ) ;
28+ updatePackageJSON ( callback ) {
29+ let pkgPath = `${ this . root } /package.json` ;
30+ let pkg = fs . readJSONSync ( pkgPath ) ;
31+ fs . writeJSONSync ( pkgPath , callback ( pkg ) || pkg , { spaces : 2 } ) ;
32+ }
33+
34+ writeFile ( filePath , contents ) {
35+ let fullPath = `${ this . root } /${ filePath } ` ;
36+ fs . ensureDirSync ( path . dirname ( fullPath ) ) ;
37+ fs . writeFileSync ( fullPath , contents , 'utf-8' ) ;
2938 }
3039
3140 readFile ( path ) {
32- return fs . readFileSync ( `${ this . appDir } /${ path } ` , 'utf-8' ) ;
41+ return fs . readFileSync ( `${ this . root } /${ path } ` , 'utf-8' ) ;
42+ }
43+
44+ removeFile ( path ) {
45+ return fs . unlinkSync ( `${ this . root } /${ path } ` ) ;
3346 }
3447
3548 teardown ( ) {
3649 if ( this . _watched ) {
3750 this . _watched . kill ( ) ;
3851 }
3952
40- this . _cleanupAppDir ( { retries : 1 } ) ;
53+ this . _cleanupRootDir ( { retries : 1 } ) ;
4154 }
4255
4356 _ember ( args ) {
4457 let ember = require . resolve ( 'ember-cli/bin/ember' ) ;
45- return execa ( 'node' , [ ember ] . concat ( args ) , { cwd : this . appDir } ) ;
58+ return execa ( 'node' , [ ember ] . concat ( args ) , { cwd : this . root } ) ;
4659 }
4760
48- _cleanupAppDir ( options ) {
61+ _cleanupRootDir ( options ) {
4962 let retries = options && options . retries || 0 ;
5063
5164 try {
52- fs . removeSync ( this . appDir ) ;
65+ fs . removeSync ( this . root ) ;
5366 } catch ( error ) {
5467 if ( retries > 0 ) {
5568 // Windows doesn't necessarily kill the process immediately, so
5669 // leave a little time before trying to remove the directory.
57- setTimeout ( ( ) => this . _cleanupAppDir ( { retries : retries - 1 } ) , 250 ) ;
70+ setTimeout ( ( ) => this . _cleanupRootDir ( { retries : retries - 1 } ) , 250 ) ;
5871 } else {
5972 // eslint-disable-next-line no-console
60- console . warn ( `Warning: unable to remove skeleton-app tmpdir ${ this . appDir } (${ error . code } )` ) ;
73+ console . warn ( `Warning: unable to remove skeleton-app tmpdir ${ this . root } (${ error . code } )` ) ;
6174 }
6275 }
6376 }
0 commit comments