Skip to content

Commit

Permalink
Merge pull request #4930 from Shopify/skip-notifications-with-json-flag
Browse files Browse the repository at this point in the history
Skip notifications when using --json
  • Loading branch information
gonzaloriestra authored Nov 27, 2024
2 parents 55eef10 + 9ed8458 commit aecf7fe
Show file tree
Hide file tree
Showing 28 changed files with 205 additions and 143 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-years-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-kit': patch
---

Skip notifications when using --json, -j or SHOPIFY_FLAG_JSON
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface appfunctionreplay {
'-c, --config <value>'?: string

/**
* Output the function run result as a JSON object.
* Output the result as JSON.
* @environment SHOPIFY_FLAG_JSON
*/
'-j, --json'?: ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface appfunctionrun {
'-i, --input <value>'?: string

/**
* Log the run result as a JSON object.
* Output the result as JSON.
* @environment SHOPIFY_FLAG_JSON
*/
'-j, --json'?: ''
Expand Down
4 changes: 2 additions & 2 deletions docs-shopify.dev/commands/interfaces/app-info.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export interface appinfo {
'-c, --config <value>'?: string

/**
* format output as JSON
* Output the result as JSON.
* @environment SHOPIFY_FLAG_JSON
*/
'--json'?: ''
'-j, --json'?: ''

/**
* Disable color output.
Expand Down
2 changes: 1 addition & 1 deletion docs-shopify.dev/commands/interfaces/app-logs.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface applogs {
'-c, --config <value>'?: string

/**
* Log the run result as a JSON object.
* Output the result as JSON.
* @environment SHOPIFY_FLAG_JSON
*/
'-j, --json'?: ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export interface appversionslist {
'-c, --config <value>'?: string

/**
* Output the versions list as JSON.
* Output the result as JSON.
* @environment SHOPIFY_FLAG_JSON
*/
'--json'?: ''
'-j, --json'?: ''

/**
* Disable color output.
Expand Down
4 changes: 2 additions & 2 deletions docs-shopify.dev/commands/interfaces/theme-info.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export interface themeinfo {
'-e, --environment <value>'?: string

/**
* Output the theme info as JSON.
* Output the result as JSON.
* @environment SHOPIFY_FLAG_JSON
*/
'--json'?: ''
'-j, --json'?: ''

/**
* Disable color output.
Expand Down
4 changes: 2 additions & 2 deletions docs-shopify.dev/commands/interfaces/theme-list.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export interface themelist {
'--id <value>'?: string

/**
* Output the theme list as JSON.
* Output the result as JSON.
* @environment SHOPIFY_FLAG_JSON
*/
'--json'?: ''
'-j, --json'?: ''

/**
* Only list themes that contain the given name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface themepush {
'-x, --ignore <value>'?: string

/**
* Output JSON instead of a UI.
* Output the result as JSON.
* @environment SHOPIFY_FLAG_JSON
*/
'-j, --json'?: ''
Expand Down
96 changes: 48 additions & 48 deletions docs-shopify.dev/generated/generated_docs_data.json

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions packages/app/src/cli/commands/app/function/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {replay} from '../../../services/function/replay.js'
import {appFlags} from '../../../flags.js'
import {showApiKeyDeprecationWarning} from '../../../prompts/deprecation-warnings.js'
import AppCommand, {AppCommandOutput} from '../../../utilities/app-command.js'
import {globalFlags} from '@shopify/cli-kit/node/cli'
import {globalFlags, jsonFlag} from '@shopify/cli-kit/node/cli'
import {Flags} from '@oclif/core'

export default class FunctionReplay extends AppCommand {
Expand All @@ -17,6 +17,7 @@ export default class FunctionReplay extends AppCommand {
...globalFlags,
...appFlags,
...functionFlags,
...jsonFlag,
'api-key': Flags.string({
hidden: true,
description: "Application's API key",
Expand All @@ -35,12 +36,6 @@ export default class FunctionReplay extends AppCommand {
'Specifies a log identifier to replay instead of selecting from a list. The identifier is provided in the output of `shopify app dev` and is the suffix of the log file name.',
env: 'SHOPIFY_FLAG_LOG',
}),
json: Flags.boolean({
char: 'j',
hidden: false,
description: 'Output the function run result as a JSON object.',
env: 'SHOPIFY_FLAG_JSON',
}),
watch: Flags.boolean({
char: 'w',
hidden: false,
Expand Down
9 changes: 2 additions & 7 deletions packages/app/src/cli/commands/app/function/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {functionFlags, inFunctionContext, getOrGenerateSchemaPath} from '../../.
import {runFunction} from '../../../services/function/runner.js'
import {appFlags} from '../../../flags.js'
import AppCommand, {AppCommandOutput} from '../../../utilities/app-command.js'
import {globalFlags} from '@shopify/cli-kit/node/cli'
import {globalFlags, jsonFlag} from '@shopify/cli-kit/node/cli'
import {Flags} from '@oclif/core'
import {renderAutocompletePrompt, isTTY} from '@shopify/cli-kit/node/ui'
import {outputDebug} from '@shopify/cli-kit/node/output'
Expand All @@ -20,6 +20,7 @@ export default class FunctionRun extends AppCommand {
...globalFlags,
...appFlags,
...functionFlags,
...jsonFlag,
input: Flags.string({
char: 'i',
description: 'The input JSON to pass to the function. If omitted, standard input is used.',
Expand All @@ -31,12 +32,6 @@ export default class FunctionRun extends AppCommand {
description: 'Name of the WebAssembly export to invoke.',
env: 'SHOPIFY_FLAG_EXPORT',
}),
json: Flags.boolean({
char: 'j',
hidden: false,
description: 'Log the run result as a JSON object.',
env: 'SHOPIFY_FLAG_JSON',
}),
}

public async run(): Promise<AppCommandOutput> {
Expand Down
8 changes: 2 additions & 6 deletions packages/app/src/cli/commands/app/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Format, info} from '../../services/info.js'
import AppCommand, {AppCommandOutput} from '../../utilities/app-command.js'
import {linkedAppContext} from '../../services/app-context.js'
import {Flags} from '@oclif/core'
import {globalFlags} from '@shopify/cli-kit/node/cli'
import {globalFlags, jsonFlag} from '@shopify/cli-kit/node/cli'
import {outputInfo} from '@shopify/cli-kit/node/output'

export default class AppInfo extends AppCommand {
Expand All @@ -21,11 +21,7 @@ export default class AppInfo extends AppCommand {
static flags = {
...globalFlags,
...appFlags,
json: Flags.boolean({
hidden: false,
description: 'format output as JSON',
env: 'SHOPIFY_FLAG_JSON',
}),
...jsonFlag,
'web-env': Flags.boolean({
hidden: false,
description: 'Outputs environment variables necessary for running and deploying web/.',
Expand Down
8 changes: 2 additions & 6 deletions packages/app/src/cli/commands/app/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {linkedAppContext} from '../../services/app-context.js'
import {storeContext} from '../../services/store-context.js'
import {Flags} from '@oclif/core'
import {normalizeStoreFqdn} from '@shopify/cli-kit/node/context/fqdn'
import {globalFlags} from '@shopify/cli-kit/node/cli'
import {globalFlags, jsonFlag} from '@shopify/cli-kit/node/cli'

export default class Logs extends AppCommand {
static summary = 'Stream detailed logs for your Shopify app.'
Expand All @@ -26,6 +26,7 @@ export default class Logs extends AppCommand {
static flags = {
...globalFlags,
...appFlags,
...jsonFlag,
'api-key': Dev.flags['api-key'],
'client-id': Dev.flags['client-id'],
store: Flags.string({
Expand All @@ -46,11 +47,6 @@ export default class Logs extends AppCommand {
options: ['success', 'failure'],
env: 'SHOPIFY_FLAG_STATUS',
}),
json: Flags.boolean({
char: 'j',
description: 'Log the run result as a JSON object.',
env: 'SHOPIFY_FLAG_JSON',
}),
}

public async run(): Promise<AppCommandOutput> {
Expand Down
8 changes: 2 additions & 6 deletions packages/app/src/cli/commands/app/versions/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import versionList from '../../../services/versions-list.js'
import {showApiKeyDeprecationWarning} from '../../../prompts/deprecation-warnings.js'
import AppCommand, {AppCommandOutput} from '../../../utilities/app-command.js'
import {linkedAppContext} from '../../../services/app-context.js'
import {globalFlags} from '@shopify/cli-kit/node/cli'
import {globalFlags, jsonFlag} from '@shopify/cli-kit/node/cli'
import {Args, Flags} from '@oclif/core'

export default class VersionsList extends AppCommand {
Expand All @@ -18,6 +18,7 @@ export default class VersionsList extends AppCommand {
static flags = {
...globalFlags,
...appFlags,
...jsonFlag,
'api-key': Flags.string({
hidden: true,
description: "Application's API key to fetch versions for.",
Expand All @@ -30,11 +31,6 @@ export default class VersionsList extends AppCommand {
env: 'SHOPIFY_FLAG_CLIENT_ID',
exclusive: ['config'],
}),
json: Flags.boolean({
description: 'Output the versions list as JSON.',
default: false,
env: 'SHOPIFY_FLAG_JSON',
}),
}

static args = {
Expand Down
5 changes: 3 additions & 2 deletions packages/app/src/cli/models/app/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
import {resolveFramework} from '@shopify/cli-kit/node/framework'
import {hashString} from '@shopify/cli-kit/node/crypto'
import {JsonMapType, decodeToml} from '@shopify/cli-kit/node/toml'
import {joinPath, dirname, basename, relativePath, relativizePath, sniffForJson} from '@shopify/cli-kit/node/path'
import {joinPath, dirname, basename, relativePath, relativizePath} from '@shopify/cli-kit/node/path'
import {AbortError} from '@shopify/cli-kit/node/error'
import {outputContent, outputDebug, OutputMessage, outputToken} from '@shopify/cli-kit/node/output'
import {joinWithAnd, slugify} from '@shopify/cli-kit/common/string'
Expand All @@ -52,6 +52,7 @@ import {renderInfo} from '@shopify/cli-kit/node/ui'
import {currentProcessIsGlobal} from '@shopify/cli-kit/node/is-global'
import {showNotificationsIfNeeded} from '@shopify/cli-kit/node/notifications-system'
import {globalCLIVersion, localCLIVersion} from '@shopify/cli-kit/node/version'
import {jsonOutputEnabled} from '@shopify/cli-kit/node/environment'

const defaultExtensionDirectory = 'extensions/*'

Expand Down Expand Up @@ -401,7 +402,7 @@ class AppLoader<TConfig extends AppConfiguration, TModuleSpec extends ExtensionS
const localVersion = dependencies['@shopify/cli'] && (await localCLIVersion(directory))
const globalVersion = await globalCLIVersion()

if (localVersion && globalVersion && !sniffForJson() && !alreadyShownCLIWarning) {
if (localVersion && globalVersion && !jsonOutputEnabled() && !alreadyShownCLIWarning) {
const currentInstallation = currentProcessIsGlobal() ? 'global installation' : 'local dependency'

const warningContent = {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"./context/local.js",
"./custom-oclif-loader.js",
"@oclif/core",
"../../private/node/constants.js",
"./path.js",
"./system.js",
"./ui.js"
Expand All @@ -91,6 +90,7 @@
"@oclif/core",
"./context/utilities.js",
"../../private/node/conf-store.js",
"../../private/node/constants.js",
"url"
]
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli-kit/src/private/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const environmentVariables = {
refreshToken: 'SHOPIFY_CLI_REFRESH_TOKEN',
otelURL: 'SHOPIFY_CLI_OTEL_EXPORTER_OTLP_ENDPOINT',
themeKitAccessDomain: 'SHOPIFY_CLI_THEME_KIT_ACCESS_DOMAIN',
json: 'SHOPIFY_FLAG_JSON',
}

export const defaultThemeKitAccessDomain = 'theme-kit-access.shopifyapps.com'
Expand Down
11 changes: 11 additions & 0 deletions packages/cli-kit/src/public/node/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {isTruthy} from './context/utilities.js'
import {cacheClear} from '../../private/node/conf-store.js'
import {environmentVariables} from '../../private/node/constants.js'
import {Flags} from '@oclif/core'

/**
Expand Down Expand Up @@ -135,6 +136,16 @@ export const globalFlags = {
}),
}

export const jsonFlag = {
json: Flags.boolean({
char: 'j',
description: 'Output the result as JSON.',
hidden: false,
default: false,
env: environmentVariables.json,
}),
}

/**
* Clear the CLI cache, used to store some API responses and handle notifications status
*/
Expand Down
12 changes: 12 additions & 0 deletions packages/cli-kit/src/public/node/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {nonRandomUUID} from './crypto.js'
import {isTruthy} from './context/utilities.js'
import {sniffForJson} from './path.js'
import {environmentVariables, systemEnvironmentVariables} from '../../private/node/constants.js'

/**
Expand Down Expand Up @@ -70,3 +72,13 @@ export function getIdentityTokenInformation(): {accessToken: string; refreshToke
userId: nonRandomUUID(identityToken),
}
}

/**
* Checks if the JSON output is enabled via flag (--json or -j) or environment variable (SHOPIFY_FLAG_JSON).
*
* @param environment - Process environment variables.
* @returns True if the JSON output is enabled, false otherwise.
*/
export function jsonOutputEnabled(environment = getEnvironmentVariables()): boolean {
return sniffForJson() || isTruthy(environment[environmentVariables.json])
}
Loading

0 comments on commit aecf7fe

Please sign in to comment.