create-astro 0.8.0 → 0.10.1

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.
Files changed (3) hide show
  1. package/dist/index.js +152 -87
  2. package/package.json +15 -11
  3. package/LICENSE +0 -34
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ import { FRAMEWORKS, COUNTER_COMPONENTS } from "./frameworks.js";
26
26
  import { TEMPLATES } from "./templates.js";
27
27
  import { createConfig } from "./config.js";
28
28
  import { logger, defaultLogLevel } from "./logger.js";
29
+ import { execa } from "execa";
29
30
  const cleanArgv = process.argv.filter((arg) => arg !== "--");
30
31
  const args = yargs(cleanArgv);
31
32
  prompts.override(args);
@@ -38,32 +39,49 @@ function mkdirp(dir) {
38
39
  throw e;
39
40
  }
40
41
  }
42
+ function isEmpty(dirPath) {
43
+ return !fs.existsSync(dirPath) || fs.readdirSync(dirPath).length === 0;
44
+ }
41
45
  const { version } = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf-8"));
46
+ const FILES_TO_REMOVE = [".stackblitzrc", "sandbox.config.json"];
42
47
  const POSTPROCESS_FILES = ["package.json", "astro.config.mjs", "CHANGELOG.md"];
43
48
  async function main() {
49
+ const pkgManager = pkgManagerFromUserAgent(process.env.npm_config_user_agent);
44
50
  logger.debug("Verbose logging turned on");
45
51
  console.log(`
46
52
  ${bold("Welcome to Astro!")} ${gray(`(create-astro v${version})`)}`);
47
- console.log(`If you encounter a problem, visit ${cyan("https://github.com/withastro/astro/issues")} to search or file a new issue.
48
- `);
49
53
  let spinner = ora({ color: "green", text: "Prepare for liftoff." });
50
54
  spinner.succeed();
51
- const cwd = args["_"][2] || ".";
52
- if (fs.existsSync(cwd)) {
53
- if (fs.readdirSync(cwd).length > 0) {
54
- const response = await prompts({
55
- type: "confirm",
56
- name: "forceOverwrite",
57
- message: "Directory not empty. Continue [force overwrite]?",
58
- initial: false
59
- });
60
- if (!response.forceOverwrite) {
61
- process.exit(1);
62
- }
63
- mkdirp(cwd);
55
+ let cwd = args["_"][2];
56
+ if (cwd && isEmpty(cwd)) {
57
+ let acknowledgeProjectDir = ora({
58
+ color: "green",
59
+ text: `Using ${bold(cwd)} as project directory.`
60
+ });
61
+ acknowledgeProjectDir.succeed();
62
+ }
63
+ if (!cwd || !isEmpty(cwd)) {
64
+ const notEmptyMsg = (dirPath) => `"${bold(dirPath)}" is not empty!`;
65
+ if (!isEmpty(cwd)) {
66
+ let rejectProjectDir = ora({ color: "red", text: notEmptyMsg(cwd) });
67
+ rejectProjectDir.fail();
64
68
  }
65
- } else {
66
- mkdirp(cwd);
69
+ const dirResponse = await prompts({
70
+ type: "text",
71
+ name: "directory",
72
+ message: "Where would you like to create your app?",
73
+ initial: "./my-astro-site",
74
+ validate(value) {
75
+ if (!isEmpty(value)) {
76
+ return notEmptyMsg(value);
77
+ }
78
+ return true;
79
+ }
80
+ });
81
+ cwd = dirResponse.directory;
82
+ }
83
+ if (!cwd) {
84
+ process.exit(1);
67
85
  }
68
86
  const options = await prompts([
69
87
  {
@@ -102,97 +120,144 @@ ${bold("Welcome to Astro!")} ${gray(`(create-astro v${version})`)}`);
102
120
  integrations = result.integrations;
103
121
  }
104
122
  spinner = ora({ color: "green", text: "Copying project files..." }).start();
105
- try {
106
- emitter.on("info", (info) => {
107
- logger.debug(info.message);
108
- });
109
- await emitter.clone(cwd);
110
- } catch (err) {
111
- logger.debug(err);
112
- console.error(red(err.message));
113
- if (err.message === "zlib: unexpected end of file") {
114
- console.log(yellow("This seems to be a cache related problem. Remove the folder '~/.degit/github/withastro' to fix this error."));
115
- console.log(yellow("For more information check out this issue: https://github.com/withastro/astro/issues/655"));
116
- }
117
- if (err.code === "MISSING_REF") {
118
- console.log(yellow("This seems to be an issue with degit. Please check if you have 'git' installed on your system, and install it if you don't have (https://git-scm.com)."));
119
- console.log(yellow("If you do have 'git' installed, please run this command with the --verbose flag and file a new issue with the command output here: https://github.com/withastro/astro/issues"));
120
- }
121
- spinner.fail();
122
- process.exit(1);
123
- }
124
- await Promise.all(POSTPROCESS_FILES.map(async (file) => {
125
- const fileLoc = path.resolve(path.join(cwd, file));
126
- switch (file) {
127
- case "CHANGELOG.md": {
128
- if (fs.existsSync(fileLoc)) {
129
- await fs.promises.unlink(fileLoc);
130
- }
131
- break;
123
+ if (!args.dryrun) {
124
+ try {
125
+ emitter.on("info", (info) => {
126
+ logger.debug(info.message);
127
+ });
128
+ await emitter.clone(cwd);
129
+ } catch (err) {
130
+ logger.debug(err);
131
+ console.error(red(err.message));
132
+ if (err.message === "zlib: unexpected end of file") {
133
+ console.log(yellow("This seems to be a cache related problem. Remove the folder '~/.degit/github/withastro' to fix this error."));
134
+ console.log(yellow("For more information check out this issue: https://github.com/withastro/astro/issues/655"));
132
135
  }
133
- case "astro.config.mjs": {
134
- if ((selectedTemplate == null ? void 0 : selectedTemplate.integrations) !== true) {
135
- break;
136
- }
137
- await fs.promises.writeFile(fileLoc, createConfig({ integrations }));
138
- break;
136
+ if (err.code === "MISSING_REF") {
137
+ console.log(yellow("This seems to be an issue with degit. Please check if you have 'git' installed on your system, and install it if you don't have (https://git-scm.com)."));
138
+ console.log(yellow("If you do have 'git' installed, please run this command with the --verbose flag and file a new issue with the command output here: https://github.com/withastro/astro/issues"));
139
139
  }
140
- case "package.json": {
141
- const packageJSON = JSON.parse(await fs.promises.readFile(fileLoc, "utf8"));
142
- delete packageJSON.snowpack;
143
- const integrationEntries = (await Promise.all(integrations.map((integration) => fetch(`https://registry.npmjs.org/${integration.packageName}/latest`).then((res) => res.json()).then((res) => {
144
- let dependencies = [[res["name"], `^${res["version"]}`]];
145
- if (res["peerDependencies"]) {
146
- for (const peer in res["peerDependencies"]) {
147
- dependencies.push([peer, res["peerDependencies"][peer]]);
140
+ spinner.fail();
141
+ process.exit(1);
142
+ }
143
+ await Promise.all([
144
+ ...FILES_TO_REMOVE.map(async (file) => {
145
+ const fileLoc = path.resolve(path.join(cwd, file));
146
+ return fs.promises.rm(fileLoc);
147
+ }),
148
+ ...POSTPROCESS_FILES.map(async (file) => {
149
+ const fileLoc = path.resolve(path.join(cwd, file));
150
+ switch (file) {
151
+ case "CHANGELOG.md": {
152
+ if (fs.existsSync(fileLoc)) {
153
+ await fs.promises.unlink(fileLoc);
148
154
  }
155
+ break;
149
156
  }
150
- return dependencies;
151
- })))).flat(1);
152
- packageJSON.devDependencies = __spreadValues(__spreadValues({}, packageJSON.devDependencies ?? {}), Object.fromEntries(integrationEntries));
153
- packageJSON.devDependencies = Object.fromEntries(Object.entries(packageJSON.devDependencies).sort((a, b) => a[0].localeCompare(b[0])));
154
- await fs.promises.writeFile(fileLoc, JSON.stringify(packageJSON, void 0, 2));
155
- break;
156
- }
157
+ case "astro.config.mjs": {
158
+ if ((selectedTemplate == null ? void 0 : selectedTemplate.integrations) !== true) {
159
+ break;
160
+ }
161
+ await fs.promises.writeFile(fileLoc, createConfig({ integrations }));
162
+ break;
163
+ }
164
+ case "package.json": {
165
+ const packageJSON = JSON.parse(await fs.promises.readFile(fileLoc, "utf8"));
166
+ delete packageJSON.snowpack;
167
+ const integrationEntries = (await Promise.all(integrations.map((integration) => fetch(`https://registry.npmjs.org/${integration.packageName}/latest`).then((res) => res.json()).then((res) => {
168
+ let dependencies = [[res["name"], `^${res["version"]}`]];
169
+ if (res["peerDependencies"]) {
170
+ for (const peer in res["peerDependencies"]) {
171
+ dependencies.push([peer, res["peerDependencies"][peer]]);
172
+ }
173
+ }
174
+ return dependencies;
175
+ })))).flat(1);
176
+ packageJSON.devDependencies = __spreadValues(__spreadValues({}, packageJSON.devDependencies ?? {}), Object.fromEntries(integrationEntries));
177
+ packageJSON.devDependencies = Object.fromEntries(Object.entries(packageJSON.devDependencies).sort((a, b) => a[0].localeCompare(b[0])));
178
+ await fs.promises.writeFile(fileLoc, JSON.stringify(packageJSON, void 0, 2));
179
+ break;
180
+ }
181
+ }
182
+ })
183
+ ]);
184
+ if ((selectedTemplate == null ? void 0 : selectedTemplate.value) === "starter") {
185
+ let importStatements = [];
186
+ let components = [];
187
+ await Promise.all(integrations.map(async (integration) => {
188
+ const component = COUNTER_COMPONENTS[integration.id];
189
+ const componentName = path.basename(component.filename, path.extname(component.filename));
190
+ const absFileLoc = path.resolve(cwd, component.filename);
191
+ importStatements.push(`import ${componentName} from '${component.filename.replace(/^src/, "..")}';`);
192
+ components.push(`<${componentName} client:visible />`);
193
+ await fs.promises.writeFile(absFileLoc, component.content);
194
+ }));
195
+ const pageFileLoc = path.resolve(path.join(cwd, "src", "pages", "index.astro"));
196
+ const content = (await fs.promises.readFile(pageFileLoc)).toString();
197
+ const newContent = content.replace(/^(\s*)\/\* ASTRO\:COMPONENT_IMPORTS \*\//gm, (_, indent) => {
198
+ return indent + importStatements.join("\n");
199
+ }).replace(/^(\s*)<!-- ASTRO:COMPONENT_MARKUP -->/gm, (_, indent) => {
200
+ return components.map((ln) => indent + ln).join("\n");
201
+ });
202
+ await fs.promises.writeFile(pageFileLoc, newContent);
157
203
  }
158
- }));
159
- if ((selectedTemplate == null ? void 0 : selectedTemplate.value) === "starter") {
160
- let importStatements = [];
161
- let components = [];
162
- await Promise.all(integrations.map(async (integration) => {
163
- const component = COUNTER_COMPONENTS[integration.id];
164
- const componentName = path.basename(component.filename, path.extname(component.filename));
165
- const absFileLoc = path.resolve(cwd, component.filename);
166
- importStatements.push(`import ${componentName} from '${component.filename.replace(/^src/, "..")}';`);
167
- components.push(`<${componentName} client:visible />`);
168
- await fs.promises.writeFile(absFileLoc, component.content);
169
- }));
170
- const pageFileLoc = path.resolve(path.join(cwd, "src", "pages", "index.astro"));
171
- const content = (await fs.promises.readFile(pageFileLoc)).toString();
172
- const newContent = content.replace(/^(\s*)\/\* ASTRO\:COMPONENT_IMPORTS \*\//gm, (_, indent) => {
173
- return indent + importStatements.join("\n");
174
- }).replace(/^(\s*)<!-- ASTRO:COMPONENT_MARKUP -->/gm, (_, indent) => {
175
- return components.map((ln) => indent + ln).join("\n");
176
- });
177
- await fs.promises.writeFile(pageFileLoc, newContent);
178
204
  }
179
205
  spinner.succeed();
180
206
  console.log(bold(green("\u2714") + " Done!"));
207
+ const installResponse = await prompts({
208
+ type: "confirm",
209
+ name: "install",
210
+ message: `Would you like us to run "${pkgManager} install?"`,
211
+ initial: true
212
+ });
213
+ if (!installResponse) {
214
+ process.exit(0);
215
+ }
216
+ if (installResponse.install) {
217
+ const installExec = execa(pkgManager, ["install"], { cwd });
218
+ const installingPackagesMsg = `Installing packages${emojiWithFallback(" \u{1F4E6}", "...")}`;
219
+ spinner = ora({ color: "green", text: installingPackagesMsg }).start();
220
+ if (!args.dryrun) {
221
+ await new Promise((resolve, reject) => {
222
+ var _a;
223
+ (_a = installExec.stdout) == null ? void 0 : _a.on("data", function(data) {
224
+ spinner.text = `${installingPackagesMsg}
225
+ ${bold(`[${pkgManager}]`)} ${data}`;
226
+ });
227
+ installExec.on("error", (error) => reject(error));
228
+ installExec.on("close", () => resolve());
229
+ });
230
+ }
231
+ spinner.succeed();
232
+ }
181
233
  console.log("\nNext steps:");
182
234
  let i = 1;
183
235
  const relative = path.relative(process.cwd(), cwd);
184
236
  if (relative !== "") {
185
237
  console.log(` ${i++}: ${bold(cyan(`cd ${relative}`))}`);
186
238
  }
187
- console.log(` ${i++}: ${bold(cyan("npm install"))} (or pnpm install, yarn, etc)`);
239
+ if (!installResponse.install) {
240
+ console.log(` ${i++}: ${bold(cyan(`${pkgManager} install`))}`);
241
+ }
188
242
  console.log(` ${i++}: ${bold(cyan('git init && git add -A && git commit -m "Initial commit"'))} (optional step)`);
189
- console.log(` ${i++}: ${bold(cyan("npm run dev"))} (or pnpm, yarn, etc)`);
243
+ const runCommand = pkgManager === "npm" ? "npm run dev" : `${pkgManager} dev`;
244
+ console.log(` ${i++}: ${bold(cyan(runCommand))}`);
190
245
  console.log(`
191
246
  To close the dev server, hit ${bold(cyan("Ctrl-C"))}`);
192
247
  console.log(`
193
248
  Stuck? Visit us at ${cyan("https://astro.build/chat")}
194
249
  `);
195
250
  }
251
+ function emojiWithFallback(char, fallback) {
252
+ return process.platform !== "win32" ? char : fallback;
253
+ }
254
+ function pkgManagerFromUserAgent(userAgent) {
255
+ if (!userAgent)
256
+ return "npm";
257
+ const pkgSpec = userAgent.split(" ")[0];
258
+ const pkgSpecArr = pkgSpec.split("/");
259
+ return pkgSpecArr[0];
260
+ }
196
261
  export {
197
262
  main,
198
263
  mkdirp
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-astro",
3
- "version": "0.8.0",
3
+ "version": "0.10.1",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -17,6 +17,12 @@
17
17
  "bin": {
18
18
  "create-astro": "./create-astro.mjs"
19
19
  },
20
+ "scripts": {
21
+ "build": "astro-scripts build \"src/**/*.ts\" && tsc",
22
+ "build:ci": "astro-scripts build \"src/**/*.ts\"",
23
+ "dev": "astro-scripts dev \"src/**/*.ts\"",
24
+ "test": "mocha --exit --timeout 20000"
25
+ },
20
26
  "files": [
21
27
  "dist",
22
28
  "create-astro.js"
@@ -25,6 +31,7 @@
25
31
  "@types/degit": "^2.8.3",
26
32
  "@types/prompts": "^2.0.14",
27
33
  "degit": "^2.8.4",
34
+ "execa": "^6.1.0",
28
35
  "kleur": "^4.1.4",
29
36
  "node-fetch": "^3.2.3",
30
37
  "ora": "^6.1.0",
@@ -32,18 +39,15 @@
32
39
  "yargs-parser": "^21.0.1"
33
40
  },
34
41
  "devDependencies": {
42
+ "@types/chai": "^4.3.1",
43
+ "@types/mocha": "^9.1.1",
35
44
  "@types/yargs-parser": "^21.0.0",
36
- "astro-scripts": "0.0.2",
45
+ "astro-scripts": "workspace:*",
46
+ "chai": "^4.3.6",
47
+ "mocha": "^9.2.2",
37
48
  "uvu": "^0.5.3"
38
49
  },
39
50
  "engines": {
40
51
  "node": "^14.15.0 || >=16.0.0"
41
- },
42
- "scripts": {
43
- "build": "astro-scripts build \"src/**/*.ts\" && tsc",
44
- "build:ci": "astro-scripts build \"src/**/*.ts\"",
45
- "dev": "astro-scripts dev \"src/**/*.ts\"",
46
- "test": "rm -rf test/fixtures && mkdir test/fixtures && node --unhandled-rejections=strict test/create-astro.test.js"
47
- },
48
- "readme": "# create-astro\n\n## Scaffolding for Astro projects\n\n**With NPM:**\n\n```bash\nnpm init astro\n```\n\n**With Yarn:**\n\n```bash\nyarn create astro\n```\n\n`create-astro` automatically runs in _interactive_ mode, but you can also specify your project name and template with command line arguments.\n\n```bash\n# npm 6.x\nnpm init astro my-astro-project --template starter\n\n# npm 7+, extra double-dash is needed:\nnpm init astro my-astro-project -- --template starter\n\n# yarn\nyarn create astro my-astro-project --template starter\n```\n[Check out the full list][examples] of example starter templates, available on GitHub.\n\nYou can also use any GitHub repo as a template:\n\n```bash\nnpm init astro my-astro-project -- --template cassidoo/shopify-react-astro\n```\n\n### CLI Flags\n\nMay be provided in place of prompts\n\n| Name | Description |\n|:-------------|:----------------------------------------------------|\n| `--template` | Specify the template name ([list][examples]) |\n| `--commit` | Specify a specific Git commit or branch to use from this repo (by default, `main` branch of this repo will be used) |\n\n### Debugging\n\nTo debug `create-astro`, you can use the `--verbose` flag which will log the output of degit and some more information about the command, this can be useful when you encounter an error and want to report it.\n\n```bash\n# npm 6.x\nnpm init astro my-astro-project --verbose\n\n# npm 7+, extra double-dash is needed:\nnpm init astro my-astro-project -- --verbose\n\n# yarn\nyarn create astro my-astro-project --verbose\n```\n\n[examples]: https://github.com/withastro/astro/tree/main/examples\n"
49
- }
52
+ }
53
+ }
package/LICENSE DELETED
@@ -1,34 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Fred K. Schott
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
22
-
23
-
24
- """
25
- This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
26
-
27
- Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
28
-
29
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30
-
31
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
32
-
33
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
- """