Skip to content

Commit

Permalink
Move upload functionality to lib/projects/upload
Browse files Browse the repository at this point in the history
  • Loading branch information
camden11 committed Dec 18, 2024
1 parent 3969141 commit b60f8b5
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 214 deletions.
2 changes: 1 addition & 1 deletion commands/project/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const { loadAndValidateOptions } = require('../../lib/validation');
const {
ensureProjectExists,
getProjectConfig,
handleProjectUpload,
logFeedbackMessage,
validateProjectConfig,
} = require('../../lib/projects');
const { handleProjectUpload } = require('../../lib/projects/upload');
const {
displayWarnLogs,
pollProjectBuildAndDeploy,
Expand Down
2 changes: 1 addition & 1 deletion commands/project/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const { uiBetaTag } = require('../../lib/ui');
const {
ensureProjectExists,
getProjectConfig,
handleProjectUpload,
validateProjectConfig,
logFeedbackMessage,
} = require('../../lib/projects');
const { handleProjectUpload } = require('../../lib/projects/upload');
const {
pollBuildStatus,
pollDeployStatus,
Expand Down
21 changes: 11 additions & 10 deletions lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -1094,16 +1094,6 @@ en:
checkIfParentAccountIsAuthed:
notAuthedError: "To develop this project locally, run {{ authCommand }} to authenticate the App Developer Account {{ accountId }} associated with {{ accountIdentifier }}."
projects:
uploadProjectFiles:
add: "Uploading {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
fail: "Failed to upload {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
succeed: "Uploaded {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
buildCreated: "Project \"{{ projectName }}\" uploaded and build #{{ buildId }} created"
handleProjectUpload:
emptySource: "Source directory \"{{ srcDir }}\" is empty. Add files to your project and rerun `{{#yellow}}hs project upload{{/yellow}}` to upload them to HubSpot."
compressed: "Project files compressed: {{ byteCount }} bytes"
compressing: "Compressing build files to \"{{ path }}\""
fileFiltered: "Ignore rule triggered for \"{{ filename }}\""
validateProjectConfig:
configNotFound: "Unable to locate a project configuration file. Try running again from a project directory, or run {{ createCommand }} to create a new project."
configMissingFields: "The project configuruation file is missing required fields."
Expand Down Expand Up @@ -1132,6 +1122,17 @@ en:
buildSucceededAutomaticallyDeploying: "Build #{{ buildId }} succeeded. {{#bold}}Automatically deploying{{/bold}} to {{ accountIdentifier }}\n"
cleanedUpTempFile: "Cleaned up temporary file {{ path }}"
viewDeploys: "View all deploys for this project in HubSpot"
projectUpload:
uploadProjectFiles:
add: "Uploading {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
fail: "Failed to upload {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
succeed: "Uploaded {{#bold}}{{ projectName }}{{/bold}} project files to {{ accountIdentifier }}"
buildCreated: "Project \"{{ projectName }}\" uploaded and build #{{ buildId }} created"
handleProjectUpload:
emptySource: "Source directory \"{{ srcDir }}\" is empty. Add files to your project and rerun `{{#yellow}}hs project upload{{/yellow}}` to upload them to HubSpot."
compressed: "Project files compressed: {{ byteCount }} bytes"
compressing: "Compressing build files to \"{{ path }}\""
fileFiltered: "Ignore rule triggered for \"{{ filename }}\""
ui:
betaTag: "{{#bold}}[BETA]{{/bold}}"
betaWarning:
Expand Down
2 changes: 1 addition & 1 deletion lib/localDev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const {
isAppDeveloperAccount,
isDeveloperTestAccount,
} = require('./accountTypes');
const { handleProjectUpload } = require('./projects');
const { handleProjectUpload } = require('./projects/upload');
const { pollProjectBuildAndDeploy } = require('./projects/buildAndDeploy');
const {
PROJECT_ERROR_TYPES,
Expand Down
2 changes: 1 addition & 1 deletion lib/projects/buildAndDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
ProjectTask,
ProjectSubtask,
ProjectPollStatusFunctionText,
} from '../../types/projects';
} from '../../types/Projects';

Check failure on line 23 in lib/projects/buildAndDeploy.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '../../types/Projects' or its corresponding type declarations.
import {
POLLING_DELAY,
PROJECT_BUILD_TEXT,
Expand Down
169 changes: 1 addition & 168 deletions lib/projects/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import fs from 'fs-extra';
import path from 'path';
import archiver from 'archiver';
import tmp, { FileResult } from 'tmp';
import findup from 'findup-sync';
import { logger } from '@hubspot/local-dev-lib/logger';
import { fetchFileFromRepository } from '@hubspot/local-dev-lib/github';
import {
createProject,
fetchProject,
uploadProject,
} from '@hubspot/local-dev-lib/api/projects';
import { isSpecifiedError } from '@hubspot/local-dev-lib/errors/index';
import { shouldIgnoreFile } from '@hubspot/local-dev-lib/ignoreRules';
import { getCwd, getAbsoluteFilePath } from '@hubspot/local-dev-lib/path';
import { downloadGithubRepoContents } from '@hubspot/local-dev-lib/github';
import { RepoPath } from '@hubspot/local-dev-lib/types/Github';
Expand All @@ -37,7 +33,7 @@ import {
ProjectAddComponentData,
ProjectTemplateRepoConfig,
ComponentTemplate,
} from '../../types/projects';
} from '../../types/Projects';

Check failure on line 36 in lib/projects/index.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '../../types/Projects' or its corresponding type declarations.

const i18nKey = 'lib.projects';

Expand Down Expand Up @@ -324,169 +320,6 @@ export async function ensureProjectExists(
}
}

async function uploadProjectFiles(
accountId: number,
projectName: string,
filePath: string,
uploadMessage: string,
platformVersion: string
): Promise<{ buildId?: number; error: unknown }> {
SpinniesManager.init({});
const accountIdentifier = uiAccountDescription(accountId);

SpinniesManager.add('upload', {
text: i18n(`${i18nKey}.uploadProjectFiles.add`, {
accountIdentifier,
projectName,
}),
succeedColor: 'white',
});

let buildId: number | undefined;
let error: unknown;

try {
const { data: upload } = await uploadProject(
accountId,
projectName,
filePath,
uploadMessage,
platformVersion
);

buildId = upload.buildId;

SpinniesManager.succeed('upload', {
text: i18n(`${i18nKey}.uploadProjectFiles.succeed`, {
accountIdentifier,
projectName,
}),
});

logger.debug(
i18n(`${i18nKey}.uploadProjectFiles.buildCreated`, {
buildId,
projectName,
})
);
} catch (err) {
SpinniesManager.fail('upload', {
text: i18n(`${i18nKey}.uploadProjectFiles.fail`, {
accountIdentifier,
projectName,
}),
});

error = err;
}

return { buildId, error };
}

type ProjectUploadCallbackFunction<T> = (
accountId: number,
projectConfig: ProjectConfig,
tempFile: FileResult,
buildId?: number
) => Promise<T | undefined>;

type ProjectUploadDefaultResult = {
uploadError?: unknown;
};

export async function handleProjectUpload<T = ProjectUploadDefaultResult>(
accountId: number,
projectConfig: ProjectConfig,
projectDir: string,
callbackFunc: ProjectUploadCallbackFunction<T>,
uploadMessage: string
) {
const srcDir = path.resolve(projectDir, projectConfig.srcDir);

const filenames = fs.readdirSync(srcDir);
if (!filenames || filenames.length === 0) {
logger.log(
i18n(`${i18nKey}.handleProjectUpload.emptySource`, {
srcDir: projectConfig.srcDir,
})
);
process.exit(EXIT_CODES.SUCCESS);
}

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

logger.debug(
i18n(`${i18nKey}.handleProjectUpload.compressing`, {
path: tempFile.name,
})
);

const output = fs.createWriteStream(tempFile.name);
const archive = archiver('zip');

const result = new Promise(resolve =>
output.on('close', async function () {
let uploadResult: ProjectUploadDefaultResult | T | undefined;

logger.debug(
i18n(`${i18nKey}.handleProjectUpload.compressed`, {
byteCount: archive.pointer(),
})
);

const { buildId, error } = await uploadProjectFiles(
accountId,
projectConfig.name,
tempFile.name,
uploadMessage,
projectConfig.platformVersion
);

if (error) {
console.log(error);
uploadResult = { uploadError: error };
} else if (callbackFunc) {
console.log('callbackfunc');
uploadResult = await callbackFunc(
accountId,
projectConfig,
tempFile,
buildId
);
}
resolve(uploadResult || {});
})
);

archive.pipe(output);

let loggedIgnoredNodeModule = false;

archive.directory(srcDir, false, file => {
const ignored = shouldIgnoreFile(file.name, true);
if (ignored) {
const isNodeModule = file.name.includes('node_modules');

if (!isNodeModule || !loggedIgnoredNodeModule) {
logger.debug(
i18n(`${i18nKey}.handleProjectUpload.fileFiltered`, {
filename: file.name,
})
);
}

if (isNodeModule && !loggedIgnoredNodeModule) {
loggedIgnoredNodeModule = true;
}
}
return ignored ? false : file;
});

archive.finalize();

return result;
}

export function logFeedbackMessage(buildId: number): void {
if (buildId > 0 && buildId % FEEDBACK_INTERVAL === 0) {
uiLine();
Expand Down
Loading

0 comments on commit b60f8b5

Please sign in to comment.