diff --git a/package.json b/package.json index e0dec7884..983a01c1d 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "lint": "turbo run lint", "format": "prettier --write \"**/*.{ts,tsx,md}\"", "clean": "pnpm -r --parallel exec rimraf dist .turbo node_modules", - "checkflowlibs": "cd scripts/check-libs && pnpm start" + "checkflowlibs": "cd scripts/check-flow-libs && pnpm start" }, "devDependencies": { "@turbo/gen": "^1.10.14", diff --git a/scripts/check-flow-libs/flow-lib-updater.js b/scripts/check-flow-libs/flow-lib-updater.js new file mode 100644 index 000000000..ef7c53a71 --- /dev/null +++ b/scripts/check-flow-libs/flow-lib-updater.js @@ -0,0 +1,75 @@ +import { dirname, resolve } from 'path'; +import { fileURLToPath } from 'url'; +import { readFile, writeFile } from 'fs/promises'; +import latestVersion from 'latest-version'; +import { exec } from '@actions/exec'; +import { parse as parseEnv, stringify as stringifyEnv } from 'envfile'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +/** + * This is a helper function for checking the latest version on npm against the version in the .env file for a specific site. + * You need to create the helper and then call the start method to check the version. + */ +export async function FlowLibUpdater({ siteName, packageName, envKey }) { + const sitePath = resolve(__dirname, `../../sites/${siteName}`); + const latestNpmVersion = await latestVersion(packageName); + const envPath = resolve(sitePath, '.env'); + const envContent = await readFile(envPath); + const envParsed = parseEnv(envContent); + const currentVersion = envParsed[envKey]; + + // writes the latest version from npm to the .env file + async function updateEnv() { + console.log(`update ${packageName} .env file`); + + if (!envParsed[envKey]) { + console.log(`couldn't find ${envKey} in .env file ${packagePath}`); + return; + } + + envParsed[versionKey] = latestNpmVersion; + + const envUpdated = stringifyEnv(envParsed); + + await writeFile(envPath, envUpdated); + } + + // updates all workspaces that use the package + async function updatePkgs() { + console.log(`update ${packageName} dependencies`); + + await exec(`pnpm`, [ + `--recursive`, + `-C=${__dirname}/../../`, + 'update', + `${packageName}@${latestNpmVersion}`, + ]); + } + + return { + async start() { + if (!areVersionsEqual(currentVersion, latestNpmVersion)) { + await updatePkgs(); + await updateEnv(); + + return latestNpmVersion; + } + + return null; + }, + }; +} + +// utils + +function areVersionsEqual(a, b) { + if (!a || !b) { + return false; + } + + const versionA = a.replace('^', ''); + const versionB = b.replace('^', ''); + + return versionA === versionB; +} diff --git a/scripts/check-flow-libs/index.js b/scripts/check-flow-libs/index.js new file mode 100644 index 000000000..aa88e00a4 --- /dev/null +++ b/scripts/check-flow-libs/index.js @@ -0,0 +1,23 @@ +import { FlowLibUpdater } from './flow-lib-updater.js'; + +const reactFlowUpdater = await FlowLibUpdater({ + packageName: 'reactflow', + envKey: 'NEXT_PUBLIC_REACT_FLOW_VERSION', + siteName: 'reactflow.dev', +}); +const reactFlowUpdated = await reactFlowUpdater.start(); + +if (reactFlowUpdated) { + console.log('updated reactflow'); +} + +const svelteFlowUpdater = await FlowLibUpdater({ + packageName: '@xyflow/svelte', + envKey: 'NEXT_PUBLIC_SVELTE_FLOW_VERSION', + siteName: 'svelteflow.dev', +}); +const svelteFlowUpdated = await svelteFlowUpdater.start(); + +if (svelteFlowUpdated) { + console.log('updated @xyflow/svelte'); +} diff --git a/scripts/check-libs/package.json b/scripts/check-flow-libs/package.json similarity index 91% rename from scripts/check-libs/package.json rename to scripts/check-flow-libs/package.json index e08c5a8b0..6cb9f2c24 100644 --- a/scripts/check-libs/package.json +++ b/scripts/check-flow-libs/package.json @@ -1,5 +1,5 @@ { - "name": "check-libs", + "name": "check-flow-libs", "version": "1.0.0", "private": true, "type": "module", diff --git a/scripts/check-libs/index.js b/scripts/check-libs/index.js deleted file mode 100644 index 508983367..000000000 --- a/scripts/check-libs/index.js +++ /dev/null @@ -1,55 +0,0 @@ -import { dirname, resolve } from 'path'; -import { fileURLToPath } from 'url'; - -import latestVersion from 'latest-version'; -import { - areVersionsEqual, - getPackageJSON, - updatePkgs, - updateEnvVersion, - getParsedEnv, -} from './utils.js'; - -const __dirname = dirname(fileURLToPath(import.meta.url)); - -/** - * React Flow - */ -const REACTFLOW_PACKAGE = 'reactflow'; -const REACTFLOW_ENV_KEY = 'NEXT_PUBLIC_REACT_FLOW_VERSION'; -const rfLatestVersion = await latestVersion(REACTFLOW_PACKAGE); - -const rfRootPath = resolve(__dirname, '../../sites/reactflow.dev'); -const rfPkg = await getPackageJSON(rfRootPath); -const rfPgkVersion = rfPkg.dependencies[REACTFLOW_PACKAGE]; -const rfEqual = areVersionsEqual(rfLatestVersion, rfPgkVersion); - -if (!rfEqual) { - console.log(`update all ${REACTFLOW_PACKAGE} dependencies`); - - updatePkgs(REACTFLOW_PACKAGE, rfLatestVersion); - - updateEnvVersion(rfRootPath, REACTFLOW_ENV_KEY, rfLatestVersion); -} - -/** - * Svelte Flow - */ -const SVELTEFLOW_PACKAGE = '@xyflow/svelte'; -const SVELTEFLOW_ENV_KEY = 'NEXT_PUBLIC_SVELTE_FLOW_VERSION'; -const sfLatestVersion = await latestVersion(SVELTEFLOW_PACKAGE); - -const sfRootPath = resolve(__dirname, '../../sites/svelteflow.dev'); -const sfEnvParsed = await getParsedEnv(resolve(sfRootPath, '.env')); -const sfEnvVersion = sfEnvParsed[SVELTEFLOW_ENV_KEY]; -const sfEqual = areVersionsEqual(sfLatestVersion, sfEnvVersion); - -if (!sfEqual) { - console.log( - `update ${SVELTEFLOW_PACKAGE} from ${sfEnvVersion} to ${sfLatestVersion}`, - ); - - updatePkgs(SVELTEFLOW_PACKAGE, sfLatestVersion); - - updateEnvVersion(sfRootPath, SVELTEFLOW_ENV_KEY, sfLatestVersion); -} diff --git a/scripts/check-libs/utils.js b/scripts/check-libs/utils.js deleted file mode 100644 index 92493250f..000000000 --- a/scripts/check-libs/utils.js +++ /dev/null @@ -1,65 +0,0 @@ -import { dirname, resolve } from 'path'; -import { fileURLToPath } from 'url'; - -import { readFile, writeFile } from 'fs/promises'; -import { exec } from '@actions/exec'; -import { parse as parseEnv, stringify as stringifyEnv } from 'envfile'; - -const __dirname = dirname(fileURLToPath(import.meta.url)); - -export async function getPackageJSON(packagePath) { - const json = JSON.parse( - await readFile( - new URL(resolve(packagePath, 'package.json'), import.meta.url), - ), - ); - - return json; -} - -export async function updatePkgs(packageName, version) { - console.log(`update ${packageName} for all workspaces`); - - await exec(`pnpm`, [ - `--recursive`, - `-C=${__dirname}/../../`, - 'update', - `${packageName}@${version}`, - ]); -} - -export async function getParsedEnv(envPath) { - const envContent = await readFile(envPath); - const envParsed = parseEnv(envContent); - - return envParsed; -} - -export async function updateEnvVersion(packagePath, versionKey, version) { - console.log(`update ${packagePath} .env file`); - - const envPath = resolve(packagePath, '.env'); - const envParsed = await getParsedEnv(envPath); - - if (!envParsed[versionKey]) { - console.log(`couldn't find ${versionKey} in .env file ${packagePath}`); - return; - } - - envParsed[versionKey] = version; - - const envUpdated = stringifyEnv(envParsed); - - await writeFile(envPath, envUpdated); -} - -export function areVersionsEqual(a, b) { - if (!a || !b) { - return false; - } - - const versionA = a.replace('^', ''); - const versionB = b.replace('^', ''); - - return versionA === versionB; -}