@hubspot/local-dev-lib
Advanced tools
Comparing version 0.3.4 to 0.3.5
@@ -86,5 +86,3 @@ { | ||
"pathExists": "The {{ path }} path already exists", | ||
"failedToWriteMeta": "There was an problem writing the module's meta data at {{ path }}", | ||
"fileReadFailure": "Failed to read file at {{ path }}", | ||
"failedToWrite": "failed to write to file at {{ path }}" | ||
"fileUpdateFailure": "There was a problem updating the modules files at {{ path }}: {{ errorMessage }}" | ||
} | ||
@@ -91,0 +89,0 @@ }, |
@@ -86,5 +86,3 @@ { | ||
"pathExists": "The {{ path }} path already exists", | ||
"failedToWriteMeta": "There was an problem writing the module's meta data at {{ path }}", | ||
"fileReadFailure": "Failed to read file at {{ path }}", | ||
"failedToWrite": "failed to write to file at {{ path }}" | ||
"fileUpdateFailure": "There was a problem updating the modules files at {{ path }}: {{ errorMessage }}" | ||
} | ||
@@ -91,0 +89,0 @@ }, |
@@ -11,3 +11,3 @@ "use strict"; | ||
const child_process_1 = require("child_process"); | ||
const escapeRegExp_1 = require("../../utils/escapeRegExp"); | ||
const escapeRegExp_1 = require("../escapeRegExp"); | ||
const modules_1 = require("../../utils/cms/modules"); | ||
@@ -14,0 +14,0 @@ const logger_1 = require("../logging/logger"); |
@@ -87,10 +87,5 @@ "use strict"; | ||
]; | ||
const transformFileContents = (file, metaData, getInternalVersion) => { | ||
fs_extra_1.default.readFile(file, 'utf8', (err, data) => { | ||
if (err) { | ||
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.createModule.errors.fileReadFailure`, { | ||
path: file, | ||
}); | ||
} | ||
let results = data; | ||
const updateFileContents = async (file, metaData, getInternalVersion) => { | ||
try { | ||
let fileContents = await fs_extra_1.default.readFile(file, 'utf8'); // returns Promise | ||
MODULE_STRING_TRANSFORMATIONS.forEach(entry => { | ||
@@ -100,19 +95,14 @@ const replacementString = getInternalVersion | ||
: entry.fallback; | ||
results = results.replace(entry.regex, replacementString); | ||
fileContents = fileContents.replace(entry.regex, replacementString); | ||
}); | ||
fs_extra_1.default.writeFile(file, results, 'utf8', err => { | ||
if (err) { | ||
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.createModule.errors.failedToWrite`, { | ||
path: file, | ||
}); | ||
} | ||
await fs_extra_1.default.writeFile(file, fileContents, 'utf8'); | ||
await fs_extra_1.default.appendFile(file, 'export const meta = ' + JSON.stringify(metaData, null, ' ')); | ||
} | ||
catch (error) { | ||
const { message } = error; | ||
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.createModule.errors.fileUpdateFailure`, { | ||
path: file, | ||
errorMessage: message, | ||
}); | ||
fs_extra_1.default.appendFile(file, 'export const meta = ' + JSON.stringify(metaData, null, ' '), err => { | ||
if (err) { | ||
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.createModule.errors.failedToWrite`, { | ||
path: file, | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
@@ -122,3 +112,16 @@ async function createModule(moduleDefinition, name, dest, getInternalVersion, options = { | ||
}) { | ||
const { reactType: isReactModule } = moduleDefinition; | ||
const { moduleLabel, contentTypes, global, reactType: isReactModule, } = moduleDefinition; | ||
const moduleMetaData = { | ||
label: moduleLabel, | ||
css_assets: [], | ||
external_js: [], | ||
global: global, | ||
help_text: '', | ||
host_template_types: contentTypes, | ||
js_assets: [], | ||
other_assets: [], | ||
smart_type: 'NOT_SMART', | ||
tags: [], | ||
is_available_for_new_content: false, | ||
}; | ||
const folderName = name.endsWith('.module') ? name : `${name}.module`; | ||
@@ -142,25 +145,3 @@ const destPath = !isReactModule | ||
})); | ||
// Write module meta | ||
const writeModuleMeta = ({ moduleLabel, contentTypes, global, reactType }, dest) => { | ||
const metaData = { | ||
label: moduleLabel, | ||
css_assets: [], | ||
external_js: [], | ||
global: global, | ||
help_text: '', | ||
host_template_types: contentTypes, | ||
js_assets: [], | ||
other_assets: [], | ||
smart_type: 'NOT_SMART', | ||
tags: [], | ||
is_available_for_new_content: false, | ||
}; | ||
if (!reactType) { | ||
fs_extra_1.default.writeJSONSync(dest, metaData, { spaces: 2 }); | ||
} | ||
else { | ||
transformFileContents(`${dest}/index.tsx`, metaData, getInternalVersion); | ||
} | ||
}; | ||
// Filter out ceratin fetched files from the response | ||
// Filter out certain fetched files from the response | ||
const moduleFileFilter = (src, dest) => { | ||
@@ -170,3 +151,3 @@ const emailEnabled = moduleDefinition.contentTypes.includes('EMAIL'); | ||
case 'meta.json': | ||
writeModuleMeta(moduleDefinition, dest); | ||
fs_extra_1.default.writeJSONSync(dest, moduleMetaData, { spaces: 2 }); // writing a meta.json file to standard HubL modules | ||
return false; | ||
@@ -195,5 +176,5 @@ case 'module.js': | ||
await (0, github_1.downloadGithubRepoContents)('HubSpot/cms-sample-assets', `modules/${sampleAssetPath}`, destPath, '', moduleFileFilter); | ||
// Mutating React module files after fetch | ||
// Updating React module files after fetch | ||
if (isReactModule) { | ||
writeModuleMeta(moduleDefinition, destPath); | ||
await updateFileContents(`${destPath}/index.tsx`, moduleMetaData, getInternalVersion); | ||
} | ||
@@ -204,7 +185,7 @@ } | ||
if (!name) { | ||
const defaultReactModules = await (0, github_1.listGithubRepoContents)('HubSpot/cms-sample-assets', 'modules/', 'dir'); | ||
const defaultReactModules = await (0, github_1.listGithubRepoContents)('HubSpot/cms-react', 'default-react-modules/src/components/modules/', 'dir'); | ||
return defaultReactModules; | ||
} | ||
await (0, github_1.downloadGithubRepoContents)('HubSpot/cms-sample-assets', `modules/${name}`, dest); | ||
await (0, github_1.downloadGithubRepoContents)('HubSpot/cms-react', `default-react-modules/src/components/modules/${name}`, dest); | ||
} | ||
exports.retrieveDefaultModule = retrieveDefaultModule; |
@@ -0,1 +1,2 @@ | ||
import { AxiosError } from 'axios'; | ||
import { FieldsJs } from './handleFieldsJS'; | ||
@@ -9,2 +10,7 @@ import { FileMapperInputOptions } from '../../types/Files'; | ||
saveOutput?: boolean; | ||
onAttemptCallback?: (file: string | undefined, destPath: string) => void; | ||
onSuccessCallback?: (file: string | undefined, destPath: string) => void; | ||
onFirstErrorCallback?: (file: string, destPath: string, error: AxiosError) => void; | ||
onRetryCallback?: (file: string, destPath: string) => void; | ||
onFinalErrorCallback?: (accountId: number, file: string, destPath: string, error: AxiosError) => void; | ||
}; | ||
@@ -11,0 +17,0 @@ type FilePathsByType = { |
@@ -13,3 +13,3 @@ "use strict"; | ||
const modules_1 = require("../../utils/cms/modules"); | ||
const escapeRegExp_1 = require("../../utils/escapeRegExp"); | ||
const escapeRegExp_1 = require("../escapeRegExp"); | ||
const path_2 = require("../path"); | ||
@@ -80,4 +80,35 @@ const standardErrors_1 = require("../../errors/standardErrors"); | ||
exports.getFilesByType = getFilesByType; | ||
const defaultUploadAttemptCallback = (file, destPath) => logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.uploadFolder.attempt`, { | ||
file: file || '', | ||
destPath, | ||
})); | ||
const defaultUploadSuccessCallback = (file, destPath) => logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.uploadFolder.success`, { | ||
file: file || '', | ||
destPath, | ||
})); | ||
const defaultUploadFirstErrorCallback = (file, destPath, error) => { | ||
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.uploadFolder.failed`, { file, destPath })); | ||
if (error.response && error.response.data) { | ||
logger_1.logger.debug(error.response.data); | ||
} | ||
else { | ||
logger_1.logger.debug(error.message); | ||
} | ||
}; | ||
const defaultUploadRetryCallback = (file, destPath) => logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.uploadFolder.retry`, { file, destPath })); | ||
const defaultUploadFinalErrorCallback = (accountId, file, destPath, error) => { | ||
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.uploadFolder.retryFailed`, { file, destPath })); | ||
(0, apiErrors_1.throwApiUploadError)(error, { | ||
accountId, | ||
request: destPath, | ||
payload: file, | ||
}); | ||
}; | ||
async function uploadFolder(accountId, src, dest, fileMapperOptions, commandOptions = {}, filePaths = [], mode = null) { | ||
const { saveOutput, convertFields } = commandOptions; | ||
const { saveOutput, convertFields, onAttemptCallback, onSuccessCallback, onFirstErrorCallback, onRetryCallback, onFinalErrorCallback } = commandOptions; | ||
const _onAttemptCallback = onAttemptCallback || defaultUploadAttemptCallback; | ||
const _onSuccessCallback = onSuccessCallback || defaultUploadSuccessCallback; | ||
const _onFirstErrorCallback = onFirstErrorCallback || defaultUploadFirstErrorCallback; | ||
const _onRetryCallback = onRetryCallback || defaultUploadRetryCallback; | ||
const _onFinalErrorCallback = onFinalErrorCallback || defaultUploadFinalErrorCallback; | ||
const tmpDir = convertFields | ||
@@ -108,12 +139,6 @@ ? (0, handleFieldsJS_1.createTmpDirSync)('hubspot-temp-fieldsjs-output-') | ||
return async () => { | ||
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.uploadFolder.attempt`, { | ||
file: originalFilePath || '', | ||
destPath, | ||
})); | ||
_onAttemptCallback(originalFilePath, destPath); | ||
try { | ||
await (0, fileMapper_2.upload)(accountId, file, destPath, apiOptions); | ||
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.uploadFolder.success`, { | ||
file: originalFilePath || '', | ||
destPath, | ||
})); | ||
_onSuccessCallback(originalFilePath, destPath); | ||
} | ||
@@ -125,9 +150,3 @@ catch (err) { | ||
} | ||
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.uploadFolder.failed`, { file, destPath })); | ||
if (error.response && error.response.data) { | ||
logger_1.logger.debug(error.response.data); | ||
} | ||
else { | ||
logger_1.logger.debug(error.message); | ||
} | ||
_onFirstErrorCallback(file, destPath, error); | ||
failures.push({ | ||
@@ -147,9 +166,6 @@ file, | ||
return async () => { | ||
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.uploadFolder.retry`, { file, destPath })); | ||
_onRetryCallback(file, destPath); | ||
try { | ||
await (0, fileMapper_2.upload)(accountId, file, destPath, apiOptions); | ||
logger_1.logger.log((0, lang_1.i18n)(`${i18nKey}.uploadFolder.success`, { | ||
file, | ||
destPath, | ||
})); | ||
_onSuccessCallback(file, destPath); | ||
return { | ||
@@ -162,3 +178,2 @@ resultType: files_1.FILE_UPLOAD_RESULT_TYPES.SUCCESS, | ||
catch (err) { | ||
logger_1.logger.debug((0, lang_1.i18n)(`${i18nKey}.uploadFolder.retryFailed`, { file, destPath })); | ||
const error = err; | ||
@@ -168,7 +183,8 @@ if ((0, standardErrors_1.isFatalError)(error)) { | ||
} | ||
(0, apiErrors_1.throwApiUploadError)(error, { | ||
accountId, | ||
request: destPath, | ||
payload: file, | ||
}); | ||
_onFinalErrorCallback(accountId, file, destPath, error); | ||
return { | ||
resultType: files_1.FILE_UPLOAD_RESULT_TYPES.FAILURE, | ||
error, | ||
file, | ||
}; | ||
} | ||
@@ -175,0 +191,0 @@ }; |
@@ -17,5 +17,5 @@ "use strict"; | ||
const fileMapper_2 = require("../../api/fileMapper"); | ||
const escapeRegExp_1 = require("../../utils/escapeRegExp"); | ||
const escapeRegExp_1 = require("../escapeRegExp"); | ||
const path_2 = require("../path"); | ||
const notify_1 = require("../../utils/notify"); | ||
const notify_1 = require("../notify"); | ||
const themes_1 = require("./themes"); | ||
@@ -22,0 +22,0 @@ const logger_1 = require("../logging/logger"); |
@@ -15,3 +15,3 @@ "use strict"; | ||
const http_1 = __importDefault(require("../http")); | ||
const escapeRegExp_1 = require("../utils/escapeRegExp"); | ||
const escapeRegExp_1 = require("./escapeRegExp"); | ||
const path_2 = require("./path"); | ||
@@ -18,0 +18,0 @@ const apiErrors_1 = require("../errors/apiErrors"); |
@@ -8,2 +8,5 @@ import { CLIAccount } from '../types/Accounts'; | ||
scopeGroups: Array<string>; | ||
enabledFeatures?: { | ||
[key: string]: number; | ||
}; | ||
encodedOAuthRefreshToken: string; | ||
@@ -14,3 +17,6 @@ hubName: string; | ||
export declare function accessTokenForPersonalAccessKey(accountId: number): Promise<string | undefined>; | ||
export declare function enabledFeaturesForPersonalAccessKey(accountId: number): Promise<{ | ||
[key: string]: number; | ||
} | undefined>; | ||
export declare function updateConfigWithAccessToken(token: AccessToken, personalAccessKey: string, env?: Environment, name?: string, makeDefault?: boolean): Promise<CLIAccount | null>; | ||
export {}; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.updateConfigWithAccessToken = exports.accessTokenForPersonalAccessKey = exports.getAccessToken = void 0; | ||
exports.updateConfigWithAccessToken = exports.enabledFeaturesForPersonalAccessKey = exports.accessTokenForPersonalAccessKey = exports.getAccessToken = void 0; | ||
const moment_1 = __importDefault(require("moment")); | ||
@@ -41,2 +41,3 @@ const environments_1 = require("../constants/environments"); | ||
scopeGroups: response.scopeGroups, | ||
enabledFeatures: response.enabledFeatures, | ||
encodedOAuthRefreshToken: response.encodedOAuthRefreshToken, | ||
@@ -48,3 +49,4 @@ hubName: response.hubName, | ||
async function refreshAccessToken(personalAccessKey, env = environments_1.ENVIRONMENTS.PROD, accountId) { | ||
const { accessToken, expiresAt } = await getAccessToken(personalAccessKey, env, accountId); | ||
const accessTokenResponse = await getAccessToken(personalAccessKey, env, accountId); | ||
const { accessToken, expiresAt } = accessTokenResponse; | ||
const config = (0, config_1.getAccountConfig)(accountId); | ||
@@ -61,3 +63,3 @@ (0, config_1.updateAccountConfig)({ | ||
(0, config_1.writeConfig)(); | ||
return accessToken; | ||
return accessTokenResponse; | ||
} | ||
@@ -69,3 +71,3 @@ async function getNewAccessToken(accountId, personalAccessKey, expiresAt, env) { | ||
} | ||
let accessToken; | ||
let accessTokenResponse; | ||
try { | ||
@@ -76,3 +78,3 @@ const refreshAccessPromise = refreshAccessToken(personalAccessKey, env, accountId); | ||
} | ||
accessToken = await refreshAccessPromise; | ||
accessTokenResponse = await refreshAccessPromise; | ||
} | ||
@@ -85,3 +87,3 @@ catch (e) { | ||
} | ||
return accessToken; | ||
return accessTokenResponse; | ||
} | ||
@@ -98,3 +100,3 @@ async function accessTokenForPersonalAccessKey(accountId) { | ||
(0, moment_1.default)().add(5, 'minutes').isAfter((0, moment_1.default)(authTokenInfo.expiresAt))) { | ||
return getNewAccessToken(accountId, personalAccessKey, authTokenInfo && authTokenInfo.expiresAt, env); | ||
return getNewAccessToken(accountId, personalAccessKey, authTokenInfo && authTokenInfo.expiresAt, env).then(tokenInfo => tokenInfo.accessToken); | ||
} | ||
@@ -104,2 +106,13 @@ return auth?.tokenInfo?.accessToken; | ||
exports.accessTokenForPersonalAccessKey = accessTokenForPersonalAccessKey; | ||
async function enabledFeaturesForPersonalAccessKey(accountId) { | ||
const account = (0, config_1.getAccountConfig)(accountId); | ||
if (!account) { | ||
(0, standardErrors_1.throwErrorWithMessage)(`${i18nKey}.errors.accountNotFound`, { accountId }); | ||
} | ||
const { auth, personalAccessKey, env } = account; | ||
const authTokenInfo = auth && auth.tokenInfo; | ||
const accessTokenResponse = await getNewAccessToken(accountId, personalAccessKey, authTokenInfo && authTokenInfo.expiresAt, env); | ||
return accessTokenResponse?.enabledFeatures; | ||
} | ||
exports.enabledFeaturesForPersonalAccessKey = enabledFeaturesForPersonalAccessKey; | ||
async function updateConfigWithAccessToken(token, personalAccessKey, env, name, makeDefault = false) { | ||
@@ -106,0 +119,0 @@ const { portalId, accessToken, expiresAt } = token; |
{ | ||
"name": "@hubspot/local-dev-lib", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "Provides library functionality for HubSpot local development tooling, including the HubSpot CLI", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
407688
10210
3