Skip to content

Commit

Permalink
chore(check-flow-libs): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Jun 17, 2024
1 parent 1239777 commit 97d43ef
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 122 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
75 changes: 75 additions & 0 deletions scripts/check-flow-libs/flow-lib-updater.js
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions scripts/check-flow-libs/index.js
Original file line number Diff line number Diff line change
@@ -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');
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "check-libs",
"name": "check-flow-libs",
"version": "1.0.0",
"private": true,
"type": "module",
Expand Down
55 changes: 0 additions & 55 deletions scripts/check-libs/index.js

This file was deleted.

65 changes: 0 additions & 65 deletions scripts/check-libs/utils.js

This file was deleted.

0 comments on commit 97d43ef

Please sign in to comment.