Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
全体的に見直してローカル環境から試せるように
Browse files Browse the repository at this point in the history
  • Loading branch information
proshunsuke committed Mar 9, 2021
1 parent fd3f28e commit 6468e06
Show file tree
Hide file tree
Showing 22 changed files with 3,594 additions and 4,081 deletions.
20 changes: 0 additions & 20 deletions .babelrc

This file was deleted.

5 changes: 4 additions & 1 deletion .clasp.json
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{"scriptId":"1nm2BIhYyOpqUmj-vKRr4p69bhMEGBf-Cq9opE4u3xvfyd67C38majtmI","rootDir":"./dist","projectId":"augc-260709"}
{
"scriptId":"1rzoffgXqdRqq98pRPxJSCMJnLmJ3cUTyhpjZsfm54WbHByH7swQv7n8V",
"rootDir":"./dist"
}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ dist: trusty
sudo: false
cache: yarn
script:
- make test/ci && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
- make test/ci && cat ./coverage/lcov.info | yarn coveralls
33 changes: 20 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
NODE_MODULUES_BIN_DIR := ./node_modules/.bin

setup:
$(MAKE) yarn/install

yarn/install:
install:
yarn install

webpack/build:
$(NODE_MODULUES_BIN_DIR)/webpack
build: install
yarn webpack

watch:
ENV=local yarn webpack watch

server:
yarn run functions-framework --target=getKeyakiSchedule --source=./gcpFunctions/getKeyakiSchedule

run/setSchedule:
node -e 'require("./dist/index.js");global.setSchedule();'

clasp/login:
$(NODE_MODULUES_BIN_DIR)/clasp login
yarn clasp login

clasp/push: yarn/install webpack/build
$(NODE_MODULUES_BIN_DIR)/clasp push -f
clasp/push: build
yarn clasp push -f

clasp/run:
$(NODE_MODULUES_BIN_DIR)/clasp run execute
yarn clasp run execute

clasp/open:
$(NODE_MODULUES_BIN_DIR)/clasp open
yarn clasp open

clasp/logs:
$(NODE_MODULUES_BIN_DIR)/clasp logs
yarn clasp logs

test:
$(NODE_MODULUES_BIN_DIR)/jest
ENV=production yarn jest

test/ci:
$(MAKE) webpack/build
$(NODE_MODULUES_BIN_DIR)/jest --coverage
ENV=production yarn jest --coverage
3 changes: 2 additions & 1 deletion dist/appsscript.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/script.scriptapp"
],
"exceptionLogging": "STACKDRIVER"
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
File renamed without changes.
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,39 @@
"license": "MIT",
"devDependencies": {
"@babel/core": "*",
"@babel/plugin-proposal-class-properties": "*",
"@babel/preset-env": "*",
"@babel/preset-typescript": "*",
"@google-cloud/functions-framework": "*",
"@google/clasp": "*",
"@types/google-apps-script": "*",
"@types/jest": "*",
"@types/node": "*",
"@types/node-fetch": "*",
"babel-loader": "*",
"core-js": "*",
"coveralls": "*",
"dayjs": "*",
"es3ify-webpack-plugin": "*",
"gas-webpack-plugin": "*",
"jest": "*",
"mockdate": "*",
"node-fetch": "*",
"puppeteer": "^8.0.0",
"regenerator-runtime": "*",
"ts-jest": "*",
"tslint": "*",
"ts-loader": "*",
"webpack": "*",
"webpack-cli": "*"
},
"jest": {
"moduleFileExtensions": [
"ts",
"js"
"js",
"ts"
],
"transform": {
"^.+\\.ts$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfig": "tsconfig.json"
"tsconfig": "tsconfig.json"
},
"CalendarApp": {},
"UrlFetchApp": {},
Expand Down
12 changes: 9 additions & 3 deletions src/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export default class Calendar {
Retry.retryable(3, () => {
event.deleteEvent();
});
Utilities.sleep(500); // API制限に引っかかってそうなのでsleepする
if (process.env.ENV === 'production') {
Utilities.sleep(500); // API制限に引っかかってそうなのでsleepする
}
}

/**
Expand All @@ -28,9 +30,13 @@ export default class Calendar {
}
const calendarId: string = keyakiCalendarId.calendarId;
Retry.retryable(3, () => {
CalendarApp.getCalendarById(calendarId).createAllDayEvent(schedule.title, new Date(schedule.start));
if (process.env.ENV === 'production') {
CalendarApp.getCalendarById(calendarId).createAllDayEvent(schedule.title, new Date(schedule.start));
}
});
console.info("予定を作成しました。日付: " + schedule.start + ", タイトル: " + schedule.title);
Utilities.sleep(500); // API制限に引っかかってそうなのでsleepする
if (process.env.ENV === 'production') {
Utilities.sleep(500); // API制限に引っかかってそうなのでsleepする
}
};
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Schedule from "./schedule";
import Trigger from "./lib/trigger";
import dayjs from "dayjs";
import 'regenerator-runtime';

// @ts-ignore
declare const global: {
Expand All @@ -11,7 +12,7 @@ declare const global: {
*
* @param e
*/
global.createSetScheduleTrigger = function (e: any) {
global.createSetScheduleTrigger = (e: any) => {
console.info("スケジュール更新のトリガーを作成します");
Trigger.setTrigger(dayjs());
console.info("スケジュール更新のトリガーを作成しました");
Expand All @@ -21,10 +22,10 @@ global.createSetScheduleTrigger = function (e: any) {
*
* @param e
*/
global.setSchedule = function (e: any) {
global.setSchedule = (e: any) => {
const schedule = new Schedule();
console.info("スケジュール更新を開始します");
schedule.setSchedule();
void schedule.setSchedule();
console.info("スケジュール更新が完了しました");
};

Expand Down
2 changes: 1 addition & 1 deletion src/keyakizaka/keyakiObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface KeyakiCalendarObj {
calendarId: string
}

export const getKeyakiCalendarUrl: string = "https://us-central1-augc-260709.cloudfunctions.net/getKeyakiSchedule?date=";
export const getKeyakiCalendarUrl = process.env.ENV === 'production' ? "https://us-central1-augc-260709.cloudfunctions.net/getKeyakiSchedule?date=": "http://localhost:8080?date=";

export const keyakiCalendarIds: KeyakiCalendarObj[] = [
{
Expand Down
29 changes: 24 additions & 5 deletions src/keyakizaka/keyakiSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import dayjs from "dayjs";
import Calendar from "../calendar";
import {ScheduleObj, KeyakiCalendarObj, getKeyakiCalendarUrl, keyakiCalendarIds} from "./keyakiObjects";
import Retry from "../lib/retry";
import fetch, { Response } from 'node-fetch';
import 'regenerator-runtime';

export default class KeyakiSchedule {
/**
*
* @param {dayjs.Dayjs} date
*/
setSchedule(date: dayjs.Dayjs): void {
async setSchedule(date: dayjs.Dayjs): Promise<void> {
const customUrl: string = getKeyakiCalendarUrl + date.format('YYYYMMDD');

const scheduleJson: string = Retry.retryable(3, () => {
return UrlFetchApp.fetch(customUrl).getContentText();
});
const scheduleJson = await this.getScheduleJson(customUrl);
// console.log(scheduleJson);

const scheduleList: ScheduleObj[] = JSON.parse(scheduleJson);

Expand All @@ -23,6 +24,23 @@ export default class KeyakiSchedule {
console.info(date.format('YYYY年MM月') + "分の予定を更新しました");
};

/**
*
* @param {string} customUrl
* @returns {Promise<any>}
* @private
*/
private async getScheduleJson(customUrl: string) {
if (process.env.ENV === 'production') {
return Retry.retryable(3, () => {
return UrlFetchApp.fetch(customUrl).getContentText();
});
} else {
const response = await fetch(customUrl);
return response.text();
}
}

/**
*
* @param {dayjs.Dayjs} date
Expand All @@ -31,6 +49,7 @@ export default class KeyakiSchedule {
const calendar = new Calendar();
let deleteEventCallCount: number = 0;
keyakiCalendarIds.forEach((keyakiCalendarObj: KeyakiCalendarObj) => {
if (process.env.ENV !== 'production') return;
const calendarApp: GoogleAppsScript.Calendar.Calendar = Retry.retryable(3, () => {
return CalendarApp.getCalendarById(keyakiCalendarObj.calendarId);
});
Expand All @@ -40,7 +59,7 @@ export default class KeyakiSchedule {

while (targetDate.isBefore(targetDateBeginningOfNextMonth)) {
const events = calendarApp.getEventsForDay(targetDate.toDate());
events.forEach(event => {
events.forEach((event) => {
deleteEventCallCount++;
try {
calendar.deleteEvent(event);
Expand Down
4 changes: 4 additions & 0 deletions src/lib/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class Trigger {
* @param {dayjs.Dayjs} targetDate
*/
static setTrigger(targetDate: dayjs.Dayjs): void {
if (process.env.ENV !== 'production') return;
const properties: GoogleAppsScript.Properties.Properties = PropertiesService.getScriptProperties();
properties.setProperty(TARGET_DATE_KEY, targetDate.format('YYYY-MM-DD'));
ScriptApp.newTrigger(TRIGGER_FUNCTION_NAME).timeBased().after(TRIGGER_DURATION).create();
Expand All @@ -30,16 +31,19 @@ export default class Trigger {
* @returns {string | null}
*/
static getTargetDateProperty(): string | null {
if (process.env.ENV !== 'production') return null;
const properties: GoogleAppsScript.Properties.Properties = PropertiesService.getScriptProperties();
return properties.getProperty(TARGET_DATE_KEY);
}

static deleteTargetDateProperty(): void {
if (process.env.ENV !== 'production') return;
const properties: GoogleAppsScript.Properties.Properties = PropertiesService.getScriptProperties();
properties.deleteProperty(TARGET_DATE_KEY);
}

static deleteTriggers(): void {
if (process.env.ENV !== 'production') return;
ScriptApp.getProjectTriggers().forEach((trigger: GoogleAppsScript.Script.Trigger) => {
if (trigger.getHandlerFunction() === TRIGGER_FUNCTION_NAME) {
ScriptApp.deleteTrigger(trigger);
Expand Down
12 changes: 6 additions & 6 deletions src/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import dayjs from "dayjs";

export default class Schedule {

setSchedule(): void {
const keyakiSchedule: KeyakiSchedule = new KeyakiSchedule();
const beginningOfNexYearMonth: dayjs.Dayjs = dayjs().startOf('month').add(1, 'year');
let targetBeginningOfMonth: dayjs.Dayjs = this.getTargetBeginningOfMonth();
const startDate: dayjs.Dayjs = dayjs();
async setSchedule(): Promise<void> {
const keyakiSchedule = new KeyakiSchedule();
const beginningOfNexYearMonth = dayjs().startOf('month').add(1, 'year');
let targetBeginningOfMonth = this.getTargetBeginningOfMonth();
const startDate = dayjs();

while (targetBeginningOfMonth.isBefore(beginningOfNexYearMonth)) {
keyakiSchedule.setSchedule(targetBeginningOfMonth);
await keyakiSchedule.setSchedule(targetBeginningOfMonth);
targetBeginningOfMonth = targetBeginningOfMonth.add(1, 'month');
if (Trigger.hasExceededTerminationMinutes(startDate)) {
Trigger.setTrigger(targetBeginningOfMonth);
Expand Down
4 changes: 4 additions & 0 deletions tests/calendar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import Calendar from "../src/calendar";
import {ScheduleObj} from "../src/keyakizaka/keyakiObjects";
const calendar = new Calendar();

beforeEach(() => {
jest.spyOn(console, "info").mockImplementation();
});

describe("deleteEvent", (): void => {
it("event.deleteEventが1回呼ばれること", (): void => {
Utilities.sleep = jest.fn().mockReturnThis();
Expand Down
1 change: 1 addition & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe("execute", (): void => {
Schedule.mockClear();
// @ts-ignore
Trigger.mockClear();
jest.spyOn(console, "info").mockImplementation();
});
it("createSetScheduleTriggerが呼ばれるとTrigger.setTriggerが1回呼ばれること", (): void => {
createSetScheduleTrigger();
Expand Down
Loading

0 comments on commit 6468e06

Please sign in to comment.