Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/Site/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { ExternalManager } = require('../External/ExternalManager');
const { LayoutManager, LAYOUT_DEFAULT_NAME, LAYOUT_FOLDER_PATH } = require('../Layout');
const { SiteLinkManager } = require('../html/SiteLinkManager');
const { PluginManager } = require('../plugins/PluginManager');
const Template = require('../../template/template');
const { Template } = require('./template');

const { sequentialAsyncForEach } = require('../utils/async');
const { delay } = require('../utils/delay');
Expand Down Expand Up @@ -179,7 +179,7 @@ class Site {
*/
static async initSite(rootPath, templatePath) {
try {
return await new Template(rootPath, templatePath).init();
return await new Template(rootPath, templatePath).initTemplate();
} catch (err) {
return new Error(`Failed to initialize site with given template with error: ${err.message}`);
}
Expand Down
54 changes: 54 additions & 0 deletions packages/core/src/Site/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const path = require('path');
const fs = require('fs-extra');

const fsUtil = require('../utils/fsUtil');

const requiredFiles = ['index.md', 'site.json', '_markbind/'];

const PATH_TO_TEMPLATE = '../../template';

class Template {
constructor(rootPath, templatePath) {
this.root = rootPath;
this.template = path.join(__dirname, PATH_TO_TEMPLATE, templatePath);
}

validateTemplateFromPath() {
for (let i = 0; i < requiredFiles.length; i += 1) {
const requiredFile = requiredFiles[i];
const requiredFilePath = path.join(this.template, requiredFile);

if (!fs.existsSync(requiredFilePath)) {
return false;
}
}

return true;
}

generateSiteWithTemplate() {
return new Promise((resolve, reject) => {
fs.access(this.root)
.catch(() => fs.mkdirSync(this.root))
.then(() => fsUtil.copySyncWithOptions(this.template, this.root, { overwrite: false }))
.then(resolve)
.catch(reject);
});
}

initTemplate() {
if (!this.validateTemplateFromPath()) {
throw new Error('Template validation failed. Required files does not exist');
}

return new Promise((resolve, reject) => {
this.generateSiteWithTemplate()
.then(resolve)
.catch(reject);
});
}
}

module.exports = {
Template,
};
50 changes: 0 additions & 50 deletions packages/core/template/template.js

This file was deleted.