1
1
const path = require ( "path" ) ;
2
2
const fs = require ( "graceful-fs" ) ;
3
+ const { promisify} = require ( "util" ) ;
4
+ const readFile = promisify ( fs . readFile ) ;
3
5
const parseYaml = require ( "js-yaml" ) . safeLoad ;
4
6
7
+ function resolveProjectPaths ( project ) {
8
+ project . path = path . resolve ( project . path ) ;
9
+ if ( project . dependencies ) {
10
+ project . dependencies . forEach ( resolveProjectPaths ) ;
11
+ }
12
+ return project ;
13
+ }
14
+
5
15
/**
6
16
* Translator for static resources
7
17
*
@@ -21,22 +31,22 @@ module.exports = {
21
31
* @param {Array } [options.parameters] CLI configuration options
22
32
* @returns {Promise } Promise resolving with a dependency tree
23
33
*/
24
- generateDependencyTree ( dirPath , options = { } ) {
34
+ async generateDependencyTree ( dirPath , options = { } ) {
25
35
const depFilePath = options . parameters && options . parameters [ 0 ] ||
26
36
path . join ( dirPath , "projectDependencies.yaml" ) ;
27
-
28
- return new Promise ( function ( resolve , reject ) {
29
- fs . readFile ( depFilePath , function ( err , buffer ) {
30
- if ( err ) {
31
- reject ( new Error (
32
- `[static translator] Failed to locate projectDependencies.json at path: "${ dirPath } " ` +
33
- `- Error: ${ err . message } ` ) ) ;
34
- } else {
35
- resolve ( parseYaml ( buffer . toString ( ) , {
36
- filename : depFilePath
37
- } ) ) ;
38
- }
37
+ try {
38
+ const buffer = await readFile ( depFilePath ) ;
39
+ const tree = parseYaml ( buffer . toString ( ) , {
40
+ filename : depFilePath
39
41
} ) ;
40
- } ) ;
42
+
43
+ // Ensure that all project paths are absolute
44
+ resolveProjectPaths ( tree ) ;
45
+ return tree ;
46
+ } catch ( err ) {
47
+ throw new Error (
48
+ `[static translator] Failed to load dependency tree from path ${ depFilePath } ` +
49
+ `- Error: ${ err . message } ` ) ;
50
+ }
41
51
}
42
52
} ;
0 commit comments