-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3389f2f
commit 8a1dd6d
Showing
22 changed files
with
319 additions
and
103 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
...ands/upgrade/getComposerPackageVersion.ts → ...ade/composer/getComposerPackageVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './getComposerPackageVersion'; | ||
export * from './upgradeComposerPackage'; |
4 changes: 2 additions & 2 deletions
4
...ommands/upgrade/upgradeComposerPackage.ts → ...pgrade/composer/upgradeComposerPackage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
apps/app-builder/src/commands/upgrade/getYarnPackageVersion.ts
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
apps/app-builder/src/commands/upgrade/node/executeNodeUpgrade.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {NodePackageManager, type UpgradeSubContextWithLockfile} from '../types'; | ||
import {executeCommand} from '../../../utils'; | ||
|
||
export const executeNodeUpgrade = async (context: UpgradeSubContextWithLockfile): Promise<void> => { | ||
const {config, packageName} = context; | ||
|
||
const upgradeArgs = []; | ||
|
||
switch (config.nodePackageManager) { | ||
case NodePackageManager.Bun: | ||
upgradeArgs.push('update', packageName); | ||
break; | ||
|
||
case NodePackageManager.Yarn: | ||
upgradeArgs.push('up', packageName); | ||
break; | ||
} | ||
|
||
await executeCommand(context, config.nodePackageManagerCommand, upgradeArgs); | ||
}; |
11 changes: 11 additions & 0 deletions
11
apps/app-builder/src/commands/upgrade/node/getDefaultNodeLockfilePath.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {NodePackageManager, type UpgradeSubContext} from '../types'; | ||
|
||
export const getDefaultNodeLockfilePath = (context: UpgradeSubContext): string => { | ||
switch (context.config.nodePackageManager) { | ||
case NodePackageManager.Yarn: | ||
return 'yarn.lock'; | ||
|
||
case NodePackageManager.Bun: | ||
return 'bun.lockb'; | ||
} | ||
}; |
84 changes: 84 additions & 0 deletions
84
apps/app-builder/src/commands/upgrade/node/getNodePackageVersion.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* eslint-disable max-nested-callbacks */ | ||
import {describe, expect, it, vi} from 'vitest'; | ||
import {toArray} from '@myparcel/ts-utils'; | ||
import {NodePackageManager, UpgradeMode, type UpgradeSubContextWithLockfile} from '../types'; | ||
import {createTestContext} from '../../../__tests__/createTestContext'; | ||
import {getNodePackageVersion} from './getNodePackageVersion'; | ||
|
||
const mockStdout = vi.fn(() => ''); | ||
|
||
// mock spawnSync | ||
vi.mock('child_process', () => ({ | ||
spawnSync: () => ({ | ||
status: 0, | ||
stdout: mockStdout(), | ||
}), | ||
})); | ||
|
||
type TestInput = { | ||
packageManager: NodePackageManager; | ||
stdout: string | string[]; | ||
}; | ||
|
||
describe('getNodePackageVersion', () => { | ||
it.each([ | ||
{ | ||
packageManager: NodePackageManager.Yarn, | ||
stdout: [ | ||
'"@myparcel-eslint/eslint-config-node@npm:1.3.1"', | ||
'"@myparcel-pdk/admin@npm:1.0.0-alpha.109"', | ||
'"@myparcel-pdk/app-builder@npm:1.0.0-alpha.36"', | ||
'"@myparcel-pdk/checkout@npm:1.0.0-alpha.67"', | ||
'"@myparcel/ts-utils@npm:1.0.0"', | ||
'"eslint@npm:8.0.0"', | ||
], | ||
}, | ||
{ | ||
packageManager: NodePackageManager.Bun, | ||
stdout: [ | ||
'/path/to/project node_modules', | ||
'├── @myparcel-eslint/[email protected]', | ||
'├── @myparcel-pdk/[email protected]', | ||
'├── @myparcel-pdk/[email protected]', | ||
'├── @myparcel-pdk/[email protected]', | ||
'├── @myparcel/[email protected]', | ||
'└── [email protected]', | ||
], | ||
}, | ||
] satisfies TestInput[])( | ||
'should return the correct package version for $packageManager', | ||
async ({packageManager, stdout}) => { | ||
const testContext = createTestContext(); | ||
|
||
const context = { | ||
...testContext, | ||
config: { | ||
...testContext.config, | ||
nodePackageManager: packageManager, | ||
}, | ||
lockfilePath: 'yarn.lock', | ||
mode: UpgradeMode.Node, | ||
packageName: '@myparcel-pdk/*', | ||
} satisfies UpgradeSubContextWithLockfile; | ||
|
||
mockStdout.mockImplementationOnce(() => toArray(stdout).join('\n')); | ||
|
||
const res = await getNodePackageVersion(context); | ||
|
||
expect(res).toEqual([ | ||
{ | ||
name: '@myparcel-pdk/admin', | ||
version: '1.0.0-alpha.109', | ||
}, | ||
{ | ||
name: '@myparcel-pdk/app-builder', | ||
version: '1.0.0-alpha.36', | ||
}, | ||
{ | ||
name: '@myparcel-pdk/checkout', | ||
version: '1.0.0-alpha.67', | ||
}, | ||
]); | ||
}, | ||
); | ||
}); |
29 changes: 29 additions & 0 deletions
29
apps/app-builder/src/commands/upgrade/node/getNodePackageVersion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {NodePackageManager, type ParsedEntry, type UpgradeSubContextWithLockfile} from '../types'; | ||
import {getPackageEntriesForYarn} from './getPackageEntriesForYarn'; | ||
import {getPackageEntriesForBun} from './getPackageEntriesForBun'; | ||
|
||
export const getNodePackageVersion = async (context: UpgradeSubContextWithLockfile): Promise<ParsedEntry[]> => { | ||
const {lockfilePath, packageName, config} = context; | ||
|
||
const matches: ParsedEntry[] = []; | ||
|
||
const resolvedPackageName = packageName.replace(/\*/g, '.*').replace(/\//g, '\\/'); | ||
|
||
switch (config.nodePackageManager) { | ||
case NodePackageManager.Yarn: | ||
matches.push(...(await getPackageEntriesForYarn(context, resolvedPackageName))); | ||
|
||
break; | ||
|
||
case NodePackageManager.Bun: | ||
matches.push(...(await getPackageEntriesForBun(context, resolvedPackageName))); | ||
|
||
break; | ||
} | ||
|
||
if (matches.length === 0) { | ||
throw new Error(`Package ${packageName} not found in lockfile ${lockfilePath}`); | ||
} | ||
|
||
return matches; | ||
}; |
23 changes: 23 additions & 0 deletions
23
apps/app-builder/src/commands/upgrade/node/getPackageEntriesForBun.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {type ParsedEntry, type UpgradeSubContextWithLockfile} from '../types'; | ||
import {executeCommand} from '../../../utils'; | ||
|
||
export async function getPackageEntriesForBun( | ||
context: UpgradeSubContextWithLockfile, | ||
resolvedPackageName: string, | ||
): Promise<ParsedEntry[]> { | ||
const content = await executeCommand(context, context.config.nodePackageManagerCommand, ['pm', 'ls', '--all']); | ||
|
||
return content | ||
.split('\n') | ||
.filter((line) => line.includes('@') && new RegExp(resolvedPackageName).test(line)) | ||
.map((line) => { | ||
const regex = new RegExp(`\\s(${resolvedPackageName})@(.*)`); | ||
|
||
const [, name, version] = regex.exec(line) ?? []; | ||
|
||
return { | ||
name, | ||
version, | ||
}; | ||
}); | ||
} |
26 changes: 26 additions & 0 deletions
26
apps/app-builder/src/commands/upgrade/node/getPackageEntriesForYarn.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {type ParsedEntry, type UpgradeSubContextWithLockfile} from '../types'; | ||
import {executeCommand} from '../../../utils'; | ||
|
||
export async function getPackageEntriesForYarn( | ||
context: UpgradeSubContextWithLockfile, | ||
resolvedPackageName: string, | ||
): Promise<ParsedEntry[]> { | ||
const {config} = context; | ||
|
||
const content = await executeCommand(context, config.nodePackageManagerCommand, [ | ||
'info', | ||
'--all', | ||
'--name-only', | ||
'--json', | ||
]); | ||
|
||
return content | ||
.split('\n') | ||
.map((line) => line.replace(/^"(.*)"$/, '$1')) | ||
.filter((line) => new RegExp(`^${resolvedPackageName}@npm:`).exec(line)) | ||
.map((line) => { | ||
const [name, version] = line.split('@npm:'); | ||
|
||
return {name, version}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export * from './executeNodeUpgrade'; | ||
export * from './getDefaultNodeLockfilePath'; | ||
export * from './getNodePackageVersion'; | ||
export * from './getPackageEntriesForBun'; | ||
export * from './getPackageEntriesForYarn'; | ||
export * from './upgradeNodePackage'; |
20 changes: 20 additions & 0 deletions
20
apps/app-builder/src/commands/upgrade/node/upgradeNodePackage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {type UpgradeSubMethod} from '../types'; | ||
import {getNodePackageVersion} from './getNodePackageVersion'; | ||
import {executeNodeUpgrade} from './executeNodeUpgrade'; | ||
|
||
export const upgradeNodePackage: UpgradeSubMethod = async (context) => { | ||
const {args} = context; | ||
|
||
const oldVersions = await getNodePackageVersion(context); | ||
|
||
if (!args.dryRun) { | ||
await executeNodeUpgrade(context); | ||
} | ||
|
||
const newVersions = await getNodePackageVersion(context); | ||
|
||
return { | ||
oldVersions, | ||
newVersions, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.