Skip to content
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 6 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Better Listr usage and speed improvements
  • Loading branch information
okonet committed Mar 23, 2017
commit 55664e40e233e9f5e31cda1d1b6f41e9742fba14
67 changes: 42 additions & 25 deletions lib/migrate.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
const fs = require('fs');
const diff = require('diff');
const chalk = require('chalk');
const jscodeshift = require('jscodeshift');
const transformations = require('./transformations').transformations;
const diff = require('diff');
const inquirer = require('inquirer');
const PLazy = require('p-lazy');
const Listr = require('listr');

const migrateTasks = function(ast) {
return Object.keys(transformations).map(key => {
const transform = transformations[key];
return {
title: key,
task: () => transform(ast)
};
});
};

module.exports = function tranformFile(currentConfigPath, outputConfigPath, options) {
module.exports = function transformFile(currentConfigPath, outputConfigPath, options) {
const recastOptions = Object.assign({
quote: 'single'
}, options);
let currentConfig = fs.readFileSync(currentConfigPath, 'utf8');
const ast = jscodeshift(currentConfig);
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');
Copy link
Contributor Author

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.

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: () => new Listr(migrateTasks(ast))
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(() => {
const outputConfig = ast.toSource(recastOptions);
const diffOutput = diff.diffLines(currentConfig, outputConfig);
diffOutput.map(diffLine => {
.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) {
Expand All @@ -52,15 +68,16 @@ module.exports = function tranformFile(currentConfigPath, outputConfigPath, opti
.then(answers => {
if (answers['confirmMigration']) {
// TODO validate the config
fs.writeFileSync(outputConfigPath, outputConfig, 'utf8');
process.stdout.write(chalk.green(`✔︎ New webpack v2 config file is at ${outputConfigPath}`));
fs.writeFileSync(outputConfigPath, result, 'utf8');
console.log(chalk.green(`✔︎ New webpack v2 config file is at ${outputConfigPath}`));
} else {
process.stdout.write(chalk.red('✖ Migration aborted'));
console.log(chalk.red('✖ Migration aborted'));
}
});
})
.catch(err => {
console.error(chalk.red('✖ ︎Migration aborted due to some errors'));
console.log(chalk.red('✖ ︎Migration aborted due to some errors'));
console.error(err);
process.exitCode = 1;
});
};
9 changes: 5 additions & 4 deletions lib/transformations/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const jscodeshift = require('jscodeshift');
const pEachSeries = require('p-each-series');
const PLazy = require('p-lazy');

const loadersTransform = require('./loaders/loaders');
const resolveTransform = require('./resolve/resolve');
Expand All @@ -22,15 +23,15 @@ const transformsObject = {
};

const transformations = Object.keys(transformsObject).reduce((res, key) => {
res[key] = (ast) => transformSingleAST(ast, transformsObject[key]);
res[key] = (ast, source) => transformSingleAST(ast, source, transformsObject[key]);
return res;
}, {});

function transformSingleAST(ast, transformFunction) {
return new Promise((resolve, reject) => {
function transformSingleAST(ast, source, transformFunction) {
return new PLazy((resolve, reject) => {
setTimeout(() => {
try {
resolve(transformFunction(jscodeshift, ast));
resolve(transformFunction(jscodeshift, ast, source));
} catch (err) {
reject(err);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"loader-utils": "^0.2.16",
"lodash": "^4.17.4",
"p-each-series": "^1.0.0",
"p-lazy": "^1.0.0",
"recast": "git://github.com/kalcifer/recast.git#bug/allowbreak",
"rx": "^4.1.0",
"supports-color": "^3.1.2",
Expand Down