create-astro 5.0.2 → 5.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/dependencies.d.ts +1 -1
- package/dist/actions/template.d.ts +1 -0
- package/dist/index.js +194 -177
- package/package.json +3 -3
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Context } from './context.js';
|
|
2
|
-
export declare function dependencies(ctx: Pick<Context, 'install' | 'yes' | 'prompt' | 'packageManager' | 'cwd' | 'dryRun' | 'tasks' | 'add'>): Promise<void>;
|
|
2
|
+
export declare function dependencies(ctx: Pick<Context, 'install' | 'yes' | 'prompt' | 'packageManager' | 'cwd' | 'dryRun' | 'tasks' | 'add' | 'template'>): Promise<void>;
|
|
@@ -15,3 +15,4 @@ export declare function removeTemplateMarkerSections(content: string): string;
|
|
|
15
15
|
export declare function processTemplateReadme(content: string, packageManager: string): string;
|
|
16
16
|
export declare function template(ctx: Pick<Context, 'template' | 'prompt' | 'yes' | 'dryRun' | 'exit' | 'tasks'>): Promise<void>;
|
|
17
17
|
export declare function getTemplateTarget(tmpl: string, ref?: string): string;
|
|
18
|
+
export declare function isThirdPartyTemplate(tmpl: string): boolean;
|
package/dist/index.js
CHANGED
|
@@ -291,17 +291,22 @@ import { spawn } from "node:child_process";
|
|
|
291
291
|
import { text as textFromStream } from "node:stream/consumers";
|
|
292
292
|
var WINDOWS_CMD_SHIMS = /* @__PURE__ */ new Set(["npm", "npx", "pnpm", "pnpx", "yarn", "yarnpkg", "bun", "bunx"]);
|
|
293
293
|
var text = (stream) => stream ? textFromStream(stream).then((t) => t.trimEnd()) : "";
|
|
294
|
-
function resolveCommand(command) {
|
|
295
|
-
if (process.platform !== "win32") return command;
|
|
296
|
-
if (command.includes("/") || command.includes("\\") || command.includes("."))
|
|
297
|
-
|
|
294
|
+
function resolveCommand(command, flags) {
|
|
295
|
+
if (process.platform !== "win32") return [command, flags];
|
|
296
|
+
if (command.includes("/") || command.includes("\\") || command.includes("."))
|
|
297
|
+
return [command, flags];
|
|
298
|
+
if (WINDOWS_CMD_SHIMS.has(command.toLowerCase())) {
|
|
299
|
+
return ["cmd.exe", ["/d", "/s", "/c", `${command}.cmd`, ...flags]];
|
|
300
|
+
}
|
|
301
|
+
return [command, flags];
|
|
298
302
|
}
|
|
299
303
|
async function shell(command, flags, opts = {}) {
|
|
300
304
|
let child;
|
|
301
305
|
let stdout2 = "";
|
|
302
306
|
let stderr = "";
|
|
303
307
|
try {
|
|
304
|
-
|
|
308
|
+
const [resolvedCommand, resolvedFlags] = resolveCommand(command, flags);
|
|
309
|
+
child = spawn(resolvedCommand, resolvedFlags, {
|
|
305
310
|
cwd: opts.cwd,
|
|
306
311
|
stdio: opts.stdio,
|
|
307
312
|
timeout: opts.timeout
|
|
@@ -311,15 +316,16 @@ async function shell(command, flags, opts = {}) {
|
|
|
311
316
|
child.once("close", () => resolve());
|
|
312
317
|
});
|
|
313
318
|
[stdout2, stderr] = await Promise.all([text(child.stdout), text(child.stderr), done]);
|
|
314
|
-
} catch {
|
|
315
|
-
|
|
319
|
+
} catch (e) {
|
|
320
|
+
const message = e instanceof Error ? e.message : stderr || "Unknown error";
|
|
321
|
+
throw new Error(message);
|
|
316
322
|
}
|
|
317
323
|
const { exitCode } = child;
|
|
318
324
|
if (exitCode === null) {
|
|
319
325
|
throw new Error("Timeout");
|
|
320
326
|
}
|
|
321
327
|
if (exitCode !== 0) {
|
|
322
|
-
throw new Error(stderr);
|
|
328
|
+
throw new Error(stderr || `Process exited with code ${exitCode}`);
|
|
323
329
|
}
|
|
324
330
|
return { stdout: stdout2, stderr, exitCode };
|
|
325
331
|
}
|
|
@@ -459,7 +465,7 @@ function printHelp({
|
|
|
459
465
|
if (headline) {
|
|
460
466
|
message.push(
|
|
461
467
|
linebreak(),
|
|
462
|
-
`${title(commandName)} ${color.green(`v${"5.0.
|
|
468
|
+
`${title(commandName)} ${color.green(`v${"5.0.4"}`)} ${headline}`
|
|
463
469
|
);
|
|
464
470
|
}
|
|
465
471
|
if (usage) {
|
|
@@ -558,7 +564,7 @@ async function getContext(argv) {
|
|
|
558
564
|
packageManager,
|
|
559
565
|
"astro",
|
|
560
566
|
getPackageTag(packageSpecifier),
|
|
561
|
-
"6.
|
|
567
|
+
"6.1.1"
|
|
562
568
|
),
|
|
563
569
|
skipHouston,
|
|
564
570
|
fancy,
|
|
@@ -589,8 +595,8 @@ function detectPackageManager() {
|
|
|
589
595
|
}
|
|
590
596
|
|
|
591
597
|
// src/actions/dependencies.ts
|
|
592
|
-
import
|
|
593
|
-
import
|
|
598
|
+
import fs2 from "node:fs";
|
|
599
|
+
import path2 from "node:path";
|
|
594
600
|
|
|
595
601
|
// ../internal-helpers/dist/cli.js
|
|
596
602
|
var NPM_PACKAGE_NAME_REGEX = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
|
|
@@ -606,7 +612,154 @@ function assertValidPackageName(packageName) {
|
|
|
606
612
|
}
|
|
607
613
|
|
|
608
614
|
// src/actions/dependencies.ts
|
|
615
|
+
import { color as color3 } from "@astrojs/cli-kit";
|
|
616
|
+
|
|
617
|
+
// src/actions/template.ts
|
|
618
|
+
import fs from "node:fs";
|
|
619
|
+
import path from "node:path";
|
|
609
620
|
import { color as color2 } from "@astrojs/cli-kit";
|
|
621
|
+
import { downloadTemplate } from "@bluwy/giget-core";
|
|
622
|
+
function removeTemplateMarkerSections(content) {
|
|
623
|
+
const pattern = /<!--\s*ASTRO:REMOVE:START\s*-->[\s\S]*?<!--\s*ASTRO:REMOVE:END\s*-->/gi;
|
|
624
|
+
let result = content.replace(pattern, "");
|
|
625
|
+
result = result.replace(/\n{3,}/g, "\n\n");
|
|
626
|
+
return result;
|
|
627
|
+
}
|
|
628
|
+
function processTemplateReadme(content, packageManager) {
|
|
629
|
+
let processed = removeTemplateMarkerSections(content);
|
|
630
|
+
if (packageManager !== "npm") {
|
|
631
|
+
processed = processed.replace(/\bnpm run\b/g, packageManager).replace(/\bnpm\b/g, packageManager);
|
|
632
|
+
}
|
|
633
|
+
return processed;
|
|
634
|
+
}
|
|
635
|
+
async function template(ctx) {
|
|
636
|
+
if (!ctx.template && ctx.yes) ctx.template = "basics";
|
|
637
|
+
if (ctx.template) {
|
|
638
|
+
await info("tmpl", `Using ${color2.reset(ctx.template)}${color2.dim(" as project template")}`);
|
|
639
|
+
} else {
|
|
640
|
+
const { template: tmpl } = await ctx.prompt({
|
|
641
|
+
name: "template",
|
|
642
|
+
type: "select",
|
|
643
|
+
label: title("tmpl"),
|
|
644
|
+
message: "How would you like to start your new project?",
|
|
645
|
+
initial: "basics",
|
|
646
|
+
choices: [
|
|
647
|
+
{ value: "basics", label: "A basic, helpful starter project", hint: "(recommended)" },
|
|
648
|
+
{ value: "blog", label: "Use blog template" },
|
|
649
|
+
{ value: "starlight", label: "Use docs (Starlight) template" },
|
|
650
|
+
{ value: "minimal", label: "Use minimal (empty) template" }
|
|
651
|
+
]
|
|
652
|
+
});
|
|
653
|
+
ctx.template = tmpl;
|
|
654
|
+
}
|
|
655
|
+
if (ctx.dryRun) {
|
|
656
|
+
await info("--dry-run", `Skipping template copying`);
|
|
657
|
+
} else if (ctx.template) {
|
|
658
|
+
ctx.tasks.push({
|
|
659
|
+
pending: "Template",
|
|
660
|
+
start: "Template copying...",
|
|
661
|
+
end: "Template copied",
|
|
662
|
+
while: () => copyTemplate(ctx.template, ctx).catch((e) => {
|
|
663
|
+
if (e instanceof Error) {
|
|
664
|
+
error("error", e.message);
|
|
665
|
+
process.exit(1);
|
|
666
|
+
} else {
|
|
667
|
+
error("error", "Unable to clone template.");
|
|
668
|
+
process.exit(1);
|
|
669
|
+
}
|
|
670
|
+
})
|
|
671
|
+
});
|
|
672
|
+
} else {
|
|
673
|
+
ctx.exit(1);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
var FILES_TO_REMOVE = ["CHANGELOG.md", ".codesandbox"];
|
|
677
|
+
var FILES_TO_UPDATE = {
|
|
678
|
+
"package.json": (file, overrides) => fs.promises.readFile(file, "utf-8").then((value) => {
|
|
679
|
+
const indent = /(^\s+)/m.exec(value)?.[1] ?? " ";
|
|
680
|
+
return fs.promises.writeFile(
|
|
681
|
+
file,
|
|
682
|
+
JSON.stringify(
|
|
683
|
+
Object.assign(JSON.parse(value), Object.assign(overrides, { private: void 0 })),
|
|
684
|
+
null,
|
|
685
|
+
indent
|
|
686
|
+
),
|
|
687
|
+
"utf-8"
|
|
688
|
+
);
|
|
689
|
+
})
|
|
690
|
+
};
|
|
691
|
+
function getTemplateTarget(tmpl, ref = "latest") {
|
|
692
|
+
if (tmpl === "starlight" || tmpl.startsWith("starlight/")) {
|
|
693
|
+
const [, starter = "basics"] = tmpl.split("/");
|
|
694
|
+
return `github:withastro/starlight/examples/${starter}`;
|
|
695
|
+
}
|
|
696
|
+
if (isThirdPartyTemplate(tmpl)) return tmpl;
|
|
697
|
+
if (ref === "latest") {
|
|
698
|
+
return `github:withastro/astro#examples/${tmpl}`;
|
|
699
|
+
} else {
|
|
700
|
+
return `github:withastro/astro/examples/${tmpl}#${ref}`;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
function isThirdPartyTemplate(tmpl) {
|
|
704
|
+
if (tmpl === "starlight" || tmpl.startsWith("starlight/")) return false;
|
|
705
|
+
return tmpl.includes("/");
|
|
706
|
+
}
|
|
707
|
+
async function copyTemplate(tmpl, ctx) {
|
|
708
|
+
const templateTarget = getTemplateTarget(tmpl, ctx.ref);
|
|
709
|
+
if (!ctx.dryRun) {
|
|
710
|
+
try {
|
|
711
|
+
await downloadTemplate(templateTarget, {
|
|
712
|
+
force: true,
|
|
713
|
+
cwd: ctx.cwd,
|
|
714
|
+
dir: "."
|
|
715
|
+
});
|
|
716
|
+
const readmePath = path.resolve(ctx.cwd, "README.md");
|
|
717
|
+
if (fs.existsSync(readmePath)) {
|
|
718
|
+
const readme = fs.readFileSync(readmePath, "utf8");
|
|
719
|
+
const processedReadme = processTemplateReadme(readme, ctx.packageManager);
|
|
720
|
+
fs.writeFileSync(readmePath, processedReadme);
|
|
721
|
+
}
|
|
722
|
+
} catch (err) {
|
|
723
|
+
if (ctx.cwd !== "." && ctx.cwd !== "./" && !ctx.cwd.startsWith("../")) {
|
|
724
|
+
try {
|
|
725
|
+
fs.rmdirSync(ctx.cwd);
|
|
726
|
+
} catch (_) {
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
if (err.message?.includes("404")) {
|
|
730
|
+
throw new Error(`Template ${color2.reset(tmpl)} ${color2.dim("does not exist!")}`);
|
|
731
|
+
}
|
|
732
|
+
if (err.message) {
|
|
733
|
+
error("error", err.message);
|
|
734
|
+
}
|
|
735
|
+
try {
|
|
736
|
+
if ("cause" in err) {
|
|
737
|
+
error("error", err.cause);
|
|
738
|
+
if ("cause" in err.cause) {
|
|
739
|
+
error("error", err.cause?.cause);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
} catch {
|
|
743
|
+
}
|
|
744
|
+
throw new Error(`Unable to download template ${color2.reset(tmpl)}`);
|
|
745
|
+
}
|
|
746
|
+
const removeFiles = FILES_TO_REMOVE.map(async (file) => {
|
|
747
|
+
const fileLoc = path.resolve(path.join(ctx.cwd, file));
|
|
748
|
+
if (fs.existsSync(fileLoc)) {
|
|
749
|
+
return fs.promises.rm(fileLoc, { recursive: true });
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
const updateFiles = Object.entries(FILES_TO_UPDATE).map(async ([file, update]) => {
|
|
753
|
+
const fileLoc = path.resolve(path.join(ctx.cwd, file));
|
|
754
|
+
if (fs.existsSync(fileLoc)) {
|
|
755
|
+
return update(fileLoc, { name: ctx.projectName });
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
await Promise.all([...removeFiles, ...updateFiles]);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// src/actions/dependencies.ts
|
|
610
763
|
async function dependencies(ctx) {
|
|
611
764
|
let deps = ctx.install ?? ctx.yes;
|
|
612
765
|
if (deps === void 0) {
|
|
@@ -626,6 +779,12 @@ async function dependencies(ctx) {
|
|
|
626
779
|
assertValidPackageName(addValue);
|
|
627
780
|
}
|
|
628
781
|
}
|
|
782
|
+
if (deps && ctx.template && isThirdPartyTemplate(ctx.template)) {
|
|
783
|
+
await info(
|
|
784
|
+
"warn",
|
|
785
|
+
`Third-party template detected. Installing dependencies may run lifecycle scripts. Continue only if you trust this template. Use ${color3.bold("--no-install")} to skip automatic install.`
|
|
786
|
+
);
|
|
787
|
+
}
|
|
629
788
|
if (ctx.dryRun) {
|
|
630
789
|
await info(
|
|
631
790
|
"--dry-run",
|
|
@@ -640,7 +799,7 @@ async function dependencies(ctx) {
|
|
|
640
799
|
error("error", e);
|
|
641
800
|
error(
|
|
642
801
|
"error",
|
|
643
|
-
`Dependencies failed to install, please run ${
|
|
802
|
+
`Dependencies failed to install, please run ${color3.bold(
|
|
644
803
|
ctx.packageManager + " install"
|
|
645
804
|
)} to install them manually after setup.`
|
|
646
805
|
);
|
|
@@ -657,7 +816,7 @@ async function dependencies(ctx) {
|
|
|
657
816
|
error("error", e);
|
|
658
817
|
error(
|
|
659
818
|
"error",
|
|
660
|
-
`Failed to add integrations, please run ${
|
|
819
|
+
`Failed to add integrations, please run ${color3.bold(
|
|
661
820
|
`astro add ${add.join(" ")}`
|
|
662
821
|
)} to install them manually after setup.`
|
|
663
822
|
);
|
|
@@ -687,17 +846,17 @@ async function install({ packageManager, cwd }) {
|
|
|
687
846
|
return shell(packageManager, ["install"], { cwd, timeout: 9e4, stdio: "ignore" });
|
|
688
847
|
}
|
|
689
848
|
async function ensureYarnLock({ cwd }) {
|
|
690
|
-
const yarnLock =
|
|
691
|
-
if (
|
|
692
|
-
return
|
|
849
|
+
const yarnLock = path2.join(cwd, "yarn.lock");
|
|
850
|
+
if (fs2.existsSync(yarnLock)) return;
|
|
851
|
+
return fs2.promises.writeFile(yarnLock, "", { encoding: "utf-8" });
|
|
693
852
|
}
|
|
694
853
|
|
|
695
854
|
// src/actions/git.ts
|
|
696
|
-
import
|
|
697
|
-
import
|
|
698
|
-
import { color as
|
|
855
|
+
import fs3 from "node:fs";
|
|
856
|
+
import path3 from "node:path";
|
|
857
|
+
import { color as color4 } from "@astrojs/cli-kit";
|
|
699
858
|
async function git(ctx) {
|
|
700
|
-
if (
|
|
859
|
+
if (fs3.existsSync(path3.join(ctx.cwd, ".git"))) {
|
|
701
860
|
await info("Nice!", `Git has already been initialized`);
|
|
702
861
|
return;
|
|
703
862
|
}
|
|
@@ -727,7 +886,7 @@ async function git(ctx) {
|
|
|
727
886
|
} else {
|
|
728
887
|
await info(
|
|
729
888
|
ctx.yes === false ? "git [skip]" : "Sounds good!",
|
|
730
|
-
`You can always run ${
|
|
889
|
+
`You can always run ${color4.reset("git init")}${color4.dim(" manually.")}`
|
|
731
890
|
);
|
|
732
891
|
}
|
|
733
892
|
}
|
|
@@ -774,7 +933,7 @@ function help() {
|
|
|
774
933
|
}
|
|
775
934
|
|
|
776
935
|
// src/actions/intro.ts
|
|
777
|
-
import { color as
|
|
936
|
+
import { color as color5, label as label2 } from "@astrojs/cli-kit";
|
|
778
937
|
async function intro(ctx) {
|
|
779
938
|
banner();
|
|
780
939
|
if (!ctx.skipHouston) {
|
|
@@ -784,9 +943,9 @@ async function intro(ctx) {
|
|
|
784
943
|
[
|
|
785
944
|
"Welcome",
|
|
786
945
|
"to",
|
|
787
|
-
label2("astro",
|
|
946
|
+
label2("astro", color5.bgGreen, color5.black),
|
|
788
947
|
Promise.resolve(ctx.version).then(
|
|
789
|
-
(version) => (version ?
|
|
948
|
+
(version) => (version ? color5.green(`v${version}`) : "") + ","
|
|
790
949
|
),
|
|
791
950
|
Promise.resolve(ctx.username).then((username) => `${username}!`)
|
|
792
951
|
],
|
|
@@ -798,9 +957,9 @@ async function intro(ctx) {
|
|
|
798
957
|
}
|
|
799
958
|
|
|
800
959
|
// src/actions/next-steps.ts
|
|
801
|
-
import
|
|
960
|
+
import path4 from "node:path";
|
|
802
961
|
async function next(ctx) {
|
|
803
|
-
let projectDir =
|
|
962
|
+
let projectDir = path4.relative(process.cwd(), ctx.cwd);
|
|
804
963
|
const commandMap = {
|
|
805
964
|
npm: "npm run dev",
|
|
806
965
|
bun: "bun run dev",
|
|
@@ -816,11 +975,11 @@ async function next(ctx) {
|
|
|
816
975
|
}
|
|
817
976
|
|
|
818
977
|
// src/actions/project-name.ts
|
|
819
|
-
import
|
|
820
|
-
import { color as
|
|
978
|
+
import path5 from "node:path";
|
|
979
|
+
import { color as color6, generateProjectName } from "@astrojs/cli-kit";
|
|
821
980
|
|
|
822
981
|
// src/actions/shared.ts
|
|
823
|
-
import
|
|
982
|
+
import fs4 from "node:fs";
|
|
824
983
|
var VALID_PROJECT_DIRECTORY_SAFE_LIST = [
|
|
825
984
|
".DS_Store",
|
|
826
985
|
".git",
|
|
@@ -846,10 +1005,10 @@ var VALID_PROJECT_DIRECTORY_SAFE_LIST = [
|
|
|
846
1005
|
/^yarn-error\.log/
|
|
847
1006
|
];
|
|
848
1007
|
function isEmpty(dirPath) {
|
|
849
|
-
if (!
|
|
1008
|
+
if (!fs4.existsSync(dirPath)) {
|
|
850
1009
|
return true;
|
|
851
1010
|
}
|
|
852
|
-
const conflicts =
|
|
1011
|
+
const conflicts = fs4.readdirSync(dirPath).filter((content) => {
|
|
853
1012
|
return !VALID_PROJECT_DIRECTORY_SAFE_LIST.some((safeContent) => {
|
|
854
1013
|
return typeof safeContent === "string" ? content === safeContent : safeContent.test(content);
|
|
855
1014
|
});
|
|
@@ -869,7 +1028,7 @@ async function projectName(ctx) {
|
|
|
869
1028
|
await checkCwd(ctx.cwd);
|
|
870
1029
|
if (!ctx.cwd || !isEmpty(ctx.cwd)) {
|
|
871
1030
|
if (ctx.cwd && !isEmpty(ctx.cwd)) {
|
|
872
|
-
await info("Hmm...", `${
|
|
1031
|
+
await info("Hmm...", `${color6.reset(`"${ctx.cwd}"`)}${color6.dim(` is not empty!`)}`);
|
|
873
1032
|
}
|
|
874
1033
|
if (ctx.yes) {
|
|
875
1034
|
ctx.projectName = generateProjectName();
|
|
@@ -901,7 +1060,7 @@ async function projectName(ctx) {
|
|
|
901
1060
|
} else {
|
|
902
1061
|
let name = ctx.cwd;
|
|
903
1062
|
if (name === "." || name === "./") {
|
|
904
|
-
const parts = process.cwd().split(
|
|
1063
|
+
const parts = process.cwd().split(path5.sep);
|
|
905
1064
|
name = parts[parts.length - 1];
|
|
906
1065
|
} else if (name.startsWith("./") || name.startsWith("../")) {
|
|
907
1066
|
const parts = name.split("/");
|
|
@@ -917,153 +1076,11 @@ async function checkCwd(cwd) {
|
|
|
917
1076
|
const empty = cwd && isEmpty(cwd);
|
|
918
1077
|
if (empty) {
|
|
919
1078
|
log("");
|
|
920
|
-
await info("dir", `Using ${
|
|
1079
|
+
await info("dir", `Using ${color6.reset(cwd)}${color6.dim(" as project directory")}`);
|
|
921
1080
|
}
|
|
922
1081
|
return empty;
|
|
923
1082
|
}
|
|
924
1083
|
|
|
925
|
-
// src/actions/template.ts
|
|
926
|
-
import fs4 from "node:fs";
|
|
927
|
-
import path5 from "node:path";
|
|
928
|
-
import { color as color6 } from "@astrojs/cli-kit";
|
|
929
|
-
import { downloadTemplate } from "@bluwy/giget-core";
|
|
930
|
-
function removeTemplateMarkerSections(content) {
|
|
931
|
-
const pattern = /<!--\s*ASTRO:REMOVE:START\s*-->[\s\S]*?<!--\s*ASTRO:REMOVE:END\s*-->/gi;
|
|
932
|
-
let result = content.replace(pattern, "");
|
|
933
|
-
result = result.replace(/\n{3,}/g, "\n\n");
|
|
934
|
-
return result;
|
|
935
|
-
}
|
|
936
|
-
function processTemplateReadme(content, packageManager) {
|
|
937
|
-
let processed = removeTemplateMarkerSections(content);
|
|
938
|
-
if (packageManager !== "npm") {
|
|
939
|
-
processed = processed.replace(/\bnpm run\b/g, packageManager).replace(/\bnpm\b/g, packageManager);
|
|
940
|
-
}
|
|
941
|
-
return processed;
|
|
942
|
-
}
|
|
943
|
-
async function template(ctx) {
|
|
944
|
-
if (!ctx.template && ctx.yes) ctx.template = "basics";
|
|
945
|
-
if (ctx.template) {
|
|
946
|
-
await info("tmpl", `Using ${color6.reset(ctx.template)}${color6.dim(" as project template")}`);
|
|
947
|
-
} else {
|
|
948
|
-
const { template: tmpl } = await ctx.prompt({
|
|
949
|
-
name: "template",
|
|
950
|
-
type: "select",
|
|
951
|
-
label: title("tmpl"),
|
|
952
|
-
message: "How would you like to start your new project?",
|
|
953
|
-
initial: "basics",
|
|
954
|
-
choices: [
|
|
955
|
-
{ value: "basics", label: "A basic, helpful starter project", hint: "(recommended)" },
|
|
956
|
-
{ value: "blog", label: "Use blog template" },
|
|
957
|
-
{ value: "starlight", label: "Use docs (Starlight) template" },
|
|
958
|
-
{ value: "minimal", label: "Use minimal (empty) template" }
|
|
959
|
-
]
|
|
960
|
-
});
|
|
961
|
-
ctx.template = tmpl;
|
|
962
|
-
}
|
|
963
|
-
if (ctx.dryRun) {
|
|
964
|
-
await info("--dry-run", `Skipping template copying`);
|
|
965
|
-
} else if (ctx.template) {
|
|
966
|
-
ctx.tasks.push({
|
|
967
|
-
pending: "Template",
|
|
968
|
-
start: "Template copying...",
|
|
969
|
-
end: "Template copied",
|
|
970
|
-
while: () => copyTemplate(ctx.template, ctx).catch((e) => {
|
|
971
|
-
if (e instanceof Error) {
|
|
972
|
-
error("error", e.message);
|
|
973
|
-
process.exit(1);
|
|
974
|
-
} else {
|
|
975
|
-
error("error", "Unable to clone template.");
|
|
976
|
-
process.exit(1);
|
|
977
|
-
}
|
|
978
|
-
})
|
|
979
|
-
});
|
|
980
|
-
} else {
|
|
981
|
-
ctx.exit(1);
|
|
982
|
-
}
|
|
983
|
-
}
|
|
984
|
-
var FILES_TO_REMOVE = ["CHANGELOG.md", ".codesandbox"];
|
|
985
|
-
var FILES_TO_UPDATE = {
|
|
986
|
-
"package.json": (file, overrides) => fs4.promises.readFile(file, "utf-8").then((value) => {
|
|
987
|
-
const indent = /(^\s+)/m.exec(value)?.[1] ?? " ";
|
|
988
|
-
return fs4.promises.writeFile(
|
|
989
|
-
file,
|
|
990
|
-
JSON.stringify(
|
|
991
|
-
Object.assign(JSON.parse(value), Object.assign(overrides, { private: void 0 })),
|
|
992
|
-
null,
|
|
993
|
-
indent
|
|
994
|
-
),
|
|
995
|
-
"utf-8"
|
|
996
|
-
);
|
|
997
|
-
})
|
|
998
|
-
};
|
|
999
|
-
function getTemplateTarget(tmpl, ref = "latest") {
|
|
1000
|
-
if (tmpl.startsWith("starlight")) {
|
|
1001
|
-
const [, starter = "basics"] = tmpl.split("/");
|
|
1002
|
-
return `github:withastro/starlight/examples/${starter}`;
|
|
1003
|
-
}
|
|
1004
|
-
const isThirdParty = tmpl.includes("/");
|
|
1005
|
-
if (isThirdParty) return tmpl;
|
|
1006
|
-
if (ref === "latest") {
|
|
1007
|
-
return `github:withastro/astro#examples/${tmpl}`;
|
|
1008
|
-
} else {
|
|
1009
|
-
return `github:withastro/astro/examples/${tmpl}#${ref}`;
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
async function copyTemplate(tmpl, ctx) {
|
|
1013
|
-
const templateTarget = getTemplateTarget(tmpl, ctx.ref);
|
|
1014
|
-
if (!ctx.dryRun) {
|
|
1015
|
-
try {
|
|
1016
|
-
await downloadTemplate(templateTarget, {
|
|
1017
|
-
force: true,
|
|
1018
|
-
cwd: ctx.cwd,
|
|
1019
|
-
dir: "."
|
|
1020
|
-
});
|
|
1021
|
-
const readmePath = path5.resolve(ctx.cwd, "README.md");
|
|
1022
|
-
if (fs4.existsSync(readmePath)) {
|
|
1023
|
-
const readme = fs4.readFileSync(readmePath, "utf8");
|
|
1024
|
-
const processedReadme = processTemplateReadme(readme, ctx.packageManager);
|
|
1025
|
-
fs4.writeFileSync(readmePath, processedReadme);
|
|
1026
|
-
}
|
|
1027
|
-
} catch (err) {
|
|
1028
|
-
if (ctx.cwd !== "." && ctx.cwd !== "./" && !ctx.cwd.startsWith("../")) {
|
|
1029
|
-
try {
|
|
1030
|
-
fs4.rmdirSync(ctx.cwd);
|
|
1031
|
-
} catch (_) {
|
|
1032
|
-
}
|
|
1033
|
-
}
|
|
1034
|
-
if (err.message?.includes("404")) {
|
|
1035
|
-
throw new Error(`Template ${color6.reset(tmpl)} ${color6.dim("does not exist!")}`);
|
|
1036
|
-
}
|
|
1037
|
-
if (err.message) {
|
|
1038
|
-
error("error", err.message);
|
|
1039
|
-
}
|
|
1040
|
-
try {
|
|
1041
|
-
if ("cause" in err) {
|
|
1042
|
-
error("error", err.cause);
|
|
1043
|
-
if ("cause" in err.cause) {
|
|
1044
|
-
error("error", err.cause?.cause);
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
} catch {
|
|
1048
|
-
}
|
|
1049
|
-
throw new Error(`Unable to download template ${color6.reset(tmpl)}`);
|
|
1050
|
-
}
|
|
1051
|
-
const removeFiles = FILES_TO_REMOVE.map(async (file) => {
|
|
1052
|
-
const fileLoc = path5.resolve(path5.join(ctx.cwd, file));
|
|
1053
|
-
if (fs4.existsSync(fileLoc)) {
|
|
1054
|
-
return fs4.promises.rm(fileLoc, { recursive: true });
|
|
1055
|
-
}
|
|
1056
|
-
});
|
|
1057
|
-
const updateFiles = Object.entries(FILES_TO_UPDATE).map(async ([file, update]) => {
|
|
1058
|
-
const fileLoc = path5.resolve(path5.join(ctx.cwd, file));
|
|
1059
|
-
if (fs4.existsSync(fileLoc)) {
|
|
1060
|
-
return update(fileLoc, { name: ctx.projectName });
|
|
1061
|
-
}
|
|
1062
|
-
});
|
|
1063
|
-
await Promise.all([...removeFiles, ...updateFiles]);
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
1084
|
// src/actions/verify.ts
|
|
1068
1085
|
import dns from "node:dns/promises";
|
|
1069
1086
|
import { color as color7 } from "@astrojs/cli-kit";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-astro",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "withastro",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"arg": "^5.0.2",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
33
|
+
"astro-scripts": "0.0.14",
|
|
34
|
+
"@astrojs/internal-helpers": "0.8.0"
|
|
35
35
|
},
|
|
36
36
|
"engines": {
|
|
37
37
|
"node": ">=22.12.0"
|