/
node-version.ts
44 lines (41 loc) · 1.31 KB
/
node-version.ts
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
import { validRange } from 'semver';
import { logger } from '../../../logger';
import { getSiblingFileName, readLocalFile } from '../../../util/fs';
import { PostUpdateConfig } from '../../common';
async function getNodeFile(filename: string): Promise<string> | null {
try {
const constraint = (await readLocalFile(filename, 'utf8'))
.split('\n')[0]
.replace(/^v/, '');
if (validRange(constraint)) {
logger.debug(`Using node constraint "${constraint}" from ${filename}`);
return constraint;
}
} catch (err) {
// do nothing
}
return null;
}
function getPackageJsonConstraint(
config: PostUpdateConfig
): Promise<string> | null {
const constraint = config.compatibility?.node;
if (constraint && validRange(constraint)) {
logger.debug(`Using node constraint "${constraint}" from package.json`);
return constraint;
}
return null;
}
export async function getNodeConstraint(
config: PostUpdateConfig
): Promise<string> | null {
const { packageFile } = config;
const constraint =
(await getNodeFile(getSiblingFileName(packageFile, '.nvmrc'))) ||
(await getNodeFile(getSiblingFileName(packageFile, '.node-version'))) ||
getPackageJsonConstraint(config);
if (!constraint) {
logger.debug('No node constraint found - using latest');
}
return constraint;
}