@@ -2,63 +2,76 @@ import { CONFIG_FILE_NAME_JS, CONFIG_FILE_NAME_TS, CONFIG_FILE_NAME_DISPLAY } fr
2
2
import * as path from "path" ;
3
3
import * as _ from 'lodash' ;
4
4
import * as ts from 'typescript' ;
5
- import { IFileSystem } from "../common/declarations" ;
5
+ import { IFileSystem , IProjectHelper } from "../common/declarations" ;
6
6
import { injector } from "../common/yok" ;
7
- import { IProjectData , INsConfig , IProjectConfigService } from "../definitions/project" ;
7
+ import { INsConfig , IProjectConfigService } from "../definitions/project" ;
8
8
import { IInjector } from "../common/definitions/yok" ;
9
9
10
10
export class ProjectConfigService implements IProjectConfigService {
11
- private _config : INsConfig ;
11
+ private _config : INsConfig ;
12
12
13
13
constructor (
14
14
private $fs : IFileSystem ,
15
- private $logger : ILogger ,
16
- private $injector : IInjector
15
+ private $logger : ILogger ,
16
+ private $injector : IInjector
17
17
) {
18
18
19
- }
19
+ }
20
20
21
- get projectData ( ) : IProjectData {
22
- return this . $injector . resolve ( 'projectData' ) ;
23
- }
21
+ private requireFromString ( src : string , filename : string ) : NodeModule {
22
+ // @ts -ignore
23
+ const m = new module . constructor ( ) ;
24
+ m . paths = module . paths ;
25
+ m . _compile ( src , filename ) ;
26
+ return m . exports ;
27
+ }
24
28
25
- public readConfig ( projectDir ?: string ) : INsConfig {
26
- const configJSFilePath = path . join ( projectDir || this . projectData . projectDir , CONFIG_FILE_NAME_JS ) ;
27
- const configTSFilePath = path . join ( projectDir || this . projectData . projectDir , CONFIG_FILE_NAME_TS ) ;
29
+ get projectHelper ( ) : IProjectHelper {
30
+ return this . $injector . resolve ( 'projectHelper' ) ;
31
+ }
28
32
29
- const hasTS = this . $fs . exists ( configTSFilePath ) ;
30
- const hasJS = this . $fs . exists ( configJSFilePath ) ;
33
+ public readConfig ( projectDir ?: string ) : INsConfig {
34
+ const configJSFilePath = path . join ( projectDir || this . projectHelper . projectDir , CONFIG_FILE_NAME_JS ) ;
35
+ const configTSFilePath = path . join ( projectDir || this . projectHelper . projectDir , CONFIG_FILE_NAME_TS ) ;
31
36
32
- if ( ! hasTS && ! hasJS ) {
33
- throw new Error ( `You do not appear to have a ${ CONFIG_FILE_NAME_DISPLAY } file. Please install NativeScript 7+ "npm i -g nativescript". You can also try running "ns migrate" after you have the latest installed. Exiting for now.` ) ;
34
- }
37
+ const hasTS = this . $fs . exists ( configTSFilePath ) ;
38
+ const hasJS = this . $fs . exists ( configJSFilePath ) ;
35
39
36
- if ( hasTS && hasJS ) {
37
- this . $logger . warn ( `You have both a ${ CONFIG_FILE_NAME_JS } and ${ CONFIG_FILE_NAME_TS } file. Defaulting to ${ CONFIG_FILE_NAME_TS } .` ) ;
38
- }
40
+ if ( ! hasTS && ! hasJS ) {
41
+ throw new Error ( `You do not appear to have a ${ CONFIG_FILE_NAME_DISPLAY } file. Please install NativeScript 7+ "npm i -g nativescript". You can also try running "ns migrate" after you have the latest installed. Exiting for now.` ) ;
42
+ }
43
+
44
+ if ( hasTS && hasJS ) {
45
+ this . $logger . warn ( `You have both a ${ CONFIG_FILE_NAME_JS } and ${ CONFIG_FILE_NAME_TS } file. Defaulting to ${ CONFIG_FILE_NAME_TS } .` ) ;
46
+ }
47
+
48
+ let config : INsConfig ;
39
49
40
- let config : INsConfig ;
41
-
42
50
if ( hasTS ) {
43
- const rawSource = this . $fs . readText ( configTSFilePath ) ;
44
- const transpiledSource = ts . transpileModule ( rawSource , { compilerOptions : { module : ts . ModuleKind . CommonJS } } ) ;
45
- // console.log('transpiledSource.outputText:', transpiledSource.outputText)
46
- config = eval ( transpiledSource . outputText ) ;
47
- } else {
48
- const rawSource = this . $fs . readText ( configJSFilePath ) ;
49
- // console.log('rawSource:', rawSource)
50
- config = eval ( rawSource ) ;
51
- }
51
+ const rawSource = this . $fs . readText ( configTSFilePath ) ;
52
+ const transpiledSource = ts . transpileModule ( rawSource , { compilerOptions : { module : ts . ModuleKind . CommonJS } } ) ;
53
+ const result : any = this . requireFromString ( transpiledSource . outputText , configTSFilePath ) ;
54
+ config = result [ 'default' ] ? result [ 'default' ] : result ;
55
+ // console.log('transpiledSource.outputText:', transpiledSource.outputText)
56
+ // config = eval(transpiledSource.outputText);
57
+ } else {
58
+ const rawSource = this . $fs . readText ( configJSFilePath ) ;
59
+ // console.log('rawSource:', rawSource)
60
+ // config = eval(rawSource);
61
+ config = this . requireFromString ( rawSource , configJSFilePath ) ;
62
+ }
63
+
64
+ // console.log('config: ', config);
65
+
66
+ return config ;
67
+ }
52
68
53
- return config ;
54
- }
55
-
56
- public getValue ( key : string ) : any {
57
- if ( ! this . _config ) {
58
- this . _config = this . readConfig ( ) ;
59
- }
60
- return _ . get ( this . _config , key ) ;
61
- }
69
+ public getValue ( key : string ) : any {
70
+ if ( ! this . _config ) {
71
+ this . _config = this . readConfig ( ) ;
72
+ }
73
+ return _ . get ( this . _config , key ) ;
74
+ }
62
75
}
63
76
64
77
injector . register ( 'projectConfigService' , ProjectConfigService ) ;
0 commit comments