From 0738e019243d01dff0bfa53d5c7dce1de83b03ee Mon Sep 17 00:00:00 2001 From: Alejandro Lembke Barrientos Date: Sat, 6 Aug 2022 01:31:57 +0000 Subject: [PATCH] PR-377800: fix npx and updating packages. --- bin/cli.js | 67 ++++++++++++++++++++++++++++++++++++++++++++--- package-lock.json | 18 ++++++------- package.json | 4 +-- 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 986bd0f..12b964b 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,5 +1,7 @@ #!/usr/bin/env node const { execSync } = require('child_process'); +var fs = require('fs'); + const isWin = process.platform === "win32"; const runCommand = command => { @@ -12,22 +14,79 @@ const runCommand = command => { return true; } +const replaceTextOnFile = ({ + file, + textToBeReplaced, + textReplace, + arrOfObjectsBeReplaced +}) => { + let data + try{ + data = fs.readFileSync(file, 'utf8'); + } catch (e) { + console.error(`Failed to read file ${file}`, e); + return false; + } + + let result + if(arrOfObjectsBeReplaced){ + arrOfObjectsBeReplaced.forEach( obj => { + if(result){ + result = result.replace(obj.textToBeReplaced, obj.textReplace).replace(/^\s*[\r\n]/gm, ' '); + }else{ + result = data.replace(obj.textToBeReplaced, obj.textReplace).replace(/^\s*[\r\n]/gm, ' '); + } + }) + }else{ + result = data.replace(textToBeReplaced, textReplace).replace(/^\s*[\r\n]/gm, ' '); + } + + try{ + console.log('text changed') + fs.writeFileSync(file, result, 'utf8'); + } catch (e) { + console.error(`Failed to read file ${file}`, e); + return false; + } +} + const repoName = process.argv[2]; const gitCheckoutCommand = `git clone --depth 1 https://github.com/aleleba/create-node-ts-graphql-server ${repoName}`; +console.log(`Cloning the repository with name ${repoName}`); +const checkedOut = runCommand(gitCheckoutCommand); +if(!checkedOut) process.exit(-1); + +const actualVersion = runCommandWithOutput(`cd ${repoName} && node -p "require('./package.json').version"`).toString().trim() + const installDepsCommand = `cd ${repoName} && npm install --legacy-peer-deps`; const cleanGitHistoryCommand = `cd ${repoName} && rm -rf .git && git init && git add --all -- ":!.github" ":!bin" && git commit -m "Initial commit"` const cleanGitHistoryCommandWindows = `cd ${repoName} && rmdir .git /s /q && git init && git add --all -- ":!.github" ":!bin" && git commit -m "Initial commit"` const deleteFoldersCommand = `cd ${repoName} && rm -rf .github && rm -rf bin` const deleteFoldersCommandWindows = `cd ${repoName} && rmdir .github /s /q && rmdir bin /s /q` -console.log(`Cloning the repository with name ${repoName}`); -const checkedOut = runCommand(gitCheckoutCommand); -if(!checkedOut) process.exit(-1); - console.log(`Installing dependencies for ${repoName}`); const installedDeps = runCommand(installDepsCommand); if(!installedDeps) process.exit(-1); +console.log(`Replacing Json data for ${repoName}`); +replaceTextOnFile({ + file: `./${repoName}/package.json`, + arrOfObjectsBeReplaced: [ + { + textToBeReplaced: `"bin": "./bin/cli.js",`, + textReplace: `` + }, + { + textToBeReplaced: `"version": "${actualVersion}",`, + textReplace: `"version": "0.0.1",` + }, + { + textToBeReplaced: `"name": "@aleleba/create-node-ts-graphql-server",`, + textReplace: `"name": "${repoName}",` + } + ] +}) + console.log(`Cleaning History of Git for ${repoName}`); const cleanGitHistory = isWin ? runCommand(cleanGitHistoryCommandWindows) : runCommand(cleanGitHistoryCommand); if(!cleanGitHistory) process.exit(-1); diff --git a/package-lock.json b/package-lock.json index 6cfc232..862f44e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@aleleba/create-node-ts-graphql-server", - "version": "1.0.17", + "version": "1.1.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@aleleba/create-node-ts-graphql-server", - "version": "1.0.17", + "version": "1.1.2", "license": "MIT", "dependencies": { "@graphql-tools/schema": "^8.5.1", @@ -33,7 +33,7 @@ "@babel/preset-typescript": "^7.18.6", "@babel/register": "^7.18.9", "@types/jest": "^28.1.6", - "@types/node": "^18.6.3", + "@types/node": "^18.6.4", "@types/webpack": "^5.28.0", "@types/webpack-node-externals": "^2.5.3", "@typescript-eslint/eslint-plugin": "^5.32.0", @@ -3095,9 +3095,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "18.6.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.6.3.tgz", - "integrity": "sha512-6qKpDtoaYLM+5+AFChLhHermMQxc3TOEFIDzrZLPRGHPrLEwqFkkT5Kx3ju05g6X7uDPazz3jHbKPX0KzCjntg==", + "version": "18.6.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.6.4.tgz", + "integrity": "sha512-I4BD3L+6AWiUobfxZ49DlU43gtI+FTHSv9pE2Zekg6KjMpre4ByusaljW3vYSLJrvQ1ck1hUaeVu8HVlY3vzHg==", "dev": true }, "node_modules/@types/prettier": { @@ -13297,9 +13297,9 @@ "dev": true }, "@types/node": { - "version": "18.6.3", - "resolved": "https://registry.npmjs.org/@types/node/-/node-18.6.3.tgz", - "integrity": "sha512-6qKpDtoaYLM+5+AFChLhHermMQxc3TOEFIDzrZLPRGHPrLEwqFkkT5Kx3ju05g6X7uDPazz3jHbKPX0KzCjntg==", + "version": "18.6.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.6.4.tgz", + "integrity": "sha512-I4BD3L+6AWiUobfxZ49DlU43gtI+FTHSv9pE2Zekg6KjMpre4ByusaljW3vYSLJrvQ1ck1hUaeVu8HVlY3vzHg==", "dev": true }, "@types/prettier": { diff --git a/package.json b/package.json index 921bc78..567e7e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@aleleba/create-node-ts-graphql-server", - "version": "1.1.1", + "version": "1.1.2", "description": "Node with Typescript and GraphQL Server", "bin": "./bin/cli.js", "main": "index.js", @@ -53,7 +53,7 @@ "@babel/preset-typescript": "^7.18.6", "@babel/register": "^7.18.9", "@types/jest": "^28.1.6", - "@types/node": "^18.6.3", + "@types/node": "^18.6.4", "@types/webpack": "^5.28.0", "@types/webpack-node-externals": "^2.5.3", "@typescript-eslint/eslint-plugin": "^5.32.0",