feature(cloudcmd) IO.cp -> IO.copy

This commit is contained in:
coderaiser 2020-08-14 17:13:27 +03:00
parent 13307f3861
commit 5041930685
9 changed files with 48 additions and 11 deletions

View file

@ -62,11 +62,15 @@ module.exports.read = async (url, dataType = 'text') => {
});
};
module.exports.cp = async (data) => {
module.exports.copy = async (from, to, names) => {
return await sendRequest({
method: 'PUT',
url: '/cp',
data,
url: '/copy',
data: {
from,
to,
names,
},
imgPosition,
});
};

View file

@ -26,7 +26,7 @@ module.exports.delete = handleError(IO.delete);
module.exports.patch = handleError(IO.patch);
module.exports.write = handleError(IO.write);
module.exports.read = handleError(IO.read);
module.exports.cp = handleError(IO.cp);
module.exports.copy = handleError(IO.copy);
module.exports.pack = handleError(IO.pack);
module.exports.extract = handleError(IO.extract);
module.exports.move = handleError(IO.move);

View file

@ -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.0.0",
"@putout/plugin-cloudcmd": "^1.1.0",
"apart": "^2.0.0",
"chalk": "^4.0.0",
"compression": "^1.7.4",

View file

@ -0,0 +1,2 @@
'use strict';
await IO.copy(dirPath, mp3Dir, mp3Names);

View file

@ -0,0 +1,5 @@
await IO.cp({
from: dirPath,
to: mp3Dir,
names: mp3Names,
});

View file

@ -219,7 +219,7 @@ function onPUT({name, config, body}, callback) {
} case 'rename':
return rename(rootDir, files.from, files.to, callback);
case 'cp':
case 'copy':
if (!files.from || !files.names || !files.to)
return callback(body);

View file

@ -20,9 +20,13 @@ const userMenuFile = readFileSync(userMenuPath, 'utf8');
const fixtureDir = join(__dirname, 'fixture-user-menu');
const fixtureMoveName = join(fixtureDir, 'io-mv.js');
const fixtureMoveFixName = join(fixtureDir, 'io-mv-fix.js');
const fixtureCopyName = join(fixtureDir, 'io-cp.js');
const fixtureCopyFixName = join(fixtureDir, 'io-cp-fix.js');
const fixtureMove = readFileSync(fixtureMoveName, 'utf8');
const fixtureMoveFix = readFileSync(fixtureMoveFixName, 'utf8');
const fixtureCopy = readFileSync(fixtureCopyName, 'utf8');
const fixtureCopyFix = readFileSync(fixtureCopyFixName, 'utf8');
test('cloudcmd: user menu', async (t) => {
const options = {
@ -39,7 +43,7 @@ test('cloudcmd: user menu', async (t) => {
t.end();
});
test.only('cloudcmd: user menu: io.mv', async (t) => {
test('cloudcmd: user menu: io.mv', async (t) => {
const options = {
menuName: '.cloudcmd.menu.js',
};
@ -61,3 +65,25 @@ test.only('cloudcmd: user menu: io.mv', async (t) => {
t.end();
});
test('cloudcmd: user menu: io.cp', async (t) => {
const options = {
menuName: '.cloudcmd.menu.js',
};
const {readFile} = fs.promises;
fs.promises.readFile = stub().returns(fixtureCopy);
const userMenu = reRequire('./user-menu');
const {request} = serveOnce(userMenu);
const {body} = await request.get(`/api/v1/user-menu?dir=${__dirname}`, {
options,
});
threadIt.terminate();
fs.promises.readFile = readFile;
t.equal(fixtureCopyFix, body, 'should equal');
t.end();
});

View file

@ -19,25 +19,25 @@ const {request} = require('serve-once')(cloudcmd, {
configManager,
});
test('cloudcmd: rest: cp', async (t) => {
test('cloudcmd: rest: copy', async (t) => {
const tmp = join(fixtureDir, 'tmp');
const files = {
from: '/fixture/',
to: '/fixture/tmp',
names: [
'cp.txt',
'copy.txt',
],
};
mkdirSync(tmp);
const {body} = await request.put(`/api/v1/cp`, {
const {body} = await request.put(`/api/v1/copy`, {
body: files,
});
rimraf.sync(tmp);
t.equal(body, 'copy: ok("["cp.txt"]")', 'should return result');
t.equal(body, 'copy: ok("["copy.txt"]")', 'should return result');
t.end();
});