You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by sh...@apache.org on 2021/05/20 08:24:55 UTC

[echarts-www] branch master updated: add scripts for creating spa page

This is an automated email from the ASF dual-hosted git repository.

shenyi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/echarts-www.git


The following commit(s) were added to refs/heads/master by this push:
     new 4e4864a  add scripts for creating spa page
4e4864a is described below

commit 4e4864a976125e5cae9ba0fb24175f9630c7fb4a
Author: pissang <bm...@gmail.com>
AuthorDate: Thu May 20 15:25:13 2021 +0800

    add scripts for creating spa page
---
 .gitignore                            |  3 +-
 README.md                             | 12 ++++-
 _jade/layouts/redirect.jade           | 14 +++---
 _jade/layouts/tool-spa.jade           |  6 +++
 _scss/_pages.spa.scss                 |  8 ++++
 _scss/main.scss                       |  2 +
 bin/asset/template/page-en.jade       |  8 ++++
 bin/asset/template/page-redirect.jade |  4 ++
 bin/asset/template/page-zh.jade       |  8 ++++
 bin/build.js                          | 20 +++++++--
 bin/createPage.js                     | 67 ++++++++++++++++++++++++++++
 bin/release.sh                        |  3 ++
 bin/releasePages.js                   | 21 +++++++++
 bin/uploadBOS.js                      | 84 -----------------------------------
 config/spa-pages.json                 |  1 +
 package.json                          |  4 +-
 16 files changed, 168 insertions(+), 97 deletions(-)

diff --git a/.gitignore b/.gitignore
index 6495165..0ec91ca 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,4 +7,5 @@ package-lock.json
 _generated
 .DS_Store
 
-doc-dist
\ No newline at end of file
+doc-dist
+tmp
\ No newline at end of file
diff --git a/README.md b/README.md
index 295e705..48dfa6a 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ This project is part of the source of [The Apache ECharts Official Website](http
 
 ## dev
 
-### Build localsite
+### Build website for local preview
 
 ```sh
 npm run dev
@@ -44,3 +44,13 @@ npm run watch:jade
 ```sh
 npm run release
 ```
+
+### Add SPA page
+
+Add SPA page created by [echarts-www-spa-boilerplate](https://github.com/pissang/echarts-www-spa-boilerplate)
+
+```sh
+npm run create:page
+```
+
+Then add this page entry in the menu.
\ No newline at end of file
diff --git a/_jade/layouts/redirect.jade b/_jade/layouts/redirect.jade
index fb92f07..b48a5bc 100644
--- a/_jade/layouts/redirect.jade
+++ b/_jade/layouts/redirect.jade
@@ -7,9 +7,11 @@ html
         meta(http-equiv='X-UA-Compatible', content='IE=edge')
 
         script(type='text/javascript').
-            var hash = (location.hash || '').replace('#', '');
-            var search = (location.search || '').replace('?', '');
-            var lang = navigator.language || navigator.userLanguage;
-            lang = lang.indexOf('zh') > -1 ? 'zh' : 'en';
-            location.href = '#{host}/' + lang + '/#{redirectTo}'
-                + (search ? '?' + search : '') + (hash ? '#' + hash : '');
+            (function () {
+                var hash = (location.hash || '').replace('#', '');
+                var search = (location.search || '').replace('?', '');
+                var lang = navigator.language || navigator.userLanguage;
+                lang = lang.indexOf('zh') > -1 ? 'zh' : 'en';
+                location.href = '#{host}/' + lang + '/#{redirectTo}'
+                    + (search ? '?' + search : '') + (hash ? '#' + hash : '');
+            })();
diff --git a/_jade/layouts/tool-spa.jade b/_jade/layouts/tool-spa.jade
new file mode 100644
index 0000000..712b037
--- /dev/null
+++ b/_jade/layouts/tool-spa.jade
@@ -0,0 +1,6 @@
+extends basic
+
+block content
+    .page-main
+        #page-#{pageConfig.pageName}.page-spa-container
+            include ../../_generated/spa/#{pageConfig.pageName}/body.html
\ No newline at end of file
diff --git a/_scss/_pages.spa.scss b/_scss/_pages.spa.scss
new file mode 100644
index 0000000..1b78d89
--- /dev/null
+++ b/_scss/_pages.spa.scss
@@ -0,0 +1,8 @@
+
+.page-spa-container {
+    position: absolute;
+    top: $nav-height;
+    bottom: 0;
+    left: 0;
+    right: 0;
+}
\ No newline at end of file
diff --git a/_scss/main.scss b/_scss/main.scss
index 36a8310..a2e4548 100644
--- a/_scss/main.scss
+++ b/_scss/main.scss
@@ -113,6 +113,8 @@ body {
 @import 'pages.maillist';
 @import 'pages.cheatsheet';
 @import 'pages.events';
+@import 'pages.spa';
+
 
 #apache-banner {
     position: fixed;
diff --git a/bin/asset/template/page-en.jade b/bin/asset/template/page-en.jade
new file mode 100644
index 0000000..6a27759
--- /dev/null
+++ b/bin/asset/template/page-en.jade
@@ -0,0 +1,8 @@
+extends ../layouts/tool-spa
+
+block extra_head
+
+    title #{pageConfig.pageChineseTitle} - Apache ECharts
+
+    script(type="text/javascript").
+        window.ECHARTS_WEBSITE_LANGUAGE = 'zh';
\ No newline at end of file
diff --git a/bin/asset/template/page-redirect.jade b/bin/asset/template/page-redirect.jade
new file mode 100644
index 0000000..6cbc790
--- /dev/null
+++ b/bin/asset/template/page-redirect.jade
@@ -0,0 +1,4 @@
+extends layouts/redirect
+
+block variables
+    - var redirectTo = '#{pageConfig.pageName}'
\ No newline at end of file
diff --git a/bin/asset/template/page-zh.jade b/bin/asset/template/page-zh.jade
new file mode 100644
index 0000000..6a27759
--- /dev/null
+++ b/bin/asset/template/page-zh.jade
@@ -0,0 +1,8 @@
+extends ../layouts/tool-spa
+
+block extra_head
+
+    title #{pageConfig.pageChineseTitle} - Apache ECharts
+
+    script(type="text/javascript").
+        window.ECHARTS_WEBSITE_LANGUAGE = 'zh';
\ No newline at end of file
diff --git a/bin/build.js b/bin/build.js
index fc58358..1969475 100644
--- a/bin/build.js
+++ b/bin/build.js
@@ -183,6 +183,8 @@ async function buildJade(config) {
         cwd: basePath
     });
 
+    const spaPageConfigs = JSON.parse(fs.readFileSync(path.resolve(__dirname, + '../config/spa-pages.json'), 'utf-8'));
+
     const hashes = {};
     for (let lang of ['zh', 'en']) {
         hashes[lang] = {
@@ -190,9 +192,7 @@ async function buildJade(config) {
         };
     }
 
-    for (let srcPath of srcPaths) {
-        let filePath = path.resolve(basePath, srcPath);
-
+    function prepareConfig(srcPath) {
         const lang = (srcPath.indexOf('zh/') === 0
             || srcPath.indexOf('examples/zh/') === 0) ? 'zh' : 'en';
 
@@ -202,7 +202,10 @@ async function buildJade(config) {
         cfg.cdnPayRoot = config.cdnPayRootMap[lang];
         cfg.cdnFreeRoot = config.cdnFreeRootMap[lang];
 
-        let destPath = path.resolve(cfg.releaseDestDir, srcPath.replace('.jade', '.html'));
+        const pageCfg = spaPageConfigs.find(pageCfg => srcPath.endsWith(pageCfg.entry));
+        if (pageCfg) {
+            cfg.pageConfig = pageCfg;
+        }
 
         // This props can be read in jade tpl, like: `#{cdnPayRoot}`
         assert(
@@ -216,6 +219,15 @@ async function buildJade(config) {
             && cfg.cdnPayVersion
         );
 
+        return cfg;
+    }
+
+    for (let srcPath of srcPaths) {
+        const cfg = prepareConfig(srcPath);
+        const filePath = path.resolve(basePath, srcPath);
+
+        const destPath = path.resolve(cfg.releaseDestDir, srcPath.replace('.jade', '.html'));
+
         process.stdout.write(`generating: ${destPath} ...`);
 
         try {
diff --git a/bin/createPage.js b/bin/createPage.js
new file mode 100644
index 0000000..51c6efb
--- /dev/null
+++ b/bin/createPage.js
@@ -0,0 +1,67 @@
+const fs = require('fs');
+const path = require('path');
+const readline = require("readline");
+const fse = require('fs-extra');
+const rl = readline.createInterface({
+    input: process.stdin,
+    output: process.stdout
+});
+
+const SPA_PAGE_CONFIG_PATH = path.join(__dirname, '../config/spa-pages.json');
+const JADE_PATH = path.join(__dirname, '../_jade');
+const JADE_ZH_PATH = path.join(JADE_PATH, 'zh');
+const JADE_EN_PATH = path.join(JADE_PATH, 'en');
+const spaPages = JSON.parse(fs.readFileSync(SPA_PAGE_CONFIG_PATH, 'utf-8'));
+
+function createPage({
+    projectName,
+    pageName,
+    pageTitle,
+    pageChineseTitle,
+}) {
+    const entry = path.join(pageName, 'index.jade');
+    spaPages.push({
+        projectName,
+        pageName,
+        pageTitle,
+        pageChineseTitle,
+        entry
+    });
+
+    fse.copySync(
+        path.join(__dirname, "asset/template/page-redirect.jade"),
+        path.join(JADE_PATH, `${pageName}.jade`)
+    );
+
+    fse.copySync(
+        path.join(__dirname, "asset/template/page-zh.jade"),
+        path.join(JADE_ZH_PATH, entry)
+    );
+
+    fse.copySync(
+        path.join(__dirname, "asset/template/page-en.jade"),
+        path.join(JADE_EN_PATH, entry)
+    );
+
+    fs.writeFileSync(SPA_PAGE_CONFIG_PATH, JSON.stringify(spaPages, null, 2), 'utf-8');
+}
+
+rl.question('Project Name? (It will use it locate the project): ', function (projectName) {
+    rl.question('Page Name? (It will be used to create page file): ', function (pageName) {
+        rl.question('Page Title? (It will be displayed on title): ', function (pageTitle) {
+            rl.question('Page Chinese Title? (It will be displayed on title): ', function (pageChineseTitle) {
+                rl.close();
+
+                createPage({
+                    projectName,
+                    pageName,
+                    pageTitle,
+                    pageChineseTitle
+                });
+                console.log('Page created successfully!');
+                console.log('You can change the page title in the config/spa-pages.json');
+            });
+        });
+    });
+});
+
diff --git a/bin/release.sh b/bin/release.sh
index 64193d6..e102fb4 100644
--- a/bin/release.sh
+++ b/bin/release.sh
@@ -72,6 +72,9 @@ npm run release
 cd ${currWorkingDir}
 echo "Build examples done."
 
+# Build SPA pages.
+node releasePages.js
+
 # Build www
 echo "Build www ..."
 cd ${wwwProjectDir}
diff --git a/bin/releasePages.js b/bin/releasePages.js
new file mode 100644
index 0000000..5b22ba5
--- /dev/null
+++ b/bin/releasePages.js
@@ -0,0 +1,21 @@
+const fs = require('fs');
+const shell = require('shelljs');
+const path = require('path');
+
+const spaPageConfigs = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../config/spa-pages.json'), 'utf-8'));
+
+async function run() {
+    for (const pageCfg of spaPageConfigs) {
+        const projectPath = path.resolve(__dirname, `../../${pageCfg.projectName}`);
+        shell.cd(projectPath);
+        shell.exec('npm run release');
+        shell.cd(__dirname);
+        shell.cp(
+            '-R',
+            `${projectPath}/dist`,
+            path.resolve(__dirname, '_generated/spa', pageCfg.pageName)
+        );
+    }
+}
+
+run();
\ No newline at end of file
diff --git a/bin/uploadBOS.js b/bin/uploadBOS.js
deleted file mode 100644
index 302ea55..0000000
--- a/bin/uploadBOS.js
+++ /dev/null
@@ -1,84 +0,0 @@
-const globby = require('globby');
-const chalk = require('chalk');
-const path = require('path');
-// const fs = require('fs');
-const assert = require('assert');
-const bosSDK = require('@baiducloud/sdk');
-const argv = require('yargs').argv;
-
-const projectDir = path.resolve(__dirname, '..');
-const artifactDir = path.resolve(projectDir, '../echarts-website');
-const BosClient = bosSDK.BosClient;
-
-// -------------------------------------------------------------------------
-// Usage:
-// node uploadBOS.js --doc --ak xxx --sk yyy  # upload all. Would be slow.
-// node uploadBOS.js --doc                    # upload only doc.
-// -------------------------------------------------------------------------
-
-
-assert(argv.ak && argv.sk, 'as sk MUST be provided.')
-
-const config = {
-    endpoint: 'https://bj.bcebos.com',
-    credentials: {
-        ak: argv.ak,
-        sk: argv.sk
-    }
-};
-
-const client = new BosClient(config);
-const BUCKET_NAME = 'echarts-www';
-
-async function upload(onlyDoc) {
-    const filePathList = await globby([
-        '**/*'
-    ], {
-        cwd: artifactDir
-    });
-
-    for (relativePath of filePathList) {
-
-        if (onlyDoc
-            && relativePath
-        ) {
-            if (relativePath.indexOf('zh/documents') !== 0) {
-                continue;
-            }
-            console.log('Only upload doc ...');
-        }
-
-        if (
-            /\.html$/.test(relativePath)
-            || relativePath.indexOf('en/') === 0
-            || relativePath.includes('vendors/echarts')
-            || relativePath.includes('vendors/ace')
-            || relativePath.includes('vendors/bootstrap')
-            || relativePath.includes('vendors/d3')
-            || relativePath.includes('vendors/jquery')
-            || relativePath.includes('vendors/dat.gui')
-            || relativePath.includes('vendors/jquery.lazyload')
-            || relativePath.includes('vendors/loadash')
-            || relativePath.includes('vendors/waypoint')
-            || relativePath.indexOf('zh/builder') === 0
-            || relativePath.indexOf('zh/dist') === 0
-        ) {
-            continue;
-        }
-
-        const absolutePath = path.resolve(artifactDir, relativePath);
-        const opt = {
-            // Try to force use browser cache to save money.
-            // If source changed, change the version param in URL to invalid the cache.
-            'Cache-Control': 'max-age=31536000'
-            // FIXME Should "public"?
-            // 'Cache-Control': 'public, max-age=31536000'
-        };
-
-        await client.putObjectFromFile(BUCKET_NAME, relativePath, absolutePath, opt)
-
-        console.log(chalk.green(`uploaded: ${absolutePath}`));
-    }
-}
-
-upload(!!argv.doc);
diff --git a/config/spa-pages.json b/config/spa-pages.json
new file mode 100644
index 0000000..0637a08
--- /dev/null
+++ b/config/spa-pages.json
@@ -0,0 +1 @@
+[]
\ No newline at end of file
diff --git a/package.json b/package.json
index 1fb6bc0..8f70cdd 100644
--- a/package.json
+++ b/package.json
@@ -12,6 +12,7 @@
     "jade": "node bin/build.js --env=localsite --filter=jade",
     "sass": "node bin/build.js --env=localsite --filter=sass",
     "watch": "npm run watch:jade & npm run watch:sass",
+    "create:page": "node bin/createPage.js",
     "watch:jade": "onchange '_jade/**/*' -- node bin/build.js --env=localsite --filter=jade --jade={{changed}}",
     "watch:sass": "onchange '_scss/**/*' -- npm run sass"
   },
@@ -20,7 +21,7 @@
     "@baiducloud/sdk": "^1.0.0-rc.17",
     "autoprefixer": "^9.7.3",
     "chalk": "^3.0.0",
-    "fs-extra": "^8.1.0",
+    "fs-extra": "^10.0.0",
     "globby": "^10.0.1",
     "jade": "^1.11.0",
     "less": "^3.10.3",
@@ -29,6 +30,7 @@
     "onchange": "^6.1.0",
     "postcss": "^7.0.25",
     "requirejs": "^2.3.6",
+    "shelljs": "^0.8.4",
     "uglify-js": "^3.7.3",
     "yargs": "^13.2.4"
   }

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org