Skip to content

Commit

Permalink
Remove lodash as a dependency from @plone/registry (plone#5726)
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh authored Feb 2, 2024
1 parent c734f53 commit 3278991
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 59 deletions.
1 change: 1 addition & 0 deletions packages/registry/news/5726.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove `lodash` as a dependency @sneridagh
3 changes: 1 addition & 2 deletions packages/registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
"crypto-random-string": "3.2.0",
"debug": "4.3.2",
"dependency-graph": "0.10.0",
"glob": "7.1.6",
"lodash": "4.17.21"
"glob": "7.1.6"
},
"devDependencies": {
"@parcel/packager-ts": "2.10.2",
Expand Down
98 changes: 47 additions & 51 deletions packages/registry/src/addon-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const glob = require('glob').sync;
const path = require('path');
const fs = require('fs');
const debug = require('debug')('shadowing');
const { map } = require('lodash');
const { DepGraph } = require('dependency-graph');

function getPackageBasePath(base) {
Expand Down Expand Up @@ -440,58 +439,55 @@ class AddonConfigurationRegistry {
);

reg.forEach(({ customPath, name, sourcePath }) => {
map(
glob(
`${customPath}/**/*.*(svg|png|jpg|jpeg|gif|ico|less|js|jsx|ts|tsx)`,
),
(filename) => {
function changeFileExtension(filePath) {
// Extract the current file extension
const currentExtension = filePath.split('.').pop();

// Define the mapping between file extensions
const extensionMapping = {
jsx: 'tsx',
tsx: 'jsx',
js: 'ts',
ts: 'js',
};

// Check if the current extension is in the mapping
if (currentExtension in extensionMapping) {
// Replace the current extension with the corresponding one from the mapping
const newExtension = extensionMapping[currentExtension];
const newPath = filePath.replace(
`.${currentExtension}`,
`.${newExtension}`,
);
return newPath;
} else {
// If the current extension is not in the mapping, return the original path
return filePath;
}
}

const targetPath = filename.replace(customPath, sourcePath);
// We try to find the source to shadow with the exact path
// and we try also with the extension changed in search for JS<->TS
// correspondence
if (
fs.existsSync(targetPath) ||
fs.existsSync(changeFileExtension(targetPath))
) {
aliases[
filename
.replace(customPath, name)
.replace(/\.(js|jsx|ts|tsx)$/, '')
] = path.resolve(filename);
} else {
debug(
`The file ${filename} doesn't exist in the ${name} (${targetPath}), unable to customize.`,
glob(
`${customPath}/**/*.*(svg|png|jpg|jpeg|gif|ico|less|js|jsx|ts|tsx)`,
).map((filename) => {
function changeFileExtension(filePath) {
// Extract the current file extension
const currentExtension = filePath.split('.').pop();

// Define the mapping between file extensions
const extensionMapping = {
jsx: 'tsx',
tsx: 'jsx',
js: 'ts',
ts: 'js',
};

// Check if the current extension is in the mapping
if (currentExtension in extensionMapping) {
// Replace the current extension with the corresponding one from the mapping
const newExtension = extensionMapping[currentExtension];
const newPath = filePath.replace(
`.${currentExtension}`,
`.${newExtension}`,
);
return newPath;
} else {
// If the current extension is not in the mapping, return the original path
return filePath;
}
},
);
}

const targetPath = filename.replace(customPath, sourcePath);
// We try to find the source to shadow with the exact path
// and we try also with the extension changed in search for JS<->TS
// correspondence
if (
fs.existsSync(targetPath) ||
fs.existsSync(changeFileExtension(targetPath))
) {
aliases[
filename
.replace(customPath, name)
.replace(/\.(js|jsx|ts|tsx)$/, '')
] = path.resolve(filename);
} else {
debug(
`The file ${filename} doesn't exist in the ${name} (${targetPath}), unable to customize.`,
);
}
});
});
});

Expand Down
5 changes: 2 additions & 3 deletions packages/registry/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isArray } from 'lodash';
import type {
AddonReducersConfig,
AddonRoutesConfig,
Expand Down Expand Up @@ -129,7 +128,7 @@ class Config {
if (typeof options === 'object') {
const { name, dependencies = '' } = options;
let depsString: string = '';
if (dependencies && isArray(dependencies)) {
if (dependencies && Array.isArray(dependencies)) {
depsString = dependencies.join('+');
} else if (typeof dependencies === 'string') {
depsString = dependencies;
Expand All @@ -154,7 +153,7 @@ class Config {
if (!component) {
throw new Error('No component provided');
} else {
if (dependencies && isArray(dependencies)) {
if (dependencies && Array.isArray(dependencies)) {
depsString = dependencies.join('+');
} else if (typeof dependencies === 'string') {
depsString = dependencies;
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3278991

Please sign in to comment.