mirror of
https://github.com/hanzoai/core-js.git
synced 2026-09-24 16:58:15 +00:00
177 lines
6.0 KiB
JavaScript
177 lines
6.0 KiB
JavaScript
/* eslint-disable no-console -- needed for logging */
|
|
import childProcess from 'node:child_process';
|
|
import { constants } from 'node:fs';
|
|
// eslint-disable-next-line node/no-unsupported-features/node-builtins -- ok
|
|
import { cp, access, readdir, readFile } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { promisify } from 'node:util';
|
|
|
|
const exec = promisify(childProcess.exec);
|
|
|
|
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');
|
|
const nodeModulesPath = join(execDir, 'node_modules');
|
|
if (!await isExists(nodeModulesPath)) {
|
|
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 = join(srcDir, BABEL_PATH);
|
|
const destPath = join(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 = join(srcDir, 'docs/');
|
|
const toDir = join(srcDir, 'docs/web/blog/');
|
|
const entries = await readdir(fromDir, { withFileTypes: true });
|
|
await Promise.all(entries
|
|
.filter(entry => entry.isFile())
|
|
.map(entry => cp(join(fromDir, entry.name), join(toDir, entry.name))));
|
|
console.timeEnd('Copied blog posts');
|
|
}
|
|
|
|
export async function copyCommonFiles(srcDir) {
|
|
console.log('Copying common files...');
|
|
console.time('Copied common files');
|
|
const toDir = join(srcDir, 'docs/web/');
|
|
await Promise.all([
|
|
cp(`${ srcDir }CHANGELOG.md`, `${ toDir }changelog.md`),
|
|
cp(`${ srcDir }CONTRIBUTING.md`, `${ toDir }contributing.md`),
|
|
cp(`${ srcDir }SECURITY.md`, `${ toDir }security.md`),
|
|
]);
|
|
console.timeEnd('Copied common files');
|
|
}
|
|
|
|
export async function buildAndCopyCoreJS(version, srcDir, cacheDir = null, checkout = false) {
|
|
async function bundlePackage(esModules) {
|
|
await exec(`npm run bundle-package${ esModules ? ' esmodules' : '' }`, { cwd: srcDir });
|
|
const bundleName = `core-js-bundle${ esModules ? '-esmodules' : '' }.js`;
|
|
const srcPath = join(srcDir, 'packages/core-js-bundle/minified.js');
|
|
const destPath = join(srcDir, 'website/src/public/bundles/', target, bundleName);
|
|
await cp(srcPath, destPath);
|
|
if (cacheDir !== null) {
|
|
const cachePath = join(cacheDir, target, bundleName);
|
|
await cp(srcPath, cachePath);
|
|
}
|
|
}
|
|
|
|
const target = version.branch ?? version.tag;
|
|
console.log(`Building and copying core-js for ${ target }`);
|
|
const targetBundlePath = join(cacheDir ?? '', target);
|
|
|
|
if (cacheDir !== null && await isExists(targetBundlePath)) {
|
|
console.time('Core JS bundles copied');
|
|
const bundlePath = join(targetBundlePath, 'core-js-bundle.js');
|
|
const destBundlePath = join(srcDir, 'website/src/public/bundles/', target, 'core-js-bundle.js');
|
|
await cp(bundlePath, destBundlePath);
|
|
|
|
const esmodulesBundlePath = join(targetBundlePath, 'core-js-bundle-esmodules.js');
|
|
const esmodulesDestBundlePath = join(srcDir, 'website/src/public/bundles/', target, 'core-js-bundle-esmodules.js');
|
|
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 bundlePackage(false);
|
|
await bundlePackage(true);
|
|
|
|
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, local = false) {
|
|
console.log('Building web...');
|
|
console.time('Built web');
|
|
let command = 'npm run build-website';
|
|
if (branch) command += ` branch=${ branch }`;
|
|
if (local) command += ' local';
|
|
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, 'utf8');
|
|
return JSON.parse(buffer);
|
|
}
|
|
|
|
export function expandVersionsConfig(config) {
|
|
let defaultIndex = null;
|
|
const $config = config.map(({ label, branch, path, tag, default: $default }, index) => {
|
|
if ($default) {
|
|
if (defaultIndex !== null) throw new Error('Duplicate default');
|
|
defaultIndex = index;
|
|
}
|
|
return {
|
|
branch,
|
|
default: false,
|
|
label,
|
|
path: path ?? label,
|
|
tag,
|
|
};
|
|
});
|
|
|
|
if (defaultIndex === null) throw new Error('Missed default');
|
|
|
|
return [{ ...$config[defaultIndex], default: true }, ...$config];
|
|
}
|