From f7a45f7a4d20d7dfe737b3623c41e2877346578b Mon Sep 17 00:00:00 2001 From: Martijn Date: Tue, 7 Jan 2025 10:47:51 +0100 Subject: [PATCH] feat: script to compare local plugins with hub --- check-vendure-hub-plugins.ts | 98 +++++++++++++++++++++++++++++++++++ update-vendure-hub-plugins.js | 9 ---- 2 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 check-vendure-hub-plugins.ts delete mode 100644 update-vendure-hub-plugins.js diff --git a/check-vendure-hub-plugins.ts b/check-vendure-hub-plugins.ts new file mode 100644 index 00000000..437a032c --- /dev/null +++ b/check-vendure-hub-plugins.ts @@ -0,0 +1,98 @@ +import { SimpleGraphQLClient, testConfig } from '@vendure/testing'; +import { readdir, readFile } from 'fs/promises'; +import gql from 'graphql-tag'; +import path from 'path'; + +/** + * This script compares local plugins with the content on the Vendure Hub (https://hub.vendure.io/admin/) + * + * 3. Print all plugins to be created: that are not on the Hub, but exist locally. + * 4. Print all plugins to be deleted: that exists on the Hub, but not locally. + * + * @example + * npx tsx ./check-vendure-hub-plugins.ts + */ + +const hubUrl = 'https://hub.vendure.io/shop-api/'; +testConfig.apiOptions.channelTokenKey = undefined; +const client = new SimpleGraphQLClient(testConfig, hubUrl); + +(async () => { + const localPackageNames = await getLocalPackageNames(); + const plugins = await getPinelabPluginsInHub(); + + const pluginsToBeCreated = localPackageNames.filter( + (localName) => + !plugins.some((plugin) => plugin.customFields.packageName === localName) + ); + const pluginsToBeDeleted = plugins + .filter( + (plugin) => !localPackageNames.includes(plugin.customFields.packageName) + ) + .map((plugin) => plugin.customFields.packageName); + + console.log( + '\x1b[32mPlugins to be created on Hub: \n ', + pluginsToBeCreated.join('\n ') + ); + console.log( + '\x1b[31mPlugins to be deleted from Hub: \n ', + pluginsToBeDeleted.join('\n ') + ); +})(); + +async function getPinelabPluginsInHub(): Promise { + const result = await client.query(gql` + { + products( + options: { + take: 100 + filter: { repositoryUrl: { contains: "pinelab" } } + } + ) { + totalItems + items { + id + name + customFields { + packageName + repositoryUrl + } + } + } + } + `); + return result.products.items; +} + +/** + * Get all package names of the local plugins in packages/* + */ +async function getLocalPackageNames(): Promise { + const packageDir = './packages/'; + const pluginDirectories = (await readdir(packageDir, { withFileTypes: true })) + .filter((dir) => dir.isDirectory()) + .filter((dir) => dir.name.startsWith('vendure-')); + return await Promise.all( + pluginDirectories.map(async (pluginDir) => { + const packageJsonFilePath = path.join( + packageDir, + pluginDir.name, + 'package.json' + ); + const packageJson = JSON.parse( + await readFile(packageJsonFilePath, 'utf8') + ); + return packageJson.name; + }) + ); +} + +interface Plugin { + id: string; + name: string; + customFields: { + packageName: string; + repositoryUrl: string; + }; +} diff --git a/update-vendure-hub-plugins.js b/update-vendure-hub-plugins.js deleted file mode 100644 index 7c69520b..00000000 --- a/update-vendure-hub-plugins.js +++ /dev/null @@ -1,9 +0,0 @@ -/** - * This script creates or updates all plugins in this repository on the Vendure Hub (https://hub.vendure.io/admin/) - * - * 1. Find all plugins that contain `pinelab` in the `repositoryUrl` - * 2. Update names and descriptions of all plugins in this repository that have a corresponding entry in the Vendure Hub - * 3. Create new entries for plugins that do not yet exist in the Vendure Hub - * 4. List all plugins with `pinelab` but no matching entry in the Vendure Hub in the console. These should be deleted manually - * - */