diff --git a/src/index.js b/src/index.js index 1a0d4c6..11baf50 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,22 @@ const { const appPath = process.env.SANDWORM_APP_PATH || path.join(__dirname, '..', '..', '..'); const config = loadConfig(appPath); +const getPermissions = (activity, devDependencies, prodDependencies) => { + const ignoredModules = + config && Array.isArray(config.ignoredModules) ? config.ignoredModules : []; + const permissions = getPermissionsFromActivity(activity); + + const currentPermissions = loadPermissions(appPath); + const newPermissions = getPackagePermissions({ + permissions, + devDependencies, + prodDependencies, + ignoredModules, + }); + + return {currentPermissions, newPermissions}; +} + loadSandworm({config, trustedModules: ['mocha']}); module.exports = { @@ -31,44 +47,32 @@ module.exports = { const activity = getRecordedActivity(); logger.log(`Intercepted ${activity.length} events`); stopRecordingSandwormActivity(() => { - (async () => { - loadDependencies(appPath, ([devDependencies, prodDependencies]) => { - const ignoredModules = - config && Array.isArray(config.ignoredModules) ? config.ignoredModules : []; - const permissions = getPermissionsFromActivity(activity); + loadDependencies(appPath, ([devDependencies, prodDependencies]) => { + const {currentPermissions, newPermissions} = getPermissions(activity, devDependencies, prodDependencies); - const currentPermissions = loadPermissions(appPath); - const newPermissions = getPackagePermissions({ - permissions, - devDependencies, - prodDependencies, - ignoredModules, + if (!currentPermissions) { + writePermissions(appPath, newPermissions, () => { + logger.logTestPluginFirstRunMessage(); + done(); }); + } else { + const {changes, messages} = comparePermissions(currentPermissions, newPermissions); - if (!currentPermissions) { - writePermissions(appPath, newPermissions, () => { - logger.logTestPluginFirstRunMessage(); - done(); - }); - } else { - const {changes, messages} = comparePermissions(currentPermissions, newPermissions); - - if (changes.length === 0) { - logger.success('✔ Permission snapshot matches current test run'); - } - - done( - changes.length > 0 - ? new Error( - `Sandworm: Permission mismatch:\n${messages.join( - '\n', - )}\nPlease verify and update the \`${SANDWORM_PERMISSION_FILE_NAME}\` file.`, - ) - : undefined, - ); + if (changes.length === 0) { + logger.success('✔ Permission snapshot matches current test run'); } - }); - })(); + + done( + changes.length > 0 + ? new Error( + `Sandworm: Permission mismatch:\n${messages.join( + '\n', + )}\nPlease verify and update the \`${SANDWORM_PERMISSION_FILE_NAME}\` file.`, + ) + : undefined, + ); + } + }); }); }, },