1
1
'use strict' ;
2
2
3
3
const fs = require ( 'fs-extra' ) ;
4
+ const path = require ( 'path' ) ;
4
5
const mktemp = require ( 'mktemp' ) ;
5
6
const execa = require ( 'execa' ) ;
6
7
const EventEmitter = require ( 'events' ) . EventEmitter ;
7
8
8
9
module . exports = class SkeletonApp {
9
10
constructor ( ) {
10
11
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 ) ;
13
14
}
14
15
15
16
build ( ) {
@@ -24,40 +25,52 @@ module.exports = class SkeletonApp {
24
25
return this . _watched = new WatchedBuild ( this . _ember ( [ 'serve' ] ) ) ;
25
26
}
26
27
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' ) ;
29
38
}
30
39
31
40
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 } ` ) ;
33
46
}
34
47
35
48
teardown ( ) {
36
49
if ( this . _watched ) {
37
50
this . _watched . kill ( ) ;
38
51
}
39
52
40
- this . _cleanupAppDir ( { retries : 1 } ) ;
53
+ this . _cleanupRootDir ( { retries : 1 } ) ;
41
54
}
42
55
43
56
_ember ( args ) {
44
57
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 } ) ;
46
59
}
47
60
48
- _cleanupAppDir ( options ) {
61
+ _cleanupRootDir ( options ) {
49
62
let retries = options && options . retries || 0 ;
50
63
51
64
try {
52
- fs . removeSync ( this . appDir ) ;
65
+ fs . removeSync ( this . root ) ;
53
66
} catch ( error ) {
54
67
if ( retries > 0 ) {
55
68
// Windows doesn't necessarily kill the process immediately, so
56
69
// 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 ) ;
58
71
} else {
59
72
// 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 } )` ) ;
61
74
}
62
75
}
63
76
}
0 commit comments