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