|
| 1 | +/* eslint-disable no-console -- needed for logging */ |
| 2 | +import childProcess from 'node:child_process'; |
| 3 | +import { constants } from 'node:fs'; |
| 4 | +import fs from 'node:fs/promises'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { promisify } from 'node:util'; |
| 7 | + |
| 8 | +const exec = promisify(childProcess.exec); |
| 9 | +const { cp, access, readdir, readFile } = fs; |
| 10 | + |
| 11 | +const BABEL_PATH = 'website/node_modules/@babel/standalone/babel.min.js'; |
| 12 | + |
| 13 | +export async function isExists(target) { |
| 14 | + try { |
| 15 | + await access(target, constants.F_OK); |
| 16 | + return true; |
| 17 | + } catch { |
| 18 | + return false; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export async function hasDocs(version, execDir) { |
| 23 | + const target = version.branch ? `origin/${ version.branch }` : version.tag; |
| 24 | + console.log(`Checking if docs exist in "${ target }"...`); |
| 25 | + try { |
| 26 | + await exec(`git ls-tree -r --name-only ${ target } | grep "docs/web/docs/"`, { cwd: execDir }); |
| 27 | + } catch { |
| 28 | + throw new Error(`Docs not found in "${ target }".`); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export async function installDependencies(execDir) { |
| 33 | + console.log('Installing dependencies...'); |
| 34 | + console.time('Installed dependencies'); |
| 35 | + await exec('npm ci', { cwd: execDir }); |
| 36 | + console.timeEnd('Installed dependencies'); |
| 37 | +} |
| 38 | + |
| 39 | +export async function copyBabelStandalone(srcDir) { |
| 40 | + console.log('Copying Babel standalone...'); |
| 41 | + await installDependencies(`${ srcDir }website`); |
| 42 | + console.time('Copied Babel standalone'); |
| 43 | + const babelPath = `${ srcDir }${ BABEL_PATH }`; |
| 44 | + const destPath = `${ srcDir }website/src/public/babel.min.js`; |
| 45 | + await cp(babelPath, destPath); |
| 46 | + console.timeEnd('Copied Babel standalone'); |
| 47 | +} |
| 48 | + |
| 49 | +export async function copyBlogPosts(srcDir) { |
| 50 | + console.log('Copying blog posts...'); |
| 51 | + console.time('Copied blog posts'); |
| 52 | + const fromDir = `${ srcDir }docs/`; |
| 53 | + const toDir = `${ srcDir }docs/web/blog/`; |
| 54 | + const entries = await readdir(fromDir, { withFileTypes: true }); |
| 55 | + for (const entry of entries) { |
| 56 | + if (entry.isFile()) { |
| 57 | + const srcFile = path.join(fromDir, entry.name); |
| 58 | + const destFile = path.join(toDir, entry.name); |
| 59 | + await cp(srcFile, destFile); |
| 60 | + } |
| 61 | + } |
| 62 | + console.timeEnd('Copied blog posts'); |
| 63 | +} |
| 64 | + |
| 65 | +export async function copyCommonFiles(srcDir) { |
| 66 | + console.log('Copying common files...'); |
| 67 | + console.time('Copied common files'); |
| 68 | + const fromDir = `${ srcDir }`; |
| 69 | + const toDir = `${ srcDir }docs/web/`; |
| 70 | + await cp(`${ fromDir }CHANGELOG.md`, `${ toDir }changelog.md`); |
| 71 | + await cp(`${ fromDir }CONTRIBUTING.md`, `${ toDir }contributing.md`); |
| 72 | + await cp(`${ fromDir }SECURITY.md`, `${ toDir }security.md`); |
| 73 | + console.timeEnd('Copied common files'); |
| 74 | +} |
| 75 | + |
| 76 | +export async function buildAndCopyCoreJS(version, checkout, srcDir, destDir) { |
| 77 | + const target = version.branch ?? version.tag; |
| 78 | + const name = version.path ?? version.label; |
| 79 | + console.log(`Building and copying core-js for ${ target }`); |
| 80 | + const targetBundlePath = `${ destDir }/${ target }/`; |
| 81 | + |
| 82 | + if (await isExists(targetBundlePath)) { |
| 83 | + console.time('Core JS bundles copied'); |
| 84 | + const bundlePath = `${ targetBundlePath }core-js-bundle.js`; |
| 85 | + const destBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle.js`; |
| 86 | + const esmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`; |
| 87 | + const esmodulesDestBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`; |
| 88 | + await cp(bundlePath, destBundlePath); |
| 89 | + await cp(esmodulesBundlePath, esmodulesDestBundlePath); |
| 90 | + console.timeEnd('Core JS bundles copied'); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + console.time('Core JS bundles built'); |
| 95 | + if (checkout) { |
| 96 | + await checkoutVersion(version, srcDir); |
| 97 | + } |
| 98 | + await installDependencies(srcDir); |
| 99 | + await exec('npm run bundle-package', { cwd: srcDir }); |
| 100 | + const bundlePath = `${ srcDir }packages/core-js-bundle/minified.js`; |
| 101 | + const destPath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle.js`; |
| 102 | + const destBundlePath = `${ targetBundlePath }core-js-bundle.js`; |
| 103 | + await cp(bundlePath, destPath); |
| 104 | + await cp(bundlePath, destBundlePath); |
| 105 | + |
| 106 | + await exec('npm run bundle-package esmodules', { cwd: srcDir }); |
| 107 | + const esmodulesBundlePath = `${ srcDir }packages/core-js-bundle/minified.js`; |
| 108 | + const esmodulesDestBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`; |
| 109 | + const destEsmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`; |
| 110 | + await cp(esmodulesBundlePath, esmodulesDestBundlePath); |
| 111 | + await cp(esmodulesBundlePath, destEsmodulesBundlePath); |
| 112 | + console.timeEnd('Core JS bundles built'); |
| 113 | +} |
| 114 | + |
| 115 | +export async function checkoutVersion(version, execDir) { |
| 116 | + if (version.branch) { |
| 117 | + await exec(`git checkout origin/${ version.branch }`, { cwd: execDir }); |
| 118 | + } else { |
| 119 | + await exec(`git checkout ${ version.tag }`, { cwd: execDir }); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +export async function buildWeb(branch, execDir) { |
| 124 | + console.log('Building web...'); |
| 125 | + console.time('Built web'); |
| 126 | + let command = 'npm run build-web'; |
| 127 | + if (branch) command += ` branch=${ branch }`; |
| 128 | + const stdout = await exec(command, { cwd: execDir }); |
| 129 | + console.timeEnd('Built web'); |
| 130 | + return stdout; |
| 131 | +} |
| 132 | + |
| 133 | +export async function getCurrentBranch(execDir) { |
| 134 | + console.log('Getting current branch...'); |
| 135 | + console.time('Got current branch'); |
| 136 | + const { stdout } = await exec('git rev-parse --abbrev-ref HEAD', { cwd: execDir }); |
| 137 | + console.timeEnd('Got current branch'); |
| 138 | + return stdout.trim(); |
| 139 | +} |
| 140 | + |
| 141 | +export async function getDefaultVersion(versionFile, defaultVersion = null) { |
| 142 | + if (defaultVersion) return defaultVersion; |
| 143 | + |
| 144 | + const versions = await readJSON(versionFile); |
| 145 | + return versions.find(v => v.default)?.label; |
| 146 | +} |
| 147 | + |
| 148 | +export async function readJSON(filePath) { |
| 149 | + const buffer = await readFile(filePath); |
| 150 | + return JSON.parse(buffer); |
| 151 | +} |
0 commit comments