From a3c3ff58d4deef6819bafd8fa90b4bfd6cdea353 Mon Sep 17 00:00:00 2001 From: Vijay Pratap Date: Mon, 9 Sep 2024 00:38:14 +0530 Subject: [PATCH] Y2024 --- publish.sh | 2 +- .../createMernApp.js | 110 ++++++++---------- .../create-mernjs-app-updated/lib/init.js | 77 ------------ .../lib/utils/Helpers.js | 27 +++++ .../create-mernjs-app-updated/package.json | 3 +- .../packages/create-mernjs-app/lib/init.js | 4 +- .../packages/create-mernjs-app/package.json | 4 +- 7 files changed, 80 insertions(+), 147 deletions(-) delete mode 100644 templates/packages/create-mernjs-app-updated/lib/init.js diff --git a/publish.sh b/publish.sh index 75aed268..bc1f337c 100644 --- a/publish.sh +++ b/publish.sh @@ -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 diff --git a/templates/packages/create-mernjs-app-updated/createMernApp.js b/templates/packages/create-mernjs-app-updated/createMernApp.js index 6778c90b..e3b859fb 100755 --- a/templates/packages/create-mernjs-app-updated/createMernApp.js +++ b/templates/packages/create-mernjs-app-updated/createMernApp.js @@ -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'); @@ -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) => { @@ -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('') .usage(`${chalk.green('')}`) - .option('-y, --yes', 'skip Is this OK ? step') - .option('--template ', 'specify a GitHub template repository') - .option('--template-dir ', '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); @@ -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('')}`); - 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); } } diff --git a/templates/packages/create-mernjs-app-updated/lib/init.js b/templates/packages/create-mernjs-app-updated/lib/init.js deleted file mode 100644 index 9a02b437..00000000 --- a/templates/packages/create-mernjs-app-updated/lib/init.js +++ /dev/null @@ -1,77 +0,0 @@ -const path = require('path') -const chalk = require("chalk") -const fs = require("fs-extra"); -const { spawn } = require("child_process"); -const Constants = require('./utils/Constants') -const Helpers = require('./utils/Helpers') - -module.exports = async (projectname, { project_type, template }) => { - const currentWorkingDir = process.cwd() - let templates = "" - switch (template) { - case "library": - templates = "library" - break; - case "package": - templates = "package" - break; - case "snippets": - templates = "snippets" - break; - default: - templates = 'app' - break; - } - console.log(' ') - console.log(chalk.hex('#4c84ff')(`Creating a new ${chalk.yellow(projectname)} project in`), chalk.green(`${currentWorkingDir}/${projectname}`)) - console.log(' ') - 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 gitSourcePath = path.join(__dirname, `utils/gitignore.js`) - - const appID = Helpers.generateRadomeString(32) - - Helpers.createDirAndCopy(sourcePath, destinationPath) - .then((success) => { - Helpers.copyGitignoreFile(gitSourcePath, destinationPath) - console.log(" ") - let message = `Congratulations! Your ${chalk.yellow(projectname)} project has been Created at` - console.log(message, chalk.green(`${currentWorkingDir}/${projectname}`)) - - process.chdir(destinationPath) - Helpers.rewritePackageName(`${destinationPath}/package.json`, projectname) - - if (project_typ === 'react-native-app') Helpers.rewritePackageName(`${destinationPath}/app.json`, projectname) - - console.log(" ") - console.log(chalk.hex('#4c84ff').bold("Installing dependencies... This might take a couple of minutes.")); - console.log(" ") - let command = 'npm install --legacy-peer-deps' - const child = spawn(command, { stdio: "inherit", shell: true }); - child.on("close", code => { - if (code !== 0) return console.log({ message: `${command}` }); - console.log(" "); - console.log(chalk.hex('#008000').bold(' We suggest that you begin by typing:')) - console.log(" "); - console.log(chalk.hex('#4c84ff')(` cd ${projectname}`)) - 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(" "); - console.log("All Done!"); - console.log(" "); - console.log(" "); - process.exit(0); - }) - }).catch(error => { - console.log(destinationPath) - console.log(error) - fs.rmdirSync(destinationPath, { recursive: true, force: true }) - console.log(chalk.red.bold('Unexpected error occurred.')); - process.exit(0); - }); -} \ No newline at end of file diff --git a/templates/packages/create-mernjs-app-updated/lib/utils/Helpers.js b/templates/packages/create-mernjs-app-updated/lib/utils/Helpers.js index 3ddf4c6d..adaa5c64 100755 --- a/templates/packages/create-mernjs-app-updated/lib/utils/Helpers.js +++ b/templates/packages/create-mernjs-app-updated/lib/utils/Helpers.js @@ -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(" "); @@ -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()); + }); + }); } \ No newline at end of file diff --git a/templates/packages/create-mernjs-app-updated/package.json b/templates/packages/create-mernjs-app-updated/package.json index ffa0bc80..adb7f305 100644 --- a/templates/packages/create-mernjs-app-updated/package.json +++ b/templates/packages/create-mernjs-app-updated/package.json @@ -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": { @@ -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", diff --git a/templates/packages/create-mernjs-app/lib/init.js b/templates/packages/create-mernjs-app/lib/init.js index 9a02b437..761809b0 100644 --- a/templates/packages/create-mernjs-app/lib/init.js +++ b/templates/packages/create-mernjs-app/lib/init.js @@ -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(" ") diff --git a/templates/packages/create-mernjs-app/package.json b/templates/packages/create-mernjs-app/package.json index fcbd610b..293eabde 100644 --- a/templates/packages/create-mernjs-app/package.json +++ b/templates/packages/create-mernjs-app/package.json @@ -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": { @@ -38,4 +38,4 @@ }, "homepage": "https://github.com/mernjs/create-mern-app#readme", "gitHead": "e2f4c703bdc504d5d978cce68a01cd0a35118cce" -} \ No newline at end of file +}