module.exports.getCacheHash = function(hexoConfig){ var result = null; if( existsOption(hexoConfig) && fs.existsSync(getCachePath(hexoConfig)) ){ if(!read_cacheData){ read_cacheData = fs.readFileSync( getCachePath(hexoConfig) ); read_cacheData = JSON.parse(read_cacheData); } if(!read_cacheData.hash){ return ""; }else{ return read_cacheData.hash; } }else{ return ""; } };
module.exports.getCache = function(post, hexoConfig){ var result = null; if( existsOption(hexoConfig) && fs.existsSync(getCachePath(hexoConfig)) && !hexoConfig.generator_amp.isCacheUpdate ){ if(!read_cacheData){ // console.log("\u001b[31mrefreshed cache: \u001b[0m" + post.path); read_cacheData = fs.readFileSync( getCachePath(hexoConfig) ); read_cacheData = JSON.parse(read_cacheData); } for( var i=0; i<read_cacheData.ampData.length; i++){ if( read_cacheData.ampData[i].path == post.path ){ if( read_cacheData.ampData[i].date_eyeCatch == String(post.updated._i) && read_cacheData.ampData[i].date_amp == String(post.updated._i) ){ result = read_cacheData.ampData[i]; // console.log("use cached: "+post.path); break; } } } } return result; };
//--------------------------------------- // Writing cache file //--------------------------------------- function createCacheFile(inPath, hexoConfig, inPostPath , inDate , inEyeCatchImage , inTitleImageForAmp, inEyeCatchImageProperty , inXml , callback){ var cacheData; var newObj = { "path" : "" , "date_eyeCatch" : "" , "date_amp" : "" , "eyeCatchImage" : "" , "titleImageForAmp" : "" , "eyeCatchImageProperty" : "" , "xml" : "" }; var addObj = getAddObj(inPath, inPostPath , inDate , inEyeCatchImage , inTitleImageForAmp, inEyeCatchImageProperty , inXml); if( !fs.existsSync(getCachePath(hexoConfig)) || hexoConfig.generator_amp.isCacheClear ){ cacheData = { "hash": hexoConfig.generator_amp.hash , "ampData":[ assign(newObj,addObj) ] }; hexoConfig.generator_amp.isCacheClear = false; }else{ var isAddedData = false; var cacheData = fs.readFileSync(inPath); cacheData = JSON.parse(cacheData); for( var i=0; i<cacheData.ampData.length; i++){ if( cacheData.ampData[i].path == inPostPath ){ if( addObj.xml ){ if( cacheData.ampData[i].date_amp != inDate ){ cacheData.ampData[i] = assign(cacheData.ampData[i],addObj); } }else{ if( cacheData.ampData[i].date_eyeCatch != inDate ){ cacheData.ampData[i] = assign(cacheData.ampData[i],addObj); } } isAddedData = true; break; } } if(!isAddedData){ cacheData.ampData.push( addObj ); } } cacheData.hash = hexoConfig.generator_amp.hash mkdirp.sync( pathFn.dirname(inPath) ); fs.writeFileSync(inPath , JSON.stringify(cacheData)); read_cacheData = cacheData; // console.log("cached: "+inPostPath); newObj = null; addObj = null; if(callback)callback(); }
module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { const log = ctx.log; const defaultPath = pathFn.join(base, '_config.yml'); let paths; if (!configPaths) { log.w('No config file entered.'); return pathFn.join(base, '_config.yml'); } // determine if comma or space separated if (configPaths.includes(',')) { paths = configPaths.replace(' ', '').split(','); } else { // only one config let configPath = pathFn.isAbsolute(configPaths) ? configPaths : pathFn.resolve(base, configPaths); if (!fs.existsSync(configPath)) { log.w(`Config file ${configPaths} not found, using default.`); configPath = defaultPath; } return configPath; } const numPaths = paths.length; // combine files const combinedConfig = {}; let count = 0; for (let i = 0; i < numPaths; i++) { let configPath = ''; if (pathFn.isAbsolute(paths[i])) { configPath = paths[i]; } else { configPath = pathFn.join(base, paths[i]); } if (!fs.existsSync(configPath)) { log.w(`Config file ${paths[i]} not found.`); continue; } // files read synchronously to ensure proper overwrite order const file = fs.readFileSync(configPath); const ext = pathFn.extname(paths[i]).toLowerCase(); if (ext === '.yml') { merge(combinedConfig, yml.load(file)); count++; } else if (ext === '.json') { merge(combinedConfig, yml.safeLoad(file, {json: true})); count++; } else { log.w(`Config file ${paths[i]} not supported type.`); } } if (count === 0) { log.e('No config files found. Using _config.yml.'); return defaultPath; } log.i('Config based on', count, 'files'); const multiconfigRoot = outputDir || base; const outputPath = pathFn.join(multiconfigRoot, '_multiconfig.yml'); log.d(`Writing _multiconfig.yml to ${outputPath}`); fs.writeFileSync(outputPath, yml.dump(combinedConfig)); // write file and return path return outputPath; };
hexo.extend.helper.register('fileExists', function(path) { return fs.existsSync(path); });
hexo.extend.helper.register("fileExists", function(fileName) { return fs.existsSync("source/" + fileName); });
return function multiConfigPath(base, configPaths) { var log = ctx.log; var defaultPath = pathFn.join(base, '_config.yml'); var paths; if (!configPaths) { log.w('No config file entered.'); return pathFn.join(base, '_config.yml'); } // determine if comma or space separated if (configPaths.indexOf(',') > -1) { paths = configPaths.replace(' ', '').split(','); } else { // only one config if (!fs.existsSync(pathFn.join(base, configPaths))) { log.w('Config file ' + configPaths + ' not found, using default.'); return defaultPath; } return pathFn.resolve(base, configPaths); } var numPaths = paths.length; // combine files var combinedConfig = {}; var count = 0; for (var i = 0; i < numPaths; i++) { if (!fs.existsSync(pathFn.join(base, paths[i]))) { log.w('Config file ' + paths[i] + ' not found.'); continue; } // files read synchronously to ensure proper overwrite order var file = fs.readFileSync(pathFn.join(base, paths[i])); var ext = pathFn.extname(paths[i]).toLowerCase(); if (ext === '.yml') { _.merge(combinedConfig, yml.load(file)); count++; } else if (ext === '.json') { _.merge(combinedConfig, yml.safeLoad(file, {json: true})); count++; } else { log.w('Config file ' + paths[i] + ' not supported type.'); } } if (count === 0) { log.e('No config files found. Using _config.yml.'); return defaultPath; } log.i('Config based on', count, 'files'); var outputPath = pathFn.join(base, '_multiconfig.yml'); fs.writeFileSync(outputPath, yml.dump(combinedConfig)); // write file and return path return outputPath; };