find-babel-config
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -1,5 +0,20 @@ | ||
# Change Log | ||
# Changelog | ||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. | ||
## [2.0.0](https://github.com/tleunen/find-babel-config/compare/v1.2.0...v2.0.0) (2023-01-09) | ||
### ⚠ BREAKING CHANGES | ||
* The order or config file lookup is ".babelrc, .babelrc.js, babel.config.js, babel.config.json, package.json" | ||
* Node 16 is the minimum supported version | ||
### Bug Fixes | ||
* Fix order or config file lookup to be the same in sync and async functions ([#38](https://github.com/tleunen/find-babel-config/issues/38)) ([4fde4bb](https://github.com/tleunen/find-babel-config/commit/4fde4bbe9afec0d9ecc413d26c88d94fef33848f)) | ||
* Update dependencies ([#37](https://github.com/tleunen/find-babel-config/issues/37)) ([4198a93](https://github.com/tleunen/find-babel-config/commit/4198a93f68cda2a1d9004d542fb0435df4065615)) | ||
<a name="1.2.0"></a> | ||
@@ -6,0 +21,0 @@ # [1.2.0](https://github.com/tleunen/find-babel-config/compare/v1.1.0...v1.2.0) (2019-03-04) |
325
lib/index.js
@@ -1,179 +0,216 @@ | ||
'use strict'; | ||
"use strict"; | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var JSON5 = require('json5'); | ||
var pathExists = require('path-exists'); | ||
const path = require('path'); | ||
var INFINITY = 1 / 0; | ||
var BABELRC_FILENAME = '.babelrc'; | ||
var BABELRC_JS_FILENAME = '.babelrc.js'; | ||
var BABEL_CONFIG_JS_FILENAME = 'babel.config.js'; | ||
var PACKAGE_FILENAME = 'package.json'; | ||
const fs = require('fs'); | ||
var nullConf = { file: null, config: null }; | ||
const JSON5 = require('json5'); | ||
const pathExists = require('path-exists'); | ||
const INFINITY = 1 / 0; | ||
const BABELRC_FILENAME = '.babelrc'; | ||
const BABELRC_JS_FILENAME = '.babelrc.js'; | ||
const BABEL_CONFIG_JS_FILENAME = 'babel.config.js'; | ||
const BABEL_CONFIG_JSON_FILENAME = 'babel.config.json'; | ||
const PACKAGE_FILENAME = 'package.json'; | ||
const nullConf = { | ||
file: null, | ||
config: null | ||
}; | ||
function getBabelJsConfig(file) { | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
var configModule = require(file); | ||
// eslint-disable-next-line global-require, import/no-dynamic-require | ||
const configModule = require(file); | ||
if (typeof configModule === 'function') { | ||
return configModule(); | ||
} | ||
if (typeof configModule === 'function') { | ||
return configModule(); | ||
} // eslint-disable-next-line no-underscore-dangle | ||
// eslint-disable-next-line no-underscore-dangle | ||
return configModule && configModule.__esModule ? configModule.default : configModule; | ||
return configModule && configModule.__esModule ? configModule.default : configModule; | ||
} | ||
function asyncFind(resolve, dir, depth) { | ||
if (depth < 0) { | ||
return resolve(nullConf); | ||
if (depth < 0) { | ||
return resolve(nullConf); | ||
} | ||
const babelrc = path.join(dir, BABELRC_FILENAME); | ||
return pathExists(babelrc).then(exists => { | ||
if (exists) { | ||
fs.readFile(babelrc, 'utf8', (err, data) => { | ||
if (!err) { | ||
resolve({ | ||
file: babelrc, | ||
config: JSON5.parse(data) | ||
}); | ||
} | ||
}); | ||
} | ||
var babelrc = path.join(dir, BABELRC_FILENAME); | ||
return pathExists(babelrc).then(function (exists) { | ||
if (exists) { | ||
fs.readFile(babelrc, 'utf8', function (err, data) { | ||
if (!err) { | ||
resolve({ | ||
file: babelrc, | ||
config: JSON5.parse(data) | ||
}); | ||
} | ||
}); | ||
return exists; | ||
}).then(exists => { | ||
if (!exists) { | ||
const babelJSrc = path.join(dir, BABELRC_JS_FILENAME); | ||
return pathExists(babelJSrc).then(ex => { | ||
if (ex) { | ||
const config = getBabelJsConfig(babelJSrc); | ||
resolve({ | ||
file: babelJSrc, | ||
config | ||
}); | ||
} | ||
return exists; | ||
}).then(function (exists) { | ||
if (!exists) { | ||
var babelJSrc = path.join(dir, BABELRC_JS_FILENAME); | ||
return pathExists(babelJSrc).then(function (ex) { | ||
if (ex) { | ||
var config = getBabelJsConfig(babelJSrc); | ||
resolve({ | ||
file: babelJSrc, | ||
config | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
return exists; | ||
}).then(exists => { | ||
if (!exists) { | ||
const babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME); | ||
return pathExists(babelConfigJSrc).then(ex => { | ||
if (ex) { | ||
const config = getBabelJsConfig(babelConfigJSrc); | ||
resolve({ | ||
file: babelConfigJSrc, | ||
config | ||
}); | ||
} | ||
return exists; | ||
}).then(function (exists) { | ||
if (!exists) { | ||
var packageFile = path.join(dir, PACKAGE_FILENAME); | ||
return pathExists(packageFile).then(function (ex) { | ||
if (ex) { | ||
fs.readFile(packageFile, 'utf8', function (err, data) { | ||
var packageJson = JSON.parse(data); | ||
if (packageJson.babel) { | ||
resolve({ | ||
file: packageFile, | ||
config: packageJson.babel | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
return exists; | ||
}).then(exists => { | ||
if (!exists) { | ||
const babelConfigJsonSrc = path.join(dir, BABEL_CONFIG_JSON_FILENAME); | ||
return pathExists(babelConfigJsonSrc).then(ex => { | ||
if (ex) { | ||
fs.readFile(babelConfigJsonSrc, 'utf8', (err, data) => { | ||
if (!err) { | ||
resolve({ | ||
file: babelConfigJsonSrc, | ||
config: JSON5.parse(data) | ||
}); | ||
} | ||
}); | ||
} | ||
return exists; | ||
}).then(function (exists) { | ||
if (!exists) { | ||
var babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME); | ||
return pathExists(babelConfigJSrc).then(function (ex) { | ||
if (ex) { | ||
var config = getBabelJsConfig(babelConfigJSrc); | ||
resolve({ | ||
file: babelConfigJSrc, | ||
config | ||
}); | ||
} | ||
}); | ||
} | ||
return exists; | ||
}).then(function (exists) { | ||
if (!exists) { | ||
var nextDir = path.dirname(dir); | ||
if (nextDir === dir) { | ||
resolve(nullConf); | ||
} else { | ||
asyncFind(resolve, nextDir, depth - 1); | ||
}); | ||
} | ||
return exists; | ||
}).then(exists => { | ||
if (!exists) { | ||
const packageFile = path.join(dir, PACKAGE_FILENAME); | ||
return pathExists(packageFile).then(ex => { | ||
if (ex) { | ||
fs.readFile(packageFile, 'utf8', (err, data) => { | ||
const packageJson = JSON.parse(data); | ||
if (packageJson.babel) { | ||
resolve({ | ||
file: packageFile, | ||
config: packageJson.babel | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
module.exports = function findBabelConfig(start) { | ||
var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : INFINITY; | ||
return exists; | ||
}).then(exists => { | ||
if (!exists) { | ||
const nextDir = path.dirname(dir); | ||
if (!start) { | ||
return new Promise(function (resolve) { | ||
return resolve(nullConf); | ||
}); | ||
if (nextDir === dir) { | ||
resolve(nullConf); | ||
} else { | ||
asyncFind(resolve, nextDir, depth - 1); | ||
} | ||
} | ||
}); | ||
} | ||
var dir = path.isAbsolute(start) ? start : path.join(process.cwd(), start); | ||
module.exports = function findBabelConfig(start, depth = INFINITY) { | ||
if (!start) { | ||
return new Promise(resolve => resolve(nullConf)); | ||
} | ||
return new Promise(function (resolve) { | ||
asyncFind(resolve, dir, depth); | ||
}); | ||
const dir = path.isAbsolute(start) ? start : path.join(process.cwd(), start); | ||
return new Promise(resolve => { | ||
asyncFind(resolve, dir, depth); | ||
}); | ||
}; | ||
module.exports.sync = function findBabelConfigSync(start) { | ||
var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : INFINITY; | ||
module.exports.sync = function findBabelConfigSync(start, depth = INFINITY) { | ||
if (!start) { | ||
return nullConf; | ||
} | ||
if (!start) { | ||
return nullConf; | ||
let dir = path.isAbsolute(start) ? start : path.join(process.cwd(), start); | ||
let loopLeft = depth; | ||
do { | ||
const babelrc = path.join(dir, BABELRC_FILENAME); | ||
if (pathExists.sync(babelrc)) { | ||
const babelrcContent = fs.readFileSync(babelrc, 'utf8'); | ||
return { | ||
file: babelrc, | ||
config: JSON5.parse(babelrcContent) | ||
}; | ||
} | ||
var dir = path.isAbsolute(start) ? start : path.join(process.cwd(), start); | ||
var loopLeft = depth; | ||
const babelJSrc = path.join(dir, BABELRC_JS_FILENAME); | ||
// eslint-disable-next-line no-cond-assign | ||
do { | ||
var babelrc = path.join(dir, BABELRC_FILENAME); | ||
if (pathExists.sync(babelrc)) { | ||
var babelrcContent = fs.readFileSync(babelrc, 'utf8'); | ||
return { | ||
file: babelrc, | ||
config: JSON5.parse(babelrcContent) | ||
}; | ||
} | ||
if (pathExists.sync(babelJSrc)) { | ||
const config = getBabelJsConfig(babelJSrc); | ||
return { | ||
file: babelJSrc, | ||
config | ||
}; | ||
} | ||
var babelJSrc = path.join(dir, BABELRC_JS_FILENAME); | ||
if (pathExists.sync(babelJSrc)) { | ||
var config = getBabelJsConfig(babelJSrc); | ||
return { | ||
file: babelJSrc, | ||
config | ||
}; | ||
} | ||
const babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME); | ||
var babelConfigJSrc = path.join(dir, BABEL_CONFIG_JS_FILENAME); | ||
if (pathExists.sync(babelConfigJSrc)) { | ||
var _config = getBabelJsConfig(babelConfigJSrc); | ||
return { | ||
file: babelConfigJSrc, | ||
config: _config | ||
}; | ||
} | ||
if (pathExists.sync(babelConfigJSrc)) { | ||
const config = getBabelJsConfig(babelConfigJSrc); | ||
return { | ||
file: babelConfigJSrc, | ||
config | ||
}; | ||
} | ||
var packageFile = path.join(dir, PACKAGE_FILENAME); | ||
if (pathExists.sync(packageFile)) { | ||
var packageContent = fs.readFileSync(packageFile, 'utf8'); | ||
var packageJson = JSON.parse(packageContent); | ||
if (packageJson.babel) { | ||
return { | ||
file: packageFile, | ||
config: packageJson.babel | ||
}; | ||
} | ||
} | ||
const babelConfigJsonSrc = path.join(dir, BABEL_CONFIG_JSON_FILENAME); | ||
if (loopLeft === 0) { | ||
return nullConf; | ||
} | ||
if (pathExists.sync(babelConfigJsonSrc)) { | ||
const babelConfigContent = fs.readFileSync(babelConfigJsonSrc, 'utf8'); | ||
return { | ||
file: babelConfigJsonSrc, | ||
config: JSON5.parse(babelConfigContent) | ||
}; | ||
} | ||
loopLeft -= 1; | ||
} while (dir !== (dir = path.dirname(dir))); | ||
const packageFile = path.join(dir, PACKAGE_FILENAME); | ||
return nullConf; | ||
if (pathExists.sync(packageFile)) { | ||
const packageContent = fs.readFileSync(packageFile, 'utf8'); | ||
const packageJson = JSON.parse(packageContent); | ||
if (packageJson.babel) { | ||
return { | ||
file: packageFile, | ||
config: packageJson.babel | ||
}; | ||
} | ||
} | ||
if (loopLeft === 0) { | ||
return nullConf; | ||
} | ||
loopLeft -= 1; // eslint-disable-next-line no-cond-assign | ||
} while (dir !== (dir = path.dirname(dir))); | ||
return nullConf; | ||
}; |
{ | ||
"name": "find-babel-config", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"main": "lib/index.js", | ||
@@ -11,3 +11,3 @@ "description": "Find the closest babel config based on a directory", | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=16.0.0" | ||
}, | ||
@@ -24,18 +24,17 @@ "author": "Tommy Leunen <tommy.leunen@gmail.com> (http://tommyleunen.com)", | ||
"dependencies": { | ||
"json5": "^0.5.1", | ||
"path-exists": "^3.0.0" | ||
"json5": "^2.1.1", | ||
"path-exists": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-jest": "^20.0.0", | ||
"babel-preset-env": "^1.4.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-airbnb-base": "^11.1.3", | ||
"eslint-plugin-import": "^2.2.0", | ||
"jest": "^20.0.0", | ||
"standard-version": "^4.0.0" | ||
"@babel/cli": "^7.7.5", | ||
"@babel/preset-env": "^7.7.6", | ||
"babel-jest": "^24.9.0", | ||
"eslint": "^6.7.2", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-plugin-import": "^2.19.1", | ||
"jest": "^24.9.0", | ||
"standard-version": "^7.0.1" | ||
}, | ||
"scripts": { | ||
"lint": "eslint src test", | ||
"pretest": "npm run lint", | ||
"test:coverage": "jest --coverage", | ||
@@ -42,0 +41,0 @@ "test:watch": "jest --watch", |
@@ -37,3 +37,3 @@ # find-babel-config | ||
const { file, config } = findBabelConfig.sync(directory); | ||
// if c === null, the config wasn't found | ||
// if file === null, the config wasn't found. (Also config === null) | ||
if (file) { | ||
@@ -40,0 +40,0 @@ // file is the file in which the config is found |
Sorry, the diff of this file is not supported yet
179
13498
+ Addedjson5@2.2.3(transitive)
+ Addedpath-exists@4.0.0(transitive)
- Removedjson5@0.5.1(transitive)
- Removedpath-exists@3.0.0(transitive)
Updatedjson5@^2.1.1
Updatedpath-exists@^4.0.0