Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ Create .NET and other projects from NetCoreTemplates GitHub repositories.
## Usage

```bash
npx create-net <repo> <ProjectName>
npx create-net <repo> [ProjectName]
```

If `ProjectName` is not specified, the script will use the current directory name and extract the template into the current directory (which must be empty).

### Examples

**Create a project from NetCoreTemplates organization:**
**Create a project in a new directory:**

```bash
npx create-net nextjs MyProject
```

This downloads from: `https://github.com/NetCoreTemplates/nextjs`
This downloads from: `https://github.com/NetCoreTemplates/nextjs` and creates a `MyProject` folder.

**Create a project in the current directory:**

```bash
mkdir my-project
cd my-project
npx create-net nextjs
```

This uses the current directory name (`my-project`) and extracts the template into the current directory.

**Create a project from a different organization:**

Expand All @@ -29,8 +41,8 @@ This downloads from: `https://github.com/NetFrameworkTemplates/web-netfx`
## What it does

1. **Downloads** the GitHub repository archive from the specified repository
2. **Extracts** the archive into a folder named `<ProjectName>`
3. **Replaces** all variations of `MyApp` with variations of your `<ProjectName>`:
2. **Extracts** the archive into a folder named `<ProjectName>` (or current directory if no ProjectName specified)
3. **Replaces** all variations of `MyApp` with variations of your `<ProjectName>` (or current directory name):
- `My_App` → `Your_Project`
- `My App` → `Your Project`
- `my-app` → `your-project`
Expand Down
74 changes: 60 additions & 14 deletions bin/create-net.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,28 @@ const AdmZip = require('adm-zip');
// Parse command line arguments
const args = process.argv.slice(2);

if (args.length < 2) {
console.error('Usage: npx create-net <repo> <ProjectName>');
console.error('Example: npx create-net nextjs MyProject');
console.error('Example: npx create-net NetFrameworkTemplates/web-netfx MyProject');
if (args.length < 1) {
console.error('Usage: npx create-net <repo> [ProjectName]');
console.error('');
console.error('If ProjectName is not specified, uses current directory name and extracts into current directory.');
console.error('');
console.error('Examples:');
console.error(' npx create-net nextjs MyProject');
console.error(' npx create-net NetFrameworkTemplates/web-netfx MyProject');
console.error(' npx create-net nextjs (uses current directory name)');
process.exit(1);
}

const [repo, projectName] = args;
const repo = args[0];
let projectName = args[1];
let extractToCurrentDir = false;

// If no project name specified, use current directory name
if (!projectName) {
projectName = path.basename(process.cwd());
extractToCurrentDir = true;
console.log(`No project name specified, using current directory name: "${projectName}"`);
}

// Determine organization and repository
let organization = 'NetCoreTemplates';
Expand All @@ -31,17 +45,32 @@ if (repo.includes('/')) {
// Construct GitHub archive URL
const archiveUrl = `https://github.com/${organization}/${repository}/archive/refs/heads/main.zip`;
const tempZipPath = path.join(process.cwd(), 'temp-download.zip');
const projectPath = path.join(process.cwd(), projectName);
const projectPath = extractToCurrentDir ? process.cwd() : path.join(process.cwd(), projectName);

console.log(`Creating project "${projectName}" from ${organization}/${repository}...`);
console.log(`Downloading from: ${archiveUrl}`);

// Check if project directory already exists
if (fs.existsSync(projectPath)) {
// Check if project directory already exists (only when creating a new directory)
if (!extractToCurrentDir && fs.existsSync(projectPath)) {
console.error(`Error: Directory "${projectName}" already exists.`);
process.exit(1);
}

// Check if current directory is not empty (when extracting to current dir)
if (extractToCurrentDir) {
const currentDirContents = fs.readdirSync(process.cwd()).filter(item =>
item !== 'node_modules' &&
item !== '.git' &&
!item.startsWith('.')
);

if (currentDirContents.length > 0) {
console.error(`Error: Current directory is not empty. Please run this command in an empty directory.`);
console.error(`Found: ${currentDirContents.join(', ')}`);
process.exit(1);
}
}

// Function to download file from URL
function downloadFile(url, destination) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -220,12 +249,23 @@ async function main() {
const tempExtractPath = path.join(process.cwd(), 'temp-extract');
zip.extractAllTo(tempExtractPath, true);

// Move the extracted folder to the project name
const extractedPath = path.join(tempExtractPath, rootFolder);
fs.renameSync(extractedPath, projectPath);

if (extractToCurrentDir) {
// Move contents of extracted folder to current directory
const items = fs.readdirSync(extractedPath);
for (const item of items) {
const srcPath = path.join(extractedPath, item);
const destPath = path.join(projectPath, item);
fs.renameSync(srcPath, destPath);
}
} else {
// Move the extracted folder to the project name
fs.renameSync(extractedPath, projectPath);
}

// Clean up temp extract directory
fs.rmdirSync(tempExtractPath, { recursive: true });
fs.rmSync(tempExtractPath, { recursive: true, force: true });

// Clean up temp zip file
fs.unlinkSync(tempZipPath);
Expand Down Expand Up @@ -258,9 +298,15 @@ async function main() {
runNpmInstall(projectPath);

console.log('\n✓ Project created successfully!');
console.log(`\nNext steps:`);
console.log(` cd ${projectName}`);
console.log(` npm start (or appropriate command for your template)`);

if (!extractToCurrentDir) {
console.log(`\nNext steps:`);
console.log(` cd ${projectName}`);
console.log(` npm start (or appropriate command for your template)`);
} else {
console.log(`\nNext steps:`);
console.log(` npm start (or appropriate command for your template)`);
}

} catch (err) {
console.error('Error creating project:', err.message);
Expand Down