Skip to content

Commit

Permalink
Update deps to secure version disable log by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Roettig committed Jan 24, 2022
1 parent d37c416 commit da53a66
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 316 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"prettier-eslint": "^8.2.2",
"semantic-release": "^18.0.1",
"ts-jest": "^27.0.5",
"typedoc": "^0.22.5",
"typedoc-plugin-markdown": "^3.11.3",
"typedoc": "^0.22.11",
"typedoc-plugin-markdown": "^3.11.12",
"typescript": "^4.3.5"
},
"keywords": [
Expand Down
103 changes: 64 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { stat: fsStat } = require('fs/promises');
const { requireTargetFile } = require('./requireTargetFile');

enum LogLevel {
'none' = -1,
'warn' = 0,
'debug' = 2,
}
Expand All @@ -21,17 +22,17 @@ class ExtendLocalIntercept {

constructor(
targetables: any,
options: ExtendInterceptOptions = { logLevel: LogLevel.warn },
options: ExtendInterceptOptions = { logLevel: LogLevel.none },
) {
this.targetables = targetables;
this.logLevel = options.logLevel;
}

/**
* @param fileExtension
* @param targetablesSearchPaths
* @param magentoPath
*/
* @param fileExtension
* @param targetablesSearchPaths
* @param magentoPath
*/
public allowCustomTargetables = async (
fileExtension = '*.targetables.js',
targetablesSearchPaths = ['src/components', 'src/RootComponents'],
Expand All @@ -50,9 +51,10 @@ class ExtendLocalIntercept {
fileExtension.length - 3,
);

const callBack = (file: string) => file
.replace(pathReplacement, '')
.replace(replaceRegex, `${magentoPath}/venia-ui/lib/$<type>`);
const callBack = (file: string) =>
file
.replace(pathReplacement, '')
.replace(replaceRegex, `${magentoPath}/venia-ui/lib/$<type>`);

const compListMap = await this.resolveCoreFiles(paths, callBack);
compListMap.forEach((props) => {
Expand All @@ -61,25 +63,36 @@ class ExtendLocalIntercept {
relativePath.replace('node_modules/', ''),
);

this.log(LogLevel.debug, 'Intercept', `${currentPath}/${myPath}`);
this.log(
LogLevel.debug,
'Intercept',
`${currentPath}/${myPath}`,
);

const componentInterceptor = requireTargetFile(
`${currentPath}/${myPath}`,
);

if (componentInterceptor && componentInterceptor.interceptComponent) {
if (
componentInterceptor &&
componentInterceptor.interceptComponent
) {
componentInterceptor.interceptComponent(component);
} else {
this.log(LogLevel.warn, 'No interceptComponent export in', `${currentPath}/${myPath}`);
this.log(
LogLevel.warn,
'No interceptComponent export in',
`${currentPath}/${myPath}`,
);
}
});
};

/**
* @param fileExtension
* @param targetablesSearchPaths
* @param magentoPath
*/
* @param fileExtension
* @param targetablesSearchPaths
* @param magentoPath
*/
public allowCssOverwrites = async (
fileExtension = '*.module.css',
targetablesSearchPaths = ['src/components', 'src/RootComponents'],
Expand All @@ -91,19 +104,20 @@ class ExtendLocalIntercept {
);

const replaceRegex = this.buildRegex(targetablesSearchPaths);
const callBack = (file: string) => file.replace(
replaceRegex,
`${magentoPath}/venia-ui/lib/$<type>`,
);
const callBack = (file: string) =>
file.replace(
replaceRegex,
`${magentoPath}/venia-ui/lib/$<type>`,
);

const compListMap = await this.resolveCoreFiles(paths, callBack);

compListMap.forEach((props) => {
const { relativePath, myPath } = props;

/* This means we have matched a local file to something in venia-ui!
* Find the JS component from our CSS file name
*/
* Find the JS component from our CSS file name
*/
const jsComponent = relativePath
.replace('node_modules/', '')
.replace(fileExtension.substring(1), '.js');
Expand All @@ -120,24 +134,36 @@ class ExtendLocalIntercept {
});
};

private resolveCoreFiles = async (paths : string[], callBack: (path: string) => string) => {
private resolveCoreFiles = async (
paths: string[],
callBack: (path: string) => string,
) => {
const currentPath = process.cwd();

const relativePathMap: { myPath: string; relativePath: string; }[] = [];

await Promise.all(paths.map(async (myPath: string) => {
const relativePath = callBack(myPath);
const absolutePath = path.resolve(currentPath, relativePath);

try {
const stat = await fsStat(absolutePath);
if (stat && stat.isFile()) {
relativePathMap.push({ myPath, relativePath });
const relativePathMap: {
myPath: string;
relativePath: string;
}[] = [];

await Promise.all(
paths.map(async (myPath: string) => {
const relativePath = callBack(myPath);
const absolutePath = path.resolve(currentPath, relativePath);

try {
const stat = await fsStat(absolutePath);
if (stat && stat.isFile()) {
relativePathMap.push({ myPath, relativePath });
}
} catch (error) {
this.log(
LogLevel.warn,
'File not exits in core',
absolutePath,
);
}
} catch (error) {
this.log(LogLevel.warn, 'File not exits in core', absolutePath);
}
}));
}),
);

return relativePathMap;
};
Expand All @@ -163,9 +189,8 @@ class ExtendLocalIntercept {
return this.componentsCache[modulePath];
}

this.componentsCache[modulePath] = this.targetables.reactComponent(
modulePath,
);
this.componentsCache[modulePath] =
this.targetables.reactComponent(modulePath);

return this.componentsCache[modulePath];
};
Expand Down

0 comments on commit da53a66

Please sign in to comment.