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

docs: use embedded build for preview examples #3343

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ versions.json
!/docs/api/index.md
/docs/api/api-search-index.json
/docs/public/api-diff-index.json
/docs/public/faker.js

# Faker
TAGS
Expand Down
18 changes: 13 additions & 5 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { DefaultTheme } from 'vitepress/theme';
import { apiPages } from './api-pages';
import {
algoliaIndex,
commitHash,
isReleaseBranch,
version,
versionBannerInfix,
versionLabel,
Expand All @@ -13,7 +15,13 @@ type SidebarItem = DefaultTheme.SidebarItem;

const description =
'Generate massive amounts of fake (but reasonable) data for testing and development.';
const image = 'https://fakerjs.dev/social-image.png';
const socialImage = 'https://fakerjs.dev/social-image.png';
const consoleDownload = isReleaseBranch
? `https://cdn.jsdelivr.net/npm/@faker-js/faker@${version}/+esm`
: '/faker.js';
const consoleVersion = isReleaseBranch
? version
: `${version.replace(/-.*$/, '')}-preview+${commitHash}`;

function getSideBarWithExpandedEntry(entryToExpand: string): SidebarItem[] {
const links: SidebarItem[] = [
Expand Down Expand Up @@ -111,12 +119,12 @@ const config: UserConfig<DefaultTheme.Config> = {
['meta', { name: 'theme-color', content: '#40af7c' }],
['meta', { name: 'og:title', content: 'FakerJS' }],
['meta', { name: 'og:description', content: description }],
['meta', { name: 'og:image', content: image }],
['meta', { name: 'og:image', content: socialImage }],
['meta', { name: 'twitter:card', content: 'summary_large_image' }],
['meta', { name: 'twitter:title', content: 'FakerJS' }],
['meta', { name: 'twitter:description', content: description }],
['meta', { name: 'twitter:site', content: '@faker_js' }],
['meta', { name: 'twitter:image', content: image }],
['meta', { name: 'twitter:image', content: socialImage }],
['meta', { name: 'twitter:image:alt', content: 'The FakerJS logo' }],
['link', { rel: 'me', href: 'https://fosstodon.org/@faker_js' }],
[
Expand All @@ -127,9 +135,9 @@ const logStyle = 'background: rgba(16, 183, 127, 0.14); color: rgba(255, 255, 24
console.log(\`%cIf you would like to test Faker in the browser console, you can do so using 'await enableFaker()'.
If you would like to test Faker in a playground, visit https://new.fakerjs.dev.\`, logStyle);
async function enableFaker() {
const imported = await import('https://cdn.jsdelivr.net/npm/@faker-js/faker@${version}/+esm');
const imported = await import('${consoleDownload}');
Object.assign(globalThis, imported);
console.log(\`%cYou can now start using Faker v${version}:
console.log(\`%cYou can now start using Faker v${consoleVersion}:
e.g. 'faker.food.description()' or 'fakerZH_CN.person.firstName()'
For other languages please refer to https://fakerjs.dev/guide/localization.html#available-locales
For a full list of all methods please refer to https://fakerjs.dev/api/\`, logStyle);
Expand Down
12 changes: 11 additions & 1 deletion docs/.vitepress/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function readBranchName(): string {
);
}

function readCommitHash(): string {
return execSync('git rev-parse HEAD').toString('utf8').trim() || 'unknown';
}

function readOtherLatestReleaseTagNames(): string[] {
const tags = execSync('git tag -l').toString('utf8').split('\n');
const latestTagByMajor: Record<string, string> = {};
Expand All @@ -34,10 +38,11 @@ function readOtherLatestReleaseTagNames(): string[] {
const {
CONTEXT: deployContext = 'local',
BRANCH: branchName = readBranchName(),
COMMIT_REF: commitRef = readCommitHash(),
} = process.env;

const otherVersions = readOtherLatestReleaseTagNames();
const isReleaseBranch = /^v\d+$/.test(branchName);
export const isReleaseBranch = /^v\d+$/.test(branchName);

/**
* The text of the version banner describing the current version.
Expand Down Expand Up @@ -65,6 +70,11 @@ export const versionBannerInfix: string | null = (() => {
*/
export const version = version_;

/**
* The commit hash of the current version.
*/
export const commitHash = commitRef.substring(0, 7);

/**
* The version label to display in the top-right corner of the site.
*/
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"generate": "run-s generate:locales generate:api-docs",
"generate:api-docs": "tsx ./scripts/apidocs.ts",
"generate:locales": "tsx ./scripts/generate-locales.ts",
"docs:build": "run-s generate:api-docs docs:build:run",
"docs:build": "run-s generate:api-docs docs:build:embedded docs:build:run",
"docs:build:embedded": "tsup-node --entry.faker src/index.ts --format esm --outDir docs/public --no-dts --no-clean",
"docs:build:run": "vitepress build docs",
"docs:build:ci": "run-s build docs:build",
"docs:dev": "run-s generate:api-docs docs:dev:run",
"docs:dev": "run-s generate:api-docs docs:build:embedded docs:dev:run",
"docs:dev:run": "vitepress dev docs",
"docs:serve": "vitepress serve docs --port 5173",
"docs:diff": "tsx ./scripts/diff.ts",
Expand Down
Loading