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

feat: implement interactive version command #377

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
110 changes: 101 additions & 9 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"prettify": "npx prettier . --write"
},
"dependencies": {
"@wixc3/resolve-directory-context": "^1.0.3",
"@wixc3/resolve-directory-context": "^1.0.4",
"cli-cursor": "^4.0.0",
"colorette": "^2.0.16",
"commander": "^8.3.0",
"p-queue": "^7.1.0",
Expand Down
17 changes: 17 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ program
});
});

program
.command('version [target]')
.description('careful upgrade semver version of selected packages')
.option('--dry-run', 'no actual file system operations', false)
.option('--mode', 'major | minor | patch', undefined) // TODO: expend to all Modes
.option('--identifier', 'identifier for the release', '')
.action(async (targetPath: string, { dryRun, mode, identifier }) => {
const { version } = await import('./commands/version.js');

version({
directoryPath: path.resolve(targetPath || ''),
identifier,
dryRun,
mode,
});
});

program
.command('upgrade [target]')
.description('upgrade dependencies and devDependencies of all packages')
Expand Down
43 changes: 43 additions & 0 deletions src/commands/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { resolveDirectoryContext, getRootPackage, childPackagesFromContext } from '@wixc3/resolve-directory-context';
import { upgrade } from '../utils/workspace-upgrade.js';
import { versionSelector } from '../utils/version-selector.js';
import { type Modes, preProcessPackages } from '../utils/semver.js';

export interface VersionOptions {
directoryPath: string;
identifier: string;
dryRun: boolean;
mode: Modes;
}

export function version({ directoryPath, dryRun, mode, identifier }: VersionOptions) {
const project = directoryPath;

const releaseIdentifier = identifier;
const directoryContext = resolveDirectoryContext(project);

const packages = childPackagesFromContext(directoryContext);
const rootPackage = getRootPackage(directoryContext);
const packagesJson = packages.map(({ packageJson }) => packageJson);
const ignoredPackages = new Set<number>();
const { possibleVersions } = preProcessPackages({
packagesJson,
releaseIdentifier,
ignoredPackages,
});

versionSelector({
packages,
possibleVersions,
project,
rootPackage,
mode,
onSelect(versionMap) {
upgrade(project, versionMap, dryRun);
process.exit(0); // TODO
},
onCancel() {
process.exit(0); // TODO
},
});
}
18 changes: 18 additions & 0 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,21 @@ export function currentGitCommitHash(cwd = process.cwd()): string | undefined {
const { stdout, status } = spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8', cwd });
return status === 0 ? stdout.trim() : undefined;
}

// TODO: use status === 0 pattern
export const getGitStatus = (cwd: string) => spawnSync('git status', { shell: true, encoding: 'utf-8', cwd }).stdout;

export const gitCommit = (cwd: string, message: string) =>
spawnSync(`git commit -am ${JSON.stringify(message)}`, { shell: true, encoding: 'utf-8', cwd }).stdout;

export const gitPush = (cwd: string) => spawnSync(`git push --tags`, { shell: true, encoding: 'utf-8', cwd }).stdout;

export const gitTag = (cwd: string, version: string, packageName?: string) =>
spawnSync(gitTagCommand(version, packageName), {
shell: true,
encoding: 'utf-8',
cwd,
}).stdout;

export const gitTagCommand = (version: string, packageName?: string) =>
`git tag -a ${packageName ? `${packageName}@${version}` : `v${version}`}`;
5 changes: 5 additions & 0 deletions src/utils/npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { spawnSync } from 'child_process';

// TODO: use status === 0 pattern
export const updateLockFile = (cwd: string) =>
spawnSync('npm install --package-lock-only', { shell: true, encoding: 'utf-8', cwd }).stdout;
Loading