From dda80e4fc95235f0ce9928ebb74c7e70a4ddeed1 Mon Sep 17 00:00:00 2001 From: eanplatter Date: Fri, 22 Jul 2016 07:45:56 -0500 Subject: [PATCH 1/2] Recursively copy the template folder --- scripts/init.js | 52 +++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/scripts/init.js b/scripts/init.js index 5bf839308c6..af33fb8f751 100644 --- a/scripts/init.js +++ b/scripts/init.js @@ -35,25 +35,39 @@ module.exports = function(hostPath, appName, verbose) { ); // Copy the files for the user - function copySync(src, dest) { - return fs.writeFileSync(dest, fs.readFileSync(src)); + function copyFileSync(src, dest) { + var target = dest; + if (fs.existsSync(dest)) { + if (fs.lstatSync(dest).isDirectory()) { + target = path.join(dest, path.basename(src)); + } + } + fs.writeFileSync(target, fs.readFileSync(src)); } - fs.mkdirSync(path.join(hostPath, 'src')); - fs.readdirSync(path.join(selfPath, 'template/src')).forEach(function(filename) { - copySync( - path.join(selfPath, 'template/src', filename), - path.join(hostPath, 'src', filename) - ); - }); - fs.readdirSync(path.join(selfPath, 'template')).forEach(function(filename) { - if (fs.lstatSync(path.join(selfPath, 'template', filename)).isDirectory()) { - return - } - copySync( - path.join(selfPath, 'template', filename), - path.join(hostPath, filename) - ); - }); + + function copyFolderSync(src, dest) { + var files = []; + var targetFolder = path.join(dest, path.basename(src)).replace('/template', ''); + if (!fs.existsSync(targetFolder)) { + fs.mkdirSync(targetFolder); + } + if (fs.lstatSync(src).isDirectory()) { + files = fs.readdirSync(src); + files.forEach(function (file) { + var currentSrc = path.join(src, file); + if (fs.lstatSync(currentSrc).isDirectory()) { + copyFolderSync(currentSrc, targetFolder); + } else { + copyFileSync(currentSrc, targetFolder); + } + } ); + } + } + + copyFolderSync( + path.join(selfPath, 'template'), + hostPath + ) // Run another npm install for react and react-dom console.log('Installing react and react-dom from npm...'); @@ -71,7 +85,7 @@ module.exports = function(hostPath, appName, verbose) { // Make sure to display the right way to cd var cdpath; - if (path.join(process.cwd(), appName) == hostPath) { + if (path.join(process.cwd(), appName) === hostPath) { cdpath = appName; } else { cdpath = hostPath; From ab5259e055de57c85a3a25a5f6a1bfb0523b3c81 Mon Sep 17 00:00:00 2001 From: eanplatter Date: Fri, 22 Jul 2016 08:25:09 -0500 Subject: [PATCH 2/2] add fs-extra library --- package.json | 1 + scripts/init.js | 36 ++---------------------------------- 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index b65e5100421..3cdcf3ce507 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ }, "devDependencies": { "bundle-deps": "^1.0.0", + "fs-extra": "^0.30.0", "react": "^15.2.1", "react-dom": "^15.2.1" }, diff --git a/scripts/init.js b/scripts/init.js index af33fb8f751..fce0a8c241c 100644 --- a/scripts/init.js +++ b/scripts/init.js @@ -7,7 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -var fs = require('fs'); +var fs = require('fs-extra'); var path = require('path'); var spawn = require('cross-spawn'); @@ -35,39 +35,7 @@ module.exports = function(hostPath, appName, verbose) { ); // Copy the files for the user - function copyFileSync(src, dest) { - var target = dest; - if (fs.existsSync(dest)) { - if (fs.lstatSync(dest).isDirectory()) { - target = path.join(dest, path.basename(src)); - } - } - fs.writeFileSync(target, fs.readFileSync(src)); - } - - function copyFolderSync(src, dest) { - var files = []; - var targetFolder = path.join(dest, path.basename(src)).replace('/template', ''); - if (!fs.existsSync(targetFolder)) { - fs.mkdirSync(targetFolder); - } - if (fs.lstatSync(src).isDirectory()) { - files = fs.readdirSync(src); - files.forEach(function (file) { - var currentSrc = path.join(src, file); - if (fs.lstatSync(currentSrc).isDirectory()) { - copyFolderSync(currentSrc, targetFolder); - } else { - copyFileSync(currentSrc, targetFolder); - } - } ); - } - } - - copyFolderSync( - path.join(selfPath, 'template'), - hostPath - ) + fs.copySync(path.join(selfPath, 'template'), hostPath); // Run another npm install for react and react-dom console.log('Installing react and react-dom from npm...');