-
-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Use listr to display progress and errors for transformations (…
…#92) * Promisify transform functions and use listr to show success/errors * Improve startup time by requiring code in appropriate branches * Added throwing webpack config fixture
- Loading branch information
Showing
7 changed files
with
224 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,83 @@ | ||
const fs = require('fs'); | ||
const diff = require('diff'); | ||
const chalk = require('chalk'); | ||
const transform = require('./transformations').transform; | ||
const diff = require('diff'); | ||
const inquirer = require('inquirer'); | ||
const PLazy = require('p-lazy'); | ||
const Listr = require('listr'); | ||
|
||
module.exports = (currentConfigPath, outputConfigPath) => { | ||
let currentConfig = fs.readFileSync(currentConfigPath, 'utf8'); | ||
const outputConfig = transform(currentConfig); | ||
const diffOutput = diff.diffLines(currentConfig, outputConfig); | ||
diffOutput.map(diffLine => { | ||
if (diffLine.added) { | ||
process.stdout.write(chalk.green(`+ ${diffLine.value}`)); | ||
} else if (diffLine.removed) { | ||
process.stdout.write(chalk.red(`- ${diffLine.value}`)); | ||
} | ||
}); | ||
inquirer | ||
.prompt([ | ||
{ | ||
type: 'confirm', | ||
name: 'confirmMigration', | ||
message: 'Are you sure these changes are fine?', | ||
default: 'Y' | ||
} | ||
]) | ||
.then(answers => { | ||
if (answers['confirmMigration']) { | ||
// TODO validate the config | ||
fs.writeFileSync(outputConfigPath, outputConfig, 'utf8'); | ||
process.stdout.write(chalk.green(`Congratulations! Your new webpack v2 config file is at ${outputConfigPath}`)); | ||
} else { | ||
process.stdout.write(chalk.yellow('Migration aborted')); | ||
module.exports = function transformFile(currentConfigPath, outputConfigPath, options) { | ||
const recastOptions = Object.assign({ | ||
quote: 'single' | ||
}, options); | ||
const tasks = new Listr([ | ||
{ | ||
title: 'Reading webpack config', | ||
task: (ctx) => new PLazy((resolve, reject) => { | ||
fs.readFile(currentConfigPath, 'utf8', (err, content) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
try { | ||
console.time('Reading webpack config'); | ||
const jscodeshift = require('jscodeshift'); | ||
ctx.source = content; | ||
ctx.ast = jscodeshift(content); | ||
resolve(); | ||
console.timeEnd('Reading webpack config'); | ||
} catch (err) { | ||
reject('Error generating AST', err); | ||
} | ||
}); | ||
}) | ||
}, | ||
{ | ||
title: 'Migrating config from v1 to v2', | ||
task: (ctx) => { | ||
const transformations = require('./transformations').transformations; | ||
return new Listr(Object.keys(transformations).map(key => { | ||
const transform = transformations[key]; | ||
return { | ||
title: key, | ||
task: () => transform(ctx.ast, ctx.source) | ||
}; | ||
})); | ||
} | ||
} | ||
]); | ||
|
||
tasks.run() | ||
.then((ctx) => { | ||
const result = ctx.ast.toSource(recastOptions); | ||
const diffOutput = diff.diffLines(ctx.source, result); | ||
diffOutput.forEach(diffLine => { | ||
if (diffLine.added) { | ||
process.stdout.write(chalk.green(`+ ${diffLine.value}`)); | ||
} else if (diffLine.removed) { | ||
process.stdout.write(chalk.red(`- ${diffLine.value}`)); | ||
} | ||
}); | ||
inquirer | ||
.prompt([ | ||
{ | ||
type: 'confirm', | ||
name: 'confirmMigration', | ||
message: 'Are you sure these changes are fine?', | ||
default: 'Y' | ||
} | ||
]) | ||
.then(answers => { | ||
if (answers['confirmMigration']) { | ||
// TODO validate the config | ||
fs.writeFileSync(outputConfigPath, result, 'utf8'); | ||
console.log(chalk.green(`✔︎ New webpack v2 config file is at ${outputConfigPath}`)); | ||
} else { | ||
console.log(chalk.red('✖ Migration aborted')); | ||
} | ||
}); | ||
}) | ||
.catch(err => { | ||
console.log(chalk.red('✖ ︎Migration aborted due to some errors')); | ||
console.error(err); | ||
process.exitCode = 1; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
var webpack = require('webpack'); | ||
var nodeEnvironment = process.env.NODE_ENV | ||
var _ = require('lodash'); | ||
|
||
var config = { | ||
entry: { | ||
'lib': './app/index.js', | ||
'email': './app/email.js' | ||
}, | ||
plugins: [ | ||
new webpack.DefinePlugin({ | ||
'INCLUDE_ALL_MODULES': function includeAllModulesGlobalFn(modulesArray, application) { | ||
modulesArray.forEach(function executeModuleIncludesFn(moduleFn) { | ||
moduleFn(application); | ||
}); | ||
}, | ||
ENVIRONMENT: JSON.stringify(nodeEnvironment) | ||
}) | ||
], | ||
output: { | ||
path: __dirname + '/app', | ||
filename: 'bundle.js' | ||
}, | ||
resolve: { | ||
root: __dirname + '/app' | ||
}, | ||
module: { | ||
// preLoaders: [ | ||
// { test: /\.js?$/, loader: 'eslint', exclude: /node_modules/ } | ||
// ], | ||
loaders: [ | ||
{ test: /\.js$/, exclude: /(node_modules)/, loader: 'babel' }, | ||
{ test: /\.html/, exclude: [/(node_modules)/, /src\/index\.html/], loader: 'html-loader' }, | ||
{ test: /\.s?css$/, loader: 'style!css!sass' }, | ||
{ test: /\.(png|jpg)$/, loader: 'url-loader?mimetype=image/png' } | ||
] | ||
}, | ||
// extra configuration options. | ||
// eslint: { | ||
// configFile: '.eslintrc.js' | ||
// } | ||
} | ||
|
||
switch (nodeEnvironment) { | ||
case 'production': | ||
config.plugins.push(new webpack.optimize.UglifyJsPlugin()); | ||
case 'preproduction': | ||
config.output.path = __dirname + '/dist'; | ||
config.plugins.push(new webpack.optimize.DedupePlugin()); | ||
config.plugins.push(new webpack.optimize.OccurenceOrderPlugin()); | ||
|
||
config.output.filename = '[name].js'; | ||
|
||
config.entry = { | ||
'lib': ['./app/index.js', 'angular', 'lodash'], | ||
'email': ['./app/email.js', 'angular'] | ||
}; | ||
|
||
config.devtool = 'source-map'; | ||
config.output.libraryTarget = 'commonjs2'; | ||
|
||
break; | ||
|
||
case 'test': | ||
config.entry = './index.js'; | ||
break; | ||
|
||
case 'development': | ||
config.entry = { | ||
'lib': ['./app/index.js', 'webpack/hot/dev-server'], | ||
'email': ['./app/email.js', 'webpack/hot/dev-server'] | ||
}; | ||
config.output.filename = '[name].js'; | ||
config.devtool = 'source-map'; | ||
break; | ||
|
||
default: | ||
console.warn('Unknown or Undefined Node Environment. Please refer to package.json for available build commands.'); | ||
} | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.