Skip to content

Commit

Permalink
feat: Add project initialization functionality
Browse files Browse the repository at this point in the history
This commit adds the ability to initialize a new project by prompting the user for a package name and destination directory. It then creates the project directory if it doesn't exist, copies all files from the root directory to the destination (excluding specified ones), and updates the package.json file with the new name and author. Additionally, it offers an option to install packages and run the project immediately after initialization. If confirmed, it installs packages using pnpm and runs `pnpm dev` to start the local development server. Otherwise, it provides instructions on how to manually install packages and run `pnpm dev`.
  • Loading branch information
zanhk committed Sep 11, 2023
1 parent a802c85 commit 2781811
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 43 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@ npm create astro-starter@latest

- ✅ Tailwind CSS
- ✅ Alpine js
- ✅ Typescript
- ✅ Localization (with astro-i18n-aut)
- ✅ Dark/light mode
- ✅ Blog
- ✅ Discussions (thanks to giscus)
- ✅ CMS for editing blog post (thanks to decap CMS)
- ✅ Sitemap (localized)
- ✅ RSS (localized)
- ❌ PWA (Follow tutorial below to add it)

### 🧞 Commands

All commands are run from the root of the project, from a terminal:

| Command | Action |
| :------------------------- | :----------------------------------------------- |
| `pnpm install` | Installs dependencies |
| `pnpm run dev` | Starts local dev server at `localhost:4321` |
| `pnpm run build` | Build your production site to `./dist/` |
| `pnpm run preview` | Preview your build locally, before deploying |
| `pnpm run astro ...` | Run CLI commands like `astro add`, `astro check` |
| `pnpm run astro -- --help` | Get help using the Astro CLI |
| Command | Action |
| :--------------------- | :----------------------------------------------- |
| `pnpm install` | Installs dependencies |
| `pnpm dev` | Starts local dev server at `localhost:4321` |
| `pnpm build` | Build your production site to `./dist/` |
| `pnpm preview` | Preview your build locally, before deploying |
| `pnpm astro ...` | Run CLI commands like `astro add`, `astro check` |
| `pnpm astro -- --help` | Get help using the Astro CLI |

If you want to switch to npm make sure to remove pnpm-lock.yaml and node_modules folder and then run `npm install`

Expand Down Expand Up @@ -89,8 +93,5 @@ If you use netlify it's actually easier, you will need to change in the file `as

### 👀 Want to learn more?

Check out [our documentation](https://docs.astro.build) or jump into our [Discord server](https://astro.build/chat).
Check out [Astro documentation](https://docs.astro.build) or jump into Astro [Discord server](https://astro.build/chat).

### Credit

This theme is based off of the lovely [Bear Blog](https://github.com/HermanMartinus/bearblog/).
87 changes: 56 additions & 31 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,70 @@ function copyRecursive(src, dest) {
}
}

rl.question('Where would you like to create the new project? (Provide a directory path, use "." for current directory)', (destination) => {
rl.question("Do you want to install the packages now? (y/n) ", (answer) => {
try {
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination, { recursive: true });
}
function getGitAuthorName() {
try {
const name = execSync("git config user.name", { encoding: "utf8" }).trim();
return name;
} catch (error) {
console.warn("Failed to get git user name:", error.message);
return "your name"; // or a default value
}
}

// Initialize the package with npm
execSync(`cd ${destination} && npm init -y`, { stdio: "inherit" });
rl.question("How would you like to name your package? ", (packageName) => {
rl.question('Where would you like to create the new project? (Provide a directory path, use "." for current directory)', (destination) => {
rl.question("Do you want to install the packages now? (y/n) ", (answer) => {
try {
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination, { recursive: true });
}

// Copy all files and subdirectories from the root directory to the destination, excluding the specified ones
copyRecursive(rootDir, destination);
// Copy all files and subdirectories from the root directory to the destination, excluding the specified ones
copyRecursive(rootDir, destination);

console.log(`Project initialized in ${destination}`);
// Update package.json with the new name and author
const packageJsonPath = path.join(destination, "package.json");
const packageData = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
if (packageName) {
packageData.name = packageName;
}
packageData.varsion = "0.0.1";
packageData.author = getGitAuthorName(); // Set the author from git config
delete packageData.bin; // Remove the bin property from package.json
delete packageData.files;
delete packageData.main;
delete packageData.repository;
delete packageData.homepage;
delete packageData.bugs;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageData, null, 2));

// Check user's answer and run pnpm install if confirmed
if (answer.toLowerCase() === "y" || answer.toLowerCase() === "yes") {
execSync(`cd ${destination} && pnpm install`, { stdio: "inherit" });
console.log(`Packages installed successfully in ${destination}`);
console.log(`Project initialized in ${destination}`);

rl.question("Do you want to run the project now? (y/n) ", (answer) => {
try {
if (answer.toLowerCase() === "y" || answer.toLowerCase() === "yes") {
execSync(`cd ${destination} && pnpm dev`, { stdio: "inherit" });
} else {
console.log(`You can run 'pnpm dev' in ${destination} whenever you're ready.`);
// Check user's answer and run pnpm install if confirmed
if (answer.toLowerCase() === "y" || answer.toLowerCase() === "yes") {
execSync(`cd ${destination} && pnpm install`, { stdio: "inherit" });
console.log(`Packages installed successfully in ${destination}`);

rl.question("Do you want to run the project now? (y/n) ", (answer) => {
try {
if (answer.toLowerCase() === "y" || answer.toLowerCase() === "yes") {
execSync(`cd ${destination} && pnpm dev`, { stdio: "inherit" });
} else {
console.log(`You can run 'pnpm dev' in ${destination} whenever you're ready.`);
}
} catch (error) {
console.error("Failed to run the project:", error.message);
}
} catch (error) {
console.error("Failed to run the project:", error.message);
}
rl.close();
});
} else {
console.log(`You can run 'pnpm install' in ${destination} whenever you're ready.`);
rl.close();
});
} else {
console.log(`You can run 'pnpm install' in ${destination} whenever you're ready.`);
}
} catch (error) {
console.error("Failed to create the project:", error.message);
rl.close();
}
} catch (error) {
console.error("Failed to create the project:", error.message);
rl.close();
}
});
});
});

0 comments on commit 2781811

Please sign in to comment.