fix: make create-astro component injection less brittle (#623)
This commit is contained in:
parent
bcf715df7a
commit
ff89a6a7a6
1 changed files with 8 additions and 24 deletions
|
@ -159,30 +159,14 @@ export async function main() {
|
||||||
|
|
||||||
const pageFileLoc = path.resolve(path.join(cwd, 'src', 'pages', 'index.astro'));
|
const pageFileLoc = path.resolve(path.join(cwd, 'src', 'pages', 'index.astro'));
|
||||||
const content = (await fs.promises.readFile(pageFileLoc)).toString();
|
const content = (await fs.promises.readFile(pageFileLoc)).toString();
|
||||||
const lines = content.split('\n');
|
const newContent = content
|
||||||
const indent = ' ';
|
.replace(/^(\s*)\/\* ASTRO\:COMPONENT_IMPORTS \*\//gm, (_, indent) => {
|
||||||
const doc = `\n<!--
|
return indent + importStatements.join('\n');
|
||||||
- Use imported Framework Components directly in your markup!
|
})
|
||||||
-
|
.replace(/^(\s*)<!-- ASTRO:COMPONENT_MARKUP -->/gm, (_, indent) => {
|
||||||
- Note: by default, components are NOT interactive on the client.
|
return components.map(ln => indent + ln).join('\n');
|
||||||
- The \`:visible\` directive tells Astro to make it interactive.
|
});
|
||||||
-
|
await fs.promises.writeFile(pageFileLoc, newContent);
|
||||||
- See https://github.com/snowpackjs/astro/blob/main/docs/core-concepts/component-hydration.md
|
|
||||||
-->
|
|
||||||
`;
|
|
||||||
lines.splice(
|
|
||||||
41,
|
|
||||||
0,
|
|
||||||
importStatements.length > 0
|
|
||||||
? doc
|
|
||||||
.split('\n')
|
|
||||||
.map((ln) => `${indent}${ln}`)
|
|
||||||
.join('\n')
|
|
||||||
: '',
|
|
||||||
...components.map((ln) => `${indent}${ln}`)
|
|
||||||
);
|
|
||||||
lines.splice(3, 0, importStatements.length > 0 ? `// Framework Component Imports` : '', ...importStatements);
|
|
||||||
await fs.promises.writeFile(pageFileLoc, lines.join('\n'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(bold(green('✔') + ' Done!'));
|
console.log(bold(green('✔') + ' Done!'));
|
||||||
|
|
Loading…
Reference in a new issue