From 99194a8f1472c37036867b2b168a5892a92de415 Mon Sep 17 00:00:00 2001 From: Stephane Kamga Date: Thu, 14 Feb 2019 16:05:50 +0100 Subject: [PATCH 1/2] update package.json to fix issue with jest include additional parameter to upload in specific folder add command to create transformations --- config/transform.js | 13 +++++++++++++ config/upload.js | 4 ++-- index.js | 28 +++++++++++++++++++++++++-- package.json | 6 +++++- test/__snapshots__/index.test.js.snap | 2 ++ test/index.test.js | 10 ++++++++++ 6 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 config/transform.js diff --git a/config/transform.js b/config/transform.js new file mode 100644 index 0000000..349ce65 --- /dev/null +++ b/config/transform.js @@ -0,0 +1,13 @@ +const cloudinary = require('./configCloudinary') + +function createTransform (name, parameters, next) { + cloudinary.v2.api.create_transformation(name, parameters, (error, result) => { + if (error) { + return console.log(error) + } else { + console.log(`transformation ${name} created!`) + } + return next + }) +} +module.exports = createTransform diff --git a/config/upload.js b/config/upload.js index 8156a6c..d32c943 100644 --- a/config/upload.js +++ b/config/upload.js @@ -1,6 +1,6 @@ const cloudinary = require('./configCloudinary') -function upload (data, next) { +function upload (data, next, options) { data.forEach(filePath => { cloudinary.uploader.upload(filePath, (result, next) => { if (result.error) { @@ -13,7 +13,7 @@ height: ${result.height} url: ${result.url || result.secure_url}`) } return next - }) + }, options) }) } diff --git a/index.js b/index.js index e3d5203..5e4b73f 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const upload = require('./config/upload') const search = require('./config/search') const update = require('./config/update') const deleteFile = require('./config/delete') +const createTransform = require('./config/transform') const program = require('commander') const {prompt} = require('inquirer') @@ -86,15 +87,20 @@ program .command('upload ') .alias('u') .description('Upload file(s)') + .option('-f, --folder ', 'upload to the following folder on cloudinary') .option('-a, --array', 'Upload more than 1 file') .action((file, options, next) => { + var uploadOptions = {} + if (options.folder) { + uploadOptions.folder = options.folder + } if (options.array) { const array = [] array.push(file, program.args) const files = array.toString().split(',') - upload(files, next) + upload(files, next, uploadOptions) } else { - upload([file], next) + upload([file], next, uploadOptions) } console.log('Uploading...') }) @@ -162,4 +168,22 @@ program }) }) + /** + * Setting command to create a transformation + * @param {String} name:parameters + * e.g. $ cloudTool transform small_fill w_150,h_100,c_fill + */ +program + .command('transform ') + .alias('t') + .description('create a transformation') + .action((name, parameters, next) => { + if (name && parameters) { + createTransform(name, parameters, next) + } else { + console.log('name and parameter should be defined!') + } + console.log('Creating transformation...') + }) + program.parse(process.argv) diff --git a/package.json b/package.json index 150f5c7..a7a2669 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.1", "description": "Manage images from cloudinary.com using your command line.", "bin": { - "cloudTool": "index.js" + "cloudTool": "./index.js" }, "main": "index.js", "scripts": { @@ -18,6 +18,10 @@ "type": "git", "url": "git+https://github.com/knrt10/cloudinary-cli" }, + + "jest": { + "testURL": "http://localhost/" + }, "author": "Kautilya Tripathi ", "license": "ISC", "devDependencies": { diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 4c072cf..64a44fb 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -1,5 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`createTransform successfully create a transformation 1`] = `undefined`; + exports[`delete return error if wrong publicId 1`] = `undefined`; exports[`delete successfully delete file 1`] = `undefined`; diff --git a/test/index.test.js b/test/index.test.js index bd90d10..23d7809 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,6 +3,7 @@ const file = require('../config/file') let upload = require('../config/upload') let update = require('../config/update') let deleteFun = require('../config/delete') +let createTransform = require('../config/transform') describe('env', () => { const data = { @@ -60,3 +61,12 @@ describe('delete', () => { expect(deleteFun(['oldname'])).toMatchSnapshot() }) }) + +describe('createTransform', () => { + it('successfully create a transformation', () => { + createTransform = jest.fn().mockImplementation((name, parameters) => { + console.log('transformation testTransform created!') + }) + expect(createTransform('testTransform', 'w_100,h_200')).toMatchSnapshot() + }) +}) From 0d127f8b02d012b78fa43d375c57dfe613663927 Mon Sep 17 00:00:00 2001 From: Stephane Kamga Date: Thu, 14 Feb 2019 16:37:38 +0100 Subject: [PATCH 2/2] update readme.me with new command --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 11c2ec5..327f9b3 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ Usage: cloudTool [options] [command] list|s [options] Search files and list them rename|r Remane your public_id delete|d [options] Delete your file(s) + transform|t Create a transformation + ``` Run `cloudTool -h` to see particular commands usage @@ -53,6 +55,12 @@ Run `cloudTool -h` to see particular commands usage **Multiple files** - `cloudTool u -a ...` +``` +Options: + + -f, --folder upload to a specific folder on cloudinary + +``` # How to fetch **Help command** @@ -90,3 +98,8 @@ Options: **Multiple files** - `cloudTool d -a ...` + +# How to create a transformation + +`cloudTool transform ` +e.g. `cloudTool transform boximg w_220,h_140,c_fill`