From 50419306852b2b59e623d4e3c13451e643f905da Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 14 Aug 2020 17:13:27 +0300 Subject: [PATCH] feature(cloudcmd) IO.cp -> IO.copy --- client/dom/io.js | 10 +++++++--- client/dom/rest.js | 2 +- package.json | 2 +- server/fixture-user-menu/io-cp-fix.js | 2 ++ server/fixture-user-menu/io-cp.js | 5 +++++ server/rest/index.js | 2 +- server/user-menu.spec.js | 28 ++++++++++++++++++++++++++- test/fixture/{cp.txt => copy.txt} | 0 test/rest/{cp.js => copy.js} | 8 ++++---- 9 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 server/fixture-user-menu/io-cp-fix.js create mode 100644 server/fixture-user-menu/io-cp.js rename test/fixture/{cp.txt => copy.txt} (100%) rename test/rest/{cp.js => copy.js} (79%) diff --git a/client/dom/io.js b/client/dom/io.js index a93836de..e5d50b14 100644 --- a/client/dom/io.js +++ b/client/dom/io.js @@ -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, }); }; diff --git a/client/dom/rest.js b/client/dom/rest.js index 5ac87d8d..ad0e57ea 100644 --- a/client/dom/rest.js +++ b/client/dom/rest.js @@ -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); diff --git a/package.json b/package.json index 09fecce3..9bd6101a 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.0.0", + "@putout/plugin-cloudcmd": "^1.1.0", "apart": "^2.0.0", "chalk": "^4.0.0", "compression": "^1.7.4", diff --git a/server/fixture-user-menu/io-cp-fix.js b/server/fixture-user-menu/io-cp-fix.js new file mode 100644 index 00000000..8ce51b3b --- /dev/null +++ b/server/fixture-user-menu/io-cp-fix.js @@ -0,0 +1,2 @@ +'use strict'; +await IO.copy(dirPath, mp3Dir, mp3Names); diff --git a/server/fixture-user-menu/io-cp.js b/server/fixture-user-menu/io-cp.js new file mode 100644 index 00000000..5b3e0fa4 --- /dev/null +++ b/server/fixture-user-menu/io-cp.js @@ -0,0 +1,5 @@ +await IO.cp({ + from: dirPath, + to: mp3Dir, + names: mp3Names, +}); diff --git a/server/rest/index.js b/server/rest/index.js index 7e6a644f..0d6c0c7a 100644 --- a/server/rest/index.js +++ b/server/rest/index.js @@ -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); diff --git a/server/user-menu.spec.js b/server/user-menu.spec.js index f74efeec..eba01007 100644 --- a/server/user-menu.spec.js +++ b/server/user-menu.spec.js @@ -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(); +}); + diff --git a/test/fixture/cp.txt b/test/fixture/copy.txt similarity index 100% rename from test/fixture/cp.txt rename to test/fixture/copy.txt diff --git a/test/rest/cp.js b/test/rest/copy.js similarity index 79% rename from test/rest/cp.js rename to test/rest/copy.js index e3bce034..8d41e566 100644 --- a/test/rest/cp.js +++ b/test/rest/copy.js @@ -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(); });