@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
114 lines • 4.46 kB
JavaScript
import { isTruthy } from './context/utilities.js';
import { launchCLI as defaultLaunchCli } from './cli-launcher.js';
import { cacheClear } from '../../private/node/conf-store.js';
import { environmentVariables } from '../../private/node/constants.js';
import { Flags } from '@oclif/core';
async function exitIfOldNodeVersion(versions = process.versions) {
const nodeVersion = versions.node;
const nodeMajorVersion = Number(nodeVersion.split('.')[0]);
const currentSupportedNodeVersion = 18;
if (nodeMajorVersion < currentSupportedNodeVersion) {
const { renderError } = await import('./ui.js');
renderError({
headline: 'Upgrade to a supported Node version now.',
body: [
`Node ${nodeMajorVersion} has reached end-of-life and poses security risks. When you upgrade to a`,
{
link: {
url: 'https://nodejs.dev/en/about/previous-releases',
label: 'supported version',
},
},
{ char: ',' },
"you'll be able to use Shopify CLI without interruption.",
],
});
process.exit(1);
}
}
function setupEnvironmentVariables(options, argv = process.argv, env = process.env) {
/**
* By setting DEBUG=* when --verbose is passed we are increasing the
* verbosity of oclif. Oclif uses debug (https://www.npmjs.com/package/debug)
* for logging, and it's configured through the DEBUG= environment variable.
*/
if (argv.includes('--verbose')) {
env.DEBUG = env.DEBUG ?? '*';
}
if (options.development) {
env.SHOPIFY_CLI_ENV = env.SHOPIFY_CLI_ENV ?? 'development';
}
}
function forceNoColor(argv = process.argv, env = process.env) {
if (argv.includes('--no-color') ||
isTruthy(env.NO_COLOR) ||
isTruthy(env.SHOPIFY_FLAG_NO_COLOR) ||
env.TERM === 'dumb') {
env.FORCE_COLOR = '0';
}
}
/**
* A function that abstracts away setting up the environment and running
* a CLI
* @param options - Options.
*/
export async function runCLI(options, launchCLI = defaultLaunchCli, argv = process.argv, env = process.env, versions = process.versions) {
setupEnvironmentVariables(options, argv, env);
if (options.runInCreateMode) {
await addInitToArgvWhenRunningCreateCLI(options, argv);
}
forceNoColor(argv, env);
await exitIfOldNodeVersion(versions);
return launchCLI({ moduleURL: options.moduleURL });
}
async function addInitToArgvWhenRunningCreateCLI(options, argv = process.argv) {
const { findUpAndReadPackageJson } = await import('./node-package-manager.js');
const { moduleDirectory } = await import('./path.js');
const packageJson = await findUpAndReadPackageJson(moduleDirectory(options.moduleURL));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const packageName = packageJson.content.name;
const name = packageName.replace('@shopify/create-', '');
const initIndex = argv.findIndex((arg) => arg.includes('init'));
if (initIndex === -1) {
const initIndex = argv.findIndex((arg) => arg.match(new RegExp(`bin(\\/|\\\\)+(create-${name}|dev|run)`))) + 1;
argv.splice(initIndex, 0, 'init');
}
}
/**
* A function for create-x CLIs that automatically runs the "init" command.
*/
export async function runCreateCLI(options, launchCLI = defaultLaunchCli, argv = process.argv, env = process.env, versions = process.versions) {
return runCLI({ ...options, runInCreateMode: true }, launchCLI, argv, env, versions);
}
/**
* An object that contains the flags that
* are shared across all the commands.
*/
export const globalFlags = {
'no-color': Flags.boolean({
hidden: false,
description: 'Disable color output.',
env: 'SHOPIFY_FLAG_NO_COLOR',
}),
verbose: Flags.boolean({
hidden: false,
description: 'Increase the verbosity of the output.',
env: 'SHOPIFY_FLAG_VERBOSE',
}),
};
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
*/
export function clearCache() {
cacheClear();
}
//# sourceMappingURL=cli.js.map