sdk(astro,nextjs): add astro sdk and ensure window.op always first on nextjs

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-05-06 22:10:38 +02:00
parent 0189b922f2
commit 2d8f6f36f6
17 changed files with 2110 additions and 83 deletions

View File

@@ -4,7 +4,7 @@
"private": true,
"license": "MIT",
"scripts": {
"publish": "pnpm dlx ts-node publish.ts",
"publish": "jiti publish.ts",
"typecheck": "tsc --noEmit"
},
"dependencies": {

View File

@@ -1,6 +1,6 @@
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { join, resolve } from 'node:path';
import arg from 'arg';
import type { ReleaseType } from 'semver';
import semver, { RELEASE_TYPES } from 'semver';
@@ -9,9 +9,14 @@ import semver, { RELEASE_TYPES } from 'semver';
interface PackageJson {
name: string;
version: string;
scripts?: Record<string, string>;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
[key: string]: unknown;
config?: {
transformPackageJson?: boolean;
transformEnvs: boolean;
};
}
interface PackageInfo extends PackageJson {
@@ -21,15 +26,16 @@ interface PackageInfo extends PackageJson {
interface PublishConfig {
registry: string;
test: boolean;
clear: boolean;
}
// Utility functions
const workspacePath = (relativePath: string) =>
path.resolve(__dirname, '../../', relativePath);
resolve(__dirname, '../../', relativePath);
const savePackageJson = (absPath: string, data: PackageJson) => {
fs.writeFileSync(absPath, JSON.stringify(data, null, 2), 'utf-8');
execSync(`npx biome format ${absPath} --fix`);
};
const exit = (message: string, error?: unknown) => {
@@ -54,7 +60,7 @@ const getNextVersion = (version: string, type: ReleaseType): string => {
const nextVersion = semver.inc(version, type);
if (!nextVersion) throw new Error('Invalid version');
return type.startsWith('pre')
? nextVersion.replace(/-.*$/, '-beta')
? nextVersion.replace(/-.*$/, '-rc')
: nextVersion;
};
@@ -70,7 +76,7 @@ const loadPackages = (
return Object.fromEntries(
sdks.map((sdk) => {
const pkgPath = path.join(sdksPath, sdk, 'package.json');
const pkgPath = join(sdksPath, sdk, 'package.json');
const pkgJson = JSON.parse(
fs.readFileSync(pkgPath, 'utf-8'),
) as PackageJson;
@@ -111,30 +117,10 @@ const updatePackageJsonForRelease = (
dependents: string[],
): void => {
const { nextVersion, localPath, ...restPkgJson } = packages[name]!;
const newPkgJson: PackageJson = {
let newPkgJson: PackageJson = {
...restPkgJson,
private: false,
type: 'module',
main: './dist/index.js',
module: './dist/index.mjs',
types: './dist/index.d.ts',
files: ['dist'],
exports: {
'.': {
import: './dist/index.js',
require: './dist/index.cjs',
types: './dist/index.d.ts',
},
...(name === '@openpanel/nextjs'
? {
'./server': {
import: './dist/server.js',
require: './dist/server.cjs',
types: './dist/server.d.ts',
},
}
: {}),
},
version: nextVersion,
dependencies: Object.fromEntries(
Object.entries(restPkgJson.dependencies || {}).map(
@@ -142,17 +128,87 @@ const updatePackageJsonForRelease = (
depName,
dependents.includes(depName)
? packages[depName]?.nextVersion ||
depVersion.replace(/-local$/, '')
: depVersion.replace(/-local$/, ''),
depVersion.replace(/-local$/, '').replace(/^workspace:/, '')
: depVersion.replace(/-local$/, '').replace(/^workspace:/, ''),
],
),
),
};
if (packages[name]!.config?.transformPackageJson !== false) {
newPkgJson = {
...newPkgJson,
main: './dist/index.js',
module: './dist/index.mjs',
types: './dist/index.d.ts',
files: ['dist'],
exports: restPkgJson.exports ?? {
'.': {
import: './dist/index.js',
require: './dist/index.cjs',
types: './dist/index.d.ts',
},
...(name === '@openpanel/nextjs'
? {
'./server': {
import: './dist/server.js',
require: './dist/server.cjs',
types: './dist/server.d.ts',
},
}
: {}),
},
};
}
savePackageJson(workspacePath(`${localPath}/package.json`), newPkgJson);
packages[name]!.dependencies = newPkgJson.dependencies;
};
const searchAndReplace = (path: string, search: RegExp, replace: string) => {
const files = fs.readdirSync(path);
for (const file of files) {
const fullpath = join(path, file);
if (file === 'node_modules') {
continue;
}
if (file.includes('.')) {
const content = fs.readFileSync(fullpath, {
encoding: 'utf-8',
});
const match = content.match(search);
if (match) {
console.log(`✏️ Will replace ${search} with ${replace} in ${file}`);
const newContent = content.replaceAll(search, replace);
fs.writeFileSync(fullpath, newContent, {
encoding: 'utf-8',
});
}
} else {
searchAndReplace(fullpath, search, replace);
}
}
};
const transformPackages = (
packages: Record<string, PackageInfo>,
dependents: string[],
): void => {
for (const dep of dependents) {
const pkg = packages[dep];
if (pkg && pkg.config?.transformEnvs === true) {
const currentVersion = pkg.version;
const nextVersion = pkg.nextVersion;
searchAndReplace(
workspacePath(pkg.localPath),
new RegExp(`${currentVersion}`, 'g'),
nextVersion,
);
}
}
};
const buildPackages = (
packages: Record<string, PackageInfo>,
dependents: string[],
@@ -166,6 +222,10 @@ const buildPackages = (
});
for (const dep of dependents) {
if (!packages[dep]?.scripts?.build) {
console.log(`🔨 Skipping build for ${dep}`);
continue;
}
console.log(`🔨 Building ${dep}`);
const cmd = `pnpm build ${versionEnvs.join(' ')}`;
console.log(` Running: ${cmd}`);
@@ -180,7 +240,7 @@ const publishPackages = (
dependents: string[],
config: PublishConfig,
): void => {
if (config.test) {
if (config.clear) {
execSync('rm -rf ~/.local/share/verdaccio/storage/@openpanel');
}
@@ -203,9 +263,8 @@ const restoreAndUpdateLocal = (
dependents: string[],
): void => {
const filesToRestore = dependents
.map((dep) => workspacePath(packages[dep]!.localPath))
.map((dep) => join(workspacePath(packages[dep]!.localPath), 'package.json'))
.join(' ');
console.log(`git checkout ${filesToRestore}`);
execSync(`git checkout ${filesToRestore}`);
@@ -221,9 +280,9 @@ const restoreAndUpdateLocal = (
([depName, depVersion]) => [
depName,
dependents.includes(depName)
? `${packages[depName]!.nextVersion}-local`
? `workspace:${packages[depName]!.nextVersion}-local`
: packages[depName]
? `${packages[depName]!.version}-local`
? `workspace:${packages[depName]!.version}-local`
: depVersion,
],
),
@@ -251,9 +310,10 @@ function main() {
const args = arg({
'--name': String,
'--publish': Boolean,
'--test': Boolean,
'--npm': Boolean,
'--skip-git': Boolean,
'--type': String,
'--clear': Boolean,
});
if (!args['--skip-git']) {
@@ -287,14 +347,16 @@ function main() {
updatePackageJsonForRelease(packages, dep, dependents);
}
transformPackages(packages, dependents);
buildPackages(packages, dependents);
if (args['--publish']) {
const config: PublishConfig = {
registry: args['--test']
? 'http://localhost:4873'
: 'https://registry.npmjs.org',
test: args['--test'] || false,
registry: args['--npm']
? 'https://registry.npmjs.org'
: 'http://localhost:4873',
clear: args['--clear'] || false,
};
publishPackages(packages, dependents, config);