1
1
import {
2
2
Configuration ,
3
3
} from "@css-blocks/core" ;
4
- import { Transform , cosmiconfig } from "cosmiconfig" ;
4
+ import { cosmiconfigSync as cosmiconfig , TransformSync } from "cosmiconfig" ;
5
5
import * as debugGenerator from "debug" ;
6
6
import merge = require( "lodash.merge" ) ;
7
7
import { dirname , resolve } from "path" ;
@@ -16,7 +16,7 @@ type UnknownObject = {[k: string]: unknown};
16
16
*
17
17
* If preprocessors is a string, attempts to load a javascript file from that location.
18
18
*/
19
- const transform : Transform = async ( result ) => {
19
+ const transform : TransformSync = ( result ) => {
20
20
if ( ! result ) return null ;
21
21
debug ( `Processing raw configuration loaded from ${ result . filepath } ` ) ;
22
22
let dir = dirname ( result . filepath ) ;
@@ -30,14 +30,14 @@ const transform: Transform = async (result) => {
30
30
if ( typeof config . preprocessors === "string" ) {
31
31
let file = resolve ( dir , config . preprocessors ) ;
32
32
debug ( `Loading preprocessors from ${ file } ` ) ;
33
- config . preprocessors = await import ( file ) as UnknownObject ;
33
+ config . preprocessors = require ( file ) as UnknownObject ;
34
34
}
35
35
36
36
// if it's a string, load a file that exports an importer and optionally some data.
37
37
if ( typeof config . importer === "string" ) {
38
38
let file = resolve ( dir , config . importer ) ;
39
39
debug ( `Loading importer from ${ file } ` ) ;
40
- let { importer, data} = await import ( file ) as UnknownObject ;
40
+ let { importer, data} = require ( file ) as UnknownObject ;
41
41
config . importer = importer ;
42
42
if ( data ) {
43
43
config . importerData = data ;
@@ -51,7 +51,7 @@ const transform: Transform = async (result) => {
51
51
let baseConfigFile = resolve ( dir , config . extends ) ;
52
52
delete config . extends ;
53
53
debug ( `Extending configuration found at: ${ baseConfigFile } ` ) ;
54
- let baseConfig = await _load ( baseConfigFile , transform ) ;
54
+ let baseConfig = _load ( baseConfigFile , transform ) ;
55
55
// we don't want to merge or copy the importer object or the importer data object.
56
56
let importer = config . importer || baseConfig . importer ;
57
57
let importerData = config . importerData || baseConfig . importerData ;
@@ -71,10 +71,10 @@ const transform: Transform = async (result) => {
71
71
* This transform only runs on the final configuration file. It does not run on
72
72
* any configuration file that is being extended.
73
73
*/
74
- const transformFinal : Transform = async ( result ) => {
74
+ const transformFinal : TransformSync = ( result ) => {
75
75
if ( ! result ) return null ;
76
76
debug ( `Using configuration file found at ${ result . filepath } ` ) ;
77
- result = await transform ( result ) ;
77
+ result = transform ( result ) ;
78
78
if ( ! result ) return null ;
79
79
if ( ! result . config . rootDir ) {
80
80
let dir = dirname ( result . filepath ) ;
@@ -84,6 +84,12 @@ const transformFinal: Transform = async (result) => {
84
84
return result ;
85
85
} ;
86
86
87
+ const SEARCH_PLACES = [
88
+ "css-blocks.config.json" ,
89
+ "css-blocks.config.js" ,
90
+ "package.json" ,
91
+ ] ;
92
+
87
93
/**
88
94
* Starting in the directory provided, work up the directory hierarchy looking
89
95
* for css-blocks configuration files.
@@ -94,19 +100,27 @@ const transformFinal: Transform = async (result) => {
94
100
* @param [searchDirectory] (optional) The directory to start looking in.
95
101
* Defaults to the current working directory.
96
102
*/
97
- export async function search ( searchDirectory ?: string ) : Promise < Partial < Configuration > | null > {
103
+ export function searchSync ( searchDirectory ?: string ) : Partial < Configuration > | null {
98
104
let loader = cosmiconfig ( "css-blocks" , {
99
105
transform : transformFinal ,
100
- searchPlaces : [
101
- "css-blocks.config.json" ,
102
- "css-blocks.config.js" ,
103
- "package.json" ,
104
- ] ,
106
+ searchPlaces : SEARCH_PLACES ,
105
107
} ) ;
106
- let result = await loader . search ( searchDirectory ) ;
108
+ let result = loader . search ( searchDirectory ) ;
107
109
return result && result . config as Partial < Configuration > ;
108
110
}
109
111
112
+ /**
113
+ * Async wrapper for searchSync for backwards compatibility.
114
+ * @see {searchSync}
115
+ */
116
+ export function search ( searchDirectory ?: string ) {
117
+ try {
118
+ return Promise . resolve ( searchSync ( searchDirectory ) ) ;
119
+ } catch ( e ) {
120
+ return Promise . reject ( e ) ;
121
+ }
122
+ }
123
+
110
124
/**
111
125
* Load configuration from a known path to the specific file.
112
126
* Supports .js and .json files. If it's a file named "package.json",
@@ -121,10 +135,10 @@ export async function load(configPath: string): Promise<Partial<Configuration>>
121
135
return _load ( configPath , transformFinal ) ;
122
136
}
123
137
124
- async function _load ( configPath : string , transform : Transform ) : Promise < Partial < Configuration > > {
138
+ function _load ( configPath : string , transform : TransformSync ) : Partial < Configuration > {
125
139
let loader = cosmiconfig ( "css-blocks" , {
126
140
transform,
127
141
} ) ;
128
- let result = await loader . load ( configPath ) ;
142
+ let result = loader . load ( configPath ) ;
129
143
return result ! . config as Partial < Configuration > ;
130
144
}
0 commit comments