|
'use strict'; |
|
|
|
var fs = require('fs'); |
|
var Docxtemplater = require('docxtemplater'); |
|
|
|
var day_format = ['일', '월', '화', '수', '목', '금', '토']; |
|
|
|
var templateData = { |
|
year: '2015', |
|
month: '5', |
|
date: '9', |
|
day: '월', |
|
subject: '-s 인수에 주제를 넣으세요', |
|
name: '-n 인수에 파일이름을 넣으세요' |
|
}; |
|
var monday = new Date(); |
|
|
|
for (let i = 0; i < process.argv.length; i++) { |
|
var arg = process.argv; |
|
if ((arg[i] === '-d') && (i < process.argv.length - 1)) { |
|
var dateStr = arg[i + 1]; |
|
if (!dateStr.trim().match(/^\d{4}\.\d{2}.\d{2}$/)) { |
|
console.log('Error : -d 날짜는 이 형식에 맞추세요. yyyy.mm.dd'); |
|
console.log('예제 : 1992.05.09'); |
|
} |
|
var matched = dateStr.trim().match(/^(\d{4})\.(\d{2}).(\d{2})$/); |
|
|
|
templateData.year = matched[1]; |
|
templateData.month = matched[2]; |
|
templateData.date = matched[3]; |
|
monday = new Date(Number.parseInt(matched[1]), Number.parseInt(matched[2]) - 1, Number.parseInt(matched[3])); |
|
monday.setFullYear(Number.parseInt(matched[1])); |
|
|
|
i++; |
|
} |
|
if ((arg[i] === '-s') && (i < process.argv.length - 1)) { |
|
templateData.subject = arg[i + 1]; |
|
|
|
i++; |
|
} |
|
if ((arg[i] === '-n') && (i < process.argv.length - 1)) { |
|
templateData.name = arg[i + 1]; |
|
|
|
i++; |
|
} |
|
if ((arg[i] === 'help') && (i < process.argv.length - 1)) { |
|
console.log('command : -d -s'); |
|
console.log('-d yyyy.mm.dd'); |
|
console.log('-s "some text"'); |
|
} |
|
} |
|
|
|
// set as monday |
|
monday.setDate(monday.getDate() - (monday.getDay() - 1)); |
|
if (monday > new Date()) { |
|
monday.setDate(monday.getDate() - 7); |
|
} |
|
|
|
for (let i = 0; i < 5; i++) { |
|
var today = new Date(monday); |
|
today.setDate(monday.getDate() + i); |
|
|
|
templateData.year = today.getFullYear(); |
|
templateData.month = today.getMonth() + 1; |
|
templateData.date = today.getDate(); |
|
templateData.day = day_format[today.getDay()]; |
|
|
|
var content = fs.readFileSync(__dirname + '/연구노트_input.docx', 'binary'); |
|
var doc = new Docxtemplater(content); |
|
|
|
doc.setData(templateData); |
|
doc.render(); |
|
var buf = doc.getZip().generate({type: 'nodebuffer'}); |
|
|
|
fs.writeFileSync(__dirname + '/' + templateData.name + |
|
templateData.year.toString() + pad(templateData.month, 2, 0).toString() + pad(templateData.date, 2, 0).toString() + |
|
'_' + day_format[today.getDay()] + |
|
'.docx', buf); |
|
} |
|
|
|
// from somewhere of stackoverflow |
|
function pad (n, width, z) { |
|
z = z || '0'; |
|
n = n + ''; |
|
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; |
|
} |