refactor(bin): use `resolveConfigFile.sync()` (#2728)

* refactor(bin): resolveConfig

* refactor(bin): getOptionsForFile

* refactor(bin): remove `mapSeries`
master
Ika 2017-09-02 20:17:23 +08:00 committed by Lucas Azzola
parent 088aa71b07
commit 8a6b56be41
1 changed files with 38 additions and 60 deletions

View File

@ -102,32 +102,27 @@ if (argv["find-config-path"] && filepatterns.length) {
}
function getOptionsForFile(filePath) {
const optionsPromise =
argv["config"] === false
? Promise.resolve(null)
: resolver.resolveConfig(filePath);
const options =
argv["config"] === false ? null : resolver.resolveConfig.sync(filePath);
return optionsPromise
.then(options => {
const parsedArgs = minimist(args, {
boolean: booleanOptionNames,
string: stringOptionNames,
default: Object.assign(
{
semi: true,
"bracket-spacing": true,
parser: "babylon"
},
dashifyObject(options)
)
});
return getOptions(Object.assign({}, argv, parsedArgs));
})
.catch(error => {
console.error("Invalid configuration file:", error.toString());
process.exit(2);
try {
const parsedArgs = minimist(args, {
boolean: booleanOptionNames,
string: stringOptionNames,
default: Object.assign(
{
semi: true,
"bracket-spacing": true,
parser: "babylon"
},
dashifyObject(options)
)
});
return getOptions(Object.assign({}, argv, parsedArgs));
} catch (error) {
console.error("Invalid configuration file:", error.toString());
process.exit(2);
}
}
function getOptions(argv) {
@ -324,17 +319,17 @@ if (argv["find-config-path"]) {
resolveConfig(argv["find-config-path"]);
} else if (stdin) {
getStream(process.stdin).then(input => {
getOptionsForFile(process.cwd()).then(options => {
if (listDifferent(input, options, "(stdin)")) {
return;
}
const options = getOptionsForFile(process.cwd());
try {
writeOutput(format(input, options), options);
} catch (e) {
handleError("stdin", e);
}
});
if (listDifferent(input, options, "(stdin)")) {
return;
}
try {
writeOutput(format(input, options), options);
} catch (e) {
handleError("stdin", e);
}
});
} else {
eachFilename(filepatterns, (filename, options) => {
@ -433,13 +428,12 @@ function listDifferent(input, options, filename) {
}
function resolveConfig(filePath) {
resolver.resolveConfigFile(filePath).then(configFile => {
if (configFile) {
console.log(path.relative(process.cwd(), configFile));
} else {
process.exitCode = 1;
}
});
const configFile = resolver.resolveConfigFile.sync(filePath);
if (configFile) {
console.log(path.relative(process.cwd(), configFile));
} else {
process.exitCode = 1;
}
}
function writeOutput(result, options) {
@ -481,12 +475,9 @@ function eachFilename(patterns, callback) {
process.exitCode = 2;
return;
}
// Use map series to ensure idempotency
mapSeries(ignorer.filter(filePaths), filePath => {
return getOptionsForFile(filePath).then(options =>
callback(filePath, options)
);
});
ignorer
.filter(filePaths)
.forEach(filePath => callback(filePath, getOptionsForFile(filePath)));
})
.catch(err => {
console.error(
@ -496,16 +487,3 @@ function eachFilename(patterns, callback) {
process.exitCode = 2;
});
}
function mapSeries(array, iteratee) {
let current = Promise.resolve();
const promises = array.map((item, i) => {
current = current.then(() => {
return iteratee(item, i, array);
});
return current;
});
return Promise.all(promises);
}