Skip to content

Commit

Permalink
Y2024
Browse files Browse the repository at this point in the history
  • Loading branch information
vprtsingh committed Sep 8, 2024
1 parent d827d50 commit a3c3ff5
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 147 deletions.
2 changes: 1 addition & 1 deletion publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ REPO_NAME="create-mern-app"

# Step 5: Navigate to the package directory
echo "Navigating to package directory 'packages/create-mernjs-app'..."
cd src/packages/create-mernjs-app
cd templates/packages/create-mernjs-app
check_last_command

# Confirm package.json exists
Expand Down
110 changes: 46 additions & 64 deletions templates/packages/create-mernjs-app-updated/createMernApp.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { exec, spawn } = require("child_process"); // Added spawn here
const { exec, spawn } = require("child_process");
const chalk = require("chalk");
const commander = require("commander");
const { prompt } = require("inquirer")
const { prompt } = require("inquirer");
const fs = require("fs-extra");
const Constants = require('./lib/utils/Constants');
const yosay = require('yosay');
Expand Down Expand Up @@ -39,11 +39,12 @@ async function sparseCloneRepo(repoUrl, subdirectory, destinationPath) {
`cd ${destinationPath}`, // Move to the destination directory
`git remote add origin ${repoUrl}`, // Add the remote repo URL
`git config core.sparseCheckout true`, // Enable sparse checkout
`echo "${subdirectory}" >> .git/info/sparse-checkout`, // Specify the subdirectory you want to clone
`git pull origin master`, // Pull from the master branch (adjust to main if needed)
`mv ${subdirectory}/* ./` // Move files from the subdirectory to the root
// `rm -rf ${subdirectory}`, // Remove the empty subdirectory
// `rm -rf .git` // Optional: Remove .git to avoid including Git history
`echo "${subdirectory}" >> .git/info/sparse-checkout`, // Specify the subdirectory to clone
`git pull origin master`, // Pull from the master branch (or 'main' if necessary)
`mv ${subdirectory}/* ./`, // Move files from the subdirectory to the root
`rm -rf ${subdirectory}`, // Remove the empty subdirectory
`rm -rf templates`,
`rm -rf .git` // Optional: Remove .git to avoid including Git history
].join(' && ');

exec(commands, (error, stdout, stderr) => {
Expand All @@ -56,70 +57,60 @@ async function sparseCloneRepo(repoUrl, subdirectory, destinationPath) {
});
}


async function init() {
const program = new commander.Command(Constants.package.name)
.version(Constants.package.version)
.arguments('<project-directory>')
.usage(`${chalk.green('<project-directory>')}`)
.option('-y, --yes', 'skip Is this OK ? step')
.option('--template <github-repo-url>', 'specify a GitHub template repository')
.option('--template-dir <subdirectory>', 'specify a subdirectory in the template repository to use as the template')
.action(async (project_name, cmd_obj) => {
projectName = project_name;
.option('-y, --yes', 'skip Is this OK? step')
.action(async (project_name) => {
const projectName = project_name;
checkAppName(project_name);

Helpers.checkForLatestVersion().then(async (latest) => {
if (latest && semver.lt(Constants.package.version, latest)) {
const message1 = `You are running \`create-mernjs-app\` ${chalk.bold.red(Constants.package.version)}, which is behind the latest release ${chalk.bold.green(latest)}.`;
const message2 = 'We recommend always using the latest version of create-mernjs-app.';
const message3 = 'npm install create-mernjs-app -g';
const message4 = `https://github.com/mernjs/create-mern-app/releases`;
console.log('\n');
console.log(yosay(`${chalk.yellow(message1)} \n\n ${chalk.cyan(message2)} \n\n ${chalk.bold.green(message3)} \n\n ${chalk.blue(message4)} `, { maxLength: 55 }));
console.log('\n');
console.log(chalk.red('Please update to the latest version.'));
process.exit(1);
} else {
const currentPath = process.cwd();
const destinationPath = `${currentPath}/${project_name}`;
if (fs.existsSync(destinationPath)) {
console.log('');
console.log(chalk.red("Folder already exists"));
console.log('');
return;
}
console.log('Press ^C at any time to quit.');
prompt(cmd_obj?.template ? Constants.select_library_form : Constants.select_form).then(async (data) => {
const repoUrl = cmd_obj?.template || 'https://github.com/mernjs/create-mern-app';
const subdirectory = cmd_obj?.templateDir || 'default-template-folder';

console.log(chalk.green(`Cloning the template from ${repoUrl}, subdirectory: ${subdirectory}...`));
try {
await sparseCloneRepo(repoUrl, subdirectory, destinationPath);
console.log(chalk.green(`Template cloned successfully to ${destinationPath}`));
Helpers.rewritePackageName(`${destinationPath}/package.json`, project_name);
console.log(chalk.hex('#4c84ff').bold("Installing dependencies... This might take a couple of minutes."));
const command = 'npm install --legacy-peer-deps';
const child = spawn(command, { stdio: "inherit", shell: true }); // Updated spawn usage
child.on("close", (code) => {
if (code !== 0) return;
console.log(" ");
console.log(chalk.hex('#008000').bold(' We suggest that you begin by typing:'));
console.log(" ");
console.log(chalk.hex('#4c84ff')(` cd ${project_name}`));
console.log(` Go to your project directory`);
console.log(" ");
console.log(chalk.hex('#4c84ff')(` ${Constants.mernjs.serve}`));
console.log(` Starts the development server.`);
console.log(" ");
console.log("All Done!");
process.exit(0);
});
} catch (error) {
console.error(chalk.red('Error cloning repository:'), error);
// fs.rmdirSync(destinationPath, { recursive: true, force: true });
// Hardcoded repository URL and subdirectory
const repoUrl = 'https://github.com/mernjs/create-mern-app'; // Hardcoded URL
const subdirectory = 'templates/app/electronjs-boilerplate'; // Hardcoded subdirectory (e.g., 'packages')


// Create a loader with ora
console.log(`Creating a new ${chalk.yellow(project_name)} project in ${chalk.green(destinationPath)}`)

try {
await sparseCloneRepo(repoUrl, subdirectory, destinationPath);
let message = `Congratulations! Your ${chalk.yellow(project_name)} project has been Created at`
console.log(message, chalk.green(`${destinationPath}`));

process.chdir(destinationPath)

Helpers.rewritePackageName(`${destinationPath} / package.json`, project_name);
console.log(chalk.hex('#4c84ff').bold("Installing dependencies..."));

const command = 'npm install';
const child = spawn(command, { stdio: "inherit", shell: true });

child.on("close", (code) => {
if (code !== 0) return;
console.log(chalk.hex('#008000').bold('Project setup complete.'));
process.exit(0);
}
});
});
} catch (error) {
spinner.fail('Error cloning repository');
console.error(chalk.red('Error:', error));
fs.rmdirSync(destinationPath, { recursive: true, force: true });
process.exit(0);
}
}
}).catch((error) => {
console.log(error);
Expand All @@ -128,17 +119,8 @@ async function init() {
})
.parse(process.argv);

if (typeof projectName === 'undefined') {
console.log();
console.error('Please specify the project directory:');
console.log();
console.log(` ${chalk.cyan(`${program.name()}`)} ${chalk.green('<project-directory>')}`);
console.log();
console.log('For example:');
console.log(` ${chalk.cyan(`${program.name()}`)} ${chalk.green('my_project')}`);
console.log();
console.log(`Run ${chalk.cyan(`${program.name()} --help`)} to see all options.`);
console.log();
if (typeof program.args[0] === 'undefined') {
console.error('Please specify the project directory.');
process.exit(1);
}
}
Expand Down
77 changes: 0 additions & 77 deletions templates/packages/create-mernjs-app-updated/lib/init.js

This file was deleted.

27 changes: 27 additions & 0 deletions templates/packages/create-mernjs-app-updated/lib/utils/Helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs-extra");
const { spawn } = require("child_process");
const https = require('https');
const { exec, spawn } = require("child_process");

function errorMessaage(error) {
console.log(" ");
Expand Down Expand Up @@ -87,4 +88,30 @@ exports.checkForLatestVersion = () => {
reject();
});
});
}

async function sparseCloneRepo(subdirectory, destinationPath) {
const repoUrl = 'https://github.com/mernjs/create-mern-app'; // Hardcoded URL
return new Promise((resolve, reject) => {
const commands = [
`git init ${destinationPath}`, // Initialize a new git repo at destinationPath
`cd ${destinationPath}`, // Move to the destination directory
`git remote add origin ${repoUrl}`, // Add the remote repo URL
`git config core.sparseCheckout true`, // Enable sparse checkout
`echo "${subdirectory}" >> .git/info/sparse-checkout`, // Specify the subdirectory to clone
`git pull origin master`, // Pull from the master branch (or 'main' if necessary)
`mv ${subdirectory}/* ./`, // Move files from the subdirectory to the root
`rm -rf ${subdirectory}`, // Remove the empty subdirectory
`rm -rf templates`,
`rm -rf .git` // Optional: Remove .git to avoid including Git history
].join(' && ');

exec(commands, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
resolve(stdout.trim());
});
});
}
3 changes: 2 additions & 1 deletion templates/packages/create-mernjs-app-updated/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-mernjs-app-2",
"version": "21.0.45",
"version": "21.0.52",
"description": "The MERN App provides a versatile set of boilerplates that streamline the development process for various applications. Whether you're building a web app, mobile app, desktop app, Chrome extension, or an NPM package in JavaScript, our comprehensive templates will help you get started quickly and efficiently.",
"main": "./index.js",
"scripts": {
Expand All @@ -20,6 +20,7 @@
"https": "^1.0.0",
"inquirer": "^8.0.0",
"mernjs": "github:mernjs/create-mern-app#1618aa3a6792267b74f6119120f3f09da97748b6",
"ora": "^8.1.0",
"path": "^0.12.7",
"semver": "^7.3.8",
"shelljs": "^0.8.5",
Expand Down
4 changes: 2 additions & 2 deletions templates/packages/create-mernjs-app/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ module.exports = async (projectname, { project_type, template }) => {
let project_typ = project_type.replace(/ /g, '-').toLowerCase()

const destinationPath = `${currentWorkingDir}/${projectname}`
const sourcePath = path.join(__dirname, `../../../node_modules/create-mernjs-app/node_modules/mernjs/src/${templates}/${project_typ}/`)
const sourcePath = `templates/${templates}/${project_typ}`
const gitSourcePath = path.join(__dirname, `utils/gitignore.js`)

const appID = Helpers.generateRadomeString(32)

Helpers.createDirAndCopy(sourcePath, destinationPath)
Helpers.sparseCloneRepo(sourcePath, destinationPath)
.then((success) => {
Helpers.copyGitignoreFile(gitSourcePath, destinationPath)
console.log(" ")
Expand Down
4 changes: 2 additions & 2 deletions templates/packages/create-mernjs-app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-mernjs-app",
"version": "21.0.37",
"version": "21.0.38",
"description": "The MERN App provides a versatile set of boilerplates that streamline the development process for various applications. Whether you're building a web app, mobile app, desktop app, Chrome extension, or an NPM package in JavaScript, our comprehensive templates will help you get started quickly and efficiently.",
"main": "./index.js",
"scripts": {
Expand Down Expand Up @@ -38,4 +38,4 @@
},
"homepage": "https://github.com/mernjs/create-mern-app#readme",
"gitHead": "e2f4c703bdc504d5d978cce68a01cd0a35118cce"
}
}

0 comments on commit a3c3ff5

Please sign in to comment.