diff --git a/.gitignore b/.gitignore index 12255bfd..c6f29842 100644 --- a/.gitignore +++ b/.gitignore @@ -19,9 +19,6 @@ modules/jquery-mousewheel modules/execon modules/emitify -legacy -server_ - .nyc_output *.swp diff --git a/.npmignore b/.npmignore index b3951b1a..2ae7e11d 100644 --- a/.npmignore +++ b/.npmignore @@ -28,8 +28,5 @@ bin/legacy.js client -legacy/bin/release.js -legacy/bin/legacy.js - webpack.config.js diff --git a/bin/legacy.js b/bin/legacy.js deleted file mode 100755 index 890a56a0..00000000 --- a/bin/legacy.js +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env node - -'use strict'; - -const fs = require('fs'); -const path = require('path'); - -const dir = path.join(__dirname, '../server'); -const dir_ = path.join(__dirname, '../legacy/server'); - -const setDir = (name) => { - return path.join(dir_, name); -}; - -fs.readdirSync(dir) - .map(fillFile) - .map(writeFile); - -function fillFile(name) { - return { - name: setDir(name), - data: `module.exports = require('../../server_/${name}');` - }; -} - -function writeFile({name, data}) { - return fs.writeFileSync(name, data); -} - diff --git a/common/cloudfunc.js b/common/cloudfunc.js index e4b9f994..fb262c0e 100644 --- a/common/cloudfunc.js +++ b/common/cloudfunc.js @@ -1,4 +1,231 @@ 'use strict'; -module.exports = require('../server/cloudfunc'); +const rendy = require('rendy'); +const store = require('fullstore/legacy'); +const Entity = require('./entity'); + +/* КОНСТАНТЫ (общие для клиента и сервера)*/ + +/* название программы */ +const NAME = 'Cloud Commander'; +const FS = '/fs'; + +const Path = store(); + +Path('/'); + +module.exports.FS = FS; +module.exports.apiURL = '/api/v1'; +module.exports.MAX_FILE_SIZE = 500 * 1024; +module.exports.Entity = Entity; + +module.exports.formatMsg = (msg, name, status) => { + status = status || 'ok'; + + if (name) + name = '("' + name + '")'; + else + name = ''; + + return msg + ': ' + status + name; +}; + +/** + * Функция возвращает заголовок веб страницы + * @path + */ +module.exports.getTitle = (path) => { + return NAME + ' - ' + (path || Path()); +}; + +/** Функция получает адреса каждого каталога в пути + * возвращаеться массив каталогов + * @param url - адрес каталога + */ +function getPathLink(url, prefix, template) { + if (!url) + throw Error('url could not be empty!'); + + if (!template) + throw Error('template could not be empty!'); + + const names = url + .split('/') + .slice(1, -1); + + const allNames = ['/', ...names]; + const length = allNames.length - 1; + + let path = '/'; + + const pathHTML = allNames.map((name, index) => { + const isLast = index === length; + + if (index) + path += name + '/'; + + if (index && isLast) + return name + '/'; + + const slash = index ? '/' : ''; + + return rendy(template, { + path, + name, + slash, + prefix, + }); + }).join(''); + + return pathHTML; +} + +/** + * Функция строит таблицу файлв из JSON-информации о файлах + * @param params - информация о файлах + * + */ +module.exports.buildFromJSON = (params) => { + const prefix = params.prefix; + const template = params.template; + const templateFile = template.file; + const templateLink = template.link; + const json = params.data; + + const {path, files} = json; + + const sort = params.sort || 'name'; + const order = params.order || 'asc'; + + /* + * Строим путь каталога в котором мы находимся + * со всеми подкаталогами + */ + const htmlPath = getPathLink(path, prefix, template.pathLink); + + let fileTable = rendy(template.path, { + link : prefix + FS + path, + fullPath : path, + path : htmlPath + }); + + let name = 'name'; + let size = 'size'; + let date = 'date'; + const owner = 'owner'; + const mode = 'mode'; + const arrow = order === 'asc' ? '↑' : '↓'; + + if (sort === 'name' && order !== 'asc') + name += arrow; + else if (sort === 'size') + size += arrow; + else if (sort === 'date') + date += arrow; + + const header = rendy(templateFile, { + tag : 'div', + attribute : 'data-name="js-fm-header" ', + className : 'fm-header', + type : '', + name, + size, + date, + owner, + mode, + }); + + /* сохраняем путь */ + Path(path); + + fileTable += header + ''; + + return fileTable; +}; + +function getType(size) { + if (size === 'dir') + return 'directory'; + + return 'text-file'; +} + +function getAttribute(size) { + if (size === 'dir') + return ''; + + return 'target="_blank" '; +} + +function getSize(size) { + if (size === 'dir') + return '<dir>'; + + return size; +} diff --git a/common/entity.js b/common/entity.js index 0aa9ab40..8472a1bd 100644 --- a/common/entity.js +++ b/common/entity.js @@ -1,4 +1,32 @@ 'use strict'; -module.exports = require('../server/common/entity'); +const Entities = { + ' ': ' ', + '<': '<', + '>': '>', +}; + +const keys = Object.keys(Entities); + +module.exports.encode = (str) => { + keys.forEach((code) => { + const char = Entities[code]; + const reg = RegExp(char, 'g'); + + str = str.replace(reg, code); + }); + + return str; +}; + +module.exports.decode = (str) => { + keys.forEach((code) => { + const char = Entities[code]; + const reg = RegExp(code, 'g'); + + str = str.replace(reg, char); + }); + + return str; +}; diff --git a/package.json b/package.json index 974e4d0f..5ac16190 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,7 @@ "server" ], "bin": { - "cloudcmd": "legacy/bin/cloudcmd.js", - "cloudcmd+": "bin/cloudcmd.js" + "cloudcmd": "bin/cloudcmd.js" }, "config": { "dirs": "bin server test webpack.config.js" @@ -80,8 +79,6 @@ "docker:rm-old": "redrun --parallel docker:rm:*", "coverage": "nyc npm test", "report": "nyc report --reporter=text-lcov | coveralls", - "6to5:bin": "babel bin -d legacy/bin", - "6to5:server": "babel server -d server_", "6to5:client": "webpack --progress", "6to5:client:dev": "NODE_ENV=development redrun 6to5:client", "watch:client": "redrun 6to5:client -- --watch", @@ -93,16 +90,9 @@ "watch:coverage": "nodemon -w server -w test -w common -x \"npm run coverage\"", "w:c": "redrun watch:client", "w:c:d": "redrun watch:client:dev", - "build": "redrun rm:* 6to5:* mkdir:* legacy:*", + "build": "redrun 6to5:*", "build:client": "redrun 6to5:client", "build:client:dev": "redrun 6to5:client:dev", - "legacy:package": "echo \"module.exports = require('../package');\" > legacy/package.js", - "mkdir:server": "mkdirp legacy/server", - "mkdir:json": "mkdirp legacy/json", - "legacy:server": "bin/legacy.js", - "legacy:help": "cp json/help.json legacy/json/", - "rm:server": "rimraf server_ legacy", - "rm:client": "rimraf dist dist-dev", "heroku-postbuild": "redrun 6to5:client", "postheroku-postbuild": "npm i gritty" }, @@ -205,8 +195,8 @@ "yaspeller": "^3.0.0" }, "engines": { - "node": ">=0.10" + "node": ">=4.0.0" }, "license": "MIT", - "main": "legacy/server/cloudcmd.js" + "main": "server/cloudcmd.js" } diff --git a/server/cloudcmd.js b/server/cloudcmd.js index 68a8f7ce..4ab85022 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -2,8 +2,9 @@ const DIR = __dirname + '/'; const DIR_ROOT = DIR + '../'; +const DIR_COMMON = DIR + '../common/'; -const cloudfunc = require(DIR + 'cloudfunc'); +const cloudfunc = require(DIR_COMMON + 'cloudfunc'); const auth = require(DIR + 'auth'); const config = require(DIR + 'config'); const modulas = require(DIR + 'modulas'); diff --git a/server/cloudfunc.js b/server/cloudfunc.js deleted file mode 100644 index 93af81e8..00000000 --- a/server/cloudfunc.js +++ /dev/null @@ -1,231 +0,0 @@ -'use strict'; - -const rendy = require('rendy'); -const store = require('fullstore/legacy'); -const Entity = require('./common/entity'); - -/* КОНСТАНТЫ (общие для клиента и сервера)*/ - -/* название программы */ -const NAME = 'Cloud Commander'; -const FS = '/fs'; - -const Path = store(); - -Path('/'); - -module.exports.FS = FS; -module.exports.apiURL = '/api/v1'; -module.exports.MAX_FILE_SIZE = 500 * 1024; -module.exports.Entity = Entity; - -module.exports.formatMsg = (msg, name, status) => { - status = status || 'ok'; - - if (name) - name = '("' + name + '")'; - else - name = ''; - - return msg + ': ' + status + name; -}; - -/** - * Функция возвращает заголовок веб страницы - * @path - */ -module.exports.getTitle = (path) => { - return NAME + ' - ' + (path || Path()); -}; - -/** Функция получает адреса каждого каталога в пути - * возвращаеться массив каталогов - * @param url - адрес каталога - */ -function getPathLink(url, prefix, template) { - if (!url) - throw Error('url could not be empty!'); - - if (!template) - throw Error('template could not be empty!'); - - const names = url - .split('/') - .slice(1, -1); - - const allNames = ['/', ...names]; - const length = allNames.length - 1; - - let path = '/'; - - const pathHTML = allNames.map((name, index) => { - const isLast = index === length; - - if (index) - path += name + '/'; - - if (index && isLast) - return name + '/'; - - const slash = index ? '/' : ''; - - return rendy(template, { - path, - name, - slash, - prefix, - }); - }).join(''); - - return pathHTML; -} - -/** - * Функция строит таблицу файлв из JSON-информации о файлах - * @param params - информация о файлах - * - */ -module.exports.buildFromJSON = (params) => { - const prefix = params.prefix; - const template = params.template; - const templateFile = template.file; - const templateLink = template.link; - const json = params.data; - - const {path, files} = json; - - const sort = params.sort || 'name'; - const order = params.order || 'asc'; - - /* - * Строим путь каталога в котором мы находимся - * со всеми подкаталогами - */ - const htmlPath = getPathLink(path, prefix, template.pathLink); - - let fileTable = rendy(template.path, { - link : prefix + FS + path, - fullPath : path, - path : htmlPath - }); - - let name = 'name'; - let size = 'size'; - let date = 'date'; - const owner = 'owner'; - const mode = 'mode'; - const arrow = order === 'asc' ? '↑' : '↓'; - - if (sort === 'name' && order !== 'asc') - name += arrow; - else if (sort === 'size') - size += arrow; - else if (sort === 'date') - date += arrow; - - const header = rendy(templateFile, { - tag : 'div', - attribute : 'data-name="js-fm-header" ', - className : 'fm-header', - type : '', - name, - size, - date, - owner, - mode, - }); - - /* сохраняем путь */ - Path(path); - - fileTable += header + ''; - - return fileTable; -}; - -function getType(size) { - if (size === 'dir') - return 'directory'; - - return 'text-file'; -} - -function getAttribute(size) { - if (size === 'dir') - return ''; - - return 'target="_blank" '; -} - -function getSize(size) { - if (size === 'dir') - return '<dir>'; - - return size; -} - diff --git a/server/common/entity.js b/server/common/entity.js deleted file mode 100644 index 8472a1bd..00000000 --- a/server/common/entity.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict'; - -const Entities = { - ' ': ' ', - '<': '<', - '>': '>', -}; - -const keys = Object.keys(Entities); - -module.exports.encode = (str) => { - keys.forEach((code) => { - const char = Entities[code]; - const reg = RegExp(char, 'g'); - - str = str.replace(reg, code); - }); - - return str; -}; - -module.exports.decode = (str) => { - keys.forEach((code) => { - const char = Entities[code]; - const reg = RegExp(code, 'g'); - - str = str.replace(reg, char); - }); - - return str; -}; - diff --git a/server/config.js b/server/config.js index e085419f..9e0d7246 100644 --- a/server/config.js +++ b/server/config.js @@ -1,13 +1,14 @@ 'use strict'; const DIR_SERVER = __dirname + '/'; +const DIR_COMMON = '../common/'; const DIR = DIR_SERVER + '../'; const path = require('path'); const fs = require('fs'); const exit = require(DIR_SERVER + 'exit'); -const CloudFunc = require(DIR_SERVER + 'cloudfunc'); +const CloudFunc = require(DIR_COMMON + 'cloudfunc'); const pullout = require('pullout/legacy'); const ponse = require('ponse'); diff --git a/server/rest.js b/server/rest.js index fe8370e5..0b80f717 100644 --- a/server/rest.js +++ b/server/rest.js @@ -1,11 +1,12 @@ 'use strict'; const DIR = './'; +const DIR_COMMON = '../common/'; const path = require('path'); const root = require(DIR + 'root'); const config = require(DIR + 'config'); -const CloudFunc = require(DIR + 'cloudfunc'); +const CloudFunc = require(DIR_COMMON + 'cloudfunc'); const markdown = require(DIR + 'markdown'); const jaguar = require('jaguar/legacy'); diff --git a/server/route.js b/server/route.js index f548abac..faad57eb 100644 --- a/server/route.js +++ b/server/route.js @@ -4,6 +4,7 @@ const DIR = __dirname + '/../'; const DIR_TMPL = DIR + 'tmpl/'; const DIR_HTML = DIR + 'html/'; const DIR_SERVER = './'; +const DIR_COMMON = '../common/'; const DIR_FS = DIR_TMPL + 'fs/'; const fs = require('fs'); @@ -20,7 +21,7 @@ const apart = require('apart'); const config = require(DIR_SERVER + 'config'); const root = require(DIR_SERVER + 'root'); const prefixer = require(DIR_SERVER + 'prefixer'); -const CloudFunc = require(DIR_SERVER + 'cloudfunc'); +const CloudFunc = require(DIR_COMMON + 'cloudfunc'); const prefix = squad(prefixer, apart(config, 'prefix')); const isDev = process.env.NODE_ENV === 'development';