Skip to content

Commit

Permalink
Merge pull request #564 from HubSpot/improve-hs-upload
Browse files Browse the repository at this point in the history
Roll hs init step into hs project upload
  • Loading branch information
anthmatic authored Sep 29, 2021
2 parents 3646c12 + 370500c commit 04215c1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
6 changes: 1 addition & 5 deletions packages/cli/commands/project/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ exports.handler = async options => {
if (e.statusCode === 400) {
logger.error(e.error.message);
} else {
logApiErrorInstance(
accountId,
e,
new ApiErrorContext({ accountId, projectPath })
);
logApiErrorInstance(e, new ApiErrorContext({ accountId, projectPath }));
}
}
};
Expand Down
26 changes: 17 additions & 9 deletions packages/cli/commands/project/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const {
getProjectConfig,
validateProjectConfig,
pollBuildStatus,
ensureProjectExists,
} = require('../../lib/projects');

const loadAndValidateOptions = async options => {
Expand Down Expand Up @@ -83,12 +84,13 @@ const uploadProjectFiles = async (accountId, projectName, filePath) => {
)} project files to ${chalk.bold(accountId)}`,
});

logApiErrorInstance(err, {
context: new ApiErrorContext({
logApiErrorInstance(
err,
new ApiErrorContext({
accountId,
projectName,
}),
});
})
);
}
};

Expand All @@ -100,10 +102,14 @@ exports.handler = async options => {

trackCommandUsage('project-upload', { projectPath }, accountId);

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

validateProjectConfig(projectConfig, projectDir);

validateProjectConfig(projectConfig);
await ensureProjectExists(accountId, projectConfig.name);

const tempFile = tmp.fileSync({ postfix: '.zip' });

Expand Down Expand Up @@ -131,8 +137,10 @@ exports.handler = async options => {

archive.pipe(output);

archive.directory(path.resolve(cwd, projectConfig.srcDir), false, file =>
shouldIgnoreFile(file.name) ? false : file
archive.directory(
path.resolve(projectDir, projectConfig.srcDir),
false,
file => (shouldIgnoreFile(file.name) ? false : file)
);

archive.finalize();
Expand Down
47 changes: 44 additions & 3 deletions packages/cli/lib/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ const {
PROJECT_DEPLOY_STATUS,
PROJECT_DEPLOY_STATUS_TEXT,
} = require('@hubspot/cli-lib/lib/constants');
const { getBuildStatus, getDeployStatus } = require('@hubspot/cli-lib/api/dfs');
const {
getBuildStatus,
getDeployStatus,
fetchProject,
createProject,
} = require('@hubspot/cli-lib/api/dfs');
const {
logApiErrorInstance,
ApiErrorContext,
} = require('@hubspot/cli-lib/errorHandlers');

const isBuildComplete = build => {
return (
Expand Down Expand Up @@ -88,7 +97,7 @@ const getOrCreateProjectConfig = async projectPath => {
return projectConfig;
};

const validateProjectConfig = projectConfig => {
const validateProjectConfig = (projectConfig, projectDir) => {
if (!projectConfig) {
logger.error(
`Project config not found. Try running 'hs project init' first.`
Expand All @@ -103,14 +112,45 @@ const validateProjectConfig = projectConfig => {
process.exit(1);
}

if (!fs.existsSync(projectConfig.srcDir)) {
if (!fs.existsSync(path.resolve(projectDir, projectConfig.srcDir))) {
logger.error(
`Project source directory '${projectConfig.srcDir}' does not exist.`
);
process.exit(1);
}
};

const ensureProjectExists = async (accountId, projectName) => {
try {
await fetchProject(accountId, projectName);
} catch (err) {
if (err.statusCode === 404) {
const { shouldCreateProject } = await prompt([
{
name: 'shouldCreateProject',
message: `The project ${projectName} does not exist in ${accountId}. Would you like to create it?`,
type: 'confirm',
},
]);

if (shouldCreateProject) {
try {
return createProject(accountId, projectName);
} catch (err) {
return logApiErrorInstance(err, new ApiErrorContext({ accountId }));
}
} else {
return logger.log(
`Your project ${chalk.bold(
projectName
)} could not be found in ${chalk.bold(accountId)}.`
);
}
}
logApiErrorInstance(err, new ApiErrorContext({ accountId }));
}
};

const getProjectDetailUrl = (projectName, accountId) => {
if (!projectName) return;

Expand Down Expand Up @@ -335,4 +375,5 @@ module.exports = {
showWelcomeMessage,
pollBuildStatus,
pollDeployStatus,
ensureProjectExists,
};

0 comments on commit 04215c1

Please sign in to comment.