Skip to content

Commit

Permalink
feat: new command copy field
Browse files Browse the repository at this point in the history
  • Loading branch information
nflatley-zengenti committed Mar 21, 2024
1 parent 1c39880 commit 1f27c2d
Show file tree
Hide file tree
Showing 12 changed files with 628 additions and 43 deletions.
338 changes: 325 additions & 13 deletions packages/contensis-cli/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/contensis-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"jsonpath-mapper": "^1.1.0",
"keytar": "^7.9.0",
"lodash": "^4.17.21",
"migratortron": "^1.0.0-beta.41",
"migratortron": "^1.0.0-beta.45",
"nanospinner": "^1.1.0",
"node-fetch": "^2.6.7",
"parse-git-config": "^3.0.0",
Expand Down
71 changes: 71 additions & 0 deletions packages/contensis-cli/src/commands/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Command } from 'commander';
import { CopyField } from 'migratortron';
import { cliCommand } from '~/services/ContensisCliService';
import {
commit,
concurrency,
ignoreErrors,
mapContensisOpts,
outputEntries,
} from './globalOptions';

export const makeCopyCommand = () => {
const copy = new Command()
.command('copy')
.description('copy command')
.addHelpText('after', `\n`)
.showHelpAfterError(true)
.exitOverride();

copy
.command('field')
.description('copy the contents of one content type field to another')
.argument(
'<contentTypeId>',
'the api id of the content type containing the fields to copy'
)
.argument('<fieldId>', 'the id of the field to copy from')
.argument('<destinationId>', 'the id of the field to copy to')
.option(
'-t --template <template>',
'apply a liquidjs template to modify the copied field content'
)
.addOption(commit)
.addOption(concurrency)
.addOption(ignoreErrors)
.addOption(outputEntries)
.usage('<contentTypeId> <fieldId> <destinationId> (all arguments required)')
.addHelpText(
'after',
`
Example call:
> copy field blog authorName contributors\n`
)
.action(
async (
contentTypeId: string,
fieldId: string,
destinationId: string,
opts: any
) => {
const copyField: CopyField = {
contentTypeId,
fieldId,
destinationId,
template: opts.template,
};

return await cliCommand(
['copy', 'project', contentTypeId, fieldId, destinationId],
opts,
mapContensisOpts({ copyField })
).CopyEntryField({
commit: opts.commit,
fromFile: opts.fromFile,
logOutput: opts.outputEntries,
});
}
);

return copy;
};
1 change: 0 additions & 1 deletion packages/contensis-cli/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Command } from 'commander';
import { Project } from 'contensis-core-api';
import { cliCommand } from '~/services/ContensisCliService';
import { shell } from '~/shell';
import { isUuid } from '~/util';

export const makeCreateCommand = () => {
const create = new Command()
Expand Down
22 changes: 11 additions & 11 deletions packages/contensis-cli/src/commands/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,24 @@ Example call:
});

sharedGetEntryOptions(
program
.command('entries')
.description('get entries')
.argument(
'[search phrase]',
'get entries with the search phrase, use quotes for multiple words'
)
program
.command('entries')
.description('get entries')
.argument(
'[search phrase]',
'get entries with the search phrase, use quotes for multiple words'
)
.addOption(contentTypes)
.option(
.option(
'-d --dependents',
'find and return any dependencies of all found entries'
)
'find and return any dependencies of all found entries'
)
)
.addOption(
new Option(
'--data-format <dataFormat>',
'find and return entries of a specific data format'
)
)
.choices(['entry', 'asset', 'webpage'])
.default('entry')
)
Expand Down
18 changes: 17 additions & 1 deletion packages/contensis-cli/src/commands/globalOptions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Command, Option } from 'commander';
import { MigrateRequest } from 'migratortron';
import { url } from '~/util';

// Map various input options into a request to be processed
// by Migratortron / Contensis import library
export const mapContensisOpts = (opts: any = {}) => ({
export const mapContensisOpts = (opts: any = {}): MigrateRequest => ({
source:
opts.sourceAlias || opts.sourceProjectId
? {
Expand All @@ -14,6 +15,8 @@ export const mapContensisOpts = (opts: any = {}) => ({
}
: undefined,
models: opts.modelIds,
copyField: opts.copyField,
// convert various cli options into MigrateRequest.query format
query:
opts.id ||
opts.entryIds ||
Expand All @@ -38,6 +41,7 @@ export const mapContensisOpts = (opts: any = {}) => ({
zenQL: opts.zenql,
transformGuids: !opts.preserveGuids,
ignoreErrors: opts.ignoreErrors,
concurrency: opts.concurrency ? Number(opts.concurrency) : undefined,
});

/* Output options */
Expand Down Expand Up @@ -121,6 +125,18 @@ export const ignoreErrors = new Option(
'commit the import ignoring any reported errors'
).default(false);

export const outputEntries = new Option(
'-oe --output-entries <outputEntries>',
'which details of the entries included in the import to output'
)
.choices(['errors', 'changes', 'all'])
.default('errors');

export const concurrency = new Option(
'-conc --concurrency <concurrency>',
'the number of entries to load in parallel'
).default(2);

export const addConnectOptions = (program: Command) =>
program.addOption(alias.hideHelp()).addOption(project.hideHelp());

Expand Down
12 changes: 4 additions & 8 deletions packages/contensis-cli/src/commands/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Command, Option } from 'commander';
import { cliCommand } from '~/services/ContensisCliService';
import {
commit,
concurrency,
getEntryOptions,
ignoreErrors,
mapContensisOpts,
outputEntries,
} from './globalOptions';

export const makeImportCommand = () => {
Expand Down Expand Up @@ -113,14 +115,8 @@ Example call:
'-preserve --preserve-guids',
'include this flag when you are importing entries that you have previously exported and wish to update'
)
.addOption(
new Option(
'-oe --output-entries <outputEntries>',
'which details of the entries included in the import to output'
)
.choices(['errors', 'changes', 'all'])
.default('errors')
)
.addOption(concurrency)
.addOption(outputEntries)
.addOption(ignoreErrors)
.addHelpText(
'after',
Expand Down
10 changes: 7 additions & 3 deletions packages/contensis-cli/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Command } from 'commander';
import { Logger } from '~/util/logger';
import { LIB_VERSION } from '~/version';
import { makeConnectCommand } from './connect';
import { makeCopyCommand } from './copy';
import { makeCreateCommand } from './create';
import { makeDevCommand } from './dev';
import { makeDiffCommand } from './diff';
Expand Down Expand Up @@ -49,6 +50,9 @@ const commands = () => {
program.addCommand(
addGlobalOptions(makeCreateCommand()).copyInheritedSettings(program)
);
program.addCommand(
addGlobalOptions(makeCopyCommand()).copyInheritedSettings(program)
);
program.addCommand(
addConnectOptions(
addAuthenticationOptions(makeDevCommand())
Expand All @@ -58,9 +62,9 @@ const commands = () => {
addGlobalOptions(makeExecuteCommand()).copyInheritedSettings(program)
);
program.addCommand(
addGlobalOptions(
addImportOptions(makeDiffCommand())
).copyInheritedSettings(program)
addGlobalOptions(addImportOptions(makeDiffCommand())).copyInheritedSettings(
program
)
);
program.addCommand(
addGlobalOptions(makeGetCommand()).copyInheritedSettings(program)
Expand Down
73 changes: 73 additions & 0 deletions packages/contensis-cli/src/services/ContensisCliService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,79 @@ class ContensisCli {
}
};

CopyEntryField = async ({
commit,
fromFile,
logOutput,
}: {
commit: boolean;
fromFile: string;
logOutput: string;
}) => {
const { currentEnv, currentProject, log, messages } = this;

const contensis = await this.ConnectContensisImport({
commit,
fromFile,
importDataType: 'entries',
});

if (contensis) {
log.line();
if (contensis.isPreview) {
console.log(log.successText(` -- IMPORT PREVIEW -- `));
} else {
console.log(log.warningText(` *** COMMITTING IMPORT *** `));
}

const [err, result] = await to(
contensis.content.copy.MigrateFieldContent()
);

if (err) logError(err);
if (result)
await this.HandleFormattingAndOutput(result, () => {
// print the migrateResult to console
printEntriesMigrateResult(this, result, {
showAll: logOutput === 'all',
showDiff: logOutput === 'all' || logOutput === 'changes',
showChanged: logOutput === 'changes',
});
});

if (
result &&
!err &&
!result.errors?.length &&
((!commit && result.entriesToMigrate[currentProject].totalCount) ||
(commit &&
(result.migrateResult?.created || result.migrateResult?.updated)))
) {
log.success(
messages.entries.imported(
currentEnv,
commit,
commit
? (result.migrateResult?.created || 0) +
(result.migrateResult?.updated || 0)
: result.entriesToMigrate[currentProject].totalCount
)
);
if (!commit) {
log.raw(``);
log.help(messages.entries.commitTip());
}
} else {
log.error(messages.entries.failedImport(currentEnv), err);
if (!result?.entriesToMigrate?.[currentProject]?.totalCount)
log.help(messages.entries.notFound(currentEnv));
}
} else {
log.warning(messages.models.noList(currentProject));
log.help(messages.connect.tip());
}
};

GetNodes = async (rootPath: string, depth = 0) => {
const { currentProject, log, messages } = this;
const contensis = await this.ConnectContensis();
Expand Down
3 changes: 2 additions & 1 deletion packages/contensis-cli/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class ContensisShell {
availableCommands.push('login', 'list projects', 'set project');
if (userId)
availableCommands.push(
'copy field',
'create key',
'create project',
'create role',
Expand Down Expand Up @@ -306,4 +307,4 @@ process.stdin.on('data', key => {
}
});

// // process.env.http_proxy = 'http://127.0.0.1:8888';
// process.env.http_proxy = 'http://127.0.0.1:8888';
3 changes: 2 additions & 1 deletion packages/contensis-cli/src/util/console.printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { deconstructApiError } from './error';
import { Logger, addNewLines } from './logger';
import {
BlockVersion,
CopyFieldResult,
EntriesResult,
MigrateModelsResult,
MigrateNodesTree,
Expand Down Expand Up @@ -116,7 +117,7 @@ export const printBlockVersion = (

export const printEntriesMigrateResult = (
{ log, messages, currentProject }: ContensisCli,
migrateResult: EntriesResult,
migrateResult: EntriesResult | CopyFieldResult,
{
action = 'import',
showDiff = false,
Expand Down

0 comments on commit 1f27c2d

Please sign in to comment.