extract copy/preserve logic into a helper in lib/core/fs

preserved files should end with <number>.<ext> vs. <ext>.<number>
This commit is contained in:
Michael Bradley, Jr 2018-10-03 13:51:51 -05:00 committed by Pascal Precht
parent dc8b171354
commit ae878fd7c8
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
2 changed files with 25 additions and 25 deletions

View File

@ -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);
}

View File

@ -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
};