Skip to content

Commit 27f44f8

Browse files
michael-ciniawskydanez
authored andcommitted
style(src): modernize code
1 parent be187cd commit 27f44f8

File tree

7 files changed

+48
-229
lines changed

7 files changed

+48
-229
lines changed

src/config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const path = require("path");
2+
const exists = require("./utils/exists");
3+
4+
const configs = [".babelrc", ".babelrc.js", "package.json"];
5+
6+
module.exports = function find(fs, start) {
7+
for (let config of configs) {
8+
config = path.join(start, config);
9+
10+
if (exists(fs, config)) {
11+
if (
12+
path.basename(config) !== "package.json" ||
13+
typeof require(config).babel === "object"
14+
) {
15+
return config;
16+
}
17+
}
18+
}
19+
20+
const up = path.dirname(start);
21+
22+
// Reached root
23+
if (up !== start) {
24+
return find(fs, up);
25+
}
26+
};

src/fs-cache.js

Lines changed: 0 additions & 186 deletions
This file was deleted.

src/resolve-rc.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/utils/exists.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
module.exports = function(fileSystem, filename) {
1+
module.exports = function(fs, config) {
22
let exists = false;
33

44
try {
5-
exists = fileSystem.statSync(filename).isFile();
5+
exists = fs.statSync(config).isFile();
66
} catch (err) {
77
if (err.code !== "ENOENT") throw err;
88
}

src/utils/read.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const path = require("path");
22

3-
module.exports = function readBabelConfig(fileSystem, filename) {
4-
if (path.basename(filename) === "package.json") {
5-
const pkg = require(filename);
3+
module.exports = function(fs, config) {
4+
if (path.basename(config) === "package.json") {
5+
const pkg = require(config);
66

77
return JSON.stringify(pkg.babel);
88
}
99

10-
// Webpack `fs` return Buffer
11-
return fileSystem.readFileSync(filename).toString("utf8");
10+
// memoryFS (webpack) returns a {Buffer}
11+
return fs.readFileSync(config).toString("utf-8");
1212
};

src/utils/relative.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const path = require("path");
22

3-
module.exports = function relative(sourceRoot, filename) {
4-
const rootPath = sourceRoot.replace(/\\/g, "/").split("/")[1];
5-
const fileRootPath = filename.replace(/\\/g, "/").split("/")[1];
3+
module.exports = function relative(root, file) {
4+
const rootPath = root.replace(/\\/g, "/").split("/")[1];
5+
const filePath = file.replace(/\\/g, "/").split("/")[1];
66

7-
// If the file is in a completely different root folder use the absolute path of file.
8-
if (rootPath && rootPath !== fileRootPath) {
9-
return filename;
7+
// If the file is in a completely different root folder
8+
// use the absolute path of the file
9+
if (rootPath && rootPath !== filePath) {
10+
return file;
1011
}
1112

12-
return path.relative(sourceRoot, filename);
13+
return path.relative(root, file);
1314
};
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import test from "ava";
2-
import path from "path";
3-
import resolveRc from "../lib/resolve-rc";
2+
43
import fs from "fs";
4+
import path from "path";
5+
6+
import rc from "../lib/config";
57

68
test("should find the .babelrc file", t => {
79
const start = path.join(__dirname, "fixtures/babelrc-test/1/2/3");
8-
const result = resolveRc(fs, start);
10+
const result = rc(fs, start);
911

1012
t.is(result, path.join(__dirname, "fixtures/babelrc-test/.babelrc"));
1113
});
1214

1315
test("should find the .babelrc.js config", t => {
1416
const start = path.join(__dirname, "fixtures/babelrc-test/1/2/5/4");
15-
const result = resolveRc(fs, start);
17+
const result = rc(fs, start);
1618

1719
t.is(result, path.join(__dirname, "fixtures/babelrc-test/1/2/5/.babelrc.js"));
1820
});
1921

2022
test("should find the package.json babel config", t => {
2123
const start = path.join(__dirname, "fixtures/package-test");
22-
const result = resolveRc(fs, start);
24+
const result = rc(fs, start);
2325

2426
t.is(result, path.join(__dirname, "fixtures/package-test/package.json"));
2527
});

0 commit comments

Comments
 (0)