Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor initSite method in Site/index.js #2267

Merged
merged 7 commits into from
Apr 10, 2023
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
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