1- /* eslint-disable no-console -- needed for logging */
21import childProcess from 'node:child_process' ;
32import { constants } from 'node:fs' ;
4- import fs from 'node:fs/promises' ;
5- import { promisify } from 'node:util' ;
63import path from 'node:path' ;
4+ import { promisify } from 'node:util' ;
75
86const exec = promisify ( childProcess . exec ) ;
9- const { access , cp , readdir } = fs ;
7+ const { cp , access , readdir } = fs ;
108
11- const BUNDLES_DIR = 'bundles' ;
129const BABEL_PATH = 'website/node_modules/@babel/standalone/babel.min.js' ;
13-
1410const BUILD_SRC_DIR = `./` ;
1511const BUILD_WEBSITE_SRC_DIR = `${ BUILD_SRC_DIR } /website` ;
1612
17- async function isExists ( target ) {
13+ export async function isExists ( target ) {
1814 try {
1915 await access ( target , constants . F_OK ) ;
2016 return true ;
@@ -23,123 +19,121 @@ async function isExists(target) {
2319 }
2420}
2521
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 ;
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+ }
3330}
3431
35- async function installDependencies ( dir = BUILD_SRC_DIR ) {
32+ export async function installDependencies ( execDir ) {
3633 console . log ( 'Installing dependencies...' ) ;
3734 console . time ( 'Installed dependencies' ) ;
38- await exec ( 'npm ci' , { cwd : dir } ) ;
35+ await exec ( 'npm ci' , { cwd : execDir } ) ;
3936 console . timeEnd ( 'Installed dependencies' ) ;
4037}
4138
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 } ".` ) ;
39+ export async function copyBabelStandalone ( srcDir ) {
40+ console . log ( 'Copying Babel standalone...' ) ;
41+ await installDependencies ( BUILD_WEBSITE_SRC_DIR ) ;
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+ }
4961 }
62+ console . timeEnd ( 'Copied blog posts' ) ;
5063}
5164
52- async function buildAndCopyCoreJS ( version ) {
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 ) {
5377 const target = version . branch ?? version . tag ;
5478 const name = version . path ?? version . label ;
5579 console . log ( `Building and copying core-js for ${ target } ` ) ;
56- const targetBundlePath = `${ BUNDLES_DIR } /${ target } /` ;
80+ const targetBundlePath = `${ destDir } /${ target } /` ;
5781
5882 if ( await isExists ( targetBundlePath ) ) {
5983 console . time ( 'Core JS bundles copied' ) ;
6084 const bundlePath = `${ targetBundlePath } core-js-bundle.js` ;
61- const destBundlePath = `${ BUILD_SRC_DIR } website/src/public/bundles/${ name } /core-js-bundle.js` ;
85+ const destBundlePath = `${ srcDir } website/src/public/bundles/${ name } /core-js-bundle.js` ;
6286 const esmodulesBundlePath = `${ targetBundlePath } core-js-bundle-esmodules.js` ;
63- const esmodulesDestBundlePath = `${ BUILD_SRC_DIR } website/src/public/bundles/${ name } /core-js-bundle-esmodules.js` ;
87+ const esmodulesDestBundlePath = `${ srcDir } website/src/public/bundles/${ name } /core-js-bundle-esmodules.js` ;
6488 await cp ( bundlePath , destBundlePath ) ;
6589 await cp ( esmodulesBundlePath , esmodulesDestBundlePath ) ;
6690 console . timeEnd ( 'Core JS bundles copied' ) ;
6791 return ;
6892 }
6993
7094 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` ;
95+ if ( checkout ) {
96+ await checkoutVersion ( version ) ;
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` ;
75102 const destBundlePath = `${ targetBundlePath } core-js-bundle.js` ;
76103 await cp ( bundlePath , destPath ) ;
77104 await cp ( bundlePath , destBundlePath ) ;
78105
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` ;
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` ;
82109 const destEsmodulesBundlePath = `${ targetBundlePath } core-js-bundle-esmodules.js` ;
83110 await cp ( esmodulesBundlePath , esmodulesDestBundlePath ) ;
84111 await cp ( esmodulesBundlePath , destEsmodulesBundlePath ) ;
85112 console . timeEnd ( 'Core JS bundles built' ) ;
86113}
87114
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- }
115+ export async function checkoutVersion ( version ) {
116+ if ( version . branch ) {
117+ await exec ( `git checkout origin/${ version . branch } ` , { cwd : BUILD_SRC_DIR } ) ;
118+ } else {
119+ await exec ( `git checkout ${ version . tag } ` , { cwd : BUILD_SRC_DIR } ) ;
110120 }
111- console . timeEnd ( 'Copied blog posts' ) ;
112121}
113122
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+ 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 ;
123131}
124132
125- async function getCurrentBranch ( ) {
133+ export async function getCurrentBranch ( ) {
134+ console . log ( 'Getting current branch...' ) ;
135+ console . time ( 'Got current branch' ) ;
126136 const { stdout } = await exec ( 'git rev-parse --abbrev-ref HEAD' , { cwd : BUILD_SRC_DIR } ) ;
137+ console . timeEnd ( 'Got current branch' ) ;
127138 return stdout . trim ( ) ;
128139}
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