sitemap_upload.js 4.1 KB
Newer Older
李少辉-开发者's avatar
李少辉-开发者 已提交
1 2 3 4 5 6 7 8 9 10 11 12
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"
李少辉-开发者's avatar
李少辉-开发者 已提交
13
const root_repo_git_url = "git@gitcode.net:dev-cloud/spring-docs.git"
李少辉-开发者's avatar
李少辉-开发者 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127


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)
})