Skip to content

Commit

Permalink
Refactor initSite method in Site/index.js (#2267)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhw-1 authored Apr 10, 2023
1 parent fc027af commit 0012f63
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 23 deletions.
7 changes: 4 additions & 3 deletions packages/cli/src/cmd/init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs-extra');
const path = require('path');

const { Site } = require('@markbind/core');
const { Site, Template } = require('@markbind/core');

const logger = require('../util/logger');

Expand All @@ -15,7 +15,8 @@ function init(root, options) {
}
}

Site.initSite(rootFolder, options.template)
const template = new Template(rootFolder, options.template);
template.init()
.then(() => {
logger.info('Initialization success.');
})
Expand All @@ -34,7 +35,7 @@ function init(root, options) {
}
})
.catch((error) => {
logger.error(error.message);
logger.error(`Failed to initialize site with given template with error: ${error.message}`);
process.exitCode = 1;
});
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const Site = require('./src/Site');
const { Template } = require('./src/Site/template');

module.exports = {
Site,
Template,
};
16 changes: 0 additions & 16 deletions packages/core/src/Site/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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');

const { sequentialAsyncForEach } = require('../utils/async');
const { delay } = require('../utils/delay');
Expand Down Expand Up @@ -170,21 +169,6 @@ class Site {
}
}

/**
* Static method for initializing a markbind site.
* Generate the site.json and an index.md file.
*
* @param rootPath
* @param templatePath
*/
static async initSite(rootPath, templatePath) {
try {
return await new Template(rootPath, templatePath).initTemplate();
} catch (err) {
return new Error(`Failed to initialize site with given template with error: ${err.message}`);
}
}

beforeSiteGenerate() {
this.variableProcessor.invalidateCache();
this.externalManager.reset();
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/Site/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ export class Template {
});
}

initTemplate() {
/**
* A method for initializing a markbind site according to the given template.
* Generate the site.json and an index.md file.
*/
async init() {
if (!this.validateTemplateFromPath()) {
throw new Error('Template validation failed. Required files does not exist');
throw new Error('Template validation failed. Required files does not exist.');
}

return new Promise((resolve, reject) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/test/unit/Site.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require('path');
const fs = require('fs-extra');
const ghpages = require('gh-pages');
const Site = require('../../src/Site');
const { Template } = require('../../src/Site/template');

const {
INDEX_MD_DEFAULT,
Expand Down Expand Up @@ -37,10 +38,11 @@ test('Site Init with invalid template fails', async () => {

fs.vol.fromJSON(json, '');

Site.initSite('', DEFAULT_TEMPLATE)
const template = new Template('', DEFAULT_TEMPLATE);
await template.init('', DEFAULT_TEMPLATE)
.catch((err) => {
expect(err).toEqual(
new Error('Template validation failed. Required files does not exist'));
new Error('Template validation failed. Required files does not exist.'));
});
});

Expand Down

0 comments on commit 0012f63

Please sign in to comment.