提交 aaf34220 编写于 作者: 李少辉-开发者's avatar 李少辉-开发者 🎧

add sitemap upload script

Signed-off-by: 李少辉-开发者's avatarlish <lish@csdn.net>
上级 d643e52f
......@@ -3,9 +3,10 @@
"docs:dev": "yarn fix-memory-limit && yarn readme:copy && vuepress dev docs --no-cache",
"docs:build": "yarn fix-memory-limit && yarn readme:copy && vuepress build docs && yarn seo:copy && yarn mv",
"readme:copy": "cp -r README.md readme docs/ && cp -r README.md readme docs/en/",
"seo:gether": "node sitemap_upload.js",
"seo:copy": "cp -r seo/*.* docs/.vuepress/dist/",
"fix-memory-limit": "cross-env LIMIT=8192 increase-memory-limit",
"mv":"rm -rf public && mkdir -p public && mv docs/.vuepress/dist public"
"mv": "rm -rf public && mkdir -p public && mv docs/.vuepress/dist public"
},
"devDependencies": {
"cross-env": "^7.0.3",
......@@ -14,5 +15,8 @@
"vuepress": "^1.9.7",
"vuepress-plugin-autometa": "^0.1.13",
"vuepress-plugin-sitemap": "^2.3.1"
},
"dependencies": {
"xml2js": "^0.4.23"
}
}
const fs = require('fs');
const xml2js = require('xml2js');
const c_process = require('child_process');
const parser = new xml2js.Parser({ignoreAttrs: false});
const builder = new xml2js.Builder();
const relative_down_sitemap_path = "./public/sitemap.xml"
const store_path = './up_domain'
const relative_up_sitemap_path = store_path + "/public/sitemap.xml"
const relative_up_sitemap_dir = store_path + "/public/"
const up_sub_sitemap_name = "spring-sitemap.xml"
const root_repo_git_url = "https://lshemail:a03103498356@gitcode.net/dev-cloud/oss-pedia.git"
const up_domain = "https://docs.gitcode.net/"
const down_domain = "https://docs.gitcode.net/spring/guide/"
// 读取xml 文件内容
function get_sitemap_content(sitemap_path){
return fs.readFileSync(sitemap_path)
}
// 解析xml
function parse_sitemap_content(sitemap_string){
return new Promise(function(resolve, reject){
parser.parseStringPromise(sitemap_string).then(function(sitemap_content){
resolve(sitemap_content)
}).catch(function(err){
reject(err)
})
})
}
// build xml
function build_sitemap_from_json_content(sitemap_json){
return new Promise(function(resolve, reject){
var xml = builder.buildObject(sitemap_json);
resolve(xml)
})
}
// 添加子站sitemap
function ensure_up_sitemap(up_sitemap, up_sub_sitemap_name){
var flag = false
up_sitemap.sitemapindex.sitemap.forEach(item => {
if(item.loc[0].endsWith(up_sub_sitemap_name)){
flag = true
item.lastmod = JSON.stringify(new Date())
}
})
if(!flag){
up_sitemap.sitemapindex.sitemap.push({
loc: [ up_domain + up_sub_sitemap_name],
lastmod: [ JSON.stringify(new Date()) ]
})
}
return up_sitemap
}
// 子站 sitemap 更新时间
function ensure_down_sitemap(down_sitemap){
down_sitemap.urlset.url.forEach(item => {
console.log(item.loc)
item.lastmod = JSON.stringify(new Date())
})
return down_sitemap
}
// 保存到文件
function write_to_file(path, content){
fs.writeFileSync(path, content)
}
// 获取仓库
function fetch_up_repo(repo_url, store_path){
return new Promise(function(resolve, reject){
var cmd = "git clone --depth=1 " + repo_url + " " + store_path
c_process.exec(cmd, function(error, stdout, stderr) {
if(error) return reject(stderr)
resolve(stdout)
});
})
}
// 推送仓库
function push_up_repo(store_path){
return new Promise(function(resolve, reject){
var cmd = "git -C " + store_path + " add . ;git -C " + store_path + " commit -m 'update sitemap';git -C " + store_path + " push"
console.log(cmd)
c_process.exec(cmd, function(error, stdout, stderr) {
if(error) return reject(stderr)
resolve(stdout)
});
})
}
// 删除
function clean(store_path){
return new Promise(function(resolve, reject){
var cmd = "rm -rf " + store_path
console.log(cmd)
c_process.exec(cmd, function(error, stdout, stderr) {
if(error) return reject(stderr)
resolve(stdout)
});
})
}
fetch_up_repo(root_repo_git_url, store_path).then(function(result){
var down_sitemap_content = get_sitemap_content(relative_down_sitemap_path)
return parse_sitemap_content(down_sitemap_content)
}).then(function(result){
var down_sitemap_json = ensure_down_sitemap(result)
return build_sitemap_from_json_content(down_sitemap_json)
}).then(function(down_sitemap_xml){
write_to_file(relative_up_sitemap_dir + up_sub_sitemap_name, down_sitemap_xml)
var up_sitemap_content = get_sitemap_content(relative_up_sitemap_path)
return parse_sitemap_content(up_sitemap_content)
}).then(function(result){
var up_sitemap_json = ensure_up_sitemap(result, up_sub_sitemap_name)
return build_sitemap_from_json_content(up_sitemap_json)
}).then(function(up_sitemap_xml){
write_to_file(relative_up_sitemap_path, up_sitemap_xml)
return push_up_repo(store_path)
}).then(function(result){
return clean(store_path)
}).then(function(result){
console.log("Done!!")
}).catch(function(err){
console.log("Err:", err)
})
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册