Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skuba migrate node 22 #1735

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions src/cli/migrate/nodeVersion/getNode22TypesVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { execSync } from 'child_process';

export const getNode22TypesVersion = () =>
execSync("npm show @types/node@^22 version --json | jq '.[-1]'").toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could generalise a bit:

Suggested change
export const getNode22TypesVersion = () =>
execSync("npm show @types/node@^22 version --json | jq '.[-1]'").toString();
export const getNodeTypesVersionForMajor = (major: number) =>
execSync(`npm show @types/node@^${major} version --json | jq '.[-1]'`).toString();

34 changes: 26 additions & 8 deletions src/cli/migrate/nodeVersion/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import memfs, { vol } from 'memfs';

import { getLatestNode22Types, nodeVersionMigration } from '.';
import * as getNode22TypesVersionModule from './getNode22TypesVersion';

import { getNode22TypeVersion, nodeVersionMigration } from '.';

jest
.spyOn(getNode22TypesVersionModule, 'getNode22TypesVersion')
.mockReturnValue('22.9.0');

jest.mock('fs-extra', () => memfs);
jest.mock('fast-glob', () => ({
Expand All @@ -13,6 +19,8 @@ const volToJson = () => vol.toJSON(process.cwd(), undefined, true);

beforeEach(() => vol.reset());

afterEach(() => jest.clearAllMocks());

describe('nodeVersionMigration', () => {
const scenarios: Array<{
filesBefore: Record<string, string>;
Expand Down Expand Up @@ -193,19 +201,29 @@ describe('nodeVersionMigration', () => {
);
});

describe('getLatestNode22Types', () => {
describe('getNodeTypesVersion', () => {
it('finds the latest node22 types version', () => {
const { version, err } = getLatestNode22Types();
const { version, err } = getNode22TypeVersion();
expect(version).toBe('22.9.0');
expect(err).toBeUndefined();
});

it('defaults to 22.9.0 if the exec returns an invalid version', () => {
jest
.spyOn(getNode22TypesVersionModule, 'getNode22TypesVersion')
.mockReturnValue('This is not a version');
const { version, err } = getNode22TypeVersion();
expect(version).toBe('22.9.0');
expect(err).toBe('Failed to fetch latest version, using fallback version');
});

it('defaults to 22.9.0 if the exec fails', () => {
// Mock JSON.parse to throw an error
jest.spyOn(JSON, 'parse').mockImplementationOnce(() => {
throw new Error('Failed to fetch latest version');
});
const { version, err } = getLatestNode22Types();
jest
.spyOn(getNode22TypesVersionModule, 'getNode22TypesVersion')
.mockReturnValue(
new Error('Failed to fetch latest version') as unknown as string,
);
const { version, err } = getNode22TypeVersion();
expect(version).toBe('22.9.0');
expect(err).toBe('Failed to fetch latest version, using fallback version');
});
Expand Down
17 changes: 6 additions & 11 deletions src/cli/migrate/nodeVersion/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { execSync } from 'child_process';
import { inspect } from 'util';

import { glob } from 'fast-glob';
Expand All @@ -7,6 +6,8 @@ import fs from 'fs-extra';
import { log } from '../../../utils/logging';
import { createDestinationFileReader } from '../../configure/analysis/project';

import { getNode22TypesVersion } from './getNode22TypesVersion';

type SubPatch = (
| { files: string; file?: never }
| { file: string; files?: never }
Expand All @@ -20,17 +21,11 @@ type VersionResult = {
err: string | undefined;
};

export const getLatestNode22Types = (): VersionResult => {
export const getNode22TypeVersion = (): VersionResult => {
const FALLBACK_VERSION = '22.9.0';
try {
const version = (
JSON.parse(
execSync('npm show @types/node@^22 version --json', {
encoding: 'utf8',
}),
) as string[]
).pop();
if (!version) {
const version = getNode22TypesVersion();
if (!version || !/^22.\d+\.\d+$/.test(version)) {
throw new Error('No version found');
}
return {
Expand Down Expand Up @@ -177,7 +172,7 @@ export const nodeVersionMigration = async (
) => {
log.ok(`Upgrading to Node.js ${version}`);
try {
const { version: nodeTypesVersion, err } = getLatestNode22Types();
const { version: nodeTypesVersion, err } = getNode22TypeVersion();
if (err) {
log.warn(err);
}
Expand Down