Skip to content

Commit

Permalink
updates to get logging working
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Feb 1, 2024
1 parent f124a23 commit 14ee62e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 4 deletions.
18 changes: 17 additions & 1 deletion packages/cli/commands/create/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@ const {
} = require('../../lib/prompts/createFunctionPrompt');
const { logErrorInstance } = require('../../lib/errorHandlers/standardErrors');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const { buildLogCallbacks } = require('../../lib/logCallbacks');

const i18nKey = 'cli.commands.create.subcommands.function';

const createFunctionLogCallbacks = buildLogCallbacks({
destPathAlreadyExists: `${i18nKey}.logCallbacks.destPathAlreadyExists`,
createdDest: `${i18nKey}.logCallbacks.createdDest`,
createdFunctionFile: `${i18nKey}.logCallbacks.createdFunctionFile`,
createdConfigFile: `${i18nKey}.logCallbacks.createdConfigFile`,
success: `${i18nKey}.logCallbacks.success`,
});

module.exports = {
dest: ({ name }) => name,
execute: async ({ dest }) => {
const functionDefinition = await createFunctionPrompt();
try {
await createFunction(functionDefinition, dest);
await createFunction(
functionDefinition,
dest,
{},
createFunctionLogCallbacks
);
} catch (e) {
logErrorInstance(e);
process.exit(EXIT_CODES.ERROR);
Expand Down
16 changes: 14 additions & 2 deletions packages/cli/commands/create/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,35 @@ const { i18n } = require('../../lib/lang');
const { createModulePrompt } = require('../../lib/prompts/createModulePrompt');
const { logErrorInstance } = require('../../lib/errorHandlers/standardErrors');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const { buildLogCallbacks } = require('../../lib/logCallbacks');

const i18nKey = 'cli.commands.create.subcommands.module';

const createModuleLogCallbacks = buildLogCallbacks({
creatingPath: `${i18nKey}.logCallbacks.creatingPath`,
creatingModule: `${i18nKey}.logCallbacks.creatingModule`,
});

module.exports = {
dest: ({ dest }) => dest,
validate: ({ name }) => {
if (!name) {
logger.error(i18n(`${i18nKey}.errors.nameRequired`));
return false;
}

return true;
},
execute: async ({ name, dest }) => {
const moduleDefinition = await createModulePrompt();
try {
await createModule(moduleDefinition, name, dest);
await createModule(
moduleDefinition,
name,
dest,
false,
{},
createModuleLogCallbacks
);
} catch (e) {
logErrorInstance(e);
process.exit(EXIT_CODES.ERROR);
Expand Down
13 changes: 12 additions & 1 deletion packages/cli/commands/create/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ const {
} = require('../../lib/prompts/createTemplatePrompt');
const { i18n } = require('../../lib/lang');
const { EXIT_CODES } = require('../../lib/enums/exitCodes');
const { buildLogCallbacks } = require('../../lib/logCallbacks');

const i18nKey = 'cli.commands.create.subcommands.template';

const createTemplateLogCallbacks = buildLogCallbacks({
creatingFile: `${i18nKey}.logCallbacks.creatingFile`,
});

module.exports = {
dest: ({ dest }) => dest,
validate: ({ name }) => {
Expand All @@ -22,7 +27,13 @@ module.exports = {
execute: async ({ name, dest }) => {
const { templateType } = await createTemplatePrompt();
try {
await createTemplate(name, dest, templateType);
await createTemplate(
name,
dest,
templateType,
null,
createTemplateLogCallbacks
);
} catch (e) {
logErrorInstance(e);
process.exit(EXIT_CODES.ERROR);
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/lang/en.lyaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,23 @@ en:
apiSamples: "Loading available API samples"
success:
sampleCreated: "Please, follow {{ filePath }}/README.md to find out how to run the sample"
function:
logCallbacks:
destPathAlreadyExists: "The {{ path }} path already exists"
createdDest: "Created {{ path }}"
failedToCreateFile: "The file {{ configFilePath }} could not be created"
createdFunctionFile: "Created {{ path }}"
createdConfigFile: "Created {{ path }}"
success: "A function for the endpoint '/_hcms/api/{{ endpointPath }}' has been created. Upload {{ folderName }} to try it out"
module:
logCallbacks:
creatingModule: "Creating module at {{ path }}"
creatingPath: "Creating {{ path }}"
errors:
nameRequired: "The \"name\" argument is required when creating a Custom Module."
template:
logCallbacks:
creatingFile: "Creating file at {{ path }}"
errors:
nameRequired: "The \"name\" argument is required when creating a Template."
customObject:
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/lib/commonOpts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const Logger = require('@hubspot/cli-lib/logger');
const {
setLogLevel: setLocalDevLibLogLevel,
} = require('@hubspot/local-dev-lib/logger');
const { DEFAULT_MODE, Mode } = require('@hubspot/cli-lib');
const {
getAccountId: getAccountIdFromConfig,
Expand Down Expand Up @@ -66,8 +69,16 @@ const setLogLevel = (options = {}) => {
const { debug } = options;
if (debug) {
Logger.setLogLevel(LOG_LEVEL.DEBUG);

// Update the log level in local-dev-lib's instance of the logger
// This will evenutally replace cli-lib's version of it
setLocalDevLibLogLevel(LOG_LEVEL.DEBUG);
} else {
Logger.setLogLevel(LOG_LEVEL.LOG);

// Update the log level in local-dev-lib's instance of the logger
// This will evenutally replace cli-lib's version of it
setLocalDevLibLogLevel(LOG_LEVEL.LOG);
}
};

Expand Down
7 changes: 7 additions & 0 deletions packages/cli/lib/process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const readline = require('readline');
const { logger, setLogLevel, LOG_LEVEL } = require('@hubspot/cli-lib/logger');
const {
setLogLevel: setLocalDevLibLogLevel,
} = require('@hubspot/local-dev-lib/logger');
const { i18n } = require('./lang');

const i18nKey = 'cli.lib.process';
Expand Down Expand Up @@ -28,6 +31,10 @@ const handleExit = callback => {
// Prevent logs when terminal closes
if (isSIGHUP) {
setLogLevel(LOG_LEVEL.NONE);

// Update the log level in local-dev-lib's instance of the logger
// This will evenutally replace cli-lib's version of it
setLocalDevLibLogLevel(LOG_LEVEL.LOG);
}

logger.debug(i18n(`${i18nKey}.exitDebug`, { signal }));
Expand Down
7 changes: 7 additions & 0 deletions packages/serverless-dev-runtime/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const bodyParser = require('body-parser');
const cors = require('cors');
const chalk = require('chalk');
const { logger, setLogLevel, LOG_LEVEL } = require('@hubspot/cli-lib/logger');
const {
setLogLevel: setLocalDevLibLogLevel,
} = require('@hubspot/local-dev-lib/logger');
const {
getTableContents,
getTableHeader,
Expand Down Expand Up @@ -192,6 +195,10 @@ const start = props => {
setOptions(props);
const { path: functionPath, watch, debug } = options;
setLogLevel(debug ? LOG_LEVEL.DEBUG : LOG_LEVEL.LOG);

// Update the log level in local-dev-lib's instance of the logger
// This will evenutally replace cli-lib's version of it
setLocalDevLibLogLevel(debug ? LOG_LEVEL.DEBUG : LOG_LEVEL.LOG);
if (watch) {
watchFolder(functionPath, restartServer);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/webpack-cms-plugins/HubSpotAutoUploadPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ const {
setLogLevel,
setLogger,
} = require('@hubspot/cli-lib/logger');
const {
setLogLevel: setLocalDevLibLogLevel,
} = require('@hubspot/local-dev-lib/logger');
const path = require('path');

setLogLevel(LOG_LEVEL.LOG);
// Update the log level in local-dev-lib's instance of the logger
// This will evenutally replace cli-lib's version of it
setLocalDevLibLogLevel(LOG_LEVEL.LOG);
loadConfig();
checkAndWarnGitInclusion(getConfigPath());

Expand Down

0 comments on commit 14ee62e

Please sign in to comment.