(versionFilePath: string)
| 6 | import path from 'path'; |
| 7 | |
| 8 | export function getNodeVersionFromFile(versionFilePath: string): string | null { |
| 9 | if (!fs.existsSync(versionFilePath)) { |
| 10 | throw new Error( |
| 11 | `The specified node version file at: ${versionFilePath} does not exist` |
| 12 | ); |
| 13 | } |
| 14 | |
| 15 | const contents = fs.readFileSync(versionFilePath, 'utf8'); |
| 16 | |
| 17 | // Try parsing the file as an NPM `package.json` file. |
| 18 | try { |
| 19 | const manifest = JSON.parse(contents); |
| 20 | |
| 21 | // Presume package.json file. |
| 22 | if (typeof manifest === 'object' && !!manifest) { |
| 23 | // Support Volta. |
| 24 | // See https://docs.volta.sh/guide/understanding#managing-your-project |
| 25 | if (manifest.volta?.node) { |
| 26 | return manifest.volta.node; |
| 27 | } |
| 28 | |
| 29 | // support devEngines from npm 11 |
| 30 | if (manifest.devEngines?.runtime) { |
| 31 | // find an entry with name set to node and having set a version. |
| 32 | // the devEngines.runtime can either be an object or an array of objects |
| 33 | const nodeEntry = [manifest.devEngines.runtime] |
| 34 | .flat() |
| 35 | .find(({name, version}) => name?.toLowerCase() === 'node' && version); |
| 36 | if (nodeEntry) { |
| 37 | return nodeEntry.version; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if (manifest.engines?.node) { |
| 42 | return manifest.engines.node; |
| 43 | } |
| 44 | |
| 45 | // Support Volta workspaces. |
| 46 | // See https://docs.volta.sh/advanced/workspaces |
| 47 | if (manifest.volta?.extends) { |
| 48 | const extendedFilePath = path.resolve( |
| 49 | path.dirname(versionFilePath), |
| 50 | manifest.volta.extends |
| 51 | ); |
| 52 | core.info('Resolving node version from ' + extendedFilePath); |
| 53 | return getNodeVersionFromFile(extendedFilePath); |
| 54 | } |
| 55 | |
| 56 | // If contents are an object, we parsed JSON |
| 57 | // this can happen if node-version-file is a package.json |
| 58 | // yet contains no volta.node or engines.node |
| 59 | // |
| 60 | // If node-version file is _not_ JSON, control flow |
| 61 | // will not have reached these lines. |
| 62 | // |
| 63 | // And because we've reached here, we know the contents |
| 64 | // *are* JSON, so no further string parsing makes sense. |
| 65 | return null; |
no outgoing calls
no test coverage detected