Skip to content

Commit

Permalink
feat: export file
Browse files Browse the repository at this point in the history
  • Loading branch information
Diluka committed Jan 4, 2022
1 parent a173b0b commit 8b71ecd
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 158 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM node:${NODE_VERSION}-alpine
# 设置时区为CST
ENV TZ=Asia/Shanghai
ENV SERVER_PORT=3000
ARG NPM_CONFIG_REGISTRY=$NPM_CONFIG_REGISTRY
ARG NPM_CONFIG_REGISTRY
ENV NPM_CONFIG_REGISTRY=$NPM_CONFIG_REGISTRY

# Create app directory
Expand Down
60 changes: 1 addition & 59 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,59 +1 @@
import ChineseHolidays from 'chinese-holidays';
import ical from 'ical-generator';
import _ from 'lodash';
import moment from 'moment';
import http from 'http';

const port = +process.env.SERVER_PORT || 3000;

async function createCalendar() {
const calendar = ical({ name: '中国放假安排', timezone: 'Asia/Shanghai' });
console.log('正在准备节假日数据...');

const book = await ChineseHolidays.ready();
console.log('节假日数据加载完成');

for (const event of book.events()) {
console.log(event.name, event.days(), event.isHoliday(), event.isWorkingday());

const from = _.first(event.days());
const to = _.last(event.days());

const ev = calendar.createEvent({
id: `HD-CN-${+from}`,
start: moment(from),
end: moment(to).add(1, 'd'),
summary: event.name + (event.isWorkingday() ? '补班' : ''),
allDay: true,
});

if (event.isWorkingday()) {
const description = '明天要上班记得定闹钟';
ev.description(description);

event.days().map((o) =>
ev.createAlarm({
type: 'display',
trigger: moment(o).add(-4, 'hours'),
description,
}),
);
}
}

return calendar;
}

(async () => {
let calendar = await createCalendar();

setInterval(async () => {
calendar = await createCalendar();
}, 24 * 3600 * 1000);

http
.createServer((req, res) => calendar.serve(res))
.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
})();
import './lib/app.js';
19 changes: 19 additions & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import http from 'http';

import { createCalendar } from './create-calendar.js';

const port = +process.env.SERVER_PORT || 3000;

(async () => {
let calendar = await createCalendar();

setInterval(async () => {
calendar = await createCalendar();
}, 24 * 3600 * 1000);

http
.createServer((req, res) => calendar.serve(res))
.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
})();
42 changes: 42 additions & 0 deletions lib/create-calendar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import ChineseHolidays from 'chinese-holidays';
import ical from 'ical-generator';
import _ from 'lodash';
import moment from 'moment';

export async function createCalendar() {
const calendar = ical({ name: '中国放假安排', timezone: 'Asia/Shanghai' });
console.log('正在准备节假日数据...');

const book = await ChineseHolidays.ready();
console.log('节假日数据加载完成');

for (const event of book.events()) {
console.log(event.name, event.days(), event.isHoliday(), event.isWorkingday());

const from = _.first(event.days());
const to = _.last(event.days());

const ev = calendar.createEvent({
id: `HD-CN-${+from}`,
start: moment(from),
end: moment(to).add(1, 'd'),
summary: event.name + (event.isWorkingday() ? '补班' : ''),
allDay: true,
});

if (event.isWorkingday()) {
const description = '明天要上班记得定闹钟';
ev.description(description);

event.days().map((o) =>
ev.createAlarm({
type: 'display',
trigger: moment(o).add(-4, 'hours'),
description,
}),
);
}
}

return calendar;
}
4 changes: 4 additions & 0 deletions lib/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createCalendar } from './create-calendar.js';

const calendar = await createCalendar();
calendar.saveSync('./calendar.ics');
Loading

0 comments on commit 8b71ecd

Please sign in to comment.