Skip to content

Commit 5c88e24

Browse files
committedMay 23, 2020
feat: Make configuration loading synchronous with async wrapper.
Closes #365.
1 parent 314a09e commit 5c88e24

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed
 

‎packages/@css-blocks/config/src/index.ts

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
Configuration,
33
} from "@css-blocks/core";
4-
import { Transform, cosmiconfig } from "cosmiconfig";
4+
import { cosmiconfigSync as cosmiconfig, TransformSync } from "cosmiconfig";
55
import * as debugGenerator from "debug";
66
import merge = require("lodash.merge");
77
import { dirname, resolve } from "path";
@@ -16,7 +16,7 @@ type UnknownObject = {[k: string]: unknown};
1616
*
1717
* If preprocessors is a string, attempts to load a javascript file from that location.
1818
*/
19-
const transform: Transform = async (result) => {
19+
const transform: TransformSync = (result) => {
2020
if (!result) return null;
2121
debug(`Processing raw configuration loaded from ${result.filepath}`);
2222
let dir = dirname(result.filepath);
@@ -30,14 +30,14 @@ const transform: Transform = async (result) => {
3030
if (typeof config.preprocessors === "string") {
3131
let file = resolve(dir, config.preprocessors);
3232
debug(`Loading preprocessors from ${file}`);
33-
config.preprocessors = await import(file) as UnknownObject;
33+
config.preprocessors = require(file) as UnknownObject;
3434
}
3535

3636
// if it's a string, load a file that exports an importer and optionally some data.
3737
if (typeof config.importer === "string") {
3838
let file = resolve(dir, config.importer);
3939
debug(`Loading importer from ${file}`);
40-
let {importer, data} = await import(file) as UnknownObject;
40+
let {importer, data} = require(file) as UnknownObject;
4141
config.importer = importer;
4242
if (data) {
4343
config.importerData = data;
@@ -51,7 +51,7 @@ const transform: Transform = async (result) => {
5151
let baseConfigFile = resolve(dir, config.extends);
5252
delete config.extends;
5353
debug(`Extending configuration found at: ${baseConfigFile}`);
54-
let baseConfig = await _load(baseConfigFile, transform);
54+
let baseConfig = _load(baseConfigFile, transform);
5555
// we don't want to merge or copy the importer object or the importer data object.
5656
let importer = config.importer || baseConfig.importer;
5757
let importerData = config.importerData || baseConfig.importerData;
@@ -71,10 +71,10 @@ const transform: Transform = async (result) => {
7171
* This transform only runs on the final configuration file. It does not run on
7272
* any configuration file that is being extended.
7373
*/
74-
const transformFinal: Transform = async (result) => {
74+
const transformFinal: TransformSync = (result) => {
7575
if (!result) return null;
7676
debug(`Using configuration file found at ${result.filepath}`);
77-
result = await transform(result);
77+
result = transform(result);
7878
if (!result) return null;
7979
if (!result.config.rootDir) {
8080
let dir = dirname(result.filepath);
@@ -84,6 +84,12 @@ const transformFinal: Transform = async (result) => {
8484
return result;
8585
};
8686

87+
const SEARCH_PLACES = [
88+
"css-blocks.config.json",
89+
"css-blocks.config.js",
90+
"package.json",
91+
];
92+
8793
/**
8894
* Starting in the directory provided, work up the directory hierarchy looking
8995
* for css-blocks configuration files.
@@ -94,19 +100,27 @@ const transformFinal: Transform = async (result) => {
94100
* @param [searchDirectory] (optional) The directory to start looking in.
95101
* Defaults to the current working directory.
96102
*/
97-
export async function search(searchDirectory?: string): Promise<Partial<Configuration> | null> {
103+
export function searchSync(searchDirectory?: string): Partial<Configuration> | null {
98104
let loader = cosmiconfig("css-blocks", {
99105
transform: transformFinal,
100-
searchPlaces: [
101-
"css-blocks.config.json",
102-
"css-blocks.config.js",
103-
"package.json",
104-
],
106+
searchPlaces: SEARCH_PLACES,
105107
});
106-
let result = await loader.search(searchDirectory);
108+
let result = loader.search(searchDirectory);
107109
return result && result.config as Partial<Configuration>;
108110
}
109111

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+
110124
/**
111125
* Load configuration from a known path to the specific file.
112126
* 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>>
121135
return _load(configPath, transformFinal);
122136
}
123137

124-
async function _load(configPath: string, transform: Transform): Promise<Partial<Configuration>> {
138+
function _load(configPath: string, transform: TransformSync): Partial<Configuration> {
125139
let loader = cosmiconfig("css-blocks", {
126140
transform,
127141
});
128-
let result = await loader.load(configPath);
142+
let result = loader.load(configPath);
129143
return result!.config as Partial<Configuration>;
130144
}

0 commit comments

Comments
 (0)
Please sign in to comment.