-
-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Use listr to display progress and errors for transformations #92
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
709bcb7
WIP on Listr integration
okonet ac3fc7e
tests: Added throwing webpack config fixture
okonet c319ebe
Promisify transform functions and use listr to show success/errors
okonet 76a9ca4
Merge branch 'master' into feature/listr
okonet bf0427e
fix: Improve startup time by requiring code in appropriate branches
okonet 55664e4
Better Listr usage and speed improvements
okonet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch that shouldn't have made through the review :( Will remove it.