|
| 1 | +import { |
| 2 | + MergeStrategy, |
| 3 | + Rule, |
| 4 | + Tree, |
| 5 | + apply, |
| 6 | + applyTemplates, |
| 7 | + filter, |
| 8 | + mergeWith, |
| 9 | + move, |
| 10 | + renameTemplateFiles, |
| 11 | + url, |
| 12 | +} from '@angular-devkit/schematics'; |
| 13 | +import { logger, parseName } from '../../utils'; |
| 14 | +import { askLayout } from './page.questions'; |
| 15 | +import { strings } from '@angular-devkit/schematics'; |
| 16 | + |
| 17 | +export function pageFactory({ |
| 18 | + name, |
| 19 | + layout, |
| 20 | + type, |
| 21 | +}: { |
| 22 | + name: string; |
| 23 | + layout: string; |
| 24 | + type: 'astro' | 'md' | 'mdx'; |
| 25 | +}): Rule { |
| 26 | + return async (tree: Tree) => { |
| 27 | + const { name: pageName, path } = parseName('./', name); |
| 28 | + |
| 29 | + // logger.debug(parsedName); |
| 30 | + // Read layouts |
| 31 | + if (!layout) { |
| 32 | + const layoutNames = readLayouts(tree); |
| 33 | + |
| 34 | + if (layoutNames.length > 0) { |
| 35 | + layout = await askLayout(layoutNames.map((x) => strings.classify(x))); |
| 36 | + } |
| 37 | + |
| 38 | + if (layout === 'none') { |
| 39 | + layout = undefined; |
| 40 | + } |
| 41 | + } else { |
| 42 | + const layouts = tree.getDir('./src/layouts').subfiles; |
| 43 | + layout = strings.classify(layout); |
| 44 | + if (!layouts.find((x) => x === `${layout}.astro`)) { |
| 45 | + logger.error(`The layout ${layout} doesn't exist`); |
| 46 | + process.exit(1); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // If choose any layout depends how deep wil the page will be needs to have the relative path from page to layout |
| 51 | + const relativePathLayout = getRelativeLayoutPath(layout, path); |
| 52 | + |
| 53 | + // Support Files type |
| 54 | + // The page named can has a folders |
| 55 | + |
| 56 | + return addPageFile(type, relativePathLayout, layout, pageName, path); |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +function getRelativeLayoutPath(layoutName: string, pagePath: string) { |
| 61 | + const foldersBackCount = pagePath.split('/').filter((x) => x).length + 1; |
| 62 | + return `${new Array(foldersBackCount).fill('..').join('/')}/layouts/${layoutName}.astro`; |
| 63 | +} |
| 64 | + |
| 65 | +function readLayouts(tree: Tree): string[] { |
| 66 | + const layouts: string[] = []; |
| 67 | + const layoutsDirectoryPath = './src/layouts'; |
| 68 | + |
| 69 | + if (tree.getDir(layoutsDirectoryPath).subfiles.length === 0) { |
| 70 | + return layouts; |
| 71 | + } |
| 72 | + |
| 73 | + return tree.getDir(layoutsDirectoryPath).subfiles.map((x) => x.replaceAll('.astro', '')); |
| 74 | + |
| 75 | + //TODO: support layouts in subfolders |
| 76 | + // layoutFiles.forEach((file) => { |
| 77 | + // const filePath = `${layoutsDirectoryPath}/${file}`; |
| 78 | + // const content = tree.read(filePath); |
| 79 | + // if (content) { |
| 80 | + // // You can now do something with the content, like parsing it |
| 81 | + // logger.debug(content.toString('utf-8')); |
| 82 | + // } |
| 83 | + // }); |
| 84 | +} |
| 85 | + |
| 86 | +function addPageFile( |
| 87 | + fileType: 'astro' | 'md' | 'mdx', |
| 88 | + relativeLayoutPath: string, |
| 89 | + layoutName: string, |
| 90 | + pageName: string, |
| 91 | + pagePath: string, |
| 92 | +): Rule { |
| 93 | + let basePath = './src/pages'; |
| 94 | + |
| 95 | + // if (!tree.getDir('/').subdirs.find((f) => f === 'src')) { |
| 96 | + // basePath = './pages'; |
| 97 | + // } else { |
| 98 | + // basePath = basePath + '/pages'; |
| 99 | + // } |
| 100 | + |
| 101 | + const pageTypes = { |
| 102 | + astro: '__name@classify__.astro.template', |
| 103 | + md: '__pageName@classify__.md.template', |
| 104 | + mdx: '__pageName@classify__.mdx.template', |
| 105 | + }; |
| 106 | + |
| 107 | + const urlTemplates = [pageTypes[fileType]]; |
| 108 | + |
| 109 | + const template = apply(url('./files'), [ |
| 110 | + filter((path) => urlTemplates.some((urlTemplate) => path.includes(urlTemplate))), |
| 111 | + applyTemplates({ |
| 112 | + name: pageName, |
| 113 | + relativeLayoutPath, |
| 114 | + layoutName, |
| 115 | + ...strings, |
| 116 | + }), |
| 117 | + renameTemplateFiles(), |
| 118 | + move(`${basePath}${pagePath}`), |
| 119 | + ]); |
| 120 | + return mergeWith(template, MergeStrategy.Overwrite); |
| 121 | +} |
0 commit comments