-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(monorepo): update dependencies (#937)
* feat(monorepo): update dependencies * chore: remove unnecessary config * docs: update the doc Co-authored-by: shipjs <[email protected]>
- Loading branch information
Eunjae Lee
and
shipjs
authored
Nov 6, 2020
1 parent
74531a3
commit 03d47db
Showing
6 changed files
with
291 additions
and
13 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
packages/shipjs/src/helper/__tests__/dependencyUpdater.spec.js
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,104 @@ | ||
import { getListToUpdate, printListToUpdate } from '../dependencyUpdater'; | ||
import { print } from '../../util'; | ||
import { mockPrint } from '../../../tests/util'; | ||
|
||
describe('getListToUpdate', () => { | ||
const list = [ | ||
{ | ||
packagePath: 'packages/package-core', | ||
json: { | ||
name: 'core', | ||
}, | ||
}, | ||
{ | ||
packagePath: 'packages/package-js', | ||
json: { | ||
name: 'js', | ||
dependencies: { | ||
core: '^0.3.1', | ||
}, | ||
}, | ||
}, | ||
{ | ||
packagePath: 'packages/package-plugin-abc', | ||
json: { | ||
name: 'plugin-abc', | ||
dependencies: { | ||
core: '0.3.1', | ||
}, | ||
peerDependencies: { | ||
js: '~0.3.1', | ||
}, | ||
devDependencies: { | ||
js: '^0.3.1', | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
it('gets correct list', () => { | ||
const actual = getListToUpdate('0.3.2', list); | ||
expect(actual).toEqual([ | ||
{ | ||
name: 'js', | ||
packagePath: 'packages/package-js', | ||
updates: { | ||
dependencies: [ | ||
{ | ||
currentVersion: '^0.3.1', | ||
dependency: 'core', | ||
nextVersion: '^0.3.2', | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
name: 'plugin-abc', | ||
packagePath: 'packages/package-plugin-abc', | ||
updates: { | ||
dependencies: [ | ||
{ | ||
currentVersion: '0.3.1', | ||
dependency: 'core', | ||
nextVersion: '0.3.2', | ||
}, | ||
], | ||
devDependencies: [ | ||
{ | ||
currentVersion: '^0.3.1', | ||
dependency: 'js', | ||
nextVersion: '^0.3.2', | ||
}, | ||
], | ||
peerDependencies: [ | ||
{ | ||
currentVersion: '~0.3.1', | ||
dependency: 'js', | ||
nextVersion: '~0.3.2', | ||
}, | ||
], | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('prints the correct update list', () => { | ||
const output = []; | ||
mockPrint(print, output); | ||
printListToUpdate(getListToUpdate('0.3.2', list)); | ||
expect(output).toMatchInlineSnapshot(` | ||
Array [ | ||
" package: js (packages/package-js}/package.json)", | ||
" dependencies:", | ||
" core: ^0.3.1 -> ^0.3.2", | ||
" package: plugin-abc (packages/package-plugin-abc}/package.json)", | ||
" dependencies:", | ||
" core: 0.3.1 -> 0.3.2", | ||
" devDependencies:", | ||
" js: ^0.3.1 -> ^0.3.2", | ||
" peerDependencies:", | ||
" js: ~0.3.1 -> ~0.3.2", | ||
] | ||
`); | ||
}); | ||
}); |
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,119 @@ | ||
import { resolve } from 'path'; | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { print } from '../util'; | ||
import { runPrettier } from '../helper'; | ||
|
||
const typesOfDependencies = [ | ||
'dependencies', | ||
'devDependencies', | ||
'peerDependencies', | ||
]; | ||
|
||
export function getListToUpdate(_nextVersion, list) { | ||
const packageNames = list.map(({ json }) => json.name); | ||
|
||
return list | ||
.map(({ packagePath, json }) => { | ||
const updates = typesOfDependencies | ||
.map((dependencyType) => { | ||
let dependenciesToUpdate = Object.keys( | ||
json[dependencyType] || {} | ||
).filter((dependency) => packageNames.includes(dependency)); | ||
|
||
dependenciesToUpdate = dependenciesToUpdate | ||
.map((dependencyToUpdate) => { | ||
const currentVersion = json[dependencyType][dependencyToUpdate]; | ||
const prefix = | ||
currentVersion.startsWith('~') || currentVersion.startsWith('^') | ||
? currentVersion[0] | ||
: ''; | ||
const nextVersion = `${prefix}${_nextVersion}`; | ||
if (currentVersion !== nextVersion) { | ||
return { | ||
dependency: dependencyToUpdate, | ||
currentVersion, | ||
nextVersion, | ||
}; | ||
} else { | ||
return null; | ||
} | ||
}) | ||
.filter(Boolean); | ||
|
||
if (dependenciesToUpdate.length === 0) { | ||
return null; | ||
} else { | ||
return { | ||
type: dependencyType, | ||
dependenciesToUpdate, | ||
}; | ||
} | ||
}) | ||
.filter(Boolean); | ||
|
||
if (updates.length === 0) { | ||
return null; | ||
} else { | ||
return { | ||
packagePath, | ||
name: json.name, | ||
updates: updates.reduce((acc, { type, dependenciesToUpdate }) => { | ||
// eslint-disable-next-line no-param-reassign | ||
acc[type] = dependenciesToUpdate; | ||
return acc; | ||
}, {}), | ||
}; | ||
} | ||
}) | ||
.filter(Boolean); | ||
} | ||
|
||
export function printListToUpdate(list) { | ||
list.forEach(({ name, packagePath, updates }) => { | ||
print(` package: ${name} (${packagePath}}/package.json)`); | ||
Object.keys(updates).forEach((dependencyType) => { | ||
print(` ${dependencyType}:`); | ||
updates[dependencyType].forEach( | ||
({ dependency, currentVersion, nextVersion }) => { | ||
print(` ${dependency}: ${currentVersion} -> ${nextVersion}`); | ||
} | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
export async function runUpdates(list) { | ||
list.forEach(({ name, packagePath, updates }) => { | ||
print(` package: ${name} (${packagePath}}/package.json)`); | ||
const filePath = resolve(packagePath, 'package.json'); | ||
const json = JSON.parse(readFileSync(filePath).toString()); | ||
Object.keys(updates).forEach((dependencyType) => { | ||
print(` ${dependencyType}:`); | ||
updates[dependencyType].forEach( | ||
({ dependency, currentVersion, nextVersion }) => { | ||
print(` ${dependency}: ${currentVersion} -> ${nextVersion}`); | ||
json[dependencyType][dependency] = nextVersion; | ||
} | ||
); | ||
}); | ||
writeFileSync(filePath, JSON.stringify(json, null, 2)); | ||
}); | ||
|
||
await Promise.all( | ||
list.map(({ packagePath }) => | ||
runPrettier({ | ||
filePath: resolve(packagePath, 'package.json'), | ||
dir: packagePath, | ||
}) | ||
) | ||
); | ||
} | ||
|
||
export function prepareJsons(packageList) { | ||
return packageList.map((packagePath) => ({ | ||
packagePath, | ||
json: JSON.parse( | ||
readFileSync(resolve(packagePath, 'package.json')).toString() | ||
), | ||
})); | ||
} |
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
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