dce-dev-wizard
Version:
Wizard for managing development apps at Harvard DCE.
32 lines (26 loc) • 1.02 kB
text/typescript
import fs from 'fs';
import path from 'path';
// Import constants
import APP_DIRECTORY from '../constants/APP_DIRECTORY';
/**
* Get the major version of Node.js from a Dockerfile.
* @author Gabe Abrams
* @returns major version of Node.js (e.g. 22) or null if not found
*/
const getMajorNodeVersionInDockerfile = async (): Promise<number | null> => {
const dockerfilePath = path.join(APP_DIRECTORY, 'Dockerfile');
const dockerfileContents = fs.readFileSync(dockerfilePath, 'utf8');
/*
* node version can be expressed in the `FROM` directive: `FROM node:24-alpine` (legacy)
* or as the default of a `BASE_IMAGE` build arg: `ARG BASE_IMAGE=node:24-alpine`
*/
const nodeVersionRegex = /(?:BASE_IMAGE=|FROM\s+)node:(\d+)/;
const nodeMajorVersionMatch = dockerfileContents.match(nodeVersionRegex);
const nodeMajorVersion = (
nodeMajorVersionMatch
? Number.parseInt(nodeMajorVersionMatch[1], 10)
: null
);
return nodeMajorVersion;
};
export default getMajorNodeVersionInDockerfile;