Skip to content

Commit

Permalink
implemented bump-draft (#9)
Browse files Browse the repository at this point in the history
* implemented bump-draft

* used class for formating

* removig if latest exists
  • Loading branch information
akhileshgowda7 authored Jul 7, 2023
1 parent c63f73e commit eb96241
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -38,12 +39,27 @@ program
.command('new-draft')
.description('Creates a draft digital object from a given Digital Object')
.argument('<digital-object-path>', '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('<digital-object-path>', '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(
Expand Down
34 changes: 34 additions & 0 deletions src/drafting/bump-draft.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit eb96241

Please sign in to comment.