mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
feature(cloudcmd) add IO.createDirectory (b54b5486f4)
This commit is contained in:
parent
92e0e4bb91
commit
0f93c4fa1b
8 changed files with 105 additions and 70 deletions
1
HELP.md
1
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
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
27
client/dom/io/index.spec.js
Normal file
27
client/dom/io/index.spec.js
Normal file
|
|
@ -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();
|
||||
});
|
||||
55
client/dom/io/send-request.js
Normal file
55
client/dom/io/send-request.js
Normal file
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
@ -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';
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue