Skip to content
Permalink
Browse files
optimize page release
  • Loading branch information
pissang committed May 20, 2021
1 parent 4e4864a commit cf36f5271d3848994a008eaf3cc4eb5f451ce4a7
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 48 deletions.
@@ -1,6 +1,12 @@
extends basic

block content
nav(class='navbar navbar-default navbar-fixed-top doc-nav' id="ec-doc-nav")
if ecWWWLang == 'en'
include ../en/nav
else
include ../components/nav

.page-main
#page-#{pageConfig.pageName}.page-spa-container
include ../../_generated/spa/#{pageConfig.pageName}/body.html
div(id="page-#{pageConfig.pageName}" class="page-spa-container")
|!{pageConfig.bodyHtml}
@@ -1,4 +1,4 @@
extends ../layouts/tool-spa
extends ../../layouts/tool-spa

block extra_head

@@ -1,4 +1,4 @@
extends ../layouts/tool-spa
extends ../../layouts/tool-spa

block extra_head

@@ -183,7 +183,7 @@ async function buildJade(config) {
cwd: basePath
});

const spaPageConfigs = JSON.parse(fs.readFileSync(path.resolve(__dirname, + '../config/spa-pages.json'), 'utf-8'));
const spaPageConfigs = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../config/spa-pages.json'), 'utf-8'));

const hashes = {};
for (let lang of ['zh', 'en']) {
@@ -204,7 +204,14 @@ async function buildJade(config) {

const pageCfg = spaPageConfigs.find(pageCfg => srcPath.endsWith(pageCfg.entry));
if (pageCfg) {
cfg.pageConfig = pageCfg;
cfg.pageConfig = Object.assign({}, pageCfg, {
// Because jade doesn't support dynamic include. we have to read HTML and insert it in jade manually.
bodyHtml: fs.readFileSync(path.resolve(__dirname, `../_generated/spa/${pageCfg.pageName}/body.html`), 'utf-8')
});
}
else {
// Avoid error
cfg.pageConfig = {};
}

// This props can be read in jade tpl, like: `#{cdnPayRoot}`
@@ -279,7 +286,30 @@ async function buildJS(config) {
}

async function copyResource(config) {
const srcRelativePathList = await globby([

async function doCopy(pattern, cwd) {
const srcRelativePathList = await globby(pattern, {
cwd
});

for (let lang of LANGUAGES) {
for (let i = 0; i < srcRelativePathList.length; i++) {
let srcRelativePath = srcRelativePathList[i];
let srcAbsolutePath = path.resolve(cwd, srcRelativePath);
let destAbsolutePath = path.resolve(config.releaseDestDir, lang, srcRelativePath);


fse.ensureDirSync(path.dirname(destAbsolutePath));
fs.copyFileSync(srcAbsolutePath, destAbsolutePath);

replaceLog('(' + (i + 1) + '/' + srcRelativePathList.length + ') ' + chalk.green(`resource copied to: ${destAbsolutePath}`));
}
}
}

console.log();

await doCopy([
'vendors/**/*',
'images/**/*',
'asset/map/**/*',
@@ -288,24 +318,12 @@ async function copyResource(config) {
'builder/**/*',
'dist/**/*',
'video/**/*'
], {
cwd: projectDir
});

console.log();
], projectDir);

for (let lang of LANGUAGES) {
for (let i = 0; i < srcRelativePathList.length; i++) {
let srcRelativePath = srcRelativePathList[i];
let srcAbsolutePath = path.resolve(projectDir, srcRelativePath);
let destAbsolutePath = path.resolve(config.releaseDestDir, lang, srcRelativePath);

fse.ensureDirSync(path.dirname(destAbsolutePath));
fs.copyFileSync(srcAbsolutePath, destAbsolutePath);

replaceLog('(' + (i + 1) + '/' + srcRelativePathList.length + ') ' + chalk.green(`resource copied to: ${destAbsolutePath}`));
}
}
await doCopy([
'**/*',
'!**/index.html',
], path.resolve(__dirname, '../_generated/spa/'))

console.log('\ncopyResources done.');
}
@@ -20,46 +20,72 @@ function createPage({
pageChineseTitle,
}) {
const entry = path.join(pageName, 'index.jade');
spaPages.push({

const idx = spaPages.findIndex(page => page.pageName === pageName);

function doCreatePage() {
fse.copySync(
path.join(__dirname, "asset/template/page-redirect.jade"),
path.join(JADE_PATH, `${pageName}.jade`),
{ overwrite: true }
);

fse.copySync(
path.join(__dirname, "asset/template/page-zh.jade"),
path.join(JADE_ZH_PATH, entry),
{ overwrite: true }
);

fse.copySync(
path.join(__dirname, "asset/template/page-en.jade"),
path.join(JADE_EN_PATH, entry),
{ overwrite: true }
);

fs.writeFileSync(SPA_PAGE_CONFIG_PATH, JSON.stringify(spaPages, null, 2), 'utf-8');

console.log('Page created successfully!');
console.log('You can change the page title in the config/spa-pages.json');
}

const pageCfg = {
projectName,
pageName,
pageTitle,
pageChineseTitle,
entry
});

fse.copySync(
path.join(__dirname, "asset/template/page-redirect.jade"),
path.join(JADE_PATH, `${pageName}.jade`)
);
};
if (idx >= 0) {
rl.question(`Page exists. Do you wan\'t to replace it? (Yes or No): `, function (response) {
if (response.toLowerCase() === 'yes' || response.toLowerCase() === 'y') {
spaPages[idx] = pageCfg;
doCreatePage();
}
else {
return;
}

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)
);
rl.close();
});
}
else {
spaPages.push(pageCfg);
doCreatePage();

fs.writeFileSync(SPA_PAGE_CONFIG_PATH, JSON.stringify(spaPages, null, 2), 'utf-8');
rl.close();
}
}

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');
});
});
});
@@ -73,6 +73,7 @@ cd ${currWorkingDir}
echo "Build examples done."

# Build SPA pages.
cd ${thisScriptDir}
node releasePages.js

# Build www
@@ -1,19 +1,21 @@
const fs = require('fs');
const shell = require('shelljs');
const path = require('path');
const fse = require('fs-extra');

const spaPageConfigs = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../config/spa-pages.json'), 'utf-8'));

async function run() {
for (const pageCfg of spaPageConfigs) {
console.log(`Building ${pageCfg.projectName}....`);
const projectPath = path.resolve(__dirname, `../../${pageCfg.projectName}`);
shell.cd(projectPath);
shell.exec('npm run release');
shell.cd(__dirname);
shell.cp(
'-R',

fse.copySync(
`${projectPath}/dist`,
path.resolve(__dirname, '_generated/spa', pageCfg.pageName)
path.resolve(__dirname, '../_generated/spa', pageCfg.pageName)
);
}
}

0 comments on commit cf36f52

Please sign in to comment.