2025-08-25 17:02:10 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { CommandModule } from 'yargs';
|
2025-08-25 17:02:10 +00:00
|
|
|
import {
|
|
|
|
|
installExtension,
|
2025-08-26 00:04:53 +02:00
|
|
|
type ExtensionInstallMetadata,
|
2025-08-25 17:02:10 +00:00
|
|
|
} from '../../config/extension.js';
|
|
|
|
|
|
2025-08-26 14:36:55 +00:00
|
|
|
import { getErrorMessage } from '../../utils/errors.js';
|
|
|
|
|
|
2025-08-25 17:02:10 +00:00
|
|
|
interface InstallArgs {
|
|
|
|
|
source?: string;
|
|
|
|
|
path?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-02 08:15:47 -07:00
|
|
|
const ORG_REPO_REGEX = /^[a-zA-Z0-9-]+\/[\w.-]+$/;
|
|
|
|
|
|
2025-08-25 17:02:10 +00:00
|
|
|
export async function handleInstall(args: InstallArgs) {
|
|
|
|
|
try {
|
2025-09-02 08:15:47 -07:00
|
|
|
let installMetadata: ExtensionInstallMetadata;
|
|
|
|
|
|
|
|
|
|
if (args.source) {
|
|
|
|
|
const { source } = args;
|
|
|
|
|
if (
|
|
|
|
|
source.startsWith('http://') ||
|
|
|
|
|
source.startsWith('https://') ||
|
|
|
|
|
source.startsWith('git@')
|
|
|
|
|
) {
|
|
|
|
|
installMetadata = {
|
|
|
|
|
source,
|
|
|
|
|
type: 'git',
|
|
|
|
|
};
|
|
|
|
|
} else if (ORG_REPO_REGEX.test(source)) {
|
|
|
|
|
installMetadata = {
|
|
|
|
|
source: `https://github.com/${source}.git`,
|
|
|
|
|
type: 'git',
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`The source "${source}" is not a valid URL or "org/repo" format.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else if (args.path) {
|
|
|
|
|
installMetadata = {
|
|
|
|
|
source: args.path,
|
|
|
|
|
type: 'local',
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
// This should not be reached due to the yargs check.
|
|
|
|
|
throw new Error('Either --source or --path must be provided.');
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 17:02:10 +00:00
|
|
|
const extensionName = await installExtension(installMetadata);
|
|
|
|
|
console.log(
|
|
|
|
|
`Extension "${extensionName}" installed successfully and enabled.`,
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
2025-08-26 14:36:55 +00:00
|
|
|
console.error(getErrorMessage(error));
|
2025-08-25 17:02:10 +00:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const installCommand: CommandModule = {
|
|
|
|
|
command: 'install [--source | --path ]',
|
2025-09-02 08:15:47 -07:00
|
|
|
describe:
|
|
|
|
|
'Installs an extension from a git repository (URL or "org/repo") or a local path.',
|
2025-08-25 17:02:10 +00:00
|
|
|
builder: (yargs) =>
|
|
|
|
|
yargs
|
|
|
|
|
.option('source', {
|
2025-09-02 08:15:47 -07:00
|
|
|
describe: 'The git URL or "org/repo" of the extension to install.',
|
2025-08-25 17:02:10 +00:00
|
|
|
type: 'string',
|
|
|
|
|
})
|
|
|
|
|
.option('path', {
|
|
|
|
|
describe: 'Path to a local extension directory.',
|
|
|
|
|
type: 'string',
|
|
|
|
|
})
|
|
|
|
|
.conflicts('source', 'path')
|
|
|
|
|
.check((argv) => {
|
|
|
|
|
if (!argv.source && !argv.path) {
|
2025-09-02 08:15:47 -07:00
|
|
|
throw new Error('Either --source or --path must be provided.');
|
2025-08-25 17:02:10 +00:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}),
|
|
|
|
|
handler: async (argv) => {
|
|
|
|
|
await handleInstall({
|
|
|
|
|
source: argv['source'] as string | undefined,
|
|
|
|
|
path: argv['path'] as string | undefined,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|