Skip to content

Commit 8116c7d

Browse files
committedJan 26, 2020
fix: Address race condition by simplifying main loop for BEM conversion.
1 parent 07ecaf1 commit 8116c7d

File tree

1 file changed

+16
-22
lines changed
  • packages/@css-blocks/bem-to-blocks/src

1 file changed

+16
-22
lines changed
 

Diff for: ‎packages/@css-blocks/bem-to-blocks/src/index.ts

+16-22
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,22 @@ type BemToBlockClassMap = WeakMap<BemSelector, BlockClassSelector>;
1717
const EMPTY_ELEMENT_PLACEHOLDER = "EMPTY-ELEMENT-PLACEHOLDER";
1818
const COMMON_PREFIXES_FOR_MODIFIERS = ["is"];
1919

20-
export function convertBemToBlocks(files: Array<string>): Promise<void>[] {
21-
let promises: Promise<void>[] = [];
22-
files.forEach(file => {
23-
fs.readFile(file, async (_err, css) => {
24-
postcss([
25-
// Using postcss-simple-vars to pass the fileName to the plugin
26-
vars({
27-
variables: () => {return {fileName: path.relative(process.cwd(), file)}; },
28-
}),
29-
bemToBlocksPlugin,
30-
])
31-
.process(css, { from: file })
32-
.then(output => {
33-
// rewrite the file with the processed output
34-
const parsedFilePath = path.parse(file);
35-
const blockFilePath = Object.assign(parsedFilePath, {ext: `.block${parsedFilePath.ext}`, base: undefined} );
36-
promises.push(fs.writeFile(path.format(blockFilePath), output.toString()));
37-
}).catch(e => {throw (e); });
38-
39-
});
40-
});
41-
return promises;
20+
export async function convertBemToBlocks(files: Array<string>): Promise<void> {
21+
for (let file of files) {
22+
let fileName = path.relative(process.cwd(), file);
23+
let processor = postcss([
24+
// Using postcss-simple-vars to pass the fileName to the plugin
25+
vars({
26+
variables: () => { return { fileName }; },
27+
}),
28+
bemToBlocksPlugin,
29+
]);
30+
let css = fs.readFileSync(file, "utf-8");
31+
let result = await processor.process(css, { from: fileName});
32+
const parsedFilePath = path.parse(file);
33+
const blockFilePath = Object.assign(parsedFilePath, {ext: `.block${parsedFilePath.ext}`, base: undefined} );
34+
await fs.writeFile(path.format(blockFilePath), result.toString());
35+
}
4236
}
4337

4438
/**

0 commit comments

Comments
 (0)