Repository: ember-template-lint/ember-template-lint
Pull request #862 (2019-10-18 01:42:12)

✨Add option --print-pending from ember-cli-template-lint
This task was previously mentioned when the docs were updated earlier this year to port over the ability to print a pending list by @rwjblue https://github.com/ember-template-lint/ember-template-lint/pull/671#pullrequestreview-213931116

> Seems good to me, but I'd really like to migrate that to our built-in bin script (perhaps with a flag like `--print-pending`). Any chance you'd want to work on it?

This adds the option to easily populate your `.template-lintrc.js` file without needing [ember-cli-template-lint](https://github.com/ember-template-lint/ember-cli-template-lint). Should make adopting and getting started a lot easier.

To easily run against your whole project you can now do: 
```
./node_modules/.bin/ember-template-lint app/templates/** --print-pending
```

This is mostly copied over from the existing logic here https://github.com/ember-template-lint/ember-cli-template-lint/blob/master/lib/commands/print-failing.js


Patch:
diff --git a/README.md b/README.md
index 939f263b..7463b62e 100644
--- a/README.md
+++ b/README.md
@@ -76,6 +76,9 @@ Example usage:
 
 # read from stdin
 ./node_modules/.bin/ember-template-lint --filename app/templates/application.hbs < app/templates/application.hbs
+
+# print list of formated rules for use with `pending` in config file
+./node_modules/.bin/ember-template-lint app/templates/application.hbs --print-pending
 ```
 
 ### ESLint
@@ -130,7 +133,7 @@ The following properties are allowed in the root of the `.template-lintrc.js` co
   An array of module id's that are still pending. The goal of this array is to allow incorporating template linting
   into an existing project, without changing every single template file. You can add all existing templates to this `pending` listing
   and slowly work through them, while at the same time ensuring that new templates added to the project pass all defined rules.
-  * If you are using `ember-cli-template-lint` you can generate this list with: `ember template-lint:print-failing`
+  * You can generate this list with the: `./node_modules/.bin/ember-template-lint * --print-pending`
 * `ignore` -- `string[]|glob[]`
   An array of module id's that are to be completely ignored. See [ignore documentation](docs/ignore.md) for more details.
 * `plugins` -- `(string|Object)[]`
diff --git a/bin/ember-template-lint.js b/bin/ember-template-lint.js
index 1af8db47..72a9aa27 100755
--- a/bin/ember-template-lint.js
+++ b/bin/ember-template-lint.js
@@ -112,6 +112,9 @@ function parseArgv(_argv) {
         case '--verbose':
           options.named.verbose = true;
           break;
+        case '--print-pending':
+          options.named.printPending = true;
+          break;
         case '--':
           shouldHandleNamed = false;
           break;
@@ -132,7 +135,7 @@ function run() {
   let options = parseArgv(process.argv.slice(2));
 
   let {
-    named: { configPath, filename: filePathFromArgs = '' },
+    named: { configPath, filename: filePathFromArgs = '', printPending, json },
     positional,
   } = options;
 
@@ -147,6 +150,7 @@ function run() {
 
   let errors = {};
   let filesToLint;
+  let filesWithErrors = [];
 
   if (positional.length === 0 || positional.includes('-') || positional.includes(STDIN)) {
     filesToLint = new Set([STDIN]);
@@ -157,7 +161,18 @@ function run() {
   for (let relativeFilePath of filesToLint) {
     let filePath = path.resolve(relativeFilePath);
     let fileName = relativeFilePath === STDIN ? filePathFromArgs : relativeFilePath;
-    let fileErrors = lintFile(linter, filePath, fileName.slice(0, -4));
+    let moduleId = fileName.slice(0, -4);
+    let fileErrors = lintFile(linter, filePath, moduleId);
+
+    if (printPending) {
+      let failingRules = Array.from(
+        fileErrors.reduce((memo, error) => memo.add(error.rule), new Set())
+      );
+
+      if (failingRules.length > 0) {
+        filesWithErrors.push({ moduleId, only: failingRules });
+      }
+    }
 
     if (
       fileErrors.some(function(err) {
@@ -172,6 +187,22 @@ function run() {
     }
   }
 
+  if (printPending) {
+    let pendingList = JSON.stringify(filesWithErrors, null, 2);
+
+    if (json) {
+      console.log(pendingList);
+    } else {
+      console.log(
+        'Add the following to your `.template-lintrc.js` file to mark these files as pending.\n\n'
+      );
+
+      console.log(`pending: ${pendingList}`);
+    }
+
+    return;
+  }
+
   if (Object.keys(errors).length) {
     printErrors(errors, options);
   }


Test patch:
diff --git a/test/unit/bin/ember-template-lint-test.js b/test/unit/bin/ember-template-lint-test.js
index 18b10a92..8f086387 100644
--- a/test/unit/bin/ember-template-lint-test.js
+++ b/test/unit/bin/ember-template-lint-test.js
@@ -446,6 +446,52 @@ describe('ember-template-lint executable', function() {
         });
       });
     });
+
+    describe('with --print-pending param', function() {
+      it('should print a list of pending modules', function(done) {
+        execFile(
+          'node',
+          ['../../../bin/ember-template-lint.js', '.', '--print-pending'],
+          {
+            cwd: './test/fixtures/with-errors-and-warnings',
+          },
+          function(err, stdout, stderr) {
+            let expectedOutputData =
+              'Add the following to your `.template-lintrc.js` file to mark these files as pending.\n\n\npending: [\n  {\n    "moduleId": "app/templates/application",\n    "only": [\n      "no-bare-strings",\n      "no-html-comments"\n    ]\n  }\n]\n';
+
+            expect(err).to.be.ok;
+            expect(stdout).to.equal(expectedOutputData);
+            expect(stderr).to.be.empty;
+            done();
+          }
+        );
+      });
+    });
+
+    describe('with --print-pending and --json params', function() {
+      it('should print json of pending modules', function(done) {
+        execFile(
+          'node',
+          ['../../../bin/ember-template-lint.js', '.', '--print-pending', '--json'],
+          {
+            cwd: './test/fixtures/with-errors-and-warnings',
+          },
+          function(err, stdout, stderr) {
+            let expectedOutputData = [
+              {
+                moduleId: 'app/templates/application',
+                only: ['no-bare-strings', 'no-html-comments'],
+              },
+            ];
+
+            expect(err).to.be.ok;
+            expect(JSON.parse(stdout)).to.deep.equal(expectedOutputData);
+            expect(stderr).to.be.empty;
+            done();
+          }
+        );
+      });
+    });
   });
 
   describe('parseArgv', function() {
@@ -494,6 +540,15 @@ describe('ember-template-lint executable', function() {
       expect(actual).to.deep.equal(expected);
     });
 
+    it('handles --print-pending', function() {
+      let argv = ['--print-pending'];
+
+      let actual = BinScript._parseArgv(argv);
+      let expected = { named: { printPending: true }, positional: [] };
+
+      expect(actual).to.deep.equal(expected);
+    });
+
     it('processes a single "--flag value" properly', function() {
       let argv = ['--config-path', 'foo.js'];