Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: recursive option not work when directly call versionBump #30

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 2 additions & 31 deletions src/cli/parse-args.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import process from 'node:process'
import fs from 'node:fs/promises'
import fsSync from 'node:fs'
import { valid as isValidVersion } from 'semver'
import cac from 'cac'
import c from 'picocolors'
import yaml from 'js-yaml'
import { isReleaseType } from '../release-type'
import type { VersionBumpOptions } from '../types/version-bump-options'
import { version } from '../../package.json'
Expand Down Expand Up @@ -58,34 +55,8 @@ export async function parseArgs(): Promise<ParsedArgs> {
}
}

if (parsedArgs.options.recursive) {
if (parsedArgs.options.files?.length) {
console.log(c.yellow('The --recursive option is ignored when files are specified'))
}
else {
parsedArgs.options.files = [
'package.json',
'package-lock.json',
'packages/**/package.json',
'jsr.json',
'jsr.jsonc',
]

// check if pnpm-workspace.yaml exists, if so, add all workspaces to files
if (fsSync.existsSync('pnpm-workspace.yaml')) {
// read pnpm-workspace.yaml
const pnpmWorkspace = await fs.readFile('pnpm-workspace.yaml', 'utf8')
// parse yaml
const workspaces = yaml.load(pnpmWorkspace) as { packages: string[] }
// append package.json to each workspace string
const workspacesWithPackageJson = workspaces.packages.map(workspace => `${workspace}/package.json`)
// start with ! or already in files should be excluded
const withoutExcludedWorkspaces = workspacesWithPackageJson.filter(workspace => !workspace.startsWith('!') && !parsedArgs.options.files?.includes(workspace))
// add to files
parsedArgs.options.files = parsedArgs.options.files.concat(withoutExcludedWorkspaces)
}
}
}
if (parsedArgs.options.recursive && parsedArgs.options.files?.length)
console.log(c.yellow('The --recursive option is ignored when files are specified'))

return parsedArgs
}
Expand Down
37 changes: 34 additions & 3 deletions src/normalize-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import process from 'node:process'
import fs from 'node:fs/promises'
import fsSync from 'node:fs'
import fg from 'fast-glob'
import yaml from 'js-yaml'
import type { ReleaseType } from './release-type'
import { isReleaseType } from './release-type'
import type { VersionBumpOptions } from './types/version-bump-options'
Expand Down Expand Up @@ -74,6 +77,7 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
const cwd = raw.cwd || process.cwd()
const ignoreScripts = Boolean(raw.ignoreScripts)
const execute = raw.execute
const recursive = Boolean(raw.recursive)

let release: Release
if (!raw.release || raw.release === 'prompt')
Expand All @@ -100,10 +104,37 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
else if (raw.commit || tag || push)
commit = { all, noVerify, message: 'chore: release v' }

const files = await fg(
raw.files?.length
if (recursive && !raw.files?.length) {
raw.files = [
'package.json',
'package-lock.json',
'packages/**/package.json',
'jsr.json',
'jsr.jsonc',
]

// check if pnpm-workspace.yaml exists, if so, add all workspaces to files
if (fsSync.existsSync('pnpm-workspace.yaml')) {
// read pnpm-workspace.yaml
const pnpmWorkspace = await fs.readFile('pnpm-workspace.yaml', 'utf8')
// parse yaml
const workspaces = yaml.load(pnpmWorkspace) as { packages: string[] }
// append package.json to each workspace string
const workspacesWithPackageJson = workspaces.packages.map(workspace => `${workspace}/package.json`)
// start with ! or already in files should be excluded
const withoutExcludedWorkspaces = workspacesWithPackageJson.filter(workspace => !workspace.startsWith('!') && !raw.files?.includes(workspace))
// add to files
raw.files = raw.files.concat(withoutExcludedWorkspaces)
}
}
else {
raw.files = raw.files?.length
? raw.files
: ['package.json', 'package-lock.json', 'jsr.json', 'jsr.jsonc'],
: ['package.json', 'package-lock.json', 'jsr.json', 'jsr.jsonc']
}

const files = await fg(
raw.files,
{
cwd,
onlyFiles: true,
Expand Down