Skip to content

Commit cc6e199

Browse files
committed
woking upgrade dependencie
1 parent 6d0d8fa commit cc6e199

File tree

4 files changed

+93
-3
lines changed

4 files changed

+93
-3
lines changed

commands/upgrade.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import prompts from 'prompts'
66
import { coerce, compare } from 'semver'
77
import { TRANSFORM_OPTIONS } from '../config'
88
import { onCancel, promptSource } from '../utils/share'
9+
import { installDependency } from '../utils/packageManager'
910

1011
const transformerDirectory = join(__dirname, '../', 'transforms')
1112

12-
export async function upgrade(source?: string) {
13+
export async function upgrade(source?: string, options?: Record<string, unknown>) {
1314
const sourceSelected = source || (await promptSource('Which directory should the codemods be applied to?'))
1415

1516
if (!sourceSelected) {
@@ -88,6 +89,13 @@ export async function upgrade(source?: string) {
8889
console.log(`> ${results.skipped} codemods were skipped`)
8990
console.log(`> ${results.failed} codemods failed`)
9091
console.log(`> ${results.unmodified} codemods were skipped because they didn't change anything`)
92+
93+
console.log('\n> Installing latest express version')
94+
const { upgradeDependency } = options ?? {}
95+
96+
if (upgradeDependency) {
97+
installDependency(["express@latest"], sourceSelected)
98+
}
9199
}
92100

93101
function suggestCodemods(packageJson) {

index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ program
2727
.command('upgrade')
2828
.description('Upgrade your express server to the latest version.')
2929
.argument('[source]', 'Path to source files or directory to transform.')
30+
.option('--no-upgrade-dependency', 'Do not upgrade express dependency')
31+
.helpOption('-h, --help', 'Display this help message.')
3032
.action(upgrade)
3133

3234
program.parse(process.argv)

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
"version": "0.0.1",
55
"description": "Codemods for updating express servers.",
66
"main": "build/index.js",
7-
"contributors": ["Sebastian Beltran <bjohansebas@gmail.com>", "Filip Kudla <filip.kudla.dev@gmail.com>"],
7+
"contributors": [
8+
"Sebastian Beltran <bjohansebas@gmail.com>",
9+
"Filip Kudla <filip.kudla.dev@gmail.com>"
10+
],
811
"license": "MIT",
912
"bin": "build/index.js",
10-
"files": ["build/transforms/*.js", "build/commands/*.js", "build/utils/*.js", "build/config.js", "build/index.js"],
13+
"files": [
14+
"build/transforms/*.js",
15+
"build/commands/*.js",
16+
"build/utils/*.js",
17+
"build/config.js",
18+
"build/index.js"
19+
],
1120
"scripts": {
1221
"dev": "tsc -d -w -p tsconfig.json",
1322
"build": "tsc -d -p tsconfig.json",
@@ -18,7 +27,9 @@
1827
},
1928
"dependencies": {
2029
"commander": "^12.1.0",
30+
"execa": "4.1.0",
2131
"fast-glob": "^3.3.2",
32+
"find-up": "^5.0.0",
2233
"jscodeshift": "^17.1.1",
2334
"picocolors": "^1.1.1",
2435
"prompts": "^2.4.2",

utils/packageManager.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import findUp from 'find-up'
2+
import { basename } from 'node:path'
3+
import execa from 'execa'
4+
5+
const ADD_CMD_FLAG = {
6+
npm: 'install',
7+
yarn: 'add',
8+
pnpm: 'add',
9+
bun: 'add',
10+
}
11+
12+
export function getPkgManager(root: string) {
13+
try {
14+
const lockFile = findUp.sync(
15+
[
16+
'package-lock.json',
17+
'yarn.lock',
18+
'pnpm-lock.yaml',
19+
'bun.lock',
20+
'bun.lockb',
21+
],
22+
{ cwd: root }
23+
)
24+
25+
console.log(lockFile)
26+
27+
if (lockFile) {
28+
switch (basename(lockFile)) {
29+
case 'package-lock.json':
30+
return 'npm'
31+
case 'yarn.lock':
32+
return 'yarn'
33+
case 'pnpm-lock.yaml':
34+
return 'pnpm'
35+
case 'bun.lock':
36+
case 'bun.lockb':
37+
return 'bun'
38+
}
39+
}
40+
41+
return 'npm'
42+
} catch {
43+
return 'npm'
44+
}
45+
}
46+
47+
export function installDependency(
48+
packageToInstall: string[],
49+
root: string
50+
) {
51+
if (packageToInstall.length === 0) return
52+
53+
const packageManager = getPkgManager(root)
54+
55+
if (!packageManager) throw new Error('Failed to find package manager')
56+
57+
try {
58+
execa.sync(packageManager, [ADD_CMD_FLAG[packageManager], ...packageToInstall], {
59+
stdio: ['ignore', 'ignore', 'inherit'],
60+
shell: true,
61+
cwd: root,
62+
})
63+
} catch (error) {
64+
throw new Error(
65+
`Failed to install "${packageToInstall}". Please install it manually.`,
66+
{ cause: error }
67+
)
68+
}
69+
}

0 commit comments

Comments
 (0)