Skip to content
Permalink
Browse files

fix(configuration): filter ignored files before reading

-   rework file loading mechanisms:
-   get full deglobbed list of files
-   load configuration relative to files
-   filter all files matching their applicable ignore config
-   load remaining files
-   begin using util.debuglog

closes #2
  • Loading branch information
marionebl committed Mar 17, 2016
1 parent 742df22 commit 17c32f4ba8dcd67651b7230c830d84829c83ebd3
Showing with 86 additions and 38 deletions.
  1. +7 −6 index.js
  2. +32 −8 library/filter.js
  3. +19 −24 library/get-input.js
  4. +28 −0 library/list-files.js
@@ -2,21 +2,26 @@
const merge = require('lodash').merge;

const cli = require('./library/cli');
const listFiles = require('./library/list-files');
const fetchSchema = require('./library/fetch-schema');
const getConfiguration = require('./library/get-configuration');
const getInput = require('./library/get-input');
const resolveKeys = require('./library/resolve-keys');
const filter = require('./library/filter');
const lint = require('./library/lint');
const format = require('./library/format');
const print = require('./library/print');
const filter = require('./library/filter');
const pkg = require('./package');

// Main program
function main(options) {
return getInput(options.input)
return listFiles(options.input)
// Load file configurations
.then(getConfiguration)
// Filter files according to ignore config
.then(filter)
// Load file contents
.then(getInput)
// Fetch json schemas
.then(fetchSchema)
// Wait for resolution of async tasks
@@ -28,10 +33,6 @@ function main(options) {
return input;
});
})
.then(inputs => {
// Filter ignored files
return inputs.filter(filter);
})
.then(inputs => {
// Lint and validate files
return inputs.map(input => {
@@ -1,12 +1,36 @@
const debuglog = require('util').debuglog;
const relative = require('path').relative;
const basename = require('path').basename;
const minimatch = require('minimatch');
const merge = require('lodash').merge;

module.exports = input => {
const ignore = input.configuration.ignore || [];
const config = Array.isArray(ignore) ?
ignore : ignore.split(',');
return !config.some(pattern => {
return minimatch(input.path, pattern) ||
basename(input.path) === pattern;
});
const log = debuglog('jsonlint-cli');

module.exports = files => {
const configuringFiles = Promise.all(
files.map(file => {
return file.configuration
.then(configuration => {
return merge({}, file, {
configuration: configuration
});
});
})
);

return configuringFiles
.then(configuredFiles => {
log('found files:', configuredFiles.length);
return configuredFiles
.filter(configuredFile => {
const ignore = configuredFile.configuration.ignore || [];
const config = Array.isArray(ignore) ?
ignore : ignore.split(',');
return !config.some(pattern => {
return minimatch(configuredFile.path, pattern) ||
minimatch(relative(process.cwd(), configuredFile.path), pattern) ||
basename(configuredFile.path) === pattern;
});
});
});
};
@@ -1,30 +1,25 @@
const debuglog = require('util').debuglog;
const readFileNodeback = require('fs').readFile;
const dirname = require('path').dirname;
const resolve = require('path').resolve;
const getStdin = require('get-stdin');
const globby = require('globby');
const denodeify = require('denodeify');

const readFile = denodeify(readFileNodeback);
const log = debuglog('jsonlint-cli');

module.exports = input => {
if (input.length === 0) {
return Promise.resolve(
[{
content: getStdin.buffer(),
directory: process.cwd(),
path: process.cwd()
}]
);
}
return globby(input)
.then(paths => {
return paths.map(path => {
return {
content: readFile(path),
directory: dirname(resolve(process.cwd(), path)),
path: resolve(process.cwd(), path)
};
});
});
module.exports = files => {
log('reading files:', files.length);
return Promise.all(
files.map(file => {
return file.configuration
.then(configuration => {
if (file.content && file.piped) {
return file;
}
return readFile(file.path)
.then(content => {
file.content = content;
return file;
})
});
})
);
};
@@ -0,0 +1,28 @@
const dirname = require('path').dirname;
const resolve = require('path').resolve;
const globby = require('globby');
const getStdin = require('get-stdin');

module.exports = input => {
if (input.length === 0) {
return Promise.resolve(
[{
content: getStdin.buffer(),
directory: process.cwd(),
path: process.cwd(),
piped: true
}]
);
}
return globby(input)
.then(paths => {
return paths.map(path => {
return {
content: null,
directory: dirname(resolve(process.cwd(), path)),
path: resolve(process.cwd(), path),
piped: false
};
});
});
};

0 comments on commit 17c32f4

Please sign in to comment.
You can’t perform that action at this time.