美文网首页node
自动生成工作报告

自动生成工作报告

作者: 張弓 | 来源:发表于2019-07-08 08:53 被阅读0次

工作报告是每个上班族都需要写的,那么作为程序员应该如何写工作报告呢?

用到的技术:node、git、shell

这套方案,有以下好处:

  1. 自动化,不需要手写
  2. 随时记录工作内容,避免遗漏

写日志

const fs = require('fs');
const os = require('os');
const {exec} = require('child_process');

const homedir = os.homedir();
const day = new Date().getDay();
const filename = `${getSixDaysDate(1-day)}至${getSixDaysDate(6-day)}工作报告`;
const filepath = `${homedir}\\Desktop\\${filename}.md`;

const new_log = process.argv.slice(2); // 接收命令行参数

if(!fs.existsSync(filepath)) {
    exec('node report',(err)=>{
        if(err) throw err;
        write(new_log)
    });
} else {
    write(new_log)
}

// 读取日志 然后 写入日志
function write(new_log) {
    fs.readFile(filepath,'utf8',(err, data) => {
        if (err) throw err;
        // console.log(data);
        const today = getNowDate();
        const search =  new RegExp(`(${today})\n{(.*)}`);
        const arr = data.match(search);
        // console.log(arr);
        const new_text = `${arr[1]}\n{${arr[2]}${new_log}}`;
        const new_data = data.replace(arr[0],new_text);
        fs.writeFile(filepath, new_data, 'utf8', error => {
            if (error) throw error;
            console.log('工作报告已更新');
        });
    });
}

// 获取n天后的日期
function getSixDaysDate(days) {
    const date = new Date();
    date.setDate(date.getDate() + days);
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2,0);
    const day = date.getDate().toString().padStart(2,0);
    return `${year}-${month}-${day}`;
}

// 获取今日日期
function  getNowDate() {
    const date = new Date();
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2,'0');
    const day = date.getDate().toString().padStart(2,'0');
    return `${year}-${month}-${day}`;
}


module.exports = write;

生成报告

const fs = require('fs');
const os = require('os');
const homedir = os.homedir();

const day = new Date().getDay();
const filename = `${getSixDaysDate(1-day)}至${getSixDaysDate(6-day)}工作报告`;
const filepath = `${homedir}\\Desktop\\${filename}.md`;

const filecontent =
`# ${filename}

##### ${getSixDaysDate(1-day)}
{}
##### ${getSixDaysDate(2-day)}
{}
##### ${getSixDaysDate(3-day)}
{}
##### ${getSixDaysDate(4-day)}
{}
##### ${getSixDaysDate(5-day)}
{}
##### ${getSixDaysDate(6-day)}
{}
`;

fs.writeFile(filepath, filecontent, 'utf8', error => {
    if (error) return console.log(error);
    console.log(`已在桌面生成:${filename}`);
});

// 获取n天后的日期
function getSixDaysDate(days) {
    const date = new Date();
    date.setDate(date.getDate() + days);
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2,0);
    const day = date.getDate().toString().padStart(2,0);
    return `${year}-${month}-${day}`;
}

能让程序做的事情,绝不让人去做

相关文章

网友评论

    本文标题:自动生成工作报告

    本文链接:https://www.haomeiwen.com/subject/qslnhctx.html