This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
209 lines (183 loc) · 5.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/// <reference path="typings/index.d.ts" />
const nodepath = require('path');
const fs = require('fs');
const co = require('co');
const qs = require('querystring');
const _ = require('lodash');
const electron = require('electron');
const { app, BrowserWindow, shell, ipcMain, Menu, dialog} = electron;
const Twitter = require('twitter');
const TwitterAPI = require('node-twitter-api');
const config = require('./config.json');
const tapi = new TwitterAPI({
consumerKey: config.consumerKey,
consumerSecret: config.consumerSecret,
callback: 'https://www.google.co.jp/',
});
const IGNORED_FILES = ['.DS_Store'];
const template = require('./menu');
const dst = `file://${__dirname}/dst/index.html`;
let twitter = null;
let twitterSettings = {};
/**
* Create the window
* @param {Boolean} hasVerified
* @returns {Promise}
*/
const createWindow = (hasVerified) => {
const size = electron.screen.getPrimaryDisplay().workAreaSize;
const mainWindow = new BrowserWindow({
width: size.width, height: size.height,
webPreferences: { webSecurity: false }
});
return new Promise((resolve, reject) => {
if (hasVerified) {
resolve({ mainWindow });
} else {
tapi.getRequestToken((err, requestToken, requestTokenSecret, results) => {
if (err) { reject(err); }
const aurl = tapi.getAuthUrl(requestToken);
mainWindow.webContents.on('will-navigate', (event, url) => {
event.preventDefault();
const oauthVerifier = qs.parse(_.split(url, '?')[1]).oauth_verifier;
tapi.getAccessToken(requestToken, requestTokenSecret, oauthVerifier, (err, accessToken, accessTokenSecret, results) => {
if (err) { reject(err); }
tapi.verifyCredentials(accessToken, accessTokenSecret, {}, (error, data, res) => {
if (err) { reject(err); }
resolve({ accessToken, accessTokenSecret, mainWindow });
});
});
});
mainWindow.loadURL(aurl);
});
}
});
};
app.on('ready', () => {
co(function* () {
const {accessToken, accessTokenSecret, mainWindow} = yield createWindow();
twitter = new Twitter({
consumer_key: config.consumerKey,
consumer_secret: config.consumerSecret,
access_token_key: accessToken,
access_token_secret: accessTokenSecret
});
const screenName = yield new Promise((resolve, reject) => {
twitter.get('account/settings', (err, settings, res) => {
if (err) { reject(err); }
resolve(settings.screen_name);
});
});
const {iconURL, name} = yield new Promise((resolve, reject) => {
twitter.get('users/show', { screen_name: screenName }, (err, show, res) => {
if (err) { reject(err); }
const {profile_image_url, name} = show;
const iconURL = profile_image_url.replace(/_normal/, '');
resolve({ iconURL, name });
});
});
twitterSettings = { iconURL, name, screenName };
mainWindow.loadURL(dst);
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}).catch((err) => { throw new Error(err); });
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') { app.quit(); }
});
app.on('activate', () => {
if (BrowserWindow.getFocusedWindow()) { return; }
co(function* () {
const {mainWindow} = yield createWindow(true);
mainWindow.loadURL(dst);
});
});
/**
* Dig a directory. Ignore a directory whose name start with a dot.
* @param {String} dirpath
* @returns {{directories: String[], files: []}}
*/
const digDirectory = (dirpath) => {
const results = {
directories: [],
files: []
};
_.forEach(fs.readdirSync(dirpath), (basename) => {
const dot = basename.charAt(0) === '.';
const path = nodepath.join(dirpath, basename);
const state = fs.statSync(path);
if (state.isDirectory() && !dot) {
results.directories.push(path);
} else if (state.isFile() && !_.includes(IGNORED_FILES, basename)) {
results.files.push(path);
} else {
console.log(`TODO: others path ${path}`);
}
});
return results;
};
ipcMain.on('open', (event, args) => {
const current = args.path;
const basename = nodepath.basename(current);
const basestate = fs.statSync(current);
if (basestate.isDirectory()) {
event.returnValue = {
type: 'directory',
results: digDirectory(current),
basename
};
} else if (basestate.isFile()) {
event.returnValue = {
type: 'file',
results: [current],
basename
};
} else {
console.log('TODO: others path');
}
});
ipcMain.on('dig-directory', (event, args) => {
event.returnValue = digDirectory(args.path);
});
ipcMain.on('dirs', (event, args) => {
event.returnValue = fs.readdirSync(args.path);
});
ipcMain.on('read-file', (event, args) => {
event.returnValue = fs.readFileSync(args.path, 'utf-8');
});
ipcMain.on('save-file', (event, args) => {
fs.writeFileSync(args.path, args.data);
event.returnValue = null;
});
ipcMain.on('message-box-save-or-not', (event, args) => {
event.returnValue = dialog.showMessageBox(BrowserWindow.getFocusedWindow(),
{
type: 'info',
buttons: ['Save', 'Cancel', 'Don\'t Save'],
message: `Do you want to save the chnages you made to ${args.filename}?`,
detail: 'Your changes will be lost if you don\'t save them.'
}
);
});
ipcMain.on('twitter-settings', (event, args) => {
event.returnValue = twitterSettings;
});
ipcMain.on('tweet', (event, args) => {
co(function* () {
const result = yield new Promise((resolve, reject) => {
twitter.post('statuses/update', { status: args.status }, (err, tweet, res) => {
if (err) {
resolve({
type: 'error',
content: err[0]
});
}
resolve({
type: 'success',
content: 'Your message has been sent.'
});
});
});
event.returnValue = result;
});
});