This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
/
version.ts
86 lines (79 loc) · 3.71 KB
/
version.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { program } from 'commander'
import fg from 'fast-glob'
import fs from 'fs-extra'
import path from 'path'
import log, { paint } from '@techor/log'
import { readPackage } from '../utils/read-package'
const pkg = readPackage()
const defaults = {
workspaces: pkg.workspaces
}
program.command('version <version>')
.description('Bump to specific version for workspace\'s packages')
.option('-p, --prefix <symbol>', 'version prefix `^`, `~`, `>`, `>=`, `<`, `<=` ', '^')
.option('-w, --workspaces <paths>', 'Specific your workspaces', defaults.workspaces)
.option('-ls, --list', 'List current bumpable dependency tree in workspaces', false)
.action((version, { prefix, list, workspaces }) => {
const nextVersion = prefix + version
const packagesOfPath = {}
const packagesOfName = {}
const workspacePackagePaths = workspaces.map((eachWorkspace) => path.join(eachWorkspace, '*package.json'))
const updateDependencies = (dependencies, title) => {
let updated = false
for (const dependencyName in dependencies) {
if (dependencyName in packagesOfName) {
const dependencyVersion = dependencies[dependencyName]
if (dependencyVersion === '') {
dependencies[dependencyName] = nextVersion
updated = true
}
}
}
return updated
}
// Read package.json by workspaces
for (const eachPackagePath of fg.sync(workspacePackagePaths)) {
const eachPackage = fs.readJSONSync(eachPackagePath)
// Prevent version bumps of private package
if (!eachPackage.private) {
packagesOfPath[eachPackagePath] = eachPackage
packagesOfName[eachPackage.name] = eachPackage
// Bump to next verion
eachPackage.version = version
}
}
for (const eachPackagePath in packagesOfPath) {
const eachPackage = packagesOfPath[eachPackagePath]
const { dependencies, peerDependencies } = packagesOfPath[eachPackagePath]
dependencies && updateDependencies(dependencies, 'dependencies')
peerDependencies && updateDependencies(peerDependencies, 'peerDependencies')
if (!list) {
fs.writeJSONSync(eachPackagePath, eachPackage)
}
}
const workspaceDepsTree = {}
for (const name in packagesOfName) {
const { dependencies, peerDependencies } = packagesOfName[name]
const workspacePackage: any = workspaceDepsTree[paint('**' + name + '**')] = {}
const analyzeDeps = (eachDeps, key: string) => {
if (eachDeps) {
workspacePackage[key] = {}
for (const dependencyName in eachDeps) {
if (dependencyName in packagesOfName) {
const eachDependencyVersion = eachDeps[dependencyName]
workspacePackage[key][paint('**' + dependencyName + '**')] =
eachDependencyVersion === nextVersion ? null : nextVersion
}
}
if (!Object.keys(workspacePackage[key]).length) {
delete workspacePackage[key]
}
}
}
analyzeDeps(dependencies, 'dependencies')
analyzeDeps(peerDependencies, 'peerDependencies')
}
log`📦`
log.tree(workspaceDepsTree)
log.success`bump version to +${nextVersion}+ for +${Object.keys(packagesOfName).length}+ packages in all workspace`
})