Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: declare version #515

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stale-coats-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cube-dev/ui-kit': minor
---

Declare uikit version globally.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"scripts": {
"start": "pnpm storybook",
"build": "npm-run-all clear:dist -p build:* && node ./scripts/copy-files.js && node scripts/add-banner.js",
"build": "npm-run-all clear:dist -p build:* && node ./scripts/copy-files.js && node scripts/add-banner.js && node scripts/replace-version.js",
"build:esm": "tsc -p tsconfig.es.json",
"build:cjs": "tsc -p tsconfig.cjs.json",
"watch": "pnpm build:esm --watch",
Expand Down
34 changes: 34 additions & 0 deletions scripts/replace-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require('fs');
const path = require('path');

console.log('Replacing version in compiled files...');

// Read package.json and extract the version
const packageJsonPath = path.resolve(__dirname, '../package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const version = packageJson.version;

// Define the directory where compiled files are located
const distDir = path.resolve(__dirname, '../dist');

// Function to replace version in all .js files in the dist directory
function replaceVersionInFiles(dir) {
const files = fs.readdirSync(dir);

files.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);

if (stat.isDirectory()) {
replaceVersionInFiles(filePath); // Recurse into subdirectories
} else if (file.endsWith('.js')) {
let content = fs.readFileSync(filePath, 'utf8');
// Replace placeholder with version, wrapped in quotes
content = content.replace(/__UIKIT_VERSION__/g, `${version}`);
fs.writeFileSync(filePath, content);
}
});
}

// Execute the replacement
replaceVersionInFiles(distDir);
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './version';

import { CubeTextProps, Text } from './components/content/Text';
import { CubeTitleProps, Title } from './components/content/Title';
import { CubeParagraphProps, Paragraph } from './components/content/Paragraph';
Expand Down
20 changes: 20 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Window {
CubeUIKit: {
version: string;
};
}

if (window.CubeUIKit?.version) {
console.error('More than one version of CubeUIKit is loaded', {
loadedVersions: [window.CubeUIKit.version, '__UIKIT_VERSION'],
});
} else {
if (!window.CubeUIKit || !Array.isArray(window.CubeUIKit)) {
window.CubeUIKit = {
version: '__UIKIT_VERSION__',
};
} else {
window.CubeUIKit.version = '__UIKIT_VERSION__';
}
}
Loading