From 745800904322a80f799e3c8c6b91fb7ee5184b5d Mon Sep 17 00:00:00 2001 From: Gabi Dobocan Date: Thu, 29 Sep 2022 22:40:52 +0300 Subject: [PATCH] feat: update `sandworm-utils` to v1.4.0 --- package.json | 3 +-- setup/index.js | 16 ----------- setupFiles/index.js | 24 ++--------------- teardown/index.js | 66 +++++++++------------------------------------ yarn.lock | 35 ++++++++++++------------ 5 files changed, 33 insertions(+), 111 deletions(-) diff --git a/package.json b/package.json index 6fbd06f..d45cde8 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,7 @@ "license": "MIT", "homepage": "https://sandworm.dev", "dependencies": { - "sandworm": "1.3.0", - "sandworm-utils": "1.2.0" + "sandworm-utils": "1.4.0" }, "devDependencies": { "@commitlint/cli": "^17.1.2", diff --git a/setup/index.js b/setup/index.js index b37a26f..642ee09 100644 --- a/setup/index.js +++ b/setup/index.js @@ -1,24 +1,8 @@ -const path = require('path'); const { recorder: {recordSandwormActivity}, - files: {loadConfig}, logger, } = require('sandworm-utils'); -const appPath = process.env.SANDWORM_APP_PATH || path.join(__dirname, '..', '..', '..'); -let config; - -try { - config = loadConfig(appPath); - if (config) { - logger.log('Config loaded'); - } -} catch (error) { - logger.log('Error loading config:', error.message); -} - -globalThis.SANDWORM_CONFIG = config; - module.exports = async () => { logger.log('Starting listener...'); await recordSandwormActivity((err) => { diff --git a/setupFiles/index.js b/setupFiles/index.js index 51871cd..d7cf83c 100644 --- a/setupFiles/index.js +++ b/setupFiles/index.js @@ -1,28 +1,8 @@ -const Sandworm = require('sandworm'); const path = require('path'); const { - files: {loadConfig}, - logger, + sandworm: {loadSandworm}, } = require('sandworm-utils'); const appPath = process.env.SANDWORM_APP_PATH || path.join(__dirname, '..', '..', '..'); -let config; -try { - config = loadConfig(appPath); - if (config) { - logger.log('Config loaded'); - } -} catch (error) { - logger.log('Error loading config:', error.message); -} - -globalThis.SANDWORM_CONFIG = config; - -// Load Sandworm -logger.log('Setting up intercepts...'); -Sandworm.init({ - devMode: true, - aliases: config && config.aliases, -}); -logger.log('Intercepts ready'); +loadSandworm({appPath}); diff --git a/teardown/index.js b/teardown/index.js index 5575df0..51665e7 100644 --- a/teardown/index.js +++ b/teardown/index.js @@ -1,18 +1,13 @@ const path = require('path'); const { recorder: {stopRecordingSandwormActivity, getRecordedActivity}, - files: { - loadDependencies, - writePermissions, - loadPermissions, - SANDWORM_PERMISSION_FILE_NAME, - }, - permissions: {getPermissionsFromActivity, comparePermissions}, + files: {loadDependencies, loadConfig, writePermissions, loadPermissions, SANDWORM_PERMISSION_FILE_NAME}, + permissions: {getPermissionsFromActivity, getPackagePermissions, comparePermissions}, logger, } = require('sandworm-utils'); const appPath = process.env.SANDWORM_APP_PATH || path.join(__dirname, '..', '..', '..'); -const config = globalThis.SANDWORM_CONFIG; +const config = loadConfig(appPath); module.exports = async () => { const activity = getRecordedActivity(); @@ -25,62 +20,25 @@ module.exports = async () => { config && Array.isArray(config.ignoredModules) ? config.ignoredModules : []; const permissions = getPermissionsFromActivity(activity); - const newPermissions = permissions - // Filter out dev dependencies - .filter(({module}) => { - const moduleNames = module.split('>'); - const devModules = moduleNames.filter( - (name) => devDependencies.includes(name) && !prodDependencies.includes(name), - ); - return devModules.length === 0; - }) - // Filter out explicitly ignored modules - .filter(({module}) => !ignoredModules.includes(module)); - const currentPermissions = loadPermissions(appPath); + const newPermissions = getPackagePermissions({ + permissions, + devDependencies, + prodDependencies, + ignoredModules, + }); if (!currentPermissions) { await writePermissions(appPath, newPermissions); - logger.warn("It looks like this is the first time you're running Sandworm."); - logger.warn( - "A `package-permissions.json` file has been created in your app's root directory, with the per-module permissions detected during your test suite run.", - ); - logger.warn( - 'Please audit this file to understand why each item is required, then commit the file to your repository.', - ); - logger.warn('The next test run will validate permissions against this snapshot.'); + logger.logTestPluginFirstRunMessage(); } else { - const changes = comparePermissions(currentPermissions, newPermissions); - const messages = []; - - changes.forEach(({module, type, permission}) => { - switch (type) { - case 'removed-module': - messages.push(` * Existing \`${module}\` module is no longer in use`); - break; - case 'added-module': - messages.push(` * New module \`${module}\` has been added`); - break; - case 'removed-permission': - messages.push( - ` * Permission \`${permission}\` is no longer in use for module \`${module}\``, - ); - break; - case 'added-permission': - messages.push(` * Permission \`${permission}\` has been added to module \`${module}\``); - break; - default: - break; - } - }); + const {changes, messages} = comparePermissions(currentPermissions, newPermissions); if (changes.length === 0) { logger.success('✔ Permission snapshot matches current test run'); } - if( - changes.length > 0 - ) { + if (changes.length > 0) { throw new Error( `Sandworm: Permission mismatch:\n${messages.join( '\n', diff --git a/yarn.lock b/yarn.lock index dadbd61..0d34843 100644 --- a/yarn.lock +++ b/yarn.lock @@ -208,9 +208,9 @@ strip-json-comments "^3.1.1" "@humanwhocodes/config-array@^0.10.5": - version "0.10.5" - resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.10.5.tgz#bb679745224745fff1e9a41961c1d45a49f81c04" - integrity sha512-XVVDtp+dVvRxMoxSiSfasYaG02VEe1qH5cKgMQJWhol6HwzbcqoCMJi8dAGoYAO57jhUyhI6cWuRiTcRaDaYug== + version "0.10.7" + resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.10.7.tgz#6d53769fd0c222767e6452e8ebda825c22e9f0dc" + integrity sha512-MDl6D6sBsaV452/QSdX+4CXIjZhIcI0PELsxUjk4U828yd58vk3bTIvk/6w5FY+4hIy9sLW0sfrV7K7Kc++j/w== dependencies: "@humanwhocodes/object-schema" "^1.2.1" debug "^4.1.1" @@ -868,9 +868,9 @@ conventional-commits-parser@^3.2.2: through2 "^4.0.0" cosmiconfig-typescript-loader@^4.0.0: - version "4.1.0" - resolved "https://registry.yarnpkg.com/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-4.1.0.tgz#39b2f8e302d587d27a2d5fc10106635abef4a6e4" - integrity sha512-HbWIuR5O+XO5Oj9SZ5bzgrD4nN+rfhrm2PMb0FVx+t+XIvC45n8F0oTNnztXtspWGw0i2IzHaUWFD5LzV1JB4A== + version "4.1.1" + resolved "https://registry.yarnpkg.com/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-4.1.1.tgz#38dd3578344038dae40fdf09792bc2e9df529f78" + integrity sha512-9DHpa379Gp0o0Zefii35fcmuuin6q92FnLDffzdZ0l9tVd3nEobG3O+MZ06+kuBvFTSVScvNb/oHA13Nd4iipg== cosmiconfig@^7.0.0: version "7.0.1" @@ -2572,17 +2572,18 @@ safer-buffer@^2.0.2: resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg== -sandworm-utils@1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/sandworm-utils/-/sandworm-utils-1.2.0.tgz#7d58bf6c1525616ea5fa1eb96dcf1ee5bd921087" - integrity sha512-zD6bR3AFXycsCWOWKEc8f7fOgi7t8LXU5Fk6ZEoNSvF0HfJe2U+WVdXXUjhv8sluOrXsQkfLuDp3LeaITaaKag== +sandworm-utils@1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/sandworm-utils/-/sandworm-utils-1.4.0.tgz#416af0830aca22c70860ffc2c022453fbcd9c5f2" + integrity sha512-dgKSAP2ux5q79BJ7fogxUb7aO07RFWIwNblDbLdi7wYYQbTpZ6dqU4ESoLZhXxRq1W5YAyFqqdUeojMiQiKiCA== dependencies: + sandworm "1.3.1" snyk-nodejs-lockfile-parser "1.43.1" -sandworm@1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/sandworm/-/sandworm-1.3.0.tgz#64a919a95746e958ae5ea378522f6baeee0861e0" - integrity sha512-WrxvNnHkE7OeuyESyXPsPyEa+auQvsIZW+HMzzHGWZNM7hS1qIzxZNOSuB6zI50JPyy+PVuXmmzDFhHv0GOLRA== +sandworm@1.3.1: + version "1.3.1" + resolved "https://registry.yarnpkg.com/sandworm/-/sandworm-1.3.1.tgz#ac078ddfff73673b66b0ecf42a9b945d7b2e903a" + integrity sha512-saTfLxa82NSxXaDyHjwbwgxMIGWYwOcHLJqeecgJsv6VmKWFRc5xxjLjXPAq1EJ01MJbULK401ypXLx82oCiwQ== dependencies: source-map-js "1.0.2" @@ -2932,9 +2933,9 @@ type-fest@^0.8.1: integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA== typescript@^4.6.4: - version "4.8.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.3.tgz#d59344522c4bc464a65a730ac695007fdb66dd88" - integrity sha512-goMHfm00nWPa8UvR/CPSvykqf6dVV8x/dp0c5mFTMTIu0u0FlGWRioyy7Nn0PGAdHxpJZnuO/ut+PpQ8UiHAig== + version "4.8.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6" + integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ== unbox-primitive@^1.0.2: version "1.0.2"