From 443b77d8f45e5e13f23351e9e9a6e30743eafe26 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Fri, 7 Jun 2024 16:21:40 +0800 Subject: [PATCH] Upload and publish if no command is specified (#81) --- cli.js | 38 ++++++++++++++++++++------------------ config.js | 4 ++-- readme.md | 17 +++++++++++------ test/config.js | 7 +++++++ util.js | 12 ------------ 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/cli.js b/cli.js index 57e7b41..e134d16 100755 --- a/cli.js +++ b/cli.js @@ -9,31 +9,34 @@ import { upload, publish, fetchToken } from './wrapper.js'; import { isUploadSuccess, handlePublishStatus, - validateInput, } from './util.js'; const cli = meow(` Usage - $ chrome-webstore-upload + $ chrome-webstore-upload [command] - where is one of + where [command] can be one of upload, publish - Options - --source Path to either a zip file, or a directory to be zipped. Defaults to the value of webExt.sourceDir in package.json or the current directory if not specified - --extension-id The ID of the Chrome Extension (environment variable EXTENSION_ID) - --client-id OAuth2 Client ID (environment variable CLIENT_ID) - --client-secret OAuth2 Client Secret (environment variable CLIENT_SECRET) - --refresh-token OAuth2 Refresh Token (environment variable REFRESH_TOKEN) - --auto-publish Can be used with the "upload" command - --trusted-testers Can be used with the "publish" command + if the command is missing, it will both upload and publish the extension. + Options + --source Path to either a zip file or a directory to be zipped. Defaults to the value of webExt.sourceDir in package.json or the current directory if not specified + --extension-id The ID of the Chrome Extension (environment variable EXTENSION_ID) + --client-id OAuth2 Client ID (environment variable CLIENT_ID) + --client-secret OAuth2 Client Secret (environment variable CLIENT_SECRET) + --refresh-token OAuth2 Refresh Token (environment variable REFRESH_TOKEN) + --auto-publish Can be used with the "upload" command (deprecated, use \`chrome-webstore-upload\` without a command instead) + --trusted-testers Can be used with the "publish" command Examples + Upload and publish a new version, using existing environment variables and the default value for --source + $ chrome-webstore-upload + Upload new extension archive to the Chrome Web Store - $ chrome-webstore-upload upload --source extension.zip --extension-id $EXTENSION_ID --client-id $CLIENT_ID --client-secret $CLIENT_SECRET --refresh-token $REFRESH_TOKEN + $ chrome-webstore-upload upload --source my-custom-zip.zip - Publish extension (with CLIENT_ID, CLIENT_SECRET, and REFRESH_TOKEN set as env variables) + Publish the last uploaded version (whether it was uploaded via web UI or via CLI) $ chrome-webstore-upload publish --extension-id elomekmlfonmdhmpmdfldcjgdoacjcba `, { importMeta: import.meta, @@ -44,9 +47,8 @@ const cli = meow(` }, }); -const preliminaryValidation = validateInput(cli.input, cli.flags); -if (preliminaryValidation.error) { - console.error(preliminaryValidation.error); +if (cli.input.length > 1) { + console.error('Too many parameters'); cli.showHelp(1); } @@ -130,7 +132,7 @@ function errorHandler(error) { 'Probably the provided client ID is not valid. Try following the guide again', ); console.error( - 'https://github.com/fregante/chrome-webstore-upload/blob/main/How%20to%20generate%20Google%20API%20keys.md', + 'https://github.com/fregante/chrome-webstore-upload-keys', ); console.error({ clientId }); return; @@ -142,7 +144,7 @@ function errorHandler(error) { 'Probably the provided refresh token is not valid. Try following the guide again', ); console.error( - 'https://github.com/fregante/chrome-webstore-upload/blob/main/How%20to%20generate%20Google%20API%20keys.md', + 'https://github.com/fregante/chrome-webstore-upload-keys', ); console.error({ clientId, refreshToken }); return; diff --git a/config.js b/config.js index f7fc15a..3555feb 100644 --- a/config.js +++ b/config.js @@ -12,9 +12,9 @@ export default async function getConfig(command, flags) { return { apiConfig, zipPath: await findSource(flags.source), - isUpload: command === 'upload', + isUpload: command === 'upload' || !command, isPublish: command === 'publish', - autoPublish: flags.autoPublish, + autoPublish: flags.autoPublish || !command, trustedTesters: flags.trustedTesters, }; } diff --git a/readme.md b/readme.md index 16aef6f..16eb330 100644 --- a/readme.md +++ b/readme.md @@ -27,25 +27,30 @@ $ chrome-webstore-upload --help CLI Utility to quickly upload + publish extensions to the Chrome Web Store Usage - $ chrome-webstore-upload + $ chrome-webstore-upload [command] - where is one of + where [command] can be one of upload, publish + if the command is missing, it will both upload and publish the extension. + Options - --source Path to either a zip file, or a directory to be zipped (Defaults to cwd if not specified) + --source Path to either a zip file or a directory to be zipped. Defaults to the value of webExt.sourceDir in package.json or the current directory if not specified --extension-id The ID of the Chrome Extension (environment variable EXTENSION_ID) --client-id OAuth2 Client ID (environment variable CLIENT_ID) --client-secret OAuth2 Client Secret (environment variable CLIENT_SECRET) --refresh-token OAuth2 Refresh Token (environment variable REFRESH_TOKEN) - --auto-publish Can be used with the "upload" command + --auto-publish Can be used with the "upload" command (deprecated, use `chrome-webstore-upload` without a command instead) --trusted-testers Can be used with the "publish" command Examples + Upload and publish a new version, using existing environment variables and the default value for --source + $ chrome-webstore-upload + Upload new extension archive to the Chrome Web Store - $ chrome-webstore-upload upload --source extension.zip --extension-id $EXTENSION_ID --client-id $CLIENT_ID --client-secret $CLIENT_SECRET --refresh-token $REFRESH_TOKEN + $ chrome-webstore-upload upload --source my-custom-zip.zip - Publish extension (with CLIENT_ID, CLIENT_SECRET, and REFRESH_TOKEN set as env variables) + Publish the last uploaded version (whether it was uploaded via web UI or via CLI) $ chrome-webstore-upload publish --extension-id elomekmlfonmdhmpmdfldcjgdoacjcba ``` diff --git a/test/config.js b/test/config.js index 3f9bd63..5950c5c 100644 --- a/test/config.js +++ b/test/config.js @@ -57,3 +57,10 @@ test('Auto Publish', async t => { t.true(config.autoPublish); }); + +test('Auto upload and publish', async t => { + const config = await createConfig('', {}); + t.false(config.isPublish); + t.true(config.isUpload); + t.true(config.autoPublish); +}); diff --git a/util.js b/util.js index 629f3a1..472ad2f 100644 --- a/util.js +++ b/util.js @@ -19,18 +19,6 @@ export function handlePublishStatus(item) { throw item; } -export function validateInput(input) { - if (input.length === 0) { - return { error: 'Must specify "upload" or "publish"' }; - } - - if (input.length > 1) { - return { error: 'Too many parameters' }; - } - - return { valid: true }; -} - export function zipPath(root, file) { return relative(root, file); }