Skip to content

Commit

Permalink
Drop: Merge @techor/version into techor
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Feb 16, 2024
1 parent 0f58f92 commit 971c6aa
Show file tree
Hide file tree
Showing 210 changed files with 116 additions and 346 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
},
"devDependencies": {
"@techor/jest": "workspace:^",
"@techor/pack": "workspace:^",
"@techor/version": "workspace:^",
"@types/fs-extra": "^9.0.13",
"@types/node": "^20.11.0",
"commitlint-config-techor": "workspace:^",
Expand Down
2 changes: 0 additions & 2 deletions packages/repo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
"access": "public"
},
"dependencies": {
"@techor/pack": "workspace:^",
"@techor/version": "workspace:^",
"@techor/jest": "workspace:^",
"commitlint-config-techor": "workspace:^",
"eslint-config-techor": "workspace:^"
Expand Down
1 change: 1 addition & 0 deletions packages/techor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"@techor/fs": "workspace:^",
"@techor/log": "workspace:^",
"@techor/npm": "workspace:^",
"commander": "^11.0.0",
"@techor/extend": "workspace:^",
"@techor/glob": "workspace:^",
Expand Down
24 changes: 2 additions & 22 deletions packages/techor/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,9 @@ program
.version(version || '0.0.0')

import commandPack from '../commands/pack'
import commandVersion from '../commands/version'

commandPack(program)

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')
.option('-ls, --list', 'List current bumpable dependency tree in workspaces', false)
.action(async function (args, options) {
try {
const action = require(
process.env.NODE_ENV === 'test'
? '../../../version/src/actions/main'
: '@techor/version/actions/main'
)
await action(args, options)
} catch (error) {
if (error.code === 'ERR_MODULE_NOT_FOUND') {
log.i`Please run **npm** **install** **@techor/version** first`
} else {
console.error(error)
}
}
})
commandVersion(program)

program.parse()
97 changes: 97 additions & 0 deletions packages/techor/src/commands/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import type { Command } from 'commander'
import { explorePathsSync } from '@techor/glob'
import path from 'path'
import log, { paint } from '@techor/log'
import { readJSONFileSync, writeFileSync } from '@techor/fs'
import { readPNPMWorkspaces, readWorkspaces, explorePackageManager } from '@techor/npm'

export default (program: Command) => 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')
.option('-ls, --list', 'List current bumpable dependency tree in workspaces', false)
.action(async function (version, options) {
if (!options.workspaces) {
const packageManager = explorePackageManager()
switch (packageManager) {
case 'pnpm':
options.workspaces = readPNPMWorkspaces()
break
case 'npm':
options.workspaces = readWorkspaces()
break
}
}
if (!options.workspaces?.length) {
log.x`workspaces is not defined in package.json`
}
const packagesOfPath = {}
const packagesOfName = {}
const workspacePackagePaths = options.workspaces.map((eachWorkspace) => path.join(eachWorkspace, '*package.json'))
const resolveVersion = (eachVersion: string) => {
if (eachVersion.startsWith('workspace:')) {
return eachVersion.replace('workspace:', '') + version
} else if (eachVersion === '') {
return options.prefix + version
}
}
const updateDependencies = (eachDependencies) => {
for (const dependencyName in eachDependencies) {
if (dependencyName in packagesOfName) {
const dependencyVersion = eachDependencies[dependencyName]
const resolvedVersion = resolveVersion(dependencyVersion)
if (resolvedVersion) {
eachDependencies[dependencyName] = resolvedVersion
}
}
}
}

// Read package.json by workspaces
for (const eachPackagePath of explorePathsSync(workspacePackagePaths)) {
const eachPackage = readJSONFileSync(path.resolve(eachPackagePath))
// Prevent version bumps of private package
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, devDependencies, peerDependencies } = packagesOfPath[eachPackagePath]
dependencies && updateDependencies(dependencies)
devDependencies && updateDependencies(devDependencies)
peerDependencies && updateDependencies(peerDependencies)
if (!options.list) {
writeFileSync(eachPackagePath, eachPackage)
}
}

const workspaceDepsTree = {}
for (const name in packagesOfName) {
const { dependencies, peerDependencies, devDependencies } = 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 || null
}
}
}
}
analyzeDeps(dependencies, 'dependencies')
analyzeDeps(peerDependencies, 'peerDependencies')
analyzeDeps(devDependencies, 'devDependencies')
/* 防止沒有印出空 {} 的項目 */
if (!Object.keys(workspaceDepsTree[paint('**' + name + '**')]).length) {
workspaceDepsTree[paint('**' + name + '**')] = null
}
}
log`📦`
log.tree(workspaceDepsTree)
log.success`bump version to ${version} for ${Object.keys(packagesOfName).length} packages in all workspace`
})
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { copy, rm } from '../../../../utils/fs'
import { explorePathsSync } from '../../../glob/src'
import { readFileSync } from '../../../fs/src'
import { readWorkspaces } from '../../../npm/src'

const action = require('../../src/actions/main')
import { execSync } from 'child_process'

const tmpDir = path.join(__dirname, 'tmp')

Expand All @@ -15,7 +14,7 @@ beforeAll(() => {

it('bump to specific version for all workspaces', () => {
process.chdir(tmpDir)
action('2.0.0-beta.200')
execSync('tsx ../../../src/bin version 2.0.0-beta.200', { cwd: tmpDir, stdio: 'inherit' })
const workspacePackagePaths = readWorkspaces().map((eachWorkspace) => path.join(eachWorkspace, '*package.json'))
for (const eachPackagePath of explorePathsSync(workspacePackagePaths)) {
const eachPackageRaw = readFileSync(path.resolve(eachPackagePath), { encoding: 'utf8' })
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/techor/tests/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
46 changes: 0 additions & 46 deletions packages/version/package.json

This file was deleted.

91 changes: 0 additions & 91 deletions packages/version/src/actions/main.ts

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions packages/version/tsconfig.json

This file was deleted.

Loading

0 comments on commit 971c6aa

Please sign in to comment.