• R/O
  • HTTP
  • SSH
  • HTTPS

zephyr: Commit

This repository is for zephyr, which is implemented for a GUI of Eos.


Commit MetaInfo

Revisiond46bddbc371de708a21b976ffcc0e37550de1bfc (tree)
Time2016-01-26 04:59:44
Authorhimetani_cafe <fumifumi@yasu...>
Commiterhimetani_cafe

Log Message

[DEBUG] modify the way to import and export Eos, DB class

if you import Eos or DB,
var eos = require('path/to/Eos.js').instance
var db = require('path/to/DB.js').instance
db.init(); // return promise

Change Summary

Incremental Difference

--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
44 "description": "",
55 "main": "./server/app.js",
66 "scripts": {
7- "test": "mocha test/mocha/DB.test.js"
7+ "test": "mocha test/mocha/*.test.js"
88 },
99 "author": "",
1010 "license": "ISC",
--- a/server/api/v1/fileUpload/index.js
+++ b/server/api/v1/fileUpload/index.js
@@ -4,31 +4,35 @@ var express = require('express');
44 var router = express.Router()
55 var path = require('path')
66 var formidable = require('formidable')
7-var DB = require(__dirname + '/../../../../server/class/DB');
7+var DB = require(__dirname + '/../../../../server/class/DB').instance;
88
99 router.post('/', function(req, res) {
10- DB()
11- .then(function(db) {
12- var form = new formidable.IncomingForm();
13- if(process.env['NODE_ENV'] === 'debug') {
14- form.uploadDir = __dirname + '/../../../../user-specific-files/workspace.debug';
15- } else {
16- form.uploadDir = __dirname + '/../../../../user-specific-files/workspace';
17- }
10+ var form = new formidable.IncomingForm();
11+ if(process.env['NODE_ENV'] === 'debug') {
12+ form.uploadDir = __dirname + '/../../../../user-specific-files/workspace.debug';
13+ } else {
14+ form.uploadDir = __dirname + '/../../../../user-specific-files/workspace';
15+ }
1816
19- form.parse(req, function(err, fields, files) {
20- var parentDirectory = fields['parentDirectory'];
21- var keys = Object.keys(files);
22- keys.forEach(function(f) {
23- db.createFile(files[f].name, parentDirectory)
24- .then(function(path) {
25- files[f].path = form.uploadDir + '/' + path;
26- console.log(files[f].path);
27- });
28- });
29- res.send("done");
30- });
17+ DB.createFile('hoge.txt','/')
18+ .then(function(r) {
19+ console.log(r);
3120 });
21+
22+ /*
23+ form.parse(req, function(err, fields, files) {
24+ var parentDirectory = fields['parentDirectory'];
25+ var keys = Object.keys(files);
26+ keys.forEach(function(f) {
27+ db.createFile(files[f].name, parentDirectory)
28+ .then(function(path) {
29+ files[f].path = form.uploadDir + '/' + path;
30+ console.log(files[f].path);
31+ });
32+ });
33+ res.send("done");
34+ });
35+ */
3236 });
3337
3438 module.exports = router;
--- a/server/app.js
+++ b/server/app.js
@@ -2,22 +2,29 @@
22
33 'use strict';
44
5-var express = require('express'),
6- config = require('./config'),
7- app = express(),
8- EventEmitter = require('events').EventEmitter,
9- emitter = new EventEmitter,
10- server = require('http').Server(app)
5+var express = require('express');
6+var config = require('./config');
7+var app = express();
8+var EventEmitter = require('events').EventEmitter;
9+var emitter = new EventEmitter;
10+var server = require('http').Server(app)
1111
12-require('./express')(app)
13-require('./routes')(app)
12+require('./express')(app);
13+require('./routes')(app);
14+
15+// DBのコンストラクタを実行
16+var DB = require('./class/DB').instance;
17+DB.init()
18+.then(function(methods) {
19+ console.log(DB);
20+});
1421
1522 var server = app.listen(config.port, config.ip, function () {
1623 console.log('Zephyr listening at http://%s:%s', config.ip, config.port);
1724 console.log(app.get('env'))
1825 });
1926
20-var io = require('socket.io').listen(server)
21-require('./ws')(io)
27+var io = require('socket.io').listen(server);
28+require('./ws')(io);
2229
23-exports = module.exports = app;
30+//exports = module.exports = app;
--- a/server/class/DB.js
+++ b/server/class/DB.js
@@ -3,582 +3,585 @@
33 *
44 * @returns {object}
55 */
6-function DB() {
7- 'use strict';
8-
9- var fs = require('fs');
10- var Sequelize = require('sequelize');
11- var co = require('co');
12- var uuid = require('node-uuid');
13-
14- var sequelize;
15- var test = 'hello';
16-
17- /**
18- * sync
19- *
20- * @returns {promise}<DBオブジェクトの関数群のオブジェクトをresolveする>
21- */
22- function sync() {
23- return new Promise(function(resolve) {
24- var force = process.env.NODE_ENV !== 'production';
25- Files.sync({force: force})
26- .then(function() {
27- return existDirectory(q);
28- })
29- .catch(function() {
6+'use strict';
7+
8+var fs = require('fs');
9+var Sequelize = require('sequelize');
10+var co = require('co');
11+var uuid = require('node-uuid');
12+
13+var sequelize;
14+var test = 'hello';
15+var Files; // Model object of sequelize
16+
17+ var instance = {
18+ init: init,
19+ sayHello: function() {
20+ return test;
21+ },
22+ getDirectoryParentId: getDirectoryParentId,
23+ getDirectoryId: getDirectoryId,
24+ getDirectory: getDirectory,
25+ existFile: existFile,
26+ notExistFile: notExistFile,
27+ existDirectory: existDirectory,
28+ notExistDirectory: notExistDirectory,
29+ createFile: createFile,
30+ removeFile: removeFile,
31+ createDirectory: createDirectory,
32+ test1: test1,
33+ test2: test2
34+ /*
35+ removeDirectory: removeDirectory,
36+ getFilename: getFilename,
37+ moveFile: moveFile,
38+ moveDirectory: moveDirectory
39+ */
40+};
41+
42+/**
43+ * init
44+ * @constructor
45+ * @returns {promise}
46+ */
47+
48+/**
49+ * productionモード時は、db.workspaceを永続化させる
50+ * DEBUGモードの時は、db.debugを実行毎に作成する。
51+ * DEBUGモードの時は、sync()の中で、/レコードがFilesテーブルに追加される。
52+ * モードの切り替えは環境変数NODE_ENVで行っている。
53+ * zephyr serveコマンドが実行されるとNODE_ENV = 'production'
54+ * zephyr debugコマンドが実行されるとNODE_ENV = 'debug'
55+ * となる。
56+ * それぞれの設定は$ZEOHYR_HOME/cli/zephyr-serve, $ZEPHYR_HOME/zephyre_debugを参照
57+ */
58+
59+ function init() {
60+ var dbPath;
61+ var dbOption = {
62+ dialect: 'sqlite'
63+ };
64+
65+ if(process.env['NODE_ENV'] === 'production') {
66+ dbPath = __dirname + '/../../user-specific-files/db/db.workspace';
67+ // if doesn't exist workspace.db, create.
68+ try {
69+ fs.accessSync(dbPath, fs.R_OK | fs.W_OK);
70+ } catch(e) {
71+ fs.writeFileSync(dbPath, '');
72+ }
73+ dbOption.storage = dbPath;
74+
75+ } else if(process.env['NODE_ENV'] === 'debug') {
76+ dbPath = __dirname + '/../../user-specific-files/db/db.debug';
77+ try {
78+ fs.accessSync(dbPath, fs.R_OK | fs.W_OK);
79+ fs.unlinkSync(dbPath);
80+ } catch(e) {
81+ }
82+
83+ fs.writeFileSync(dbPath, '');
84+ dbOption.storage = dbPath;
85+ }
86+
87+ sequelize = new Sequelize('','','', dbOption);
88+
89+ Files = sequelize.define('file', {
90+ fileId: {
91+ type: Sequelize.UUID,
92+ field: 'file_id',
93+ primaryKey: true
94+ },
95+ name: {
96+ type: Sequelize.STRING,
97+ field: 'name',
98+ allowNull: false
99+ },
100+ parentId: {
101+ type: Sequelize.UUID,
102+ field: 'parent_id',
103+ allowNull: false
104+ },
105+ fileType: {
106+ type: Sequelize.ENUM(0,1),
107+ field: 'file_type',
108+ allowNull: false
109+ }
110+ });
111+
112+ var force = process.env.NODE_ENV !== 'production';
113+ return Files.sync({force: force})
114+ .then(function() {
115+ return insertRoot();
116+ });
117+ }
118+
119+
120+/**
121+ * sync
122+ *
123+ * @returns {promise}<DBオブジェクトの関数群のオブジェクトをresolveする>
124+ */
125+function insertRoot() {
126+ return new Promise(function(resolve, reject) {
127+ var q = {
128+ where: {
129+ name: '/'
130+ }
131+ };
132+ Files.findOne(q)
133+ .then(function(r) {
134+ if(r === null) {
30135 var root = {
31136 fileId: '1f83f620-c1ed-11e5-9657-7942989daa00', // rootのuuidは固定値
32137 name: '/',
33138 parentId: '',
34139 fileType: 0
35140 };
36- /*
37- if(process.env.NODE_ENV === 'debug') {
38- return Files.create(root);
39- }
40- */
41141 return Files.create(root);
42- })
43- .then(function() {
44- var c = {
45- sayHello: function() {
46- return test;
47- },
48- getDirectoryParentId: getDirectoryParentId,
49- getDirectoryId: getDirectoryId,
50- getDirectory: getDirectory,
51- existFile: existFile,
52- notExistFile: notExistFile,
53- existDirectory: existDirectory,
54- notExistDirectory: notExistDirectory,
55- createFile: createFile,
56- removeFile: removeFile,
57- createDirectory: createDirectory,
58- test1: test1,
59- test2: test2
60- /*
61- removeDirectory: removeDirectory,
62- getFilename: getFilename,
63- moveFile: moveFile,
64- moveDirectory: moveDirectory
65- */
66- };
67- resolve(c);
68- });
142+ }
143+ })
144+ .then(function() {
145+ resolve();
69146 });
70- }
147+ });
148+}
71149
72- /**
73- * getDirectoryParentId
74- * ディレクトリのparentIdを取得する
75- * @param name
76- * @returns {promise} ディレクトリが存在すればresolve(uuid), しなければreject
77- */
78- function getDirectoryParentId(name) {
79- return new Promise(function(resolve, reject) {
80- var q = {
81- where: {
82- name: name,
83- fileType: 0
84- }
85- };
86- Files.findAll(q)
87- .then(function(r) {
88- if(r.length === 0) {
89- reject(new Error('"' + name + '" directory doesn\'t exist.'));
90- } else {
91- var map = r.map(function(c) { return c.dataValues.parentId });
92- resolve(map);
93- }
94- });
150+/**
151+ * getDirectoryParentId
152+ * ディレクトリのparentIdを取得する
153+ * @param name
154+ * @returns {promise} ディレクトリが存在すればresolve(uuid), しなければreject
155+ */
156+function getDirectoryParentId(name) {
157+ return new Promise(function(resolve, reject) {
158+ var q = {
159+ where: {
160+ name: name,
161+ fileType: 0
162+ }
163+ };
164+ Files.findAll(q)
165+ .then(function(r) {
166+ if(r.length === 0) {
167+ reject(new Error('"' + name + '" directory doesn\'t exist.'));
168+ } else {
169+ var map = r.map(function(c) { return c.dataValues.parentId });
170+ resolve(map);
171+ }
95172 });
96- }
173+ });
174+}
175+
176+
177+/**
178+ * getDirectoryId
179+ * ディレクトリのfileIdを取得する
180+ * @param name
181+ * @returns {promise} ディレクトリが存在すればresolve(uuid), しなければreject
182+ */
183+
184+function getDirectoryId(name) {
185+ return new Promise(function(resolve, reject) {
186+ var q = {
187+ where: {
188+ name: name,
189+ fileType: 0
190+ }
191+ };
192+ Files.findAll(q)
193+ .then(function(r) {
194+ if(r.length === 0) {
195+ reject(new Error('"' + name + '" directory doesn\'t exist.'));
196+ } else {
197+ var map = r.map(function(c) { return c.dataValues.fileId });
198+ resolve(map);
199+ }
200+ });
201+ });
202+}
97203
204+/**
205+ * getDirectory
206+ * ディレクトリのfileIdを取得する
207+ * @param name
208+ * @returns {promise} ディレクトリが存在すればresolve(name), しなければreject
209+ */
98210
99- /**
100- * getDirectoryId
101- * ディレクトリのfileIdを取得する
102- * @param name
103- * @returns {promise} ディレクトリが存在すればresolve(uuid), しなければreject
104- */
211+function getDirectory(name) {
212+ return new Promise(function(resolve, reject) {
213+ var q = {
214+ where: {
215+ name: name,
216+ fileType: 0
217+ }
218+ };
219+ Files.findAll(q)
220+ .then(function(r) {
221+ if(r.length === 0) {
222+ reject(new Error('"' + name + '" directory doesn\'t exist.'));
223+ } else {
224+ var map = r.map(function(c) { return c.dataValues });
225+ resolve(map);
226+ }
227+ });
228+ });
229+}
105230
106- function getDirectoryId(name) {
107- return new Promise(function(resolve, reject) {
231+/**
232+ * existFile
233+ * 同一ディレクトリに同名のファイルが存在することを確かめる
234+ * @param {string} fileName
235+ * @param {string} parentDirectory parentDirectoryの絶対パス
236+ * @returns {promise} ファイルが存在すればresolve、存在しなければreject
237+ */
238+function existFile(fileName, parentDirectory) {
239+ return new Promise(function(resolve, reject) {
240+ existDirectory(parentDirectory)
241+ .catch(function(error) {
242+ reject(error);
243+ })
244+ .then(function(fileId) {
108245 var q = {
109246 where: {
110- name: name,
111- fileType: 0
247+ name: fileName,
248+ parentId: fileId
112249 }
113250 };
114- Files.findAll(q)
115- .then(function(r) {
116- if(r.length === 0) {
117- reject(new Error('"' + name + '" directory doesn\'t exist.'));
118- } else {
119- var map = r.map(function(c) { return c.dataValues.fileId });
120- resolve(map);
121- }
122- });
251+ return Files.findOne(q)
252+ })
253+ .then(function(r) {
254+ if(r === null) {
255+ reject(new Error("\"" + fileName + "\" does not exist in " + '"' + parentDirectory + "\" directory."));
256+ } else {
257+ resolve(r.fileId);
258+ }
123259 });
124- }
260+ });
261+}
125262
126- /**
127- * getDirectory
128- * ディレクトリのfileIdを取得する
129- * @param name
130- * @returns {promise} ディレクトリが存在すればresolve(name), しなければreject
131- */
132263
133- function getDirectory(name) {
134- return new Promise(function(resolve, reject) {
264+/**
265+ * notExistFile
266+ * 同一ディレクトリに同名のファイルが存在していないことを確かめる
267+ * @param {string}fileName
268+ * @param {string}parentDirectory
269+ * @returns {promise} ファイルが存在しなければresolve、存在すればreject
270+ */
271+function notExistFile(fileName, parentDirectory) {
272+ return new Promise(function(resolve, reject) {
273+ existDirectory(parentDirectory)
274+ .catch(function(error) {
275+ reject(error);
276+ })
277+ .then(function(fileId) {
135278 var q = {
136279 where: {
137- name: name,
138- fileType: 0
280+ name: fileName,
281+ parentId: fileId
139282 }
140283 };
141- Files.findAll(q)
142- .then(function(r) {
143- if(r.length === 0) {
144- reject(new Error('"' + name + '" directory doesn\'t exist.'));
145- } else {
146- var map = r.map(function(c) { return c.dataValues });
147- resolve(map);
148- }
149- });
150- });
151- }
152-
153- /**
154- * existFile
155- * 同一ディレクトリに同名のファイルが存在することを確かめる
156- * @param {string} fileName
157- * @param {string} parentDirectory parentDirectoryの絶対パス
158- * @returns {promise} ファイルが存在すればresolve、存在しなければreject
159- */
160- function existFile(fileName, parentDirectory) {
161- return new Promise(function(resolve, reject) {
162- existDirectory(parentDirectory)
163- .catch(function(error) {
164- reject(error);
165- })
166- .then(function(fileId) {
167- var q = {
168- where: {
169- name: fileName,
170- parentId: fileId
171- }
172- };
173- return Files.findOne(q)
174- })
175- .then(function(r) {
176- if(r === null) {
177- reject(new Error("\"" + fileName + "\" does not exist in " + '"' + parentDirectory + "\" directory."));
178- } else {
179- //console.log(r.fileId);
180- resolve(r.fileId);
181- }
182- });
284+ return Files.findOne(q)
285+ })
286+ .then(function(r) {
287+ if(r === null) {
288+ resolve();
289+ } else {
290+ reject(new Error("\"" + fileName + "\" has already existed in " + '"' + parentDirectory + "\" directory."));
291+ }
183292 });
184- }
185-
293+ });
294+}
186295
187- /**
188- * notExistFile
189- * 同一ディレクトリに同名のファイルが存在していないことを確かめる
190- * @param {string}fileName
191- * @param {string}parentDirectory
192- * @returns {promise} ファイルが存在しなければresolve、存在すればreject
193- */
194- function notExistFile(fileName, parentDirectory) {
195- return new Promise(function(resolve, reject) {
196- existDirectory(parentDirectory)
197- .catch(function(error) {
198- reject(error);
199- })
200- .then(function(fileId) {
201- var q = {
202- where: {
203- name: fileName,
204- parentId: fileId
205- }
206- };
207- return Files.findOne(q)
208- })
209- .then(function(r) {
210- if(r === null) {
211- resolve();
212- } else {
213- reject(new Error("\"" + fileName + "\" has already existed in " + '"' + parentDirectory + "\" directory."));
214- }
215- });
216- });
217- }
296+/**
297+ * existDirectory
298+ * ディレクトリが存在することを確認する
299+ * @param {string} directory
300+ * @returns {promise} ディレクトリが存在すればresolve{fileId)、存在しなければreject
301+ */
302+function existDirectory(directory) {
303+ return new Promise(function(resolve, reject) {
304+ if(!directory) {
305+ reject(new Error('parameter "directory" is undefined'));
306+ }
218307
219- /**
220- * existDirectory
221- * ディレクトリが存在することを確認する
222- * @param {string} directory
223- * @returns {promise} ディレクトリが存在すればresolve{fileId)、存在しなければreject
224- */
225- function existDirectory(directory) {
226- return new Promise(function(resolve, reject) {
227- if(!directory) {
228- reject(new Error('parameter "directory" is undefined'));
229- }
308+ var arrayDirectory;
309+ var root = directory.substr(0,1);
230310
231- var arrayDirectory;
232- var root = directory.substr(0,1);
311+ if(root !== '/') {
312+ reject(new Error('directory name should start "/" so that it is absolute path including root.'));
313+ }
233314
234- if(root !== '/') {
235- reject(new Error('directory name should start "/" so that it is absolute path including root.'));
236- }
315+ if(directory === '/') {
316+ resolve('1f83f620-c1ed-11e5-9657-7942989daa00'); // rootのuuid
317+ } else {
318+ arrayDirectory = directory.split('/');
319+ arrayDirectory.shift(); // root
320+ arrayDirectory.unshift('/');
321+ }
237322
238- if(directory === '/') {
239- resolve('1f83f620-c1ed-11e5-9657-7942989daa00'); // rootのuuid
240- } else {
241- arrayDirectory = directory.split('/');
242- arrayDirectory.shift(); // root
243- arrayDirectory.unshift('/');
244- }
323+ var directoriesPromise = arrayDirectory.map(function(name) {
324+ return getDirectory(name);
325+ });
245326
246- var directoriesPromise = arrayDirectory.map(function(name) {
247- return getDirectory(name);
248- });
249- Promise.all(directoriesPromise)
250- .then(function(r) {
251- var parentId = r[0][0].fileId;
252- var index;
253- for(var i=1;i<r.length;i++) {
254- index = r[i].map(function(c) { return c.parentId }).indexOf(parentId);
255- if(index > -1) {
256- parentId = r[i][index].fileId;
257- } else {
258- reject(new Error('"' + directory + '" directory doesn\'t exist.'));
259- }
327+ Promise.all(directoriesPromise)
328+ .then(function(r) {
329+ var parentId = r[0][0].fileId;
330+ var index;
331+ for(var i=1;i<r.length;i++) {
332+ index = r[i].map(function(c) { return c.parentId }).indexOf(parentId);
333+ if(index > -1) {
334+ parentId = r[i][index].fileId;
335+ } else {
336+ reject(new Error('"' + directory + '" directory doesn\'t exist.'));
260337 }
261- resolve(parentId);
262- })
263- .catch(function(error) {
264- reject(new Error('"' + directory + '" directory doesn\'t exist.'));
265- });
338+ }
339+ resolve(parentId);
340+ })
341+ .catch(function(error) {
342+ reject(new Error('"' + directory + '" directory doesn\'t exist.'));
266343 });
267- }
344+ });
345+}
268346
269- /**
270- * notExistDirectory
271- * ディレクトリが存在しないことを確認する
272- * @param {string} directory
273- * @returns {promise} ディレクトリが存在しなければresolve、存在すればreject
274- */
275- function notExistDirectory(directory) {
276- return new Promise(function(resolve, reject) {
277- if(!directory) {
278- resolve();
279- }
347+/**
348+ * notExistDirectory
349+ * ディレクトリが存在しないことを確認する
350+ * @param {string} directory
351+ * @returns {promise} ディレクトリが存在しなければresolve、存在すればreject
352+ */
353+function notExistDirectory(directory) {
354+ return new Promise(function(resolve, reject) {
355+ if(!directory) {
356+ resolve();
357+ }
280358
281- var arrayDirectory;
282- var root = directory.substr(0,1);
359+ var arrayDirectory;
360+ var root = directory.substr(0,1);
283361
284- if(root !== '/') {
285- resolve();
286- }
362+ if(root !== '/') {
363+ resolve();
364+ }
287365
288- if(directory === '/') {
289- reject(new Error('"' + directory + '" directory exists.'));
290- } else {
291- arrayDirectory = directory.split('/');
292- arrayDirectory.shift(); // root
293- arrayDirectory.unshift('/');
294- }
366+ if(directory === '/') {
367+ reject(new Error('"' + directory + '" directory exists.'));
368+ } else {
369+ arrayDirectory = directory.split('/');
370+ arrayDirectory.shift(); // root
371+ arrayDirectory.unshift('/');
372+ }
295373
296- var directoriesPromise = arrayDirectory.map(function(name) {
297- return getDirectory(name);
298- });
299- Promise.all(directoriesPromise)
300- .then(function(r) {
301- var parentId = r[0][0].fileId;
302- var index;
303- for(var i=1;i<r.length;i++) {
304- index = r[i].map(function(c) { return c.parentId }).indexOf(parentId);
305- if(index > -1) {
306- parentId = r[i][index].fileId;
307- } else {
308- resolve();
309- }
310- }
311- reject(new Error('"' + directory + '" directory exists.'));
312- })
313- .catch(function(error) {
314- resolve();
315- });
374+ var directoriesPromise = arrayDirectory.map(function(name) {
375+ return getDirectory(name);
316376 });
317- }
318-
319- /**
320- * createFile
321- *
322- * @param fileName
323- * @param parentDirectory
324- * @returns {promise}<sequelize.createの結果を格納したobject | Error>
325- */
326- function createFile(fileName,parentDirectory) {
327- return new Promise(function(resolve, reject) {
328- if(!fileName) {
329- reject(new Error('filename is required.'));
330- }
331- Promise.all([existDirectory(parentDirectory), notExistFile(fileName, parentDirectory) ])
332- .catch(function(error) {
333- reject(error);
334- })
335- .then(function(r) {
336- var parentId = r[0]
337- var q = {
338- fileId: uuid.v1(),
339- name: fileName,
340- parentId: parentId,
341- fileType: 1
377+ Promise.all(directoriesPromise)
378+ .then(function(r) {
379+ var parentId = r[0][0].fileId;
380+ var index;
381+ for(var i=1;i<r.length;i++) {
382+ index = r[i].map(function(c) { return c.parentId }).indexOf(parentId);
383+ if(index > -1) {
384+ parentId = r[i][index].fileId;
385+ } else {
386+ resolve();
342387 }
343- return Files.create(q)
344- })
345- .then(function(r) {
346- resolve(r.dataValues.fileId);
347- });
348- });
349- }
350-
351-
352- /**
353- * removeFile
354- * ファイルを削除する
355- * @param {string} fileName
356- * @param {string} parentDirectory
357- * @returns {promise} ファイル削除に成功すればresolve、失敗すればreject
358- */
359- function removeFile(fileName, parentDirectory) {
360- return new Promise(function(resolve, reject) {
361- existFile(fileName, parentDirectory)
362- .catch(function(error) {
363- reject(error);
364- })
365- .then(function(fileId) {
366- var q = {
367- where: {
368- fileId: fileId
369- }
370- };
371- return Files.destroy(q);
372- })
373- .then(function() {
374- resolve();
375- });
376- });
377- }
378-
379- /**
380- * createDirectory
381- * ディレクトリを作成
382- * @param directory
383- * @returns {promise} ディレクトリの作成に成功すればresolve、失敗すればreject
384- */
385- function createDirectory(directory) {
386- return new Promise(function(resolve, reject) {
387- if(!directory) {
388- reject(new Error('directory name should start "/" so that it is absolute path including root.'));
389388 }
389+ reject(new Error('"' + directory + '" directory exists.'));
390+ })
391+ .catch(function(error) {
392+ resolve();
393+ });
394+ });
395+}
390396
391- var leaf = directory.split('/').pop();
392- var parentDirectory = directory.replace('/'+leaf, '');
393- if(!parentDirectory) {
394- parentDirectory = '/';
397+/**
398+ * createFile
399+ *
400+ * @param fileName
401+ * @param parentDirectory
402+ * @returns {promise}<sequelize.createの結果を格納したobject | Error>
403+ */
404+function createFile(fileName,parentDirectory) {
405+ return new Promise(function(resolve, reject) {
406+ if(!fileName) {
407+ reject(new Error('filename is required.'));
408+ }
409+ Promise.all([existDirectory(parentDirectory), notExistFile(fileName, parentDirectory) ])
410+ .catch(function(error) {
411+ reject(error);
412+ })
413+ .then(function(r) {
414+ var parentId = r[0]
415+ var q = {
416+ fileId: uuid.v1(),
417+ name: fileName,
418+ parentId: parentId,
419+ fileType: 1
395420 }
396- Promise.all([existDirectory(parentDirectory), notExistDirectory(directory), notExistFile(leaf, parentDirectory)])
397- .catch(function(error) {
398- reject(error);
399- })
400- .then(function(r) {
401- var parentId = r[0];
402- var q = {
403- fileId: uuid.v1(),
404- name: leaf,
405- parentId: parentId,
406- fileType: 0
407- }
408- return Files.create(q)
409- })
410- .then(function(r) {
411- resolve(r.dataValues.fileId);
412- });
421+ return Files.create(q)
422+ })
423+ .then(function(r) {
424+ resolve(r.dataValues.fileId);
413425 });
414- }
426+ });
427+}
415428
416429
417- /**
418- * removeDirectory
419- * ディレクトリを削除
420- * @param directory
421- * @returns {promise} ディレクトリの削除に成功すればresolve、失敗すればreject
422- */
423- function removeDirectory(directory) {
424- return new Promise(function(resolve, reject) {
425- var leaf = directory.split('/').pop();
426- var parentDirectory = directory.replace('/'+leaf, '');
427- if(!parentDirectory) {
428- parentDirectory = '/';
429- }
430- Promise.all([existDirectory(parentDirectory), notExistDirectory(directory), notExistFile(leaf, parentDirectory)])
431- .catch(function(error) {
432- reject(error);
433- })
434- .then(function(r) {
435- var parentId = r[0];
436- var q = {
437- fileId: uuid.v1(),
438- name: leaf,
439- parentId: parentId,
440- fileType: 0
430+/**
431+ * removeFile
432+ * ファイルを削除する
433+ * @param {string} fileName
434+ * @param {string} parentDirectory
435+ * @returns {promise} ファイル削除に成功すればresolve、失敗すればreject
436+ */
437+function removeFile(fileName, parentDirectory) {
438+ return new Promise(function(resolve, reject) {
439+ existFile(fileName, parentDirectory)
440+ .catch(function(error) {
441+ reject(error);
442+ })
443+ .then(function(fileId) {
444+ var q = {
445+ where: {
446+ fileId: fileId
441447 }
442- return Files.create(q)
443- })
444- .then(function(r) {
445- resolve(r.dataValues.fileId);
446- });
448+ };
449+ return Files.destroy(q);
450+ })
451+ .then(function() {
452+ resolve();
447453 });
448- }
449-
450- /**
451- * test1
452- * test用にデータベースのレコードを追加する関数
453- * @returns {promise}
454- */
455- function test1() {
456- var q = {
457- fileId: uuid.v1(),
458- name: 'hoge.txt',
459- parentId: '1f83f620-c1ed-11e5-9657-7942989daa00', // rootのuuid
460- fileType: 1
461- };
462- return Files.create(q);
463- }
454+ });
455+}
464456
465- /**
466- * test2
467- * test用にデータベースのレコードを追加する関数
468- * @returns {promise}
469- */
457+/**
458+ * createDirectory
459+ * ディレクトリを作成
460+ * @param directory
461+ * @returns {promise} ディレクトリの作成に成功すればresolve、失敗すればreject
462+ */
463+function createDirectory(directory) {
464+ return new Promise(function(resolve, reject) {
465+ if(!directory) {
466+ reject(new Error('directory name should start "/" so that it is absolute path including root.'));
467+ }
470468
471- function test2() {
472- var q1 = {
473- fileId: uuid.v1(),
474- name: 'one',
475- parentId: '1f83f620-c1ed-11e5-9657-7942989daa00', // rootのuuid
476- fileType: 0
477- };
478- return Files.create(q1)
479- .then(function() {
480- var q2 = {
469+ var leaf = directory.split('/').pop();
470+ var parentDirectory = directory.replace('/'+leaf, '');
471+ if(!parentDirectory) {
472+ parentDirectory = '/';
473+ }
474+ Promise.all([existDirectory(parentDirectory), notExistDirectory(directory), notExistFile(leaf, parentDirectory)])
475+ .catch(function(error) {
476+ reject(error);
477+ })
478+ .then(function(r) {
479+ var parentId = r[0];
480+ var q = {
481481 fileId: uuid.v1(),
482- name: 'two',
483- parentId: q1.fileId,
482+ name: leaf,
483+ parentId: parentId,
484484 fileType: 0
485+ }
486+ return Files.create(q)
487+ })
488+ .then(function(r) {
489+ resolve(r.dataValues.fileId);
490+ });
491+ });
492+}
485493
486- };
487- return Files.create(q2);
494+
495+/**
496+ * removeDirectory
497+ * ディレクトリを削除
498+ * @param directory
499+ * @returns {promise} ディレクトリの削除に成功すればresolve、失敗すればreject
500+ */
501+function removeDirectory(directory) {
502+ return new Promise(function(resolve, reject) {
503+ var leaf = directory.split('/').pop();
504+ var parentDirectory = directory.replace('/'+leaf, '');
505+ if(!parentDirectory) {
506+ parentDirectory = '/';
507+ }
508+ Promise.all([existDirectory(parentDirectory), notExistDirectory(directory), notExistFile(leaf, parentDirectory)])
509+ .catch(function(error) {
510+ reject(error);
488511 })
489512 .then(function(r) {
490- var q3 = {
513+ var parentId = r[0];
514+ var q = {
491515 fileId: uuid.v1(),
492- name: 'two',
493- parentId: '1f83f620-c1ed-11e5-9657-7942989daa00', //rootのuuid
516+ name: leaf,
517+ parentId: parentId,
494518 fileType: 0
495- };
496- return Files.create(q3);
519+ }
520+ return Files.create(q)
497521 })
498- .then(function() {
499- var q4 = {
500- fileId: uuid.v1(),
501- name: 'hogehoge.txt',
502- parentId: q1.fileId,
503- fileType: 1
504- };
505- return Files.create(q4);
522+ .then(function(r) {
523+ resolve(r.dataValues.fileId);
506524 });
507- }
508-
525+ });
526+}
509527
510- /**
511- *
512- * @constructor
513- * @returns {promise}
514- */
515-
516- /**
517- * productionモード時は、db.workspaceを永続化させる
518- * DEBUGモードの時は、db.debugを実行毎に作成する。
519- * DEBUGモードの時は、sync()の中で、/レコードがFilesテーブルに追加される。
520- * モードの切り替えは環境変数NODE_ENVで行っている。
521- * zephyr serveコマンドが実行されるとNODE_ENV = 'production'
522- * zephyr debugコマンドが実行されるとNODE_ENV = 'debug'
523- * となる。
524- * それぞれの設定は$ZEOHYR_HOME/cli/zephyr-serve, $ZEPHYR_HOME/zephyre_debugを参照
525- */
526-
527- var dbPath;
528- var dbOption = {
529- dialect: 'sqlite'
528+/**
529+ * test1
530+ * test用にデータベースのレコードを追加する関数
531+ * @returns {promise}
532+ */
533+function test1() {
534+ var q = {
535+ fileId: uuid.v1(),
536+ name: 'hoge.txt',
537+ parentId: '1f83f620-c1ed-11e5-9657-7942989daa00', // rootのuuid
538+ fileType: 1
530539 };
540+ return Files.create(q);
541+}
531542
532- if(process.env['NODE_ENV'] === 'production') {
533- dbPath = __dirname + '/../../user-specific-files/db/db.workspace';
534- // if doesn't exist workspace.db, create.
535- try {
536- fs.accessSync(dbPath, fs.R_OK | fs.W_OK);
537- } catch(e) {
538- fs.writeFileSync(dbPath, '');
539- }
540- dbOption.storage = dbPath;
541-
542- } else if(process.env['NODE_ENV'] === 'debug') {
543- dbPath = __dirname + '/../../user-specific-files/db/db.debug';
544- try {
545- fs.accessSync(dbPath, fs.R_OK | fs.W_OK);
546- fs.unlinkSync(dbPath);
547- } catch(e) {
548- }
549-
550- fs.writeFileSync(dbPath, '');
551- dbOption.storage = dbPath;
552- }
553-
543+/**
544+ * test2
545+ * test用にデータベースのレコードを追加する関数
546+ * @returns {promise}
547+ */
554548
549+function test2() {
550+ var q1 = {
551+ fileId: uuid.v1(),
552+ name: 'one',
553+ parentId: '1f83f620-c1ed-11e5-9657-7942989daa00', // rootのuuid
554+ fileType: 0
555+ };
556+ return Files.create(q1)
557+ .then(function() {
558+ var q2 = {
559+ fileId: uuid.v1(),
560+ name: 'two',
561+ parentId: q1.fileId,
562+ fileType: 0
555563
556- sequelize = new Sequelize('','','', dbOption);
557-
558- var Files = sequelize.define('file', {
559- fileId: {
560- type: Sequelize.UUID,
561- field: 'file_id',
562- primaryKey: true
563- },
564- name: {
565- type: Sequelize.STRING,
566- field: 'name',
567- allowNull: false
568- },
569- parentId: {
570- type: Sequelize.UUID,
571- field: 'parent_id',
572- allowNull: false
573- },
574- fileType: {
575- type: Sequelize.ENUM(0,1),
576- field: 'file_type',
577- allowNull: false
578- }
564+ };
565+ return Files.create(q2);
566+ })
567+ .then(function(r) {
568+ var q3 = {
569+ fileId: uuid.v1(),
570+ name: 'two',
571+ parentId: '1f83f620-c1ed-11e5-9657-7942989daa00', //rootのuuid
572+ fileType: 0
573+ };
574+ return Files.create(q3);
575+ })
576+ .then(function() {
577+ var q4 = {
578+ fileId: uuid.v1(),
579+ name: 'hogehoge.txt',
580+ parentId: q1.fileId,
581+ fileType: 1
582+ };
583+ return Files.create(q4);
579584 });
580-
581- return sync();
582585 }
583586
584-module.exports = DB;
587+module.exports = { instance: instance };
--- a/server/class/Eos.js
+++ b/server/class/Eos.js
@@ -4,266 +4,264 @@
44 * @returns {object}
55 * function execute(command, params) {
66 */
7-function Eos() {
8-
9- /**
10- *
11- * Class variables
12- */
13-
14- // include all Eos command's info.
15- // For seaching with O(n), the object key name is command name.
16- var commandReference = {
17- mrcImageInfo: { },
18- dcdFilePrint: { }
7+var eos = {
8+ validate: validate,
9+ toExecString: toExecString,
10+ execute: execute
11+}
12+
13+
14+/**
15+ *
16+ * Class variables
17+ */
18+
19+// include all Eos command's info.
20+// For seaching with O(n), the object key name is command name.
21+var commandReference = {
22+ mrcImageInfo: { },
23+ dcdFilePrint: { }
24+};
25+
26+// for unit test
27+var workspace = ['file1.txt', 'file2.txt'];
28+
29+
30+/**
31+ *
32+ * Class variables
33+ */
34+
35+
36+/**
37+ * validate
38+ * コマンドとオプションのバリデーション
39+ * @param command
40+ * @param params
41+ * @returns {valid: boolean, message: string}
42+ */
43+function validate(command, options) {
44+ var result = { hasDone: false, // true | false
45+ comment: ''// string
1946 };
2047
21- // for unit test
22- var workspace = ['file1.txt', 'file2.txt'];
23-
24-
25- /**
26- *
27- * Class variables
28- */
29-
30-
31- /**
32- * validate
33- * コマンドとオプションのバリデーション
34- * @param command
35- * @param params
36- * @returns {valid: boolean, message: string}
37- */
38- function validate(command, options) {
39- var result = { hasDone: false, // true | false
40- comment: ''// string
41- };
42-
43- var ocf; // Array
44- var ocfObj = {}; // key-value
45-
46- try {
47- /**
48- * Check of command name
49- */
50- if(typeof command !== 'string') {
51- errorMsg = 'Command parameter need to be string';
52- throw new Error(errorMsg);
53- }
48+ var ocf; // Array
49+ var ocfObj = {}; // key-value
5450
55- var hasCommand = Object.keys(commandReference).indexOf(command) > -1;
56- if(!hasCommand) {
57- errorMsg = 'Command name is invalid';
58- throw new Error(errorMsg);
59- }
51+ try {
52+ /**
53+ * Check of command name
54+ */
55+ if(typeof command !== 'string') {
56+ errorMsg = 'Command parameter need to be string';
57+ throw new Error(errorMsg);
58+ }
6059
61- /**
62- * Check of options
63- */
60+ var hasCommand = Object.keys(commandReference).indexOf(command) > -1;
61+ if(!hasCommand) {
62+ errorMsg = 'Command name is invalid';
63+ throw new Error(errorMsg);
64+ }
6465
65- if(!(Array.isArray(options))) {
66- errorMsg = 'Options need to be Array';
67- throw new Error(errorMsg);
68- }
66+ /**
67+ * Check of options
68+ */
69+
70+ if(!(Array.isArray(options))) {
71+ errorMsg = 'Options need to be Array';
72+ throw new Error(errorMsg);
73+ }
6974
7075
71- if(options.length === 0) {
72- errorMsg = 'At least one option is required.';
73- throw new Error(errorMsg);
74- }
76+ if(options.length === 0) {
77+ errorMsg = 'At least one option is required.';
78+ throw new Error(errorMsg);
79+ }
7580
76- // translate options to key-value and check whether options include correct member
77- var optionsObj = {};
78- var hasCorrectMember = true;
79- var isArgumentsArray = true;
81+ // translate options to key-value and check whether options include correct member
82+ var optionsObj = {};
83+ var hasCorrectMember = true;
84+ var isArgumentsArray = true;
8085
81- options.forEach(function(o) {
82- if(!(o.name) && !(o.arguments)) {
83- hasCorrectMember = false;
86+ options.forEach(function(o) {
87+ if(!(o.name) && !(o.arguments)) {
88+ hasCorrectMember = false;
89+ } else {
90+ if(Array.isArray(o.arguments)) {
91+ optionsObj[o.name] = o.arguments;
8492 } else {
85- if(Array.isArray(o.arguments)) {
86- optionsObj[o.name] = o.arguments;
87- } else {
88- isArgumentsArray = false;
89- }
93+ isArgumentsArray = false;
9094 }
91- });
92-
93- // check each object has proberties "name" and "argumets"
94- if(!hasCorrectMember) {
95- errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
96- throw new Error(errorMsg);
9795 }
96+ });
9897
99- // check each "argumets" properties is Array
100- if(!isArgumentsArray) {
101- errorMsg = 'Each "arguments" properties needs to be Array';
102- throw new Error(errorMsg);
103- }
98+ // check each object has proberties "name" and "argumets"
99+ if(!hasCorrectMember) {
100+ errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
101+ throw new Error(errorMsg);
102+ }
104103
105- // Read OptionControlFile info of command
106- ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
104+ // check each "argumets" properties is Array
105+ if(!isArgumentsArray) {
106+ errorMsg = 'Each "arguments" properties needs to be Array';
107+ throw new Error(errorMsg);
108+ }
107109
108- // translate ocf info to key-value
109- var notIncludingRequiredOptions = [];
110- ocf.forEach(function(o) {
111- if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
112- notIncludingRequiredOptions.push(o.option);
113- }
114- ocfObj[o.option] = o;
115- });
110+ // Read OptionControlFile info of command
111+ ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
116112
117- // check whether all required option exist
118- if(notIncludingRequiredOptions.length > 0) {
119- errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
120- throw new Error(errorMsg);
113+ // translate ocf info to key-value
114+ var notIncludingRequiredOptions = [];
115+ ocf.forEach(function(o) {
116+ if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
117+ notIncludingRequiredOptions.push(o.option);
121118 }
119+ ocfObj[o.option] = o;
120+ });
122121
123- var invalidArgumentsNumber= [];
124- var invalidArgumentType = [];
125- var invalidOutputFileName = [];
122+ // check whether all required option exist
123+ if(notIncludingRequiredOptions.length > 0) {
124+ errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
125+ throw new Error(errorMsg);
126+ }
126127
127- // output file Regexp
128- var outRegExp = /out|append/;
128+ var invalidArgumentsNumber= [];
129+ var invalidArgumentType = [];
130+ var invalidOutputFileName = [];
129131
130- options.forEach(function(o) {
131- // option number
132- var expectNum = ocfObj[o.name].optionNumber;
133- var actualNum = o.arguments.length;
134- if(expectNum !== actualNum) {
135- invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
136- }
132+ // output file Regexp
133+ var outRegExp = /out|append/;
137134
135+ options.forEach(function(o) {
136+ // option number
137+ var expectNum = ocfObj[o.name].optionNumber;
138+ var actualNum = o.arguments.length;
139+ if(expectNum !== actualNum) {
140+ invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
141+ }
138142
139- // argType and outFile name
140- o.arguments.forEach(function(arg,i) {
141- // argType
142- var formType = ocfObj[o.name].arg[i].formType
143- if(formType === 'select') { // This argument is filename
144- var exist = workspace.indexOf(arg) > -1;
145- if(!exist) {
146- invalidArgumentType.push({name: o.name, file: arg});
147- }
148- } else {
149- var expectType = formType === 'text' ? 'string' : 'number';
150- var actualType = typeof arg;
151- if(expectType !== actualType) {
152- invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
153- }
143+
144+ // argType and outFile name
145+ o.arguments.forEach(function(arg,i) {
146+ // argType
147+ var formType = ocfObj[o.name].arg[i].formType
148+ if(formType === 'select') { // This argument is filename
149+ var exist = workspace.indexOf(arg) > -1;
150+ if(!exist) {
151+ invalidArgumentType.push({name: o.name, file: arg});
152+ }
153+ } else {
154+ var expectType = formType === 'text' ? 'string' : 'number';
155+ var actualType = typeof arg;
156+ if(expectType !== actualType) {
157+ invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
154158 }
159+ }
155160
156- // outFile name
157- if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
158- if(workspace.indexOf(o.arguments[i]) > -1) {
159- invalidOutputFileName.push({name: o.name, file: arg});
160- }
161+ // outFile name
162+ if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
163+ if(workspace.indexOf(o.arguments[i]) > -1) {
164+ invalidOutputFileName.push({name: o.name, file: arg});
161165 }
162- });
166+ }
163167 });
168+ });
164169
165- // check arguments number value
166- if(invalidArgumentsNumber.length > 0) {
167- errorMsg = '"arguments" properties is invalid number.\n';
168- invalidArgumentsNumber.forEach(function(i) {
169- errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
170- });
171- throw new Error(errorMsg);
172- }
170+ // check arguments number value
171+ if(invalidArgumentsNumber.length > 0) {
172+ errorMsg = '"arguments" properties is invalid number.\n';
173+ invalidArgumentsNumber.forEach(function(i) {
174+ errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
175+ });
176+ throw new Error(errorMsg);
177+ }
173178
174- // check arguments type
175- if(invalidArgumentType.length > 0) {
176- errorMsg = '"arguments" type is invalid.\n';
177- invalidArgumentType.forEach(function(i) {
178- if(i.file) {
179- errorMsg += i.name + ' ' + i.file + ' does not exist.\n';
180- } else {
181- errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
182- }
183- });
184- throw new Error(errorMsg);
185- }
179+ // check arguments type
180+ if(invalidArgumentType.length > 0) {
181+ errorMsg = '"arguments" type is invalid.\n';
182+ invalidArgumentType.forEach(function(i) {
183+ if(i.file) {
184+ errorMsg += i.name + ' ' + i.file + ' does not exist.\n';
185+ } else {
186+ errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
187+ }
188+ });
189+ throw new Error(errorMsg);
190+ }
186191
187- // check outFile name
188- if(invalidOutputFileName.length > 0) {
189- errorMsg = 'output file name is invalid.\n';
190- invalidOutputFileName.forEach(function(i) {
191- errorMsg += i.name + ' ' + i.file + ' has already existed.\n';
192- });
193- throw new Error(errorMsg);
194- }
195- } catch(e) {
196- result.message = e.message;
197- return result;
192+ // check outFile name
193+ if(invalidOutputFileName.length > 0) {
194+ errorMsg = 'output file name is invalid.\n';
195+ invalidOutputFileName.forEach(function(i) {
196+ errorMsg += i.name + ' ' + i.file + ' has already existed.\n';
197+ });
198+ throw new Error(errorMsg);
198199 }
200+ } catch(e) {
201+ result.message = e.message;
202+ return result;
199203 }
204+}
200205
201- /**
202- * toExecString
203- *
204- * @param command
205- * @param options
206- * @returns {string}
207- */
208- function toExecString(command, options) {
209- var ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command); // Array
210- var finalOptions = {};
211- var execStr = command + ' ';
212-
213- // set default parameters
214- ocf.forEach(function(o) {
206+/**
207+ * toExecString
208+ *
209+ * @param command
210+ * @param options
211+ * @returns {string}
212+ */
213+function toExecString(command, options) {
214+ var ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command); // Array
215+ var finalOptions = {};
216+ var execStr = command + ' ';
217+
218+ // set default parameters
219+ ocf.forEach(function(o) {
215220 o.arg.forEach(function(arg) {
216- if(!(arg.initialValue === "") && arg.initialValue) {
221+ if(!(arg.initialValue === "") && arg.initialValue) {
217222 if(!(finalOptions[o.option])) {
218- finalOptions[o.option] = [];
219- finalOptions[o.option].push(arg.initialValue);
223+ finalOptions[o.option] = [];
224+ finalOptions[o.option].push(arg.initialValue);
220225 } else {
221- finalOptions[o.option].push(arg.initialValue);
226+ finalOptions[o.option].push(arg.initialValue);
222227 }
223- }
228+ }
229+ });
224230 });
225- });
226231
227- // set user setting parameters
228- options.forEach(function(o) {
232+ // set user setting parameters
233+ options.forEach(function(o) {
229234 finalOptions[o.name] = o.arguments;
230- });
235+ });
231236
232- // set execution string
233- Object.keys(finalOptions).forEach(function(key) {
237+ // set execution string
238+ Object.keys(finalOptions).forEach(function(key) {
234239 execStr += key + ' ';
235240 finalOptions[key].forEach(function(arg) {
236- execStr += arg + ' ';
241+ execStr += arg + ' ';
242+ });
237243 });
238- });
239244
240- // remove last blank
241- execStr = execStr.slice(0,execStr.length-1);
242-
243- return execStr;
244- }
245+ // remove last blank
246+ execStr = execStr.slice(0,execStr.length-1);
245247
248+ return execStr;
249+}
246250
247- return {
248- /**
249- * execute
250- *
251- * @param command
252- * @param params
253- * @returns {object}
254- */
255- execute: function(command, options) {
256- var result = validate(command, options);
257- if(!result.hasDone) {
258- } else {
259- var str = toExecString(command, options);
260- }
261- },
262251
263- // For unit test
264- validate: validate,
265- toExecString: toExecString
252+/**
253+ * execute
254+ *
255+ * @param command
256+ * @param params
257+ * @returns {object}
258+ */
259+function execute(command, options) {
260+ var result = validate(command, options);
261+ if(!result.hasDone) {
262+ } else {
263+ var str = toExecString(command, options);
266264 }
267265 }
268266
269-module.exports = Eos;
267+module.exports = { instance: eos };
--- a/test/mocha/DB.test.js
+++ b/test/mocha/DB.test.js
@@ -1,16 +1,12 @@
11 (function() {
22 'use strict';
3- var DB = require('../../server/class/DB');
3+ var db = require('../../server/class/DB').instance;
44 var chai = require('chai');
55 var chaiAsPromised = require('chai-as-promised');
66 chai.use(chaiAsPromised);
77 var expect = chai.expect;
88 chai.should();
99
10- //process.env['NODE_ENV'] = 'production';
11-
12-
13-
1410 describe('DB クラス', function() {
1511
1612 var constructor = `
@@ -23,15 +19,9 @@
2319 */
2420 `;
2521 describe(constructor, function() {
26- var db;
2722 before(function() {
28- return DB()
29- .then(function(r) {
30- db = r;
31- });
23+ return db.init()
3224 });
33-
34-
3525 it('should return hello when call sayHello', function() {
3626 expect(db.sayHello()).to.equal('hello');
3727 });
@@ -47,12 +37,8 @@
4737 */
4838 `;
4939 describe(getDirectoryParentId, function() {
50- var db;
5140 before(function() {
52- return DB()
53- .then(function(r) {
54- db = r;
55- })
41+ return db.init()
5642 .then(function() {
5743 return db.test2();
5844 });
@@ -67,7 +53,6 @@
6753 it('should be rejected when \'two\' exists', function() {
6854 return expect(db.getDirectoryParentId('two')).to.eventually.have.length(2);
6955 });
70-
7156 });
7257
7358 var getDirectoryId = `
@@ -79,17 +64,12 @@
7964 */
8065 `;
8166 describe(getDirectoryId, function() {
82- var db;
8367 before(function() {
84- return DB()
85- .then(function(r) {
86- db = r;
87- })
68+ return db.init()
8869 .then(function() {
8970 return db.test2();
9071 });
9172 });
92-
9373 it('should be rejected when a directory does not exist', function() {
9474 return db.getDirectoryId('hoge').should.be.rejectedWith(Error, '"hoge" directory doesn\'t exist.');
9575 });
@@ -99,7 +79,6 @@
9979 it('should be rejected when \'two\' exists', function() {
10080 return expect(db.getDirectoryId('two')).to.eventually.have.length(2);
10181 });
102-
10382 });
10483
10584
@@ -111,13 +90,9 @@
11190 * @returns {promise} ディレクトリが存在すればresolve(obj), しなければreject
11291 */
11392 `;
114- describe(getDirectoryId, function() {
115- var db;
93+ describe(getDirectory, function() {
11694 before(function() {
117- return DB()
118- .then(function(r) {
119- db = r;
120- })
95+ return db.init()
12196 .then(function() {
12297 return db.test2();
12398 });
@@ -132,7 +107,6 @@
132107 it('should be rejected when \'two\' exists', function() {
133108 return expect(db.getDirectory('two')).to.eventually.have.length(2);
134109 });
135-
136110 });
137111
138112
@@ -146,12 +120,8 @@
146120 */
147121 `;
148122 describe(existFile, function() {
149- var db;
150123 before(function() {
151- return DB()
152- .then(function(r) {
153- db = r;
154- })
124+ return db.init()
155125 .then(function() {
156126 return db.test1();
157127 })
@@ -169,7 +139,6 @@
169139 it('should be resolved when file exists in a directory', function() {
170140 return expect(db.existFile('hogehoge.txt', '/one')).to.eventually.be.a('string');
171141 });
172-
173142 });
174143
175144
@@ -184,12 +153,8 @@
184153 */
185154 `;
186155 describe(notExistFile, function() {
187- var db;
188156 before(function() {
189- return DB()
190- .then(function(r) {
191- db = r;
192- })
157+ return db.init()
193158 .then(function() {
194159 return db.test1();
195160 })
@@ -207,7 +172,6 @@
207172 it('should be resolved when the same name file does not exist in directory', function() {
208173 return expect(db.notExistFile('hoge.txt', '/one')).to.eventually.equal();
209174 });
210-
211175 });
212176
213177 var existDirectory = `
@@ -219,12 +183,8 @@
219183 */
220184 `;
221185 describe(existDirectory, function() {
222- var db;
223186 before(function() {
224- return DB()
225- .then(function(r) {
226- db = r;
227- })
187+ return db.init()
228188 .then(function() {
229189 return db.test2();
230190 })
@@ -260,12 +220,8 @@
260220 */
261221 `;
262222 describe(existDirectory, function() {
263- var db;
264223 before(function() {
265- return DB()
266- .then(function(r) {
267- db = r;
268- })
224+ return db.init()
269225 .then(function() {
270226 return db.test1();
271227 })
@@ -289,7 +245,6 @@
289245 it('should be resolved(2)', function() {
290246 return expect(db.notExistDirectory('hoge')).to.eventually.equal();
291247 });
292-
293248 });
294249
295250
@@ -303,12 +258,8 @@
303258 */
304259 `;
305260 describe(createFile, function() {
306- var db;
307261 before(function() {
308- return DB()
309- .then(function(r) {
310- db = r;
311- })
262+ return db.init()
312263 .then(function() {
313264 return db.test1();
314265 })
@@ -335,7 +286,6 @@
335286 it('should be rejected(3)', function() {
336287 return db.createFile(null, '/').should.be.rejectedWith(Error, 'filename is required.');
337288 });
338-
339289 });
340290
341291
@@ -350,12 +300,8 @@
350300 `;
351301
352302 describe(removeFile, function() {
353- var db;
354303 before(function() {
355- return DB()
356- .then(function(r) {
357- db = r;
358- })
304+ return db.init()
359305 .then(function() {
360306 return db.test1();
361307 });
@@ -379,12 +325,8 @@
379325 */
380326 `;
381327 describe(createDirectory, function() {
382- var db;
383328 before(function() {
384- return DB()
385- .then(function(r) {
386- db = r;
387- })
329+ return db.init()
388330 .then(function() {
389331 return db.test1();
390332 })
@@ -394,11 +336,6 @@
394336 .then(function() {
395337 return db.createDirectory('/one/hoge');
396338 });
397- /*
398- .then(function() {
399- return db.createDirectory('/one/two/three');
400- });
401- */
402339 });
403340
404341 it('should be rejected when directory has already existed', function() {
@@ -419,8 +356,6 @@
419356 it('should be rejected(2)', function() {
420357 return db.createDirectory().should.be.rejectedWith(Error, 'directory name should start "/" so that it is absolute path including root.');
421358 });
422-
423359 });
424-
425360 });
426361 })();
--- a/test/mocha/Eos.test.js
+++ b/test/mocha/Eos.test.js
@@ -1,6 +1,6 @@
11 (function() {
22 'use strict';
3- var Eos = require('../../server/class/Eos');
3+ var eos = require('../../server/class/Eos').instance;
44 var expect = require('chai').expect;
55
66 var test1 = `
@@ -25,9 +25,7 @@
2525 `;
2626
2727 describe('Eos クラス', function() {
28- var eos;
2928 before(function() {
30- eos = new Eos();
3129 });
3230
3331 describe(test1, function() {
Show on old repository browser