sbg-api
Version:
Pre-processing all source posts before rendering from hexo. Useful for low-end devices to prevent using large number of hexo plugins.
202 lines • 7.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SBG = void 0;
var tslib_1 = require("tslib");
var bluebird_1 = tslib_1.__importDefault(require("bluebird"));
var hexo_1 = tslib_1.__importDefault(require("hexo"));
var hexo_post_parser_1 = tslib_1.__importDefault(require("hexo-post-parser"));
var sbg_utility_1 = require("sbg-utility");
var upath_1 = require("upath");
var cleaner = tslib_1.__importStar(require("./clean"));
var copy_1 = require("./deploy/copy");
var gulp_safelink_1 = require("./gulp.safelink");
var gulp_seo_1 = require("./gulp.seo");
var pcopy = tslib_1.__importStar(require("./post/copy"));
var standalone_1 = tslib_1.__importDefault(require("./post/standalone"));
var SBG = /** @class */ (function () {
/**
* Static blog generator
* @param cwd base folder
*/
function SBG(cwd, options) {
this.setConfig = sbg_utility_1.setConfig;
this.getConfig = sbg_utility_1.getConfig;
/**
* run files ends with `standalone.js` inside source posts {@link standaloneRunner}
* @returns
*/
this.standalone = function () { return (0, sbg_utility_1.chain)([{ callback: standalone_1.default }]); };
this.deploy = new (/** @class */ (function () {
function class_1(superThis) {
this.superThis = superThis;
this.copy = copy_1.deployCopy;
//
}
return class_1;
}()))(this);
if (!cwd)
cwd = process.cwd();
// fetch config
(0, sbg_utility_1.fetchConfig)(cwd);
// apply config
this.config = (0, sbg_utility_1.getConfig)();
// modify config
this.cwd = cwd;
this.config.cwd = cwd;
options = Object.assign(this.config, options || {}, { cwd: cwd });
(0, sbg_utility_1.debug)('sbg-api')('cwd', cwd);
// re-apply config
this.config = (0, sbg_utility_1.setConfig)(options);
// apply config hexo-post-parser
hexo_post_parser_1.default.setConfig(this.config);
SBG.setApi(this);
new sbg_utility_1.scheduler();
}
SBG.setApi = function (api) {
this.currentApI = api;
};
SBG.getApi = function () {
return this.currentApI;
};
/**
* get index packages
* @returns
*/
SBG.prototype.core = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
// apply current config
(0, sbg_utility_1.setConfig)(this.config);
return [4 /*yield*/, Promise.resolve().then(function () { return tslib_1.__importStar(require('./index')); })];
case 1:
// recall index
return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* Auto seo on public dir (_config_yml.public_dir) (run after generated)
* @param customPath run seo fixer on spesific folder
* @returns
*/
SBG.prototype.seo = function (customPath) {
var _this = this;
return new bluebird_1.default(function (resolve) {
(0, gulp_seo_1.taskSeo)(null, customPath || (0, upath_1.join)(_this.cwd, _this.config.public_dir)).once('end', function () {
setTimeout(function () {
resolve();
}, 3000);
});
});
};
/**
* Copy all **src-post** to **source/_posts** (run before generate)
* * see the method {@link pcopy.copyAllPosts}
* @returns
*/
// copy = () => chain([{ callback: () => pcopy.copyAllPosts(undefined, this.config) }]);
SBG.prototype.copy = function () {
var config = this.config;
return new Promise(function (resolve) {
var streamer = pcopy.copyAllPosts(undefined, config);
streamer.on('end', function () {
// wait all handler to be closed
setTimeout(function () { return resolve(null); }, 7000);
});
});
};
/**
* Anonymize external links on public dir (_config_yml.public_dir) (run after generated)
* @param customPath run anonymizer external links on spesific folder
* @returns
*/
SBG.prototype.safelink = function (customPath) {
var _this = this;
return new bluebird_1.default(function (resolve) {
(0, gulp_safelink_1.taskSafelink)(null, customPath || (0, upath_1.join)(_this.cwd, _this.config.public_dir)).once('end', function () {
setTimeout(function () {
resolve();
}, 3000);
});
});
};
/**
* generate site with hexo
*/
SBG.prototype.generate = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var hexo;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
hexo = new hexo_1.default(this.cwd);
// hexo init
return [4 /*yield*/, hexo.init().catch(sbg_utility_1.noop)];
case 1:
// hexo init
_a.sent();
return [4 /*yield*/, hexo.load().catch(sbg_utility_1.noop)];
case 2:
_a.sent();
// hexo generate
return [4 /*yield*/, hexo.call('generate').catch(sbg_utility_1.noop)];
case 3:
// hexo generate
_a.sent();
return [4 /*yield*/, hexo.exit()];
case 4:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* clean cache, auto generated posts, etc
* @see {@link cleaner.cleanDb}
* @see {@link cleaner.cleanArchive}
* @see {@link cleaner.cleanGeneratedPosts}
*/
SBG.prototype.clean = function (opt) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(opt === 'all')) return [3 /*break*/, 3];
return [4 /*yield*/, cleaner.cleanDb().catch(console.log)];
case 1:
_a.sent();
return [4 /*yield*/, cleaner.cleanArchive().catch(console.log)];
case 2:
_a.sent();
return [3 /*break*/, 9];
case 3:
if (!(opt === 'archive')) return [3 /*break*/, 5];
return [4 /*yield*/, cleaner.cleanArchive().catch(console.log)];
case 4:
_a.sent();
return [3 /*break*/, 9];
case 5:
if (!(opt === 'post')) return [3 /*break*/, 7];
(0, sbg_utility_1.debug)('clean')('generated posts');
return [4 /*yield*/, cleaner.cleanGeneratedPosts().catch(console.log)];
case 6:
_a.sent();
return [3 /*break*/, 9];
case 7: return [4 /*yield*/, cleaner.cleanDb().catch(console.log)];
case 8:
_a.sent();
_a.label = 9;
case 9: return [2 /*return*/];
}
});
});
};
return SBG;
}());
exports.SBG = SBG;
exports.default = SBG;
//# sourceMappingURL=api.js.map