From eb962417889497d266c9c60d4b4a86d06fadb030 Mon Sep 17 00:00:00 2001 From: Akhilesh Gowda <50296948+akhileshgowda7@users.noreply.github.com> Date: Fri, 7 Jul 2023 17:03:34 -0400 Subject: [PATCH] implemented bump-draft (#9) * implemented bump-draft * used class for formating * removig if latest exists --- src/cli.js | 18 +++++++++++++++++- src/drafting/bump-draft.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/drafting/bump-draft.js diff --git a/src/cli.js b/src/cli.js index e1931af..9557050 100755 --- a/src/cli.js +++ b/src/cli.js @@ -10,6 +10,7 @@ import { getContext, getProcessorVersion, parseDirectory } from './utils/context import { deploy } from './deployment/deploy.js'; import { finalize } from './finalizing/finalize.js'; import { newDraft } from './drafting/new-draft.js'; +import { bumpDraft } from './drafting/bump-draft.js'; const program = new Command(); @@ -38,12 +39,27 @@ program .command('new-draft') .description('Creates a draft digital object from a given Digital Object') .argument('', 'Path to the digital object relative to DO_HOME') - .option('--latest', 'Use the latest version of the given Digital Object for the new draft regardless of the version indicated', false) + .option( + '--latest', + 'Use the latest version of the given Digital Object for the new draft regardless of the version indicated', + false + ) .option('--force', 'Deletes the existing draft, if it already exists', false) .action((str, _options, command) => { newDraft(getContext(program, command, str)); }); +program + .command('bump-draft') + .description('Increments the latest version of the digital object based on the given option, default is --minor') + .argument('', 'Path to the digital object relative to DO_HOME') + .option('--major', 'Increments the latest digital object version by 1', false) + .option('--minor', 'Increments the latest digital object version by 0.1', false) + .option('--patch', 'Increments the latest digital object version by 0.0.1', false) + .action((str, _options, command) => { + bumpDraft(getContext(program, command, str)); + }); + program .command('enrich') .description( diff --git a/src/drafting/bump-draft.js b/src/drafting/bump-draft.js new file mode 100644 index 0000000..4700ccb --- /dev/null +++ b/src/drafting/bump-draft.js @@ -0,0 +1,34 @@ +import semver from 'semver'; +import sh from 'shelljs'; +import { getLatestDigitalObject } from '../utils/get-latest.js'; +import { resolve } from 'path'; +import { existsSync } from 'fs'; + +/** + * Converts the draft into the latest version based on the given option, default is --minor + * @param {Object} context + */ +export function bumpDraft(context) { + const { selectedDigitalObject: obj, major, patch } = context; + let latest = getLatestDigitalObject(context.doHome, obj.type, obj.name, obj.iri)?.version ?? '0'; + + const releaseType = major ? 'major' : patch ? 'patch' : 'minor'; + latest = semver.inc(semver.coerce(latest), releaseType); + + // formating the version + const newVersion = + semver.patch(latest) == 0 + ? `v${semver.major(latest)}.${semver.minor(latest)}` + : `v${semver.major(latest)}.${semver.minor(latest)}.${semver.patch(latest)}`; + + const draftPath = resolve(obj.path); + const newVersionPath = resolve(context.doHome, obj.type, obj.name, newVersion, 'raw'); + + if (existsSync(newVersionPath)) { + sh.rm('-r', newVersionPath); + } + + sh.mkdir('-p', newVersionPath); + sh.mv(`${draftPath}/*`, newVersionPath); + sh.rm('-rf', draftPath); +}