Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function getRouteConfigFromDir(paths) {
const { cwd, absPagesPath, absSrcPath, dirPath = '' } = paths;
const absPath = join(absPagesPath, dirPath);
const files = readdirSync(absPath);
const absLayoutFile = findJS(absPagesPath, '_layout');
if (absLayoutFile) {
throw new Error('root _layout.js is not supported, use layouts/index.js instead');
}
const routes = sortRoutes(
files
.filter(file => {
if (
file.charAt(0) === '.' ||
file.charAt(0) === '_' ||
/\.(test|spec|d)\.(j|t)sx?$/.test(file)
)
return false;
return true;
})
.reduce(handleFile.bind(null, paths, absPath), []),
.filter(file => {
if (
file.charAt(0) === '.' ||
file.charAt(0) === '_' ||
/\.(test|spec)\.(j|t)sx?$/.test(file)
) {
return false;
}
return true;
})
.reduce(handleFile.bind(null, paths, absPath), []),
);
if (dirPath === '' && absSrcPath) {
const globalLayoutFile =
findJS(absSrcPath, 'layouts/index') || findJS(absSrcPath, 'layout/index');
if (globalLayoutFile) {
const wrappedRoutes = [];
addRoute(
wrappedRoutes,
{
path: '/',
component: `./${winPath(relative(cwd, globalLayoutFile))}`,
routes,
},
{
componentFile: globalLayoutFile,
},
);
return wrappedRoutes;
}
}
files
.filter(file => {
if (
file.charAt(0) === '.' ||
file.charAt(0) === '_' ||
/\.(test|spec|d)\.(j|t)sx?$/.test(file)
)
return false;
return true;
})
.reduce(handleFile.bind(null, paths, absPath), []),
);
if (dirPath === '' && absSrcPath) {
const globalLayoutFile =
findJS(absSrcPath, 'layouts/index') || findJS(absSrcPath, 'layout/index');
if (globalLayoutFile) {
const wrappedRoutes = [];
addRoute(
wrappedRoutes,
{
path: '/',
component: `./${winPath(relative(cwd, globalLayoutFile))}`,
routes,
},
{
componentFile: globalLayoutFile,
},
);
return wrappedRoutes;
}
}
function getDvaJS() {
const dvaJS = findJS(paths.absSrcPath, 'dva');
if (dvaJS) {
return winPath(dvaJS);
}
}
})
.map((source, index) => {
return {
source,
specifier: `moduleBeforeRenderer${index}`,
};
});
const plugins = this.service
.applyPlugins('addRuntimePlugin', {
initialValue: [],
})
.map(plugin => {
return winPath(relative(paths.absTmpDirPath, plugin));
});
if (findJS(paths.absSrcPath, 'app')) {
plugins.push('@/app');
}
const validKeys = this.service.applyPlugins('addRuntimePluginKey', {
initialValue: [
'patchRoutes',
'render',
'rootContainer',
'modifyRouteProps',
'onRouteChange',
'modifyInitialProps',
'initialProps',
],
});
assert(
uniq(validKeys).length === validKeys.length,
`Conflict keys found in [${validKeys.join(', ')}]`,
export function getModel(cwd, api) {
const { config, winPath } = api;
const modelJSPath = findJS(cwd, 'model');
if (modelJSPath) {
return [winPath(modelJSPath)];
}
return globby
.sync(`./${config.singular ? 'model' : 'models'}/**/*.{ts,tsx,js,jsx}`, {
cwd,
})
.filter(
p =>
!p.endsWith('.d.ts') &&
!p.endsWith('.test.js') &&
!p.endsWith('.test.jsx') &&
!p.endsWith('.test.ts') &&
!p.endsWith('.test.tsx'),
)