Skip to content

Commit

Permalink
convert db.json to db.dat
Browse files Browse the repository at this point in the history
  • Loading branch information
dailyrandomphoto committed Oct 17, 2019
1 parent 1d18099 commit 6763135
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2,364 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ typings/

# next.js build output
.next
package-lock.json
35 changes: 28 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict';

const chalk = require('chalk'); // eslint-disable-line node/no-extraneous-require
const fs = require('fs');
const chalk = require('chalk'); // eslint-disable-line node/no-missing-require,node/no-extraneous-require
const {
save,
load,
toJSON
toJSON,
convert
} = require('./lib/database');

const log = hexo.log;

const config = Object.assign({
enable: false,
database_format: 'json',
database_format: 'v8se',
concurrency: 10
}, hexo.config.huge_site_plugin);

Expand All @@ -23,8 +25,27 @@ function overrideDatabase() {
return;
}
const database = hexo.database;
if (/\.json$/.test(database.options.path)) {
database.options.path = database.options.path.replace(/\.json$/, '.dat');
const orgPath = database.options.path;
if (/\.json$/.test(orgPath)) {
const newPath = database.options.path = orgPath.replace(/\.json$/, '.dat');
log.debug('Writing database to %s', chalk.magenta(newPath));

if (fs.existsSync(orgPath) && !fs.existsSync(newPath)) {
const hexoLoad = hexo.load;
const hexoWatch = hexo.watch;
const filter = () => Promise.resolve()
.then(() => { log.info('Converting %s to %s', chalk.magenta(orgPath), chalk.magenta(newPath)); })
.then(() => convert(orgPath, newPath))
.then(() => { log.debug('Converted %s to %s', chalk.magenta(orgPath), chalk.magenta(newPath)); })
.catch(err => { throw err; });

hexo.load = function() {
return filter().then(hexoLoad.bind(hexo));
};
hexo.watch = function() {
return filter().then(hexoWatch.bind(hexo));
};
}
}

// override database load, save method.
Expand All @@ -51,8 +72,8 @@ function concurrency() {
}

if (config.enable) {
log.info('=========== hexo-huge-site-plugin ===========');
log.info('config %s', chalk.magenta(JSON.stringify(config)));
log.debug('=========== %s ===========', chalk.cyan('hexo-huge-site-plugin'));
log.debug('config %s', chalk.magenta(JSON.stringify(config)));
concurrency();
overrideDatabase();
}
19 changes: 13 additions & 6 deletions lib/database.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

const Promise = require('bluebird'); // eslint-disable-line node/no-missing-require
const Database = require('warehouse'); // eslint-disable-line node/no-missing-require
const WarehouseError = require('warehouse/lib/error'); // eslint-disable-line node/no-missing-require
const Promise = require('bluebird'); // eslint-disable-line node/no-missing-require,node/no-extraneous-require
const Database = require('warehouse'); // eslint-disable-line node/no-missing-require,node/no-extraneous-require
const WarehouseError = require('warehouse/lib/error'); // eslint-disable-line node/no-missing-require,node/no-extraneous-require
const {
readFileSync,
writeFileSync
writeFileSync,
deserializeJson,
serialize,
convert
} = require('node-serialization');

/**
Expand All @@ -25,7 +28,6 @@ function load(callback) {
let oldVersion = value.meta.version;
let models = value.models;
for (let key in models) {
// console.log("key " + key);
this.model(key)._import(models[key]);
}

Expand Down Expand Up @@ -89,8 +91,13 @@ function modelToJSON(model) {
return model.toJSON();
}

function convertJson(orgPath, newPath) {
return convert(orgPath, deserializeJson, newPath, serialize);
}

module.exports = {
load,
save,
toJSON
toJSON,
convert: convertJson
};
Loading

0 comments on commit 6763135

Please sign in to comment.