-
Notifications
You must be signed in to change notification settings - Fork 1
/
detectEnv.js
105 lines (96 loc) · 2.77 KB
/
detectEnv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
// Todo: Move to own repo
/**
* You can use this in your `.eslintrc.js` as follows...
*
* @example
*
* 'use strict';
*
* const {env, ecmaVersion} = require('./detectEnv.js');
*
* module.exports = {
* extends: [
* './someConfig.js',
*
* // Good for use with `eslint-plugin-n`
* 'plugin:n/recommended-module',
*
* // Can use `ecmaVersion` for selecting a suitable config within a project which has different config
* // for different version; e.g., set here to ES5 config if not ES6+
* ...(ecmaVersion < 2015
* ? ['plugin:@brettz9/es5']
* : ['plugin:@brettz9/es6']
* )
* ],
* env,
* parserOptions: {
* ecmaVersion
* }
* };
*/
const {join} = require('path');
const semver = require('semver');
const getEnvForEcmaVersion = ({ecmaVersion, nodeVersion, node = true}) => {
// Though redundant with `ecmaVersion` for `parserOptions`, also sets globals
const env = ecmaVersion >= 2021
? {
es2021: true
}
: (ecmaVersion >= 2020
? {
es2020: true
}
// We set `ecmaVersion` for parser, but not `env` with globals if Node 8
// as it lacks those globals
: (ecmaVersion >= 2017 &&
(!node || semver.satisfies(nodeVersion, '>=9.0.0'))
? {
es2017: true
}
: (ecmaVersion > 2015
? {
es6: true
}
: {})));
return node ? {...env, node: true} : env;
};
const getEcmaVersionForNodeVersion = (nodeVersion) => {
return semver.satisfies(nodeVersion, '>=15.0.0')
? 2021
: semver.satisfies(nodeVersion, '>=14.0.0')
? 2020
: (semver.satisfies(nodeVersion, '>=12.0.0')
? 2019
: (semver.satisfies(nodeVersion, '>=10.0.0')
? 2018
: (semver.satisfies(nodeVersion, '>=8.0.0')
? 2017
: (semver.satisfies(nodeVersion, '>=7.0.0')
? 2016
: (semver.satisfies(nodeVersion, '>=7.0.0')
? 2015
: 5)))));
};
const detectNodeVersion = (packagePath) => {
let nodeVersion = '5';
try {
// eslint-disable-next-line n/global-require, import/no-dynamic-require -- Ensure available synchronously
const {engines: {node}} = require(packagePath);
nodeVersion = semver.minVersion(node);
} catch (err) {}
return nodeVersion;
};
const getEnvAndEcmaVersionForCwd = (cwd) => {
const nodeVersion = detectNodeVersion(join(cwd, 'package.json'));
const ecmaVersion = getEcmaVersionForNodeVersion(nodeVersion);
const env = getEnvForEcmaVersion({ecmaVersion, nodeVersion});
return {env, ecmaVersion};
};
module.exports = {
detectNodeVersion, getEcmaVersionForNodeVersion, getEnvForEcmaVersion,
getEnvAndEcmaVersionForCwd,
...getEnvAndEcmaVersionForCwd(
process.cwd()
)
};