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

Convert projects watch to TS #1325

Merged
merged 2 commits into from
Jan 9, 2025
Merged
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
157 changes: 91 additions & 66 deletions lib/projects/watch.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,55 @@
// @ts-nocheck
const chokidar = require('chokidar');
const path = require('path');
const chalk = require('chalk');
const { default: PQueue } = require('p-queue');
const { logError, ApiErrorContext } = require('../errorHandlers/index');
const { i18n } = require('../lang');
const { logger } = require('@hubspot/local-dev-lib/logger');
const { isAllowedExtension } = require('@hubspot/local-dev-lib/path');
const { shouldIgnoreFile } = require('@hubspot/local-dev-lib/ignoreRules');
const {
import chokidar from 'chokidar';
import path from 'path';
import chalk from 'chalk';
import PQueue from 'p-queue';
import { logger } from '@hubspot/local-dev-lib/logger';
import { isAllowedExtension } from '@hubspot/local-dev-lib/path';
import { JSR_ALLOWED_EXTENSIONS } from '@hubspot/local-dev-lib/constants/extensions';
import { shouldIgnoreFile } from '@hubspot/local-dev-lib/ignoreRules';
import {
cancelStagedBuild,
provisionBuild,
uploadFileToBuild,
deleteFileFromBuild,
queueBuild,
} = require('@hubspot/local-dev-lib/api/projects');
const { isSpecifiedError } = require('@hubspot/local-dev-lib/errors/index');
const { PROJECT_ERROR_TYPES } = require('../constants');
} from '@hubspot/local-dev-lib/api/projects';
import { isSpecifiedError } from '@hubspot/local-dev-lib/errors/index';

import { logError, ApiErrorContext } from '../errorHandlers';
import { i18n } from '../lang';
import { PROJECT_ERROR_TYPES } from '../constants';
import { ProjectConfig } from '../../types/Projects';

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

type ProjectWatchHandlerFunction = (
accountId: number,
projectName: string,
currentBuildId: number
) => Promise<void>;

type WatchEvent = {
filePath: string;
remotePath: string;
action: string;
};

const queue = new PQueue({
concurrency: 10,
});
const standbyeQueue = [];
let currentBuildId = null;
let handleBuildStatus, handleUserInput;
let timer;
const standbyQueue: WatchEvent[] = [];
let currentBuildId: number;
let handleBuildStatus: ProjectWatchHandlerFunction;
let handleUserInput: ProjectWatchHandlerFunction;
let timer: NodeJS.Timeout;

const processStandByQueue = async (accountId, projectName, platformVersion) => {
async function processStandByQueue(
accountId: number,
projectName: string,
platformVersion: string
): Promise<void> {
queue.addAll(
standbyeQueue.map(({ filePath, remotePath, action }) => {
standbyQueue.map(({ filePath, remotePath, action }) => {
return async () => {
queueFileOrFolder(
accountId,
Expand All @@ -43,25 +62,28 @@ const processStandByQueue = async (accountId, projectName, platformVersion) => {
};
})
);
standbyeQueue.length = 0;
standbyQueue.length = 0;
debounceQueueBuild(accountId, projectName, platformVersion);
};

const createNewStagingBuild = async (
accountId,
projectName,
platformVersion
) => {
}
async function createNewStagingBuild(
accountId: number,
projectName: string,
platformVersion: string
): Promise<void> {
currentBuildId = await createNewBuild(
accountId,
projectName,
platformVersion
);

handleUserInput(accountId, projectName, currentBuildId);
};
}

const debounceQueueBuild = (accountId, projectName, platformVersion) => {
function debounceQueueBuild(
accountId: number,
projectName: string,
platformVersion: string
): void {
if (timer) {
clearTimeout(timer);
}
Expand Down Expand Up @@ -93,25 +115,28 @@ const debounceQueueBuild = (accountId, projectName, platformVersion) => {

await createNewStagingBuild(accountId, projectName, platformVersion);

if (standbyeQueue.length > 0) {
if (standbyQueue.length > 0) {
await processStandByQueue(accountId, projectName, platformVersion);
}

queue.start();
logger.log(i18n(`${i18nKey}.logs.resuming`));
logger.log(`\n> Press ${chalk.bold('q')} to quit watching\n`);
}, 2000);
};
}

const queueFileOrFolder = async (
accountId,
projectName,
platformVersion,
filePath,
remotePath,
action
) => {
if (action === 'upload' && !isAllowedExtension(filePath)) {
async function queueFileOrFolder(
accountId: number,
projectName: string,
platformVersion: string,
filePath: string,
remotePath: string,
action: string
): Promise<void> {
if (
action === 'upload' &&
!isAllowedExtension(filePath, Array.from(JSR_ALLOWED_EXTENSIONS))
) {
logger.debug(i18n(`${i18nKey}.debug.extensionNotAllowed`, { filePath }));
return;
}
Expand Down Expand Up @@ -141,9 +166,13 @@ const queueFileOrFolder = async (
);
}
});
};
}

const createNewBuild = async (accountId, projectName, platformVersion) => {
async function createNewBuild(
accountId: number,
projectName: string,
platformVersion: string
) {
try {
logger.debug(i18n(`${i18nKey}.debug.attemptNewBuild`));
const {
Expand All @@ -160,22 +189,22 @@ const createNewBuild = async (accountId, projectName, platformVersion) => {
}
process.exit(1);
}
};
}

const handleWatchEvent = async (
accountId,
projectName,
platformVersion,
projectSourceDir,
filePath,
async function handleWatchEvent(
accountId: number,
projectName: string,
platformVersion: string,
projectSourceDir: string,
filePath: string,
action = 'upload'
) => {
) {
const remotePath = path.relative(projectSourceDir, filePath);
if (queue.isPaused) {
if (standbyeQueue.find(file => file.filePath === filePath)) {
if (standbyQueue.find(file => file.filePath === filePath)) {
logger.debug(i18n(`${i18nKey}.debug.fileAlreadyQueued`, { filePath }));
} else {
standbyeQueue.push({
standbyQueue.push({
filePath,
remotePath,
action,
Expand All @@ -191,15 +220,15 @@ const handleWatchEvent = async (
action
);
}
};
}

const createWatcher = async (
accountId,
projectConfig,
projectDir,
handleBuildStatusFn,
handleUserInputFn
) => {
export async function createWatcher(
accountId: number,
projectConfig: ProjectConfig,
projectDir: string,
handleBuildStatusFn: ProjectWatchHandlerFunction,
handleUserInputFn: ProjectWatchHandlerFunction
) {
const projectSourceDir = path.join(projectDir, projectConfig.srcDir);

handleBuildStatus = handleBuildStatusFn;
Expand Down Expand Up @@ -257,8 +286,4 @@ const createWatcher = async (
'deleteFolder'
);
});
};

module.exports = {
createWatcher,
};
}
Loading