Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hs mv command #363

Merged
merged 8 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cms-cli/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const customObjectCommand = require('../commands/customObject');
const functionsCommand = require('../commands/functions');
const listCommand = require('../commands/list');
const openCommand = require('../commands/open');
const mvCommand = require('../commands/mv');

const notifier = updateNotifier({ pkg });

Expand Down Expand Up @@ -72,6 +73,7 @@ const argv = yargs
aliases: 'ls',
})
.command(openCommand)
.command(mvCommand)
.help()
.recommendCommands()
.demandCommand(1, '')
Expand Down
10 changes: 2 additions & 8 deletions packages/cms-cli/commands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
const { trackCommandUsage } = require('../lib/usageTracking');
const { logDebugInfo } = require('../lib/debugInfo');
const { validatePortal } = require('../lib/validation');
const { isPathFolder } = require('../lib/filesystem');
const {
loadConfig,
validateConfig,
Expand All @@ -27,8 +28,6 @@ const {
MARKETPLACE_FOLDER,
} = require('@hubspot/cms-lib/lib/constants');

const FOLDER_DOT_EXTENSIONS = ['functions', 'module'];

const loadAndValidateOptions = async options => {
setLogLevel(options);
logDebugInfo(options);
Expand Down Expand Up @@ -69,12 +68,7 @@ exports.handler = async options => {

if (contentsResp.children.length) {
const mappedContents = contentsResp.children.map(fileOrFolder => {
const splitName = fileOrFolder.split('.');

if (
splitName.length > 1 &&
FOLDER_DOT_EXTENSIONS.indexOf(splitName[1]) === -1
) {
if (!isPathFolder(fileOrFolder)) {
return fileOrFolder;
}

Expand Down
88 changes: 88 additions & 0 deletions packages/cms-cli/commands/mv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const { moveFile } = require('@hubspot/cms-lib/api/fileMapper');
const {
loadConfig,
validateConfig,
checkAndWarnGitInclusion,
} = require('@hubspot/cms-lib');
const { logger } = require('@hubspot/cms-lib/logger');
const {
logApiErrorInstance,
ApiErrorContext,
} = require('@hubspot/cms-lib/errorHandlers');

const {
addConfigOptions,
addPortalOptions,
addUseEnvironmentOptions,
setLogLevel,
getPortalId,
} = require('../lib/commonOpts');
const { logDebugInfo } = require('../lib/debugInfo');
const { validatePortal } = require('../lib/validation');
const { trackCommandUsage } = require('../lib/usageTracking');
const { isPathFolder } = require('../lib/filesystem');

const loadAndValidateOptions = async options => {
setLogLevel(options);
logDebugInfo(options);
const { config: configPath } = options;
loadConfig(configPath, options);
checkAndWarnGitInclusion();

if (!(validateConfig() && (await validatePortal(options)))) {
process.exit(1);
}
};

const getCorrectedDestPath = (srcPath, destPath) => {
if (!isPathFolder(srcPath)) {
return destPath;
}

// Makes sure that nested folders are moved independently
return `${destPath}/${srcPath.split('/').pop()}`;
};

exports.command = 'mv <srcPath> <destPath>';
exports.describe = 'Move a remote file or folder in HubSpot';

exports.handler = async options => {
loadAndValidateOptions(options);

const { srcPath, destPath } = options;
const portalId = getPortalId(options);

trackCommandUsage('mv', {}, portalId);

try {
await moveFile(portalId, srcPath, getCorrectedDestPath(srcPath, destPath));
logger.success(`Moved "${srcPath}" to "${destPath}" in portal ${portalId}`);
} catch (error) {
logger.error(
`Moving "${srcPath}" to "${destPath}" in portal ${portalId} failed`
);
logApiErrorInstance(
error,
new ApiErrorContext({
portalId,
srcPath,
destPath,
})
);
}
};

exports.builder = yargs => {
addConfigOptions(yargs, true);
addPortalOptions(yargs, true);
addUseEnvironmentOptions(yargs, true);
yargs.positional('srcPath', {
describe: 'Remote hubspot path',
type: 'string',
});
yargs.positional('destPath', {
describe: 'Remote hubspot path',
type: 'string',
});
return yargs;
};
17 changes: 17 additions & 0 deletions packages/cms-cli/lib/filesystem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const { getCwd } = require('@hubspot/cms-lib/path');
const { FOLDER_DOT_EXTENSIONS } = require('@hubspot/cms-lib/lib/constants');

function resolveLocalPath(filepath) {
return filepath && typeof filepath === 'string'
Expand All @@ -8,6 +9,22 @@ function resolveLocalPath(filepath) {
getCwd();
}

function isPathFolder(path) {
const splitPath = path.split('/');
const fileOrFolderName = splitPath[splitPath.length - 1];
const splitName = fileOrFolderName.split('.');

if (
splitName.length > 1 &&
FOLDER_DOT_EXTENSIONS.indexOf(splitName[1]) === -1
) {
return false;
}

return true;
}

module.exports = {
resolveLocalPath,
isPathFolder,
};
16 changes: 16 additions & 0 deletions packages/cms-lib/api/fileMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ async function trackUsage(eventName, eventClass, meta = {}, portalId) {
return http.request.post(requestOptions);
}

/**
* Moves file from srcPath to destPath
*
* @async
* @param {number} portalId
* @param {string} srcPath
* @param {string} destPath
* @returns {Promise}
*/
async function moveFile(portalId, srcPath, destPath) {
return http.put(portalId, {
uri: `${FILE_MAPPER_API_PATH}/rename/${srcPath}?path=${destPath}`,
});
}

/**
* Get directory contents
*
Expand All @@ -228,5 +243,6 @@ module.exports = {
trackUsage,
upload,
createFileMapperNodeFromStreamResponse,
moveFile,
getDirectoryContentsByPath,
};
3 changes: 3 additions & 0 deletions packages/cms-lib/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ const SCOPE_GROUPS = {
const HUBSPOT_FOLDER = '@hubspot';
const MARKETPLACE_FOLDER = '@marketplace';

const FOLDER_DOT_EXTENSIONS = ['functions', 'module'];

module.exports = {
Mode,
ENVIRONMENTS,
Expand All @@ -115,4 +117,5 @@ module.exports = {
SCOPE_GROUPS,
HUBSPOT_FOLDER,
MARKETPLACE_FOLDER,
FOLDER_DOT_EXTENSIONS,
};