diff --git a/HELP.md b/HELP.md index 3551a119..8ff76d39 100644 --- a/HELP.md +++ b/HELP.md @@ -547,6 +547,7 @@ Here you can find `API` that can be used in **User Menu**. **DOM** and **CloudCm - `rename(from, to)` - rename `from` into `to` - `move(from, to, names)` - rename files with a `names` `from` into `to`; - `copy(from, to, names)` - copy files with a `names` `from` into `to`; +- `createDirectory(path)` - create directory with a `path`; ### Distribute diff --git a/client/dom/index.js b/client/dom/index.js index 9113ffe4..3a095892 100644 --- a/client/dom/index.js +++ b/client/dom/index.js @@ -69,8 +69,8 @@ function CmdProto() { * create new folder * */ - this.promptNewDir = function() { - promptNew('directory', '?dir'); + this.promptNewDir = function() { + promptNew('directory'); }; /** @@ -83,7 +83,7 @@ function CmdProto() { promptNew('file'); }; - async function promptNew(typeName, type) { + async function promptNew(typeName) { const {Dialog} = DOM; const dir = DOM.getCurrentDirPath(); const msg = 'New ' + typeName || 'File'; @@ -97,22 +97,18 @@ function CmdProto() { }; const name = getName(); - const [cancel, currentName] = await Dialog.prompt(msg, name); if (cancel) return; - const path = (type) => { - const result = dir + currentName; - - if (!type) - return result; - - return result + type; - }; + const path = `${dir}${currentName}`; + + if (typeName === 'directory') + await RESTful.createDirectory(path); + else + await RESTful.write(path); - await RESTful.write(path(type)); await CloudCmd.refresh({ currentName, }); diff --git a/client/dom/io.js b/client/dom/io/index.js similarity index 68% rename from client/dom/io.js rename to client/dom/io/index.js index e5d50b14..1219631a 100644 --- a/client/dom/io.js +++ b/client/dom/io/index.js @@ -1,27 +1,12 @@ 'use strict'; -/* global CloudCmd */ - -const {promisify} = require('es6-promisify'); - -const {FS} = require('../../common/cloudfunc'); - -const Images = require('./images'); -const load = require('./load'); +const {FS} = require('../../../common/cloudfunc'); +const sendRequest = require('./send-request'); const imgPosition = { top: true, }; -module.exports._replaceHash = replaceHash; -function replaceHash(url) { - /* - * if we send ajax request - - * no need in hash so we escape # - */ - return url.replace(/#/g, '%23'); -} - module.exports.delete = async (url, data) => { return await sendRequest({ method : 'DELETE', @@ -51,6 +36,14 @@ module.exports.write = async (url, data) => { }); }; +module.exports.createDirectory = async (url) => { + return await sendRequest({ + method: 'PUT', + url: `${FS}${url}?dir`, + imgPosition, + }); +}; + module.exports.read = async (url, dataType = 'text') => { const notLog = !url.includes('?'); @@ -157,40 +150,3 @@ module.exports.Markdown = { }, }; -const sendRequest = promisify((params, callback) => { - const p = params; - const {prefixURL} = CloudCmd; - - p.url = prefixURL + p.url; - p.url = encodeURI(p.url); - - p.url = replaceHash(p.url); - - load.ajax({ - method : p.method, - url : p.url, - data : p.data, - dataType : p.dataType, - error : (jqXHR) => { - const response = jqXHR.responseText; - - const { - statusText, - status, - } = jqXHR; - - const text = status === 404 ? response : statusText; - - callback(Error(text)); - }, - success: (data) => { - Images.hide(); - - if (!p.notLog) - CloudCmd.log(data); - - callback(null, data); - }, - }); -}); - diff --git a/client/dom/io/index.spec.js b/client/dom/io/index.spec.js new file mode 100644 index 00000000..ffe6ad4f --- /dev/null +++ b/client/dom/io/index.spec.js @@ -0,0 +1,27 @@ +'use strict'; + +const test = require('supertape'); +const stub = require('@cloudcmd/stub'); +const mockRequire = require('mock-require'); + +const {reRequire} = mockRequire; + +test('client: dom: io', (t) => { + const sendRequest = stub(); + mockRequire('./send-request', sendRequest); + + const io = reRequire('.'); + + io.createDirectory('/hello'); + + const expected = { + imgPosition: { + top: true, + }, + method: 'PUT', + url: '/fs/hello?dir', + }; + + t.ok(sendRequest.calledWith(expected)); + t.end(); +}); diff --git a/client/dom/io/send-request.js b/client/dom/io/send-request.js new file mode 100644 index 00000000..f3307764 --- /dev/null +++ b/client/dom/io/send-request.js @@ -0,0 +1,55 @@ +'use strict'; + +/* global CloudCmd */ + +const {promisify} = require('es6-promisify'); + +const Images = require('../images'); +const load = require('../load'); + +module.exports = promisify((params, callback) => { + const p = params; + const {prefixURL} = CloudCmd; + + p.url = prefixURL + p.url; + p.url = encodeURI(p.url); + + p.url = replaceHash(p.url); + + load.ajax({ + method : p.method, + url : p.url, + data : p.data, + dataType : p.dataType, + error : (jqXHR) => { + const response = jqXHR.responseText; + + const { + statusText, + status, + } = jqXHR; + + const text = status === 404 ? response : statusText; + + callback(Error(text)); + }, + success: (data) => { + Images.hide(); + + if (!p.notLog) + CloudCmd.log(data); + + callback(null, data); + }, + }); +}); + +module.exports._replaceHash = replaceHash; +function replaceHash(url) { + /* + * if we send ajax request - + * no need in hash so we escape # + */ + return url.replace(/#/g, '%23'); +} + diff --git a/client/dom/io.spec.js b/client/dom/io/send-request.spec.js similarity index 83% rename from client/dom/io.spec.js rename to client/dom/io/send-request.spec.js index c73efbac..bd14aa2e 100644 --- a/client/dom/io.spec.js +++ b/client/dom/io/send-request.spec.js @@ -1,10 +1,9 @@ 'use strict'; const test = require('supertape'); -const io = require('./io'); +const {_replaceHash} = require('./send-request'); test('cloudcmd: client: io: replaceHash', (t) => { - const {_replaceHash} = io; const url = '/hello/####world'; const result = _replaceHash(url); const expected = '/hello/%23%23%23%23world'; diff --git a/client/dom/rest.js b/client/dom/rest.js index ad0e57ea..9ce9c302 100644 --- a/client/dom/rest.js +++ b/client/dom/rest.js @@ -25,6 +25,7 @@ const handleError = (promise) => async (...args) => { module.exports.delete = handleError(IO.delete); module.exports.patch = handleError(IO.patch); module.exports.write = handleError(IO.write); +module.exports.createDirectory = handleError(IO.createDirectory); module.exports.read = handleError(IO.read); module.exports.copy = handleError(IO.copy); module.exports.pack = handleError(IO.pack); diff --git a/package.json b/package.json index 6432aade..eaa4784b 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,7 @@ "@cloudcmd/fileop": "^4.0.0", "@cloudcmd/move-files": "^3.0.0", "@cloudcmd/read-files-sync": "^2.0.0", - "@putout/plugin-cloudcmd": "^1.1.0", + "@putout/plugin-cloudcmd": "^1.2.0", "apart": "^2.0.0", "chalk": "^4.0.0", "compression": "^1.7.4",