-
Notifications
You must be signed in to change notification settings - Fork 71
feat(CMA): Add entry, asset, and content-type CRUD commands #3272
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
Open
Ivo Toby (ivo-toby)
wants to merge
11
commits into
contentful:main
Choose a base branch
from
ivo-toby:cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2db3ffc
feat: add entry, asset, and content-type CRUD commands with structure…
ivo-toby b710095
chore(packages): update package-lock
ivo-toby a70a555
chore(docs): update readme
ivo-toby 44ceab6
test: add process.exit spy to prevent test process termination
ivo-toby b759ca5
test: add firstLocaleValue to output mock and refactor exit spy usage
ivo-toby 5258d3d
refactor: migrate command handlers from legacy nested client to plain…
ivo-toby 755769b
refactor: replace legacy content-type list command with plain client
ivo-toby 2c249ea
feat(cli): add usage examples to --help output for all content commands
ivo-toby 483c16c
fix(cli): remove hardcoded environment and locale defaults
ivo-toby 2a58363
chore: linting
ivo-toby f07cb05
chore(lint): clean up production warnings
ivo-toby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| import { cleanUpTestSpaces } from '@contentful/integration-test-utils' | ||
| import { initConfig } from '../test/contentful-config' | ||
|
|
||
| // Only run integration-test cleanup when credentials are available. | ||
| // Unit tests do not need this and should not fail because of missing tokens. | ||
| const hasCredentials = !!process.env.CONTENTFUL_INTEGRATION_TEST_CMA_TOKEN | ||
|
|
||
| beforeAll(async () => { | ||
| if (!hasCredentials) return | ||
| await cleanUpTestSpaces({ threshold: 60 * 1000 }) | ||
| return initConfig() | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| if (!hasCredentials) return | ||
| return await cleanUpTestSpaces({ threshold: 60 * 1000 }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import type { Argv } from 'yargs' | ||
|
|
||
| export const command = 'asset' | ||
| export const desc = 'Manage assets' | ||
| export const builder = function (yargs: Argv): Argv { | ||
| return yargs | ||
| .commandDir('asset_cmds') | ||
| .demandCommand(4, 'Please specify a sub command.') | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { createCommand } from '../../utils/command-factory' | ||
| import { validateId } from '../../utils/validators' | ||
|
|
||
| const { command, desc, builder, handler } = createCommand({ | ||
| command: 'delete <id>', | ||
| desc: 'Delete an asset', | ||
| feature: 'asset-delete', | ||
| usage: 'Usage: contentful asset delete <id> [options]', | ||
| examples: [ | ||
| [ | ||
| 'contentful asset delete 3wtvPBbBjiMKqKGFI0MeCu', | ||
| 'Delete (prompts for confirmation)' | ||
| ], | ||
| [ | ||
| 'contentful asset delete 3wtvPBbBjiMKqKGFI0MeCu --yes', | ||
| 'Delete without confirmation' | ||
| ] | ||
| ], | ||
| needsConfirmation: true, | ||
| confirmationMessage: | ||
| 'Are you sure you want to delete this asset? This action cannot be undone.', | ||
| supportsDryRun: true, | ||
| handler: async (client, argv) => { | ||
| const id = validateId(argv.id, 'Asset ID') | ||
| const asset = await client.asset.get({ assetId: id }) | ||
| await client.asset.delete({ assetId: id }) | ||
| return { sys: asset.sys } | ||
| }, | ||
| dryRunHandler: async (client, argv) => { | ||
| const id = validateId(argv.id, 'Asset ID') | ||
| return client.asset.get({ assetId: id }) | ||
| }, | ||
| tableFormat: data => ({ | ||
| rows: [ | ||
| ['ID', data.sys.id], | ||
| ['Status', 'deleted'] | ||
| ] | ||
| }), | ||
| quietExtractor: data => [data.sys.id] | ||
| }) | ||
|
|
||
| export { command, desc, builder, handler } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { createCommand } from '../../utils/command-factory' | ||
| import { firstLocaleValue } from '../../utils/output' | ||
| import { validateId } from '../../utils/validators' | ||
| import type { AssetFileField, AssetLike } from '../../utils/contentful-types' | ||
|
|
||
| function getAssetStatus(asset: AssetLike): string { | ||
| if (asset.sys.archivedVersion) return 'archived' | ||
| if (asset.sys.publishedVersion) { | ||
| if (asset.sys.version > asset.sys.publishedVersion + 1) return 'changed' | ||
| return 'published' | ||
| } | ||
| return 'draft' | ||
| } | ||
|
|
||
| const { command, desc, builder, handler } = createCommand({ | ||
| command: 'get <id>', | ||
| desc: 'Get a single asset', | ||
| feature: 'asset-get', | ||
| usage: 'Usage: contentful asset get <id> [options]', | ||
| examples: [ | ||
| [ | ||
| 'contentful asset get 3wtvPBbBjiMKqKGFI0MeCu', | ||
| 'Get asset details as a table' | ||
| ], | ||
| [ | ||
| 'contentful asset get 3wtvPBbBjiMKqKGFI0MeCu --json', | ||
| 'Get full asset JSON (includes file URL, metadata)' | ||
| ] | ||
| ], | ||
| handler: async (client, argv) => { | ||
| const id = validateId(argv.id, 'Asset ID') | ||
| return client.asset.get({ assetId: id }) | ||
| }, | ||
| tableFormat: asset => ({ | ||
| rows: [ | ||
| ['ID', asset.sys.id], | ||
| ['Title', firstLocaleValue(asset.fields?.title) || '-'], | ||
| [ | ||
| 'File Name', | ||
| firstLocaleValue<AssetFileField>(asset.fields?.file)?.fileName || '-' | ||
| ], | ||
| ['URL', firstLocaleValue<AssetFileField>(asset.fields?.file)?.url || '-'], | ||
| [ | ||
| 'Content Type', | ||
| firstLocaleValue<AssetFileField>(asset.fields?.file)?.contentType || '-' | ||
| ], | ||
| ['Version', String(asset.sys.version)], | ||
| ['Status', getAssetStatus(asset)], | ||
| ['Updated At', asset.sys.updatedAt || '-'] | ||
| ] | ||
| }), | ||
| quietExtractor: asset => [asset.sys.id] | ||
| }) | ||
|
|
||
| export { command, desc, builder, handler } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { createCommand } from '../../utils/command-factory' | ||
| import { firstLocaleValue } from '../../utils/output' | ||
| import type { | ||
| AssetFileField, | ||
| AssetLike, | ||
| QueryParams | ||
| } from '../../utils/contentful-types' | ||
|
|
||
| function getAssetStatus(asset: AssetLike): string { | ||
| if (asset.sys.archivedVersion) return 'archived' | ||
| if (asset.sys.publishedVersion) { | ||
| if (asset.sys.version > asset.sys.publishedVersion + 1) return 'changed' | ||
| return 'published' | ||
| } | ||
| return 'draft' | ||
| } | ||
|
|
||
| const { command, desc, builder, handler } = createCommand({ | ||
| command: 'list', | ||
| desc: 'List assets', | ||
| feature: 'asset-list', | ||
| usage: 'Usage: contentful asset list [options]', | ||
| examples: [ | ||
| ['contentful asset list', 'List all assets as a table'], | ||
| ['contentful asset list --json --limit 5', 'Get first 5 assets as JSON'], | ||
| ['contentful asset list --quiet', 'Output only asset IDs (one per line)'] | ||
| ], | ||
| options: { | ||
| query: { | ||
| type: 'string', | ||
| describe: 'Additional CMA query params' | ||
| }, | ||
| limit: { | ||
| alias: 'l', | ||
| type: 'number', | ||
| describe: 'Results per page (default: 100, max: 1000)' | ||
| }, | ||
| skip: { | ||
| type: 'number', | ||
| describe: 'Offset for pagination' | ||
| } | ||
| }, | ||
| handler: async (client, argv) => { | ||
| const query: QueryParams = {} | ||
| if (argv.limit) query.limit = argv.limit | ||
| if (argv.skip) query.skip = argv.skip | ||
|
|
||
| return client.asset.getMany({ query }) | ||
| }, | ||
| tableFormat: data => ({ | ||
| head: ['ID', 'Title', 'File Name', 'Status', 'Updated At'], | ||
| rows: data.items.map((asset: AssetLike) => { | ||
| const title = firstLocaleValue(asset.fields?.title) || '-' | ||
| const file = firstLocaleValue<AssetFileField>(asset.fields?.file) | ||
| const fileName = file?.fileName || '-' | ||
| return [ | ||
| asset.sys.id, | ||
| title, | ||
| fileName, | ||
| getAssetStatus(asset), | ||
| asset.sys.updatedAt || '-' | ||
| ] | ||
| }) | ||
| }), | ||
| quietExtractor: data => data.items.map((a: AssetLike) => a.sys.id) | ||
| }) | ||
|
|
||
| export { command, desc, builder, handler } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { createCommand } from '../../utils/command-factory' | ||
| import { firstLocaleValue } from '../../utils/output' | ||
| import { validateId } from '../../utils/validators' | ||
|
|
||
| const { command, desc, builder, handler } = createCommand({ | ||
| command: 'publish <id>', | ||
| desc: 'Publish an asset', | ||
| feature: 'asset-publish', | ||
| usage: 'Usage: contentful asset publish <id> [options]', | ||
| supportsDryRun: true, | ||
| handler: async (client, argv) => { | ||
| const id = validateId(argv.id, 'Asset ID') | ||
| const asset = await client.asset.get({ assetId: id }) | ||
| return client.asset.publish({ assetId: id }, asset) | ||
| }, | ||
| dryRunHandler: async (client, argv) => { | ||
| const id = validateId(argv.id, 'Asset ID') | ||
| return client.asset.get({ assetId: id }) | ||
| }, | ||
| tableFormat: asset => ({ | ||
| rows: [ | ||
| ['ID', asset.sys.id], | ||
| ['Title', firstLocaleValue(asset.fields?.title) || '-'], | ||
| ['Version', String(asset.sys.version)], | ||
| ['Published Version', String(asset.sys.publishedVersion || '-')] | ||
| ] | ||
| }), | ||
| quietExtractor: asset => [asset.sys.id] | ||
| }) | ||
|
|
||
| export { command, desc, builder, handler } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { createCommand } from '../../utils/command-factory' | ||
| import { firstLocaleValue } from '../../utils/output' | ||
| import { validateId } from '../../utils/validators' | ||
|
|
||
| const { command, desc, builder, handler } = createCommand({ | ||
| command: 'unpublish <id>', | ||
| desc: 'Unpublish an asset', | ||
| feature: 'asset-unpublish', | ||
| usage: 'Usage: contentful asset unpublish <id> [options]', | ||
| supportsDryRun: true, | ||
| handler: async (client, argv) => { | ||
| const id = validateId(argv.id, 'Asset ID') | ||
| await client.asset.get({ assetId: id }) | ||
| return client.asset.unpublish({ assetId: id }) | ||
| }, | ||
| dryRunHandler: async (client, argv) => { | ||
| const id = validateId(argv.id, 'Asset ID') | ||
| return client.asset.get({ assetId: id }) | ||
| }, | ||
| tableFormat: asset => ({ | ||
| rows: [ | ||
| ['ID', asset.sys.id], | ||
| ['Title', firstLocaleValue(asset.fields?.title) || '-'], | ||
| ['Version', String(asset.sys.version)] | ||
| ] | ||
| }), | ||
| quietExtractor: asset => [asset.sys.id] | ||
| }) | ||
|
|
||
| export { command, desc, builder, handler } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also add these new commands to the
./docs/README.mdhttps://github.com/contentful/contentful-cli/blob/main/docs/README.mdThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As well as existing
./docs/content-type