Skip to content

Commit

Permalink
initial work for restructuring project commands
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Nov 22, 2024
1 parent eefb1e0 commit d2b2e6d
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 144 deletions.
6 changes: 1 addition & 5 deletions commands/project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-nocheck
const { addConfigOptions, addAccountOptions } = require('../lib/commonOpts');
const { i18n } = require('../lib/lang');
const { uiBetaTag } = require('../lib/ui');
const deploy = require('./project/deploy');
Expand All @@ -18,13 +17,10 @@ const installDeps = require('./project/installDeps');

const i18nKey = 'commands.project';

exports.command = 'project';
exports.command = ['project', 'projects'];
exports.describe = uiBetaTag(i18n(`${i18nKey}.describe`), false);

exports.builder = yargs => {
addConfigOptions(yargs);
addAccountOptions(yargs);

yargs
.command(create)
.command(add)
Expand Down
28 changes: 14 additions & 14 deletions commands/project/cloneApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ exports.handler = async options => {
}

let appId;
let name;
let location;
let projectName;
let projectDest;
try {
appId = options.appId;
if (!appId) {
Expand All @@ -82,10 +82,10 @@ exports.handler = async options => {
});
appId = appIdResponse.appId;
}
const { name, dest } = await createProjectPrompt('', options, true);

const projectResponse = await createProjectPrompt('', options, true);
name = projectResponse.name;
location = projectResponse.location;
projectName = name;
projectDest = options.dest || dest;
} catch (error) {
logError(error, new ApiErrorContext({ accountId: derivedAccountId }));
process.exit(EXIT_CODES.ERROR);
Expand All @@ -103,7 +103,7 @@ exports.handler = async options => {
const { status } = await poll(checkCloneStatus, derivedAccountId, exportId);
if (status === 'SUCCESS') {
// Ensure correct project folder structure exists
const baseDestPath = path.resolve(getCwd(), location);
const baseDestPath = path.resolve(getCwd(), projectDest);
const absoluteDestPath = path.resolve(baseDestPath, 'src', 'app');
fs.mkdirSync(absoluteDestPath, { recursive: true });

Expand All @@ -114,7 +114,7 @@ exports.handler = async options => {
);
await extractZipArchive(
zippedApp,
sanitizeFileName(name),
sanitizeFileName(projectName),
absoluteDestPath,
{
includesRootDir: true,
Expand All @@ -125,7 +125,7 @@ exports.handler = async options => {
// Create hsproject.json file
const configPath = path.join(baseDestPath, PROJECT_CONFIG_FILE);
const configContent = {
name,
name: projectName,
srcDir: 'src',
platformVersion: '2023.2',
};
Expand All @@ -134,7 +134,7 @@ exports.handler = async options => {
trackCommandMetadataUsage(
'clone-app',
{
type: name,
type: projectName,
assetType: appId,
successful: success,
},
Expand All @@ -153,14 +153,14 @@ exports.handler = async options => {
}
logger.log('');
uiLine();
logger.success(i18n(`${i18nKey}.cloneStatus.success`, { location }));
logger.success(i18n(`${i18nKey}.cloneStatus.success`, { dest }));
logger.log('');
process.exit(EXIT_CODES.SUCCESS);
}
} catch (error) {
trackCommandMetadataUsage(
'clone-app',
{ projectName: name, appId, status: 'FAILURE', error },
{ projectName, appId, status: 'FAILURE', error },
derivedAccountId
);

Expand All @@ -181,11 +181,11 @@ exports.handler = async options => {

exports.builder = yargs => {
yargs.options({
location: {
describe: i18n(`${i18nKey}.options.location.describe`),
dest: {
describe: i18n(`${i18nKey}.options.dest.describe`),
type: 'string',
},
appId: {
'app-id': {
describe: i18n(`${i18nKey}.options.appId.describe`),
type: 'number',
},
Expand Down
10 changes: 5 additions & 5 deletions commands/project/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports.handler = async options => {
githubRef = releaseData.tag_name;
}

const { name, template, location } = await createProjectPrompt(
const { name, template, dest } = await createProjectPrompt(
githubRef,
options
);
Expand All @@ -54,7 +54,7 @@ exports.handler = async options => {
);

await createProjectConfig(
path.resolve(getCwd(), options.location || location),
path.resolve(getCwd(), options.dest || dest),
options.name || name,
template,
options.templateSource,
Expand All @@ -77,15 +77,15 @@ exports.builder = yargs => {
describe: i18n(`${i18nKey}.options.name.describe`),
type: 'string',
},
location: {
describe: i18n(`${i18nKey}.options.location.describe`),
dest: {
describe: i18n(`${i18nKey}.options.dest.describe`),
type: 'string',
},
template: {
describe: i18n(`${i18nKey}.options.template.describe`),
type: 'string',
},
templateSource: {
'template-source': {
describe: i18n(`${i18nKey}.options.templateSource.describe`),
type: 'string',
},
Expand Down
2 changes: 1 addition & 1 deletion commands/project/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ exports.builder = yargs => {
type: 'string',
},
build: {
alias: ['buildId'],
alias: ['build-id'],
describe: i18n(`${i18nKey}.options.build.describe`),
type: 'number',
},
Expand Down
2 changes: 1 addition & 1 deletion commands/project/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const {

const i18nKey = 'commands.project.subcommands.dev';

exports.command = 'dev [--account]';
exports.command = 'dev';
exports.describe = uiBetaTag(i18n(`${i18nKey}.describe`), false);

exports.handler = async options => {
Expand Down
17 changes: 11 additions & 6 deletions commands/project/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
const path = require('path');
const chalk = require('chalk');

const { addUseEnvironmentOptions } = require('../../lib/commonOpts');
const {
addAccountOptions,
addUseEnvironmentOptions,
} = require('../../lib/commonOpts');
const { trackCommandUsage } = require('../../lib/usageTracking');
const { getCwd, sanitizeFileName } = require('@hubspot/local-dev-lib/path');
const { logError, ApiErrorContext } = require('../../lib/errorHandlers/index');
Expand All @@ -23,7 +26,7 @@ const { uiBetaTag } = require('../../lib/ui');
const i18nKey = 'commands.project.subcommands.download';
const { EXIT_CODES } = require('../../lib/enums/exitCodes');

exports.command = 'download [--project]';
exports.command = 'download';
exports.describe = uiBetaTag(i18n(`${i18nKey}.describe`), false);

exports.handler = async options => {
Expand All @@ -36,7 +39,7 @@ exports.handler = async options => {
process.exit(EXIT_CODES.ERROR);
}

const { project, dest, buildNumber, derivedAccountId } = options;
const { project, dest, build, derivedAccountId } = options;
const { project: promptedProjectName } = await downloadProjectPrompt(options);
let projectName = promptedProjectName || project;

Expand Down Expand Up @@ -67,7 +70,7 @@ exports.handler = async options => {

const absoluteDestPath = dest ? path.resolve(getCwd(), dest) : getCwd();

let buildNumberToDownload = buildNumber;
let buildNumberToDownload = build;

if (!buildNumberToDownload) {
const { data: projectBuildsResult } = await fetchProjectBuilds(
Expand Down Expand Up @@ -116,6 +119,7 @@ exports.handler = async options => {
};

exports.builder = yargs => {
addAccountOptions(yargs);
addUseEnvironmentOptions(yargs);

yargs.options({
Expand All @@ -127,8 +131,9 @@ exports.builder = yargs => {
describe: i18n(`${i18nKey}.options.dest.describe`),
type: 'string',
},
buildNumber: {
describe: i18n(`${i18nKey}.options.buildNumber.describe`),
build: {
describe: i18n(`${i18nKey}.options.build.describe`),
alias: ['build-id'],
type: 'number',
},
});
Expand Down
63 changes: 30 additions & 33 deletions commands/project/listBuilds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// @ts-nocheck
const path = require('path');

const {
addAccountOptions,
addConfigOptions,
Expand All @@ -15,7 +13,6 @@ const {
fetchProjectBuilds,
} = require('@hubspot/local-dev-lib/api/projects');
const { getTableContents, getTableHeader } = require('../../lib/ui/table');
const { getCwd } = require('@hubspot/local-dev-lib/path');
const { uiBetaTag, uiLink } = require('../../lib/ui');
const { loadAndValidateOptions } = require('../../lib/validation');
const {
Expand All @@ -29,22 +26,23 @@ const { isHubSpotHttpError } = require('@hubspot/local-dev-lib/errors/index');

const i18nKey = 'commands.project.subcommands.listBuilds';

exports.command = 'list-builds [path]';
exports.command = 'list-builds';
exports.describe = uiBetaTag(i18n(`${i18nKey}.describe`), false);

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

const { path: projectPath, limit, derivedAccountId } = options;
const { project: projectFlagValue, limit, derivedAccountId } = options;

trackCommandUsage('project-list-builds', null, derivedAccountId);

const cwd = projectPath ? path.resolve(getCwd(), projectPath) : getCwd();
const { projectConfig, projectDir } = await getProjectConfig(cwd);

validateProjectConfig(projectConfig, projectDir);
let projectName = projectFlagValue;

logger.debug(`Fetching builds for project at path: ${projectPath}`);
if (!projectName) {
const { projectConfig, projectDir } = await getProjectConfig();
validateProjectConfig(projectConfig, projectDir);
projectName = projectConfig.name;
}

const fetchAndDisplayBuilds = async (project, options) => {
const {
Expand All @@ -53,20 +51,26 @@ exports.handler = async options => {
const currentDeploy = project.deployedBuildId;
if (options && options.after) {
logger.log(
`Showing the next ${results.length} builds for ${project.name}`
i18n(`${i18nKey}.logs.showingNextBuilds`, {
count: results.length,
projectName: project.name,
})
);
} else {
logger.log(
`Showing the ${results.length} most recent builds for ${project.name}. ` +
uiLink(
'View all builds in project details.',
i18n(`${i18nKey}.logs.showingRecentBuilds`, {
count: results.length,
projectName: project.name,
viewBuildsLink: uiLink(
i18n(`${i18nKey}.logs.viewAllBuildsLink`),
getProjectDetailUrl(project.name, derivedAccountId)
)
),
})
);
}

if (results.length === 0) {
logger.log('No builds found.');
logger.log(i18n(`${i18nKey}.errors.noBuilds`));
} else {
const builds = results.map(build => {
const isCurrentlyDeployed = build.buildId === currentDeploy;
Expand Down Expand Up @@ -108,52 +112,45 @@ exports.handler = async options => {
if (paging && paging.next) {
await promptUser({
name: 'more',
message: 'Press <enter> to load more, or ctrl+c to exit',
message: i18n(`${i18nKey}.continueOrExitPrompt`),
});
await fetchAndDisplayBuilds(project, { limit, after: paging.next.after });
}
};

try {
const { data: project } = await fetchProject(
derivedAccountId,
projectConfig.name
);
const { data: project } = await fetchProject(derivedAccountId, projectName);

await fetchAndDisplayBuilds(project, { limit });
} catch (e) {
if (isHubSpotHttpError(e) && e.status === 404) {
logger.error(`Project ${projectConfig.name} not found. `);
logger.error(i18n(`${i18nKey}.errors.projectNotFound`, { projectName }));
} else {
logError(
e,
new ApiErrorContext({
accountId: derivedAccountId,
projectName: projectConfig.name,
projectName,
})
);
}
}
};

exports.builder = yargs => {
yargs.positional('path', {
describe: 'Path to a project folder',
type: 'string',
});

yargs.options({
project: {
describe: i18n(`${i18nKey}.options.project.describe`),
type: 'string',
},
limit: {
describe: 'Max number of builds to load',
describe: i18n(`${i18nKey}.options.limit.describe`),
type: 'string',
},
});

yargs.example([
[
'$0 project list-builds myProjectFolder',
'Fetch a list of builds for a project within the myProjectFolder folder',
],
['$0 project list-builds', i18n(`${i18nKey}.examples.default`)],
]);

addConfigOptions(yargs);
Expand Down
Loading

0 comments on commit d2b2e6d

Please sign in to comment.