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

add --ignore, --ignorefile options to CLI #124

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .ejslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/fixtures/ignored_by_ejslintignore.ejs
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Options:
-d, --delimiter Specify a custom delimiter ( i.e. <? instead of <% ) [string]
--preprocessor-include Allow old (pre-EJS v3) preprocessor-style includes [boolean]
--await Allow usage of await inside template [boolean]
--ignore Glob pattern(s) to ignore [array]
--ignore-file File(s) containing glob patterns to ignore [array]
```

## API
Expand Down
37 changes: 36 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
/* eslint-disable no-console */
import fs from 'fs';
import yargs from 'yargs';
import { globbySync } from 'globby';
import slash from 'slash';
Expand All @@ -25,14 +26,48 @@ const { argv } = yargs(process.argv.slice(2))
.option('await', {
describe: 'Allow usage of await in template',
type: 'boolean',
})
.option('ignore-file', {
describe:
'Optionally specify a file or files containing a list of glob expressions to ignore. Terminate the list of files by adding --, or use this as the last argument',
type: 'array',
})
.option('ignore', {
describe:
'Optionally specify a glob pattern or patterns to ignore. Terminate the list of patterns by adding --, or use this as the last argument',
type: 'array',
});

const ejsLintIgnoreExists = fs.existsSync('.ejslintignore');
const ignoreFilePaths = [];
if (ejsLintIgnoreExists) {
ignoreFilePaths.push('.ejslintignore');
}
if (Array.isArray(argv['ignore-file'])) {
ignoreFilePaths.push(...argv['ignore-file']);
}
const ignorePatterns = [...(Array.isArray(argv.ignore) ? argv.ignore : [])];
ignoreFilePaths.forEach((filepath) => {
const lines = fs
.readFileSync(filepath, 'utf-8')
.split('\n')
.filter((line) => !!line);
ignorePatterns.push(...lines);
});
const opts = {
delimiter: argv.delimiter,
preprocessorInclude: argv['preprocessor-include'],
await: argv.await,
ignore: ignorePatterns,
};
read(globbySync(argv._.map((s) => slash(s))))
read(
globbySync(
argv._.map((s) => slash(s)),
{
ignore: opts.ignore,
},
),
)
.then((res) => {
let errored = false;
res.files.forEach((file) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ejs-lint",
"version": "2.0.0",
"version": "2.1.0",
"description": "Linter/Syntax Checker for EJS Templates",
"author": "Ryan Zimmerman <opensrc@ryanzim.com> (http://ryanzim.com/)",
"license": "MIT",
Expand Down
3 changes: 3 additions & 0 deletions test/.custom_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test/fixtures/invalid.ejs
test/fixtures/preprocessor.ejs
test/fixtures/await.ejs
55 changes: 55 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,61 @@ import path from 'path';
const ejslint = path.resolve('cli.js');

suite('cli', () => {
test('.ejslintignore + custom_ignore', (done) => {
execFile(
ejslint,
['test/fixtures/*', '--ignore-file', 'test/.custom_ignore'],
(err, stdout, stderr) => {
assert.ifError(err);
assert(!stderr);
done();
},
);
});
test('.ejslintignore + custom_ignore via CLI (multiple args)', (done) => {
execFile(
ejslint,
[
'test/fixtures/*',
'--ignore',
'test/fixtures/await.ejs',
'test/fixtures/invalid.ejs',
'test/fixtures/preprocessor.ejs',
],
(err, stdout, stderr) => {
assert.ifError(err);
assert(!stderr);
done();
},
);
});
test('.ejslintignore + custom_ignore via CLI (1 arg)', (done) => {
execFile(
ejslint,
[
'test/fixtures/*',
'--preprocessor-include',
'--await',
'--ignore',
'test/fixtures/invalid.ejs',
],
(err, stdout, stderr) => {
assert.ifError(err);
assert(!stderr);
done();
},
);
});
test('.ejslintignore', (done) => {
execFile(ejslint, ['test/fixtures/*'], (err) => {
assert.equal(err.code, 1, 'expected exit code of 1');
assert.doesNotMatch(
err.message,
/test\/fixtures\/ignored_by_ejslintignore\.ejs/,
);
done();
});
});
test('valid input', (done) => {
execFile(ejslint, ['test/fixtures/valid.ejs'], (err, stdout, stderr) => {
assert.ifError(err);
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/ignored_by_ejslintignore.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% for(i=0;i<5;i++){ %>
Hello!
<% ] %>