diff --git a/bin/cli.js b/bin/cli.js index 9c5871f..38e4144 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,6 +1,6 @@ #!/usr/bin/env node const { execSync } = require('child_process'); -var fs = require('fs') +var fs = require('fs'); const isWin = process.platform === "win32"; const isAppple = process.platform === "darwin"; @@ -24,18 +24,32 @@ const runCommandWithOutput = command => { } } -const replaceTextOnFile = ({ file, textToBeReplaced, textReplace }) => { +const replaceTextOnFile = ({ + file, + textToBeReplaced, + textReplace, + arrOfObjectsBeReplaced +}) => { fs.readFile(file, 'utf8', function (err,data) { + let result if (err) { - console.log(err); - return false + return console.error(err); + } + 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, ' '); } - var result = data.replace(textToBeReplaced, textReplace); fs.writeFile(file, result, 'utf8', function (err) { if (err){ - console.log(err); - return false + return console.error(err); } }); }); @@ -70,17 +84,28 @@ console.log(`Installing dependencies for ${repoName}`); const installedDeps = runCommand(installDepsCommand); if(!installedDeps) process.exit(-1); -const deleteBin = isAppple ? runCommand(deleteBinCommandApple) : (isWin ? runCommand(deleteBinCommandWindows) : runCommand(deleteBinCommand)); +replaceTextOnFile({ + file: '../package.json', + arrOfObjectsBeReplaced: [ + { + textToBeReplaced: `"bin": "./bin/cli.js",`, + textReplace: `` + }, + { + textToBeReplaced: `"version": "${actualVersion}",`, + textReplace: `"version": "0.0.1",` + }, + { + textToBeReplaced: `"name": "@aleleba/create-react-ssr",`, + textReplace: `"name": "${repoName}",` + } + ] +}) + +/* const deleteBin = isAppple ? runCommand(deleteBinCommandApple) : (isWin ? runCommand(deleteBinCommandWindows) : runCommand(deleteBinCommand)); if(!deleteBin) process.exit(-1); -const replaceNewVersion = replaceTextOnFile({ - file: 'package.json', - textToBeReplaced: `"version": "${actualVersion}",`, - textReplace: `"version": "0.0.1",` -}) -if(!replaceNewVersion) process.exit(-1); - -/* const replaceNewVersion = runCommand(replaceNewVersionCommand) +const replaceNewVersion = runCommand(replaceNewVersionCommand) if(!replaceNewVersion) process.exit(-1); const replaceNameApp = runCommand(replaceNameAppCommand) diff --git a/package.json b/package.json index fb594a6..8dcb66a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@aleleba/create-react-ssr", - "version": "3.0.39", + "version": "3.0.40", "description": "Starter Kit of server side render of react", "bin": "./bin/cli.js", "main": "src/server/index",