Skip to content

Commit

Permalink
style(src): modernize code
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky authored and danez committed Feb 25, 2018
1 parent be187cd commit 27f44f8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 229 deletions.
26 changes: 26 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const path = require("path");
const exists = require("./utils/exists");

const configs = [".babelrc", ".babelrc.js", "package.json"];

module.exports = function find(fs, start) {
for (let config of configs) {
config = path.join(start, config);

if (exists(fs, config)) {
if (
path.basename(config) !== "package.json" ||
typeof require(config).babel === "object"
) {
return config;
}
}
}

const up = path.dirname(start);

// Reached root
if (up !== start) {
return find(fs, up);
}
};
186 changes: 0 additions & 186 deletions src/fs-cache.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/resolve-rc.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/utils/exists.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = function(fileSystem, filename) {
module.exports = function(fs, config) {
let exists = false;

try {
exists = fileSystem.statSync(filename).isFile();
exists = fs.statSync(config).isFile();
} catch (err) {
if (err.code !== "ENOENT") throw err;
}
Expand Down
10 changes: 5 additions & 5 deletions src/utils/read.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const path = require("path");

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

return JSON.stringify(pkg.babel);
}

// Webpack `fs` return Buffer
return fileSystem.readFileSync(filename).toString("utf8");
// memoryFS (webpack) returns a {Buffer}
return fs.readFileSync(config).toString("utf-8");
};
15 changes: 8 additions & 7 deletions src/utils/relative.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const path = require("path");

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

// If the file is in a completely different root folder use the absolute path of file.
if (rootPath && rootPath !== fileRootPath) {
return filename;
// If the file is in a completely different root folder
// use the absolute path of the file
if (rootPath && rootPath !== filePath) {
return file;
}

return path.relative(sourceRoot, filename);
return path.relative(root, file);
};
12 changes: 7 additions & 5 deletions test/resolverc.test.js → test/config.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import test from "ava";
import path from "path";
import resolveRc from "../lib/resolve-rc";

import fs from "fs";
import path from "path";

import rc from "../lib/config";

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

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

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

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

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

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

0 comments on commit 27f44f8

Please sign in to comment.