Skip to content
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
9 changes: 9 additions & 0 deletions .github/workflows/build-web-for-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ jobs:
username: ci
privateKey: ${{ secrets.CI_SSH_KEY }}

- name: Copy runner helpers file to remote server
uses: garygrossgarten/github-action-scp@0.9.0
with:
local: website/scripts/helpers.mjs
remote: /var/www/core-js/helpers.mjs
host: ${{ secrets.REMOTE_HOST }}
username: ci
privateKey: ${{ secrets.CI_SSH_KEY }}

- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.1
with:
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/build-web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ jobs:
username: ci
privateKey: ${{ secrets.CI_SSH_KEY }}

- name: Copy runner helpers file to remote server
uses: garygrossgarten/github-action-scp@0.9.0
with:
local: website/scripts/helpers.mjs
remote: /var/www/core-js/helpers.mjs
host: ${{ secrets.REMOTE_HOST }}
username: ci
privateKey: ${{ secrets.CI_SSH_KEY }}

- name: Setup SSH
uses: webfactory/ssh-agent@v0.9.1
with:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"prepare": "npm run zxi scripts/prepare.mjs",
"build-compat": "npm run zxi scripts/build-compat/index.mjs",
"build-web": "npm run zxi time website/index.mjs",
"build-web-local": "npm run zxi time website/build-local.mjs",
"bundle": "run-s bundle-package bundle-tests",
"bundle-package": "npm run zxi time scripts/bundle-package/bundle-package.mjs",
"bundle-tests": "npm run zxi time cd scripts/bundle-tests/bundle-tests.mjs",
Expand Down
1 change: 1 addition & 0 deletions tests/eslint/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,7 @@ export default [
'tests/compat/*.mjs',
'tests/@(compat-@(data|tools)|eslint|entries|observables|promises-aplus|unit-@(karma|node))/**',
'website/runner.mjs',
'website/helpers.mjs',
],
rules: nodeDev,
},
Expand Down
23 changes: 23 additions & 0 deletions website/build-local.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
hasDocs, copyBlogPosts, copyBabelStandalone, copyCommonFiles, buildAndCopyCoreJS, buildWeb, getCurrentBranch,
} from './scripts/helpers.mjs';

const BUILD_SRC_DIR = './';
const BUNDLES_DIR = 'bundles';

try {
console.time('Finished in');
const targetBranch = await getCurrentBranch(BUILD_SRC_DIR);

const version = { branch: targetBranch, label: targetBranch };
await hasDocs(version, BUILD_SRC_DIR);
await buildAndCopyCoreJS(version, false, BUILD_SRC_DIR, BUNDLES_DIR);

await copyBabelStandalone(BUILD_SRC_DIR);
await copyBlogPosts(BUILD_SRC_DIR);
await copyCommonFiles(BUILD_SRC_DIR);
await buildWeb(targetBranch, BUILD_SRC_DIR);
console.timeEnd('Finished in');
} catch (error) {
console.error(error);
}
26 changes: 7 additions & 19 deletions website/build.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
/* eslint-disable import/no-unresolved -- dependencies are not installed */
import { getDefaultVersion } from './scripts/helpers.mjs';
import fm from 'front-matter';
import { JSDOM } from 'jsdom';
import { Marked } from 'marked';
import { gfmHeadingId, getHeadingList } from 'marked-gfm-heading-id';
import markedAlert from 'marked-alert';
import config from './config/config.mjs';
import { fs } from 'zx';
import { argv, fs } from 'zx';

const { copy, mkdir, readFile, readJson, readdir, writeFile } = fs;

const args = process.argv;
const lastArg = args.at(-1);
const BRANCH = lastArg.startsWith('branch=') ? lastArg.slice('branch='.length) : undefined;
const DEFAULT_VERSION = await getDefaultVersion();
const BASE = BRANCH ? `/branches/${ BRANCH }/` : '/';
const branchArg = argv._.find(item => item.startsWith('branch='));
const BRANCH = branchArg ? branchArg.slice('branch='.length) : undefined;
const LOCAL = argv._.includes('local');
const BASE = LOCAL && BRANCH ? '/core-js/website/dist/' : BRANCH ? `/branches/${ BRANCH }/` : '/';
const DEFAULT_VERSION = await getDefaultVersion(config.versionsFile, BRANCH);

let htmlFileName = '';
let docsMenu = '';
let isBlog = false;
let isDocs = false;

async function getDefaultVersion() {
if (BRANCH) return BRANCH;

const versions = await readJson(config.versionsFile);
return versions.find(v => v.default)?.label;
}

async function getAllMdFiles(dir) {
const entries = await readdir(dir, { withFileTypes: true });
const files = [];
Expand Down Expand Up @@ -209,12 +203,6 @@ async function buildBlogMenu() {
return menu;
}

// eslint-disable-next-line no-unused-vars -- use it later
async function getVersionTags() {
const tagsString = await $`git tag | grep -E "^v[4-9]\\.[0-9]+\\.[0-9]+$" | sort -V`;
return tagsString.stdout.split('\n');
}

async function getVersionFromMdFile(mdPath) {
const match = mdPath.match(/\/web\/(?<version>[^/]+)\/docs\//);
return match?.groups?.version ?? DEFAULT_VERSION;
Expand Down
151 changes: 151 additions & 0 deletions website/scripts/helpers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* eslint-disable no-console -- needed for logging */
import childProcess from 'node:child_process';
import { constants } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { promisify } from 'node:util';

const exec = promisify(childProcess.exec);
const { cp, access, readdir, readFile } = fs;

const BABEL_PATH = 'website/node_modules/@babel/standalone/babel.min.js';

export async function isExists(target) {
try {
await access(target, constants.F_OK);
return true;
} catch {
return false;
}
}

export async function hasDocs(version, execDir) {
const target = version.branch ? `origin/${ version.branch }` : version.tag;
console.log(`Checking if docs exist in "${ target }"...`);
try {
await exec(`git ls-tree -r --name-only ${ target } | grep "docs/web/docs/"`, { cwd: execDir });
} catch {
throw new Error(`Docs not found in "${ target }".`);
}
}

export async function installDependencies(execDir) {
console.log('Installing dependencies...');
console.time('Installed dependencies');
await exec('npm ci', { cwd: execDir });
console.timeEnd('Installed dependencies');
}

export async function copyBabelStandalone(srcDir) {
console.log('Copying Babel standalone...');
await installDependencies(`${ srcDir }website`);
console.time('Copied Babel standalone');
const babelPath = `${ srcDir }${ BABEL_PATH }`;
const destPath = `${ srcDir }website/src/public/babel.min.js`;
await cp(babelPath, destPath);
console.timeEnd('Copied Babel standalone');
}

export async function copyBlogPosts(srcDir) {
console.log('Copying blog posts...');
console.time('Copied blog posts');
const fromDir = `${ srcDir }docs/`;
const toDir = `${ srcDir }docs/web/blog/`;
const entries = await readdir(fromDir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isFile()) {
const srcFile = path.join(fromDir, entry.name);
const destFile = path.join(toDir, entry.name);
await cp(srcFile, destFile);
}
}
console.timeEnd('Copied blog posts');
}

export async function copyCommonFiles(srcDir) {
console.log('Copying common files...');
console.time('Copied common files');
const fromDir = `${ srcDir }`;
const toDir = `${ srcDir }docs/web/`;
await cp(`${ fromDir }CHANGELOG.md`, `${ toDir }changelog.md`);
await cp(`${ fromDir }CONTRIBUTING.md`, `${ toDir }contributing.md`);
await cp(`${ fromDir }SECURITY.md`, `${ toDir }security.md`);
console.timeEnd('Copied common files');
}

export async function buildAndCopyCoreJS(version, checkout, srcDir, destDir) {
const target = version.branch ?? version.tag;
const name = version.path ?? version.label;
console.log(`Building and copying core-js for ${ target }`);
const targetBundlePath = `${ destDir }/${ target }/`;

if (await isExists(targetBundlePath)) {
console.time('Core JS bundles copied');
const bundlePath = `${ targetBundlePath }core-js-bundle.js`;
const destBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle.js`;
const esmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`;
const esmodulesDestBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
await cp(bundlePath, destBundlePath);
await cp(esmodulesBundlePath, esmodulesDestBundlePath);
console.timeEnd('Core JS bundles copied');
return;
}

console.time('Core JS bundles built');
if (checkout) {
await checkoutVersion(version, srcDir);
}
await installDependencies(srcDir);
await exec('npm run bundle-package', { cwd: srcDir });
const bundlePath = `${ srcDir }packages/core-js-bundle/minified.js`;
const destPath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle.js`;
const destBundlePath = `${ targetBundlePath }core-js-bundle.js`;
await cp(bundlePath, destPath);
await cp(bundlePath, destBundlePath);

await exec('npm run bundle-package esmodules', { cwd: srcDir });
const esmodulesBundlePath = `${ srcDir }packages/core-js-bundle/minified.js`;
const esmodulesDestBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
const destEsmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`;
await cp(esmodulesBundlePath, esmodulesDestBundlePath);
await cp(esmodulesBundlePath, destEsmodulesBundlePath);
console.timeEnd('Core JS bundles built');
}

export async function checkoutVersion(version, execDir) {
if (version.branch) {
await exec(`git checkout origin/${ version.branch }`, { cwd: execDir });
} else {
await exec(`git checkout ${ version.tag }`, { cwd: execDir });
}
}

export async function buildWeb(branch, execDir) {
console.log('Building web...');
console.time('Built web');
let command = 'npm run build-web';
if (branch) command += ` branch=${ branch }`;
const stdout = await exec(command, { cwd: execDir });
console.timeEnd('Built web');
return stdout;
}

export async function getCurrentBranch(execDir) {
console.log('Getting current branch...');
console.time('Got current branch');
const { stdout } = await exec('git rev-parse --abbrev-ref HEAD', { cwd: execDir });
console.timeEnd('Got current branch');
return stdout.trim();
}

export async function getDefaultVersion(versionFile, defaultVersion = null) {
if (defaultVersion) return defaultVersion;

const versions = await readJSON(versionFile);
return versions.find(v => v.default)?.label;
}

export async function readJSON(filePath) {
const buffer = await readFile(filePath);
return JSON.parse(buffer);
}
Loading