Skip to content

Commit eb66ab3

Browse files
committedMar 22, 2024
Adding Page schematics
1 parent e72b824 commit eb66ab3

8 files changed

+170
-8
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<% if(layoutName) { %>---
2+
import <%= layoutName %> from '<%= relativeLayoutPath %>';
3+
---
4+
<layoutName>
5+
</layoutName><% } else { %>---
6+
---<% }%>
7+
8+

‎packages/astro/src/builder-generate/page/files/__name@classify__.md.template

Whitespace-only changes.

‎packages/astro/src/builder-generate/page/files/__name@classify__.mdx.template

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { askQuestion } from '../../utils';
2+
3+
export async function askLayout(layouts: string[]): Promise<string> {
4+
const message = 'Do you want to use any layout?';
5+
return await askQuestion(message, ['none', ...layouts], layouts[0]);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"$id": "EmptyFolder",
4+
"title": "Add a page",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string",
9+
"description": "The name of the page.",
10+
"$default": {
11+
"$source": "argv",
12+
"index": 0
13+
},
14+
"x-prompt": "What name would you like to use for the page?"
15+
},
16+
"layout": {
17+
"type": "string",
18+
"description": "layout to use"
19+
},
20+
"type": {
21+
"type": "string",
22+
"enum": [
23+
"astro",
24+
"md",
25+
"mdx",
26+
"html"
27+
],
28+
"default": "astro"
29+
}
30+
}
31+
}

‎packages/astro/src/collection.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
]
1919
},
2020
"page": {
21-
"description": "",
22-
"factory": "",
23-
"schema": "",
21+
"description": "Allows to create astro page",
22+
"factory": "./builder-generate/page/page.factory#pageFactory",
23+
"schema": "./builder-generate/page/schema.json",
2424
"aliases": [
2525
"p"
2626
]

‎packages/astro/src/utils/prompt.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ export async function askConfirmation(message: string, defaultResponse: boolean)
2424
return answers['confirmation'];
2525
}
2626

27-
export async function askQuestion(
28-
message: string,
29-
choices: ListChoiceOptions[],
30-
defaultResponseIndex: number,
31-
): Promise<string | null> {
27+
export async function askQuestion<T>(message: string, choices: T[], defaultResponseIndex: T): Promise<T | null> {
3228
const question: ListQuestion = {
3329
type: 'list',
3430
name: 'answer',

0 commit comments

Comments
 (0)
Failed to load comments.