diff --git a/cmd/cmd_controller.js b/cmd/cmd_controller.js index fd02bb41a..831dbc9f8 100644 --- a/cmd/cmd_controller.js +++ b/cmd/cmd_controller.js @@ -419,33 +419,15 @@ class EmbarkController { ejectWebpack() { var fs = require('../lib/core/fs.js'); + var embarkConfig = fs.embarkPath('lib/pipeline/webpack.config.js'); var dappConfig = fs.dappPath('webpack.config.js'); - var embarkConfig = fs.embarkPath('lib/pipeline', 'webpack.config.js'); - let ext = 1; - let dappConfigOld = dappConfig; - while (fs.existsSync(dappConfigOld)) { - dappConfigOld = dappConfig + `.${ext}`; - ext++; - } - if (dappConfigOld !== dappConfig) { - fs.copySync(dappConfig, dappConfigOld); - } - fs.copySync(embarkConfig, dappConfig); - console.log(__('webpack config ejected to: ').dim.yellow); + fs.copyPreserve(embarkConfig, dappConfig); + console.log(__('webpack config ejected to:').dim.yellow); console.log(`${dappConfig}`.green); + var embarkOverrides = fs.embarkPath('lib/pipeline/webpack-overrides.js'); var dappOverrides = fs.dappPath('webpack-overrides.js'); - var embarkOverrides = fs.embarkPath('lib/pipeline', 'webpack-overrides.js'); - ext = 1; - let dappOverridesOld = dappOverrides; - while (fs.existsSync(dappOverridesOld)) { - dappOverridesOld = dappOverrides + `.${ext}`; - ext++; - } - if (dappOverridesOld !== dappOverrides) { - fs.copySync(dappOverrides, dappOverridesOld); - } - fs.copySync(embarkOverrides, dappOverrides); - console.log(__('webpack overrides ejected to: ').dim.yellow); + fs.copyPreserve(embarkOverrides, dappOverrides); + console.log(__('webpack overrides ejected to:').dim.yellow); console.log(`${dappOverrides}`.green); } diff --git a/lib/core/fs.js b/lib/core/fs.js index d88ee4352..2edfd5f3c 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -115,6 +115,23 @@ function tmpDir() { return utils.joinPath(os.tmpdir(), ...arguments); } +function copyPreserve(sourceFilePath, targetFilePath) { + const path = require('path'); + let ext = 1; + let preserved = targetFilePath; + while (fs.existsSync(preserved)) { + preserved = utils.joinPath( + path.dirname(targetFilePath), + `${path.basename(targetFilePath)}.${ext}${path.extname(targetFilePath)}` + ); + ext++; + } + if (preserved !== targetFilePath) { + fs.copySync(targetFilePath, preserved); + } + fs.copySync(sourceFilePath, targetFilePath); +} + module.exports = { mkdirpSync, mkdirp, @@ -139,5 +156,6 @@ module.exports = { embarkPath, dappPath, createWriteStream, - tmpDir + tmpDir, + copyPreserve };