-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHubSpotAutoUploadPlugin.js
157 lines (134 loc) · 4.31 KB
/
HubSpotAutoUploadPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const os = require('os');
const { upload } = require('@hubspot/local-dev-lib/api/fileMapper');
const { checkGitInclusion } = require('@hubspot/local-dev-lib/gitignore');
const {
loadConfig,
getConfigPath,
getAccountId,
} = require('@hubspot/local-dev-lib/config');
const { isAllowedExtension } = require('@hubspot/local-dev-lib/path');
const {
logger,
LOG_LEVEL,
setLogLevel,
setLogger,
} = require('@hubspot/local-dev-lib/logger');
const path = require('path');
setLogLevel(LOG_LEVEL.LOG);
loadConfig();
function checkAndWarnGitInclusion(configPath) {
try {
const { inGit, configIgnored } = checkGitInclusion(configPath);
if (!inGit || configIgnored) return;
logger.warn('Security Issue Detected');
logger.warn('The HubSpot config file can be tracked by git.');
logger.warn(`File "${configPath}"`);
logger.warn('To remediate:');
logger.warn(
`- Move the config file to your home directory: "${os.homedir()}"`
);
logger.warn(
`- Add gitignore pattern "${configPath}" to a .gitignore file in root of your repository.`
);
logger.warn(
'- Ensure that the config file has not already been pushed to a remote repository.'
);
} catch (e) {
// fail silently
logger.debug(
'Unable to determine if config file is properly ignored by git.'
);
}
}
checkAndWarnGitInclusion(getConfigPath());
const pluginName = 'HubSpotAutoUploadPlugin';
const parseValidationErrors = (responseBody = {}) => {
const errorMessages = [];
const { errors, message } = responseBody;
if (message) {
errorMessages.push(message);
}
if (errors) {
const specificErrors = errors.map(error => {
let errorMessage = error.message;
if (error.errorTokens && error.errorTokens.line) {
errorMessage = `line ${error.errorTokens.line}: ${errorMessage}`;
}
return errorMessage;
});
errorMessages.push(...specificErrors);
}
return errorMessages;
};
function logValidationErrors(error, context) {
const { response = {} } = error;
const validationErrors = parseValidationErrors(response.body);
if (validationErrors.length) {
validationErrors.forEach(err => {
logger.error(err);
});
}
logger.debug(error);
logger.debug(context);
}
class HubSpotAutoUploadPlugin {
constructor(options = {}) {
const { src, dest, portal, account, autoupload } = options;
this.src = src;
this.dest = dest;
this.autoupload = autoupload;
this.accountId = getAccountId(portal || account);
}
apply(compiler) {
const webpackLogger = compiler.getInfrastructureLogger(pluginName);
setLogger(webpackLogger);
let isFirstCompile = true;
compiler.hooks.done.tapPromise(pluginName, async stats => {
const { compilation } = stats;
const isAssetEmitted = asset => {
return (
compilation.assets[asset].emitted ||
compilation.emittedAssets.has(asset)
);
};
const assets = Object.keys(compilation.assets).filter(asset => {
return isFirstCompile || isAssetEmitted(asset);
});
isFirstCompile = false;
assets.forEach(filename => {
const outputPath = compilation.getPath(compilation.compiler.outputPath);
const filepath = path.join(outputPath, filename);
if (!this.autoupload || !isAllowedExtension(filepath)) {
return;
}
const dest = `${this.dest}/${filename}`;
upload(this.accountId, filepath, dest)
.then(() => {
webpackLogger.info(`Uploaded ${dest} to account ${this.accountId}`);
})
.catch(error => {
webpackLogger.error(`Uploading ${dest} failed`);
const context = {
accountId: this.accountId,
request: dest,
payload: filepath,
statusCode: error.statusCode,
};
if (
error.response &&
error.response.status === 400 &&
error.response.data &&
(error.response.data.message || error.response.data.errors)
) {
logValidationErrors(error, context);
} else {
console.error(error.message);
console.debug(error);
console.debug(context);
}
});
});
});
}
}
module.exports = HubSpotAutoUploadPlugin;