Skip to content

Commit 4c35804

Browse files
committed
Website builders refactoring
1 parent bd5ecf7 commit 4c35804

File tree

5 files changed

+114
-229
lines changed

5 files changed

+114
-229
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"prepare": "npm run zxi scripts/prepare.mjs",
2020
"build-compat": "npm run zxi scripts/build-compat/index.mjs",
2121
"build-web": "npm run zxi time website/index.mjs",
22-
"build-web-local": "npm run zxi time website/local.mjs",
22+
"build-web-local": "npm run zxi time website/build-local.mjs",
2323
"bundle": "run-s bundle-package bundle-tests",
2424
"bundle-package": "npm run zxi time scripts/bundle-package/bundle-package.mjs",
2525
"bundle-tests": "npm run zxi time cd scripts/bundle-tests/bundle-tests.mjs",

tests/eslint/package-lock.json

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/build-local.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
hasDocs, copyBlogPosts, copyBabelStandalone, copyCommonFiles, buildAndCopyCoreJS, buildWeb, getCurrentBranch,
3+
} from './helper.mjs';
4+
5+
const BUILD_SRC_DIR = './';
6+
const BUNDLES_DIR = 'bundles';
7+
8+
try {
9+
console.time('Finished in');
10+
const targetBranch = await getCurrentBranch(BUILD_SRC_DIR);
11+
12+
const version = { branch: targetBranch, label: targetBranch };
13+
await hasDocs(version, BUILD_SRC_DIR);
14+
await buildAndCopyCoreJS(version, false, BUILD_SRC_DIR, BUNDLES_DIR);
15+
16+
await copyBabelStandalone(BUILD_SRC_DIR);
17+
await copyBlogPosts(BUILD_SRC_DIR);
18+
await copyCommonFiles(BUILD_SRC_DIR);
19+
await buildWeb(targetBranch, BUILD_SRC_DIR);
20+
console.timeEnd('Finished in');
21+
} catch (error) {
22+
console.error(error);
23+
}
Lines changed: 79 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
/* eslint-disable no-console -- needed for logging */
21
import childProcess from 'node:child_process';
32
import { constants } from 'node:fs';
4-
import fs from 'node:fs/promises';
5-
import { promisify } from 'node:util';
63
import path from 'node:path';
4+
import { promisify } from 'node:util';
75

86
const exec = promisify(childProcess.exec);
9-
const { access, cp, readdir } = fs;
7+
const { cp, access, readdir } = fs;
108

11-
const BUNDLES_DIR = 'bundles';
129
const BABEL_PATH = 'website/node_modules/@babel/standalone/babel.min.js';
1310

14-
const BUILD_SRC_DIR = `./`;
15-
const BUILD_WEBSITE_SRC_DIR = `${ BUILD_SRC_DIR }/website`;
16-
17-
async function isExists(target) {
11+
export async function isExists(target) {
1812
try {
1913
await access(target, constants.F_OK);
2014
return true;
@@ -23,123 +17,121 @@ async function isExists(target) {
2317
}
2418
}
2519

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;
20+
export async function hasDocs(version, execDir) {
21+
const target = version.branch ? `origin/${ version.branch }` : version.tag;
22+
console.log(`Checking if docs exist in "${ target }"...`);
23+
try {
24+
await exec(`git ls-tree -r --name-only ${ target } | grep "docs/web/docs/"`, { cwd: execDir });
25+
} catch {
26+
throw new Error(`Docs not found in "${ target }".`);
27+
}
3328
}
3429

35-
async function installDependencies(dir = BUILD_SRC_DIR) {
30+
export async function installDependencies(execDir) {
3631
console.log('Installing dependencies...');
3732
console.time('Installed dependencies');
38-
await exec('npm ci', { cwd: dir });
33+
await exec('npm ci', { cwd: execDir });
3934
console.timeEnd('Installed dependencies');
4035
}
4136

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 }".`);
37+
export async function copyBabelStandalone(srcDir) {
38+
console.log('Copying Babel standalone...');
39+
await installDependencies(`${ srcDir }website`);
40+
console.time('Copied Babel standalone');
41+
const babelPath = `${ srcDir }${ BABEL_PATH }`;
42+
const destPath = `${ srcDir }website/src/public/babel.min.js`;
43+
await cp(babelPath, destPath);
44+
console.timeEnd('Copied Babel standalone');
45+
}
46+
47+
export async function copyBlogPosts(srcDir) {
48+
console.log('Copying blog posts...');
49+
console.time('Copied blog posts');
50+
const fromDir = `${ srcDir }docs/`;
51+
const toDir = `${ srcDir }docs/web/blog/`;
52+
const entries = await readdir(fromDir, { withFileTypes: true });
53+
for (const entry of entries) {
54+
if (entry.isFile()) {
55+
const srcFile = path.join(fromDir, entry.name);
56+
const destFile = path.join(toDir, entry.name);
57+
await cp(srcFile, destFile);
58+
}
4959
}
60+
console.timeEnd('Copied blog posts');
5061
}
5162

52-
async function buildAndCopyCoreJS(version) {
63+
export async function copyCommonFiles(srcDir) {
64+
console.log('Copying common files...');
65+
console.time('Copied common files');
66+
const fromDir = `${ srcDir }`;
67+
const toDir = `${ srcDir }docs/web/`;
68+
await cp(`${ fromDir }CHANGELOG.md`, `${ toDir }changelog.md`);
69+
await cp(`${ fromDir }CONTRIBUTING.md`, `${ toDir }contributing.md`);
70+
await cp(`${ fromDir }SECURITY.md`, `${ toDir }security.md`);
71+
console.timeEnd('Copied common files');
72+
}
73+
74+
export async function buildAndCopyCoreJS(version, checkout, srcDir, destDir) {
5375
const target = version.branch ?? version.tag;
5476
const name = version.path ?? version.label;
5577
console.log(`Building and copying core-js for ${ target }`);
56-
const targetBundlePath = `${ BUNDLES_DIR }/${ target }/`;
78+
const targetBundlePath = `${ destDir }/${ target }/`;
5779

5880
if (await isExists(targetBundlePath)) {
5981
console.time('Core JS bundles copied');
6082
const bundlePath = `${ targetBundlePath }core-js-bundle.js`;
61-
const destBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle.js`;
83+
const destBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle.js`;
6284
const esmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`;
63-
const esmodulesDestBundlePath = `${ BUILD_SRC_DIR }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
85+
const esmodulesDestBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
6486
await cp(bundlePath, destBundlePath);
6587
await cp(esmodulesBundlePath, esmodulesDestBundlePath);
6688
console.timeEnd('Core JS bundles copied');
6789
return;
6890
}
6991

7092
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`;
93+
if (checkout) {
94+
await checkoutVersion(version, srcDir);
95+
}
96+
await installDependencies(srcDir);
97+
await exec('npm run bundle-package', { cwd: srcDir });
98+
const bundlePath = `${ srcDir }packages/core-js-bundle/minified.js`;
99+
const destPath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle.js`;
75100
const destBundlePath = `${ targetBundlePath }core-js-bundle.js`;
76101
await cp(bundlePath, destPath);
77102
await cp(bundlePath, destBundlePath);
78103

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`;
104+
await exec('npm run bundle-package esmodules', { cwd: srcDir });
105+
const esmodulesBundlePath = `${ srcDir }packages/core-js-bundle/minified.js`;
106+
const esmodulesDestBundlePath = `${ srcDir }website/src/public/bundles/${ name }/core-js-bundle-esmodules.js`;
82107
const destEsmodulesBundlePath = `${ targetBundlePath }core-js-bundle-esmodules.js`;
83108
await cp(esmodulesBundlePath, esmodulesDestBundlePath);
84109
await cp(esmodulesBundlePath, destEsmodulesBundlePath);
85110
console.timeEnd('Core JS bundles built');
86111
}
87112

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-
}
113+
export async function checkoutVersion(version, execDir) {
114+
if (version.branch) {
115+
await exec(`git checkout origin/${ version.branch }`, { cwd: execDir });
116+
} else {
117+
await exec(`git checkout ${ version.tag }`, { cwd: execDir });
110118
}
111-
console.timeEnd('Copied blog posts');
112119
}
113120

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');
121+
export async function buildWeb(branch, execDir) {
122+
console.log('Building web...');
123+
console.time('Built web');
124+
let command = 'npm run build-web';
125+
if (branch) command += ` branch=${ branch }`;
126+
const stdout = await exec(command, { cwd: execDir });
127+
console.timeEnd('Built web');
128+
return stdout;
123129
}
124130

125-
async function getCurrentBranch() {
126-
const { stdout } = await exec('git rev-parse --abbrev-ref HEAD', { cwd: BUILD_SRC_DIR });
131+
export async function getCurrentBranch(execDir) {
132+
console.log('Getting current branch...');
133+
console.time('Got current branch');
134+
const { stdout } = await exec('git rev-parse --abbrev-ref HEAD', { cwd: execDir });
135+
console.timeEnd('Got current branch');
127136
return stdout.trim();
128137
}
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

Comments
 (0)