From 9626b4bd6d716e8f9eb48780490aabf335c753da Mon Sep 17 00:00:00 2001 From: Alfred UC Date: Tue, 2 Feb 2016 01:32:28 +0900 Subject: [PATCH 1/5] Change naming. corco -> doc --- .eslintrc | 4 +- gulpfile.js | 2 +- package.json | 6 ++- public/main.html | 67 +++++++++++++++++++++++++ public/sub.html | 27 ++++++++++ public/sub2.html | 24 +++++++++ src/_apidoc.js | 20 -------- src/app.js | 3 +- src/bin/www | 10 +++- src/controller/doc.js | 76 +++++++++++++++++++---------- src/controller/fileManager/index.js | 43 ++++++++++++---- starter.js | 5 +- 12 files changed, 222 insertions(+), 65 deletions(-) create mode 100644 public/main.html create mode 100644 public/sub.html create mode 100644 public/sub2.html delete mode 100644 src/_apidoc.js diff --git a/.eslintrc b/.eslintrc index 9502eb4..5fcede2 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,7 +14,9 @@ "no-undef": 2, "no-shadow": [1, {"hoist": "functions", "allow": ["e"] }], "no-use-before-define": [2, "nofunc"], - "no-unused-vars": [1, {"vars": "all", "args": "none"}] + "no-unused-vars": [1, {"vars": "all", "args": "none"}], + "no-dupe-keys": 2, + "no-redeclare": 2 }, "env": { "node": true diff --git a/gulpfile.js b/gulpfile.js index 75f11e8..8a321f8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -306,7 +306,7 @@ gulp.task('fetchPublicFromClient', () => { gulp.task('apidoc', () => { - const cmd = 'node_modules/apidoc/bin/apidoc -i src -o public/apidoc'; + const cmd = 'node_modules/apidoc-support-socketio/bin/apidoc-support-socketio -i src -o public/apidoc'; console.log( cmd ); syncExec( cmd ); diff --git a/package.json b/package.json index e9214cc..4bb5c39 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "private": true, "scripts": { - "apidoc": "node_modules/apidoc/bin/apidoc -i src -o public/apidoc", + "apidoc": "node_modules/apidoc-support-socketio/bin/apidoc-support-socketio -i src -o public/apidoc", "start": "node starter", "restart": "node starter --restart" }, @@ -14,6 +14,8 @@ "debug": "~2.2.0", "express": "~4.12.4", "jade": "~1.9.2", + "log4js": "^0.6.30", + "log4js-extend": "^0.1.2", "morgan": "~1.5.3", "node-sass": "^3.4.2", "node-svn-ultimate": "^1.0.2", @@ -26,7 +28,7 @@ "yargs": "^3.31.0" }, "devDependencies": { - "apidoc": "^0.14.0", + "apidoc-support-socketio": "^0.2.3", "babel-preset-es2015": "^6.3.13", "babel-register": "^6.3.13", "del": "^2.2.0", diff --git a/public/main.html b/public/main.html new file mode 100644 index 0000000..c8e6981 --- /dev/null +++ b/public/main.html @@ -0,0 +1,67 @@ + + + + + + + + + {{title}} + + + + + + + aaa + bbb + ccc +
remove
+ +
+ +
+ + + + + + + + + diff --git a/public/sub.html b/public/sub.html new file mode 100644 index 0000000..5e9d785 --- /dev/null +++ b/public/sub.html @@ -0,0 +1,27 @@ + + + + + + + + + {{title}} + + + + + + + +
+ +
+ + + + + diff --git a/public/sub2.html b/public/sub2.html new file mode 100644 index 0000000..8aea6f2 --- /dev/null +++ b/public/sub2.html @@ -0,0 +1,24 @@ + + + + + + + + + {{title}} + + + + + + + + sub2 + sub2 + sub2 + sub2 + + + + diff --git a/src/_apidoc.js b/src/_apidoc.js deleted file mode 100644 index 98d5718..0000000 --- a/src/_apidoc.js +++ /dev/null @@ -1,20 +0,0 @@ - -/** - * @api {get} /corcoFile GET /corcoFile - * @apiGroup CorcoFile - * @apiName GET /corcoFile - * @apiDescription READ the all corcoFile list - * @apiVersion 0.1.0 - * @apiUse CommonResult - * - * @apiUse CommonErrorCode - * @apiError (errorCode) { string } NO_ROOT_DIRECTORY the root directory in the server side was not found - * - * @apiSuccess { array } fileList The list of corcoFiles - * @apiSuccessExample Example data on success: - * { - * fileList: ["test1.corco","test2.corco"] - * } - * - * @apiSampleRequest /corcoFile - */ diff --git a/src/app.js b/src/app.js index da17e39..3e59aa4 100644 --- a/src/app.js +++ b/src/app.js @@ -24,11 +24,12 @@ app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); -app.use('/', router); +app.use('/', router ); app.use('/', docRouter ); //app.use(express.static( path.join(__dirname, 'public'))); app.use(express.static( process.env.PUBLIC_PATH )); +console.log( process.env.PUBLIC_PATH ); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/src/bin/www b/src/bin/www index 6e5f336..3a83bb8 100755 --- a/src/bin/www +++ b/src/bin/www @@ -4,8 +4,14 @@ * Module dependencies. */ -process.env.DEVELOPMENT = JSON.parse( process.env.DEVELOPMENT ); -process.env.PRODUCTION = JSON.parse( process.env.PRODUCTION ); +try { + process.env.DEVELOPMENT = JSON.parse( process.env.DEVELOPMENT ); + process.env.PRODUCTION = JSON.parse( process.env.PRODUCTION ); +} catch( err ) { + + process.env.DEVELOPMENT = true; + process.env.PRODUCTION = false; +} if( process.env.DEVELOPMENT ) { require("babel-register"); diff --git a/src/controller/doc.js b/src/controller/doc.js index e231622..ebe39da 100644 --- a/src/controller/doc.js +++ b/src/controller/doc.js @@ -14,15 +14,24 @@ var express = require('express'); var router = express.Router(); -var logger = require('tracer').colorConsole(); +var log4js = require('log4js'); +var log4js_extend = require("log4js-extend"); +log4js_extend(log4js, { + path: __dirname, + format: "at @name (@file:@line:@column)" +}); +var logger = log4js.getLogger(); + var DOC_DIR_PATH = process.env.DOC_DIR_PATH; var fileManager = require('./fileManager/index'); fileManager.initRootPath( DOC_DIR_PATH ); +logger.debug('29'); + /** - * @api {post} /getDocList getDocList + * @api {post} /readDocList readDocList * @apiGroup Doc - * @apiName getDocList + * @apiName readDocList * @apiDescription READ the all doc list * @apiVersion 0.1.0 * @apiUse CommonResult @@ -36,9 +45,9 @@ fileManager.initRootPath( DOC_DIR_PATH ); * fileList: ["test1.corco","test2.corco"] * } * - * @apiSampleRequest /getDocList + * @apiSampleRequest /readDocList */ -router.post('/getDocList', (req, res, next) => { +router.post('/readDocList', (req, res, next) => { fileManager.loadFileList().then( ( fileList ) => { res.send( fileList ); @@ -46,73 +55,88 @@ router.post('/getDocList', (req, res, next) => { }); /** - * @api {get} /corcoFile/:fileName GET /corcoFile/:fileName - * @apiGroup CorcoFile - * @apiName GET /corcoFile/:fileName + * @api {post} /readDoc readDoc + * @apiGroup Doc + * @apiName readDoc * @apiDescription READ the contents of specific filename * @apiVersion 0.1.0 * - * @apiSuccess { string } raw The contents of corcoFile + * @apiParam { string } fileName fileName + * * @apiSuccessExample Example data on success: * { * raw: "corco_question\n질문합니다!\ncorco_answer\n답변합니다" * } * - * @apiSampleRequest /corcoFile/test.corco + * @apiSampleRequest /readDoc + * @apiSampleRequestParam fileName="test.corco" */ -router.get('/:fileName', (req, res, next) => { +router.post('/readDoc', (req, res, next) => { + + const fileName = req.body.fileName; - logger.debug('get /corcoFile/'+req.params.fileName); + logger.debug( fileName ); + logger.debug('post /readDoc/'+fileName); //var filePath = path.join( CORCO_SVN_PATH, req.params.fileName ); - fileManager.loadFile( req.params.fileName ).then( ( fileContent ) => { - + fileManager.loadFile( fileName ).then( ( fileContent ) => { res.send({ raw: fileContent }); }); }); /** - * @api {post} /corcoFile/:fileName POST /corcoFile/:fileName - * @apiGroup CorcoFile - * @apiName POST /corcoFile + * @api {post} /createDoc createDoc + * @apiGroup Doc + * @apiName createDoc * @apiDescription CREATE a new corco file * @apiVersion 0.1.0 * + * @apiParam { string } fileName The contents of corcoFile want to be save * @apiParam { string } raw The contents of corcoFile want to be save * * @apiSuccess { string } errorCode A result of the request * @apiSuccessExample Example data on success: * { errorCode: "NO_ERROR" } * - * @apiSampleRequest /corcoFile/test.corco + * @apiSampleRequest /createDoc + * @apiSampleRequestParam fileName="test4.corco" + * @apiSampleRequestParam raw="test test test" */ -router.post('/:fileName', (req, res, next) => { +router.post('/createDoc', (req, res, next) => { console.log('wef'); - fileManager.saveFile( req.params.fileName, req.body.raw ).then( () => { + logger.info( req.body ); + fileManager.createFile( req.body.fileName, req.body.raw ).then( () => { res.send({ errorCode: 'NO_ERROR' }); + }).catch( ( err ) => { + res.send( err ); }); }); /** - * @api {put} /corcoFile/:fileName PUT /corcoFile/:fileName - * @apiGroup CorcoFile - * @apiName PUT /corcoFile + * @api {post} /updateDoc updateDoc + * @apiGroup Doc + * @apiName updateDoc * @apiDescription UPDATE a exist corco file * @apiVersion 0.1.0 * + * @apiParam { string } fileName fileName * @apiParam { string } raw The contents of corcoFile want to be save * * @apiSuccess { string } errorCode A result of the request * @apiSuccessExample Example data on success: * { errorCode: "NO_ERROR" } * - * @apiSampleRequest /corcoFile/test.corco + * @apiSampleRequest /updateDoc + * @apiSampleRequestParam fileName="test4.corco" + * @apiSampleRequestParam raw="test test test" */ -router.put('/:fileName', (req, res, next) => { +router.post('/updateDoc', (req, res, next) => { - fileManager.saveFile( req.params.fileName, req.body.raw ).then( () => { + fileManager.updateFile( req.body.fileName, req.body.raw ).then( () => { res.send({ errorCode: 'NO_ERROR' }); + }).catch( ( err ) => { + res.send( err ); }); }); diff --git a/src/controller/fileManager/index.js b/src/controller/fileManager/index.js index 209fac3..27c934f 100644 --- a/src/controller/fileManager/index.js +++ b/src/controller/fileManager/index.js @@ -6,7 +6,13 @@ const _ = require('underscore'); const path = require('path'); const pathExists = require('path-exists'); const svnUltimate = require('node-svn-ultimate'); -const logger = require('tracer').colorConsole(); +var log4js = require('log4js'); +var log4js_extend = require("log4js-extend"); +log4js_extend(log4js, { + path: __dirname, + format: "at @name (@file:@line:@column)" +}); +var logger = log4js.getLogger(); const svnOptions = { username: 'corco', password: 'corco', @@ -60,25 +66,44 @@ function realPath( relativePath ) { const fileManager = { - saveFile: ( fileName, raw ) => { + createFile: ( fileName, raw ) => { - logger.debug('saveFile'); + logger.debug('createFile'); var filePath = realPath( fileName ); - var alreadyExist = pathExists.sync( filePath ); - logger.debug('saveFile1'); + return svn.update().then( () => { + var alreadyExist = pathExists.sync( filePath ); + if( alreadyExist ) { + logger.error( { errorCode: 'ALREADY_EXIST' } ); + throw { errorCode: 'ALREADY_EXIST' }; + } + + return nodefn.call( fs.writeFile, filePath, raw ); + + }).then( function() { - return nodefn.call( fs.writeFile, filePath, raw ).then( () => { - - return svn.update(); + return svn.add( filePath ); }).then( function() { + return svn.commit( filePath ); + }); + }, + + updateFile: ( fileName, raw ) => { + + var filePath = realPath( fileName ); + + return svn.update().then( () => { + var alreadyExist = pathExists.sync( filePath ); if( ! alreadyExist ) { - return svn.add( filePath ); + logger.error( { errorCode: 'NO_EXIST_FILE' } ); + throw { errorCode: 'NO_EXIST_FILE' }; } + return nodefn.call( fs.writeFile, filePath, raw ); + }).then( function() { return svn.commit( filePath ); }); diff --git a/starter.js b/starter.js index af53cad..f80fe9f 100644 --- a/starter.js +++ b/starter.js @@ -12,7 +12,7 @@ const processName = "parallels-comment"; var pm2script = { "name": processName, - "cwd": DEVELOPMENT ? path.join( __dirname,'src') : '', + "cwd": path.join( __dirname, DEVELOPMENT ? 'src' : 'build' ), "args": ["--color"], "script": path.join('bin','www'), @@ -27,11 +27,10 @@ var pm2script = { // Default environment variables that will be injected in any environment and at any start "env": { "PORT": 3015, - "DEVELOPMENT": true, + "DEVELOPMENT": false, "PRODUCTION": false, "DOC_DIR_PATH": path.join( CDN_HOME_DIR, "files" ), - "VIEWS_PATH": path.join( CDN_HOME_DIR, "views" ), "PUBLIC_PATH": path.join( CDN_HOME_DIR, 'public' ) }, From 6b3c7231427e36527e6be40b140f566fd84b79c6 Mon Sep 17 00:00:00 2001 From: Alfred UC Date: Tue, 2 Feb 2016 01:49:20 +0900 Subject: [PATCH 2/5] change project name --- pm2script.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pm2script.json b/pm2script.json index 3b80e41..78d49e0 100644 --- a/pm2script.json +++ b/pm2script.json @@ -1,5 +1,5 @@ { - "name" : "corco-server", + "name" : "paralalles-comments", //"args" : ["--toto=heya coco", "-d", "1"], "cwd" : "src/", "args" : ["--color"], From f39e4c7461389bc2bd51980102f77376baf47513 Mon Sep 17 00:00:00 2001 From: Alfred UC Date: Tue, 2 Feb 2016 01:53:35 +0900 Subject: [PATCH 3/5] version up --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 4bb5c39..6856dff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "corco", - "version": "0.1.0", + "name": "parallels-comments", + "version": "0.2.0", "private": true, "scripts": { "apidoc": "node_modules/apidoc-support-socketio/bin/apidoc-support-socketio -i src -o public/apidoc", From f1a17a057dd74a4411c2bceb899c77061ba8ee4b Mon Sep 17 00:00:00 2001 From: Alfred UC Date: Tue, 2 Feb 2016 02:51:35 +0900 Subject: [PATCH 4/5] Implementation when no svn --- src/controller/doc.js | 18 +++++- src/controller/fileManager/index.js | 97 +++++++++++++++++++++++------ 2 files changed, 92 insertions(+), 23 deletions(-) diff --git a/src/controller/doc.js b/src/controller/doc.js index ebe39da..f49fd56 100644 --- a/src/controller/doc.js +++ b/src/controller/doc.js @@ -12,6 +12,7 @@ * @apiError (errorCode) { string } NO_ERROR success */ +var _ = require('underscore'); var express = require('express'); var router = express.Router(); var log4js = require('log4js'); @@ -26,7 +27,15 @@ var DOC_DIR_PATH = process.env.DOC_DIR_PATH; var fileManager = require('./fileManager/index'); fileManager.initRootPath( DOC_DIR_PATH ); -logger.debug('29'); +function getErrorWillSendClient( err ) { + + err = _.pick( err, 'errorCode' ); + if( _.isEmpty( err ) ) { + err = { errorCode: 'UNKNOWN_ERROR' }; + } + + return err; +} /** * @api {post} /readDocList readDocList @@ -109,7 +118,9 @@ router.post('/createDoc', (req, res, next) => { fileManager.createFile( req.body.fileName, req.body.raw ).then( () => { res.send({ errorCode: 'NO_ERROR' }); }).catch( ( err ) => { - res.send( err ); + + logger.error( err ); + res.send( getErrorWillSendClient( err ) ); }); }); @@ -136,7 +147,8 @@ router.post('/updateDoc', (req, res, next) => { fileManager.updateFile( req.body.fileName, req.body.raw ).then( () => { res.send({ errorCode: 'NO_ERROR' }); }).catch( ( err ) => { - res.send( err ); + logger.error( err ); + res.send( getErrorWillSendClient( err ) ); }); }); diff --git a/src/controller/fileManager/index.js b/src/controller/fileManager/index.js index 27c934f..5d274ea 100644 --- a/src/controller/fileManager/index.js +++ b/src/controller/fileManager/index.js @@ -13,13 +13,32 @@ log4js_extend(log4js, { format: "at @name (@file:@line:@column)" }); var logger = log4js.getLogger(); + const svnOptions = { username: 'corco', password: 'corco', cwd: process.env.CORCO_SVN_PATH }; +let rootPath = ''; +let doUseSvn = false; + const svn = { + info: function( targetPath ) { + + var deferred = when.defer(); + svnUltimate.commands.info( targetPath, ( err, info ) => { + + if( err ) { + deferred.reject( err ); + } + + deferred.resolve( info ); + }); + + return deferred.promise; + }, + add: function( filePath, options ) { var deferred = when.defer(); options = _.extend({}, svnOptions, options ); @@ -58,8 +77,6 @@ const svn = { } }; -let rootPath = ''; - function realPath( relativePath ) { return path.join( rootPath, relativePath ); } @@ -71,42 +88,70 @@ const fileManager = { logger.debug('createFile'); var filePath = realPath( fileName ); - return svn.update().then( () => { + if( doUseSvn ) { + + return svn.update().then( () => { + + var alreadyExist = pathExists.sync( filePath ); + if( alreadyExist ) { + logger.error( { errorCode: 'ALREADY_EXIST' } ); + throw { errorCode: 'ALREADY_EXIST' }; + } + + return nodefn.call( fs.writeFile, filePath, raw ); + + }).then( function() { + + return svn.add( filePath ); + + }).then( function() { + return svn.commit( filePath ); + }); + + } else { var alreadyExist = pathExists.sync( filePath ); if( alreadyExist ) { + var deferred = when.defer(); logger.error( { errorCode: 'ALREADY_EXIST' } ); - throw { errorCode: 'ALREADY_EXIST' }; + deferred.reject({ errorCode: 'ALREADY_EXIST' }); + return deferred.promise; } - return nodefn.call( fs.writeFile, filePath, raw ); - - }).then( function() { - - return svn.add( filePath ); - - }).then( function() { - return svn.commit( filePath ); - }); + } }, updateFile: ( fileName, raw ) => { var filePath = realPath( fileName ); - return svn.update().then( () => { + if( doUseSvn ) { + + return svn.update().then( () => { + + var alreadyExist = pathExists.sync( filePath ); + if( ! alreadyExist ) { + logger.error( { errorCode: 'NO_EXIST_FILE' } ); + throw { errorCode: 'NO_EXIST_FILE' }; + } + + return nodefn.call( fs.writeFile, filePath, raw ); + + }).then( function() { + return svn.commit( filePath ); + }); + + } else { var alreadyExist = pathExists.sync( filePath ); if( ! alreadyExist ) { + var deferred = when.defer(); logger.error( { errorCode: 'NO_EXIST_FILE' } ); - throw { errorCode: 'NO_EXIST_FILE' }; + deferred.reject({ errorCode: 'NO_EXIST_FILE' }); + return deferred.promise; } - return nodefn.call( fs.writeFile, filePath, raw ); - - }).then( function() { - return svn.commit( filePath ); - }); + } }, loadFile: ( fileName ) => { @@ -127,7 +172,19 @@ const fileManager = { initRootPath: ( root ) => { console.log('initRootPath'); rootPath = root; + doUseSvn; + svn.info( rootPath ).then( function( info ) { + + logger.info( info ); + doUseSvn = true; + + }).catch( err => { + + logger.error( err ); + doUseSvn = false; + }); } }; + module.exports = fileManager; From 10427f6aa712ecb2f272f8a1aff28f9f75f8b373 Mon Sep 17 00:00:00 2001 From: Alfred UC Date: Fri, 19 Feb 2016 01:09:08 +0900 Subject: [PATCH 5/5] Fix a issue only load .corco --- src/controller/fileManager/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controller/fileManager/index.js b/src/controller/fileManager/index.js index 5d274ea..4adbbcf 100644 --- a/src/controller/fileManager/index.js +++ b/src/controller/fileManager/index.js @@ -164,7 +164,8 @@ const fileManager = { return nodefn.call( fs.readdir, rootPath ).then( ( fileList ) => { return _.filter( fileList, ( fileName ) => { - if( /\.corco$/.test( fileName )) return true; + if( /^\./.test( fileName )) return false; + return true; }); }); },