Skip to content

Commit

Permalink
feat: 🎸 ignore files specified in .bladeignore
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Jan 26, 2021
1 parent 47145ed commit 57e76b7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,19 @@ describe('The blade formatter CLI', () => {

expect(originalContent).toEqual(result);
});

test('Do nothing if path is included in ignore file', async () => {
expect(fs.existsSync('.bladeignore')).toBe(true);
expect(fs.readFileSync('.bladeignore').toString()).toContain(
'__tests__/**/ignore_target_file.blade.php',
);
const response = cmd.executeSync(
path.resolve(__basedir, 'bin', 'blade-formatter'),
[`${__dirname}/fixtures/ignore_target_file.blade.php`, '-c'],
);
const output = response.output.join('\n');

expect(output).not.toContain('ignore_target_file.blade.php');
expect(output).toContain('All matched files are formatted');
});
});
3 changes: 3 additions & 0 deletions __tests__/fixtures/ignore_target_file.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
this file should be ignored
</div>
37 changes: 37 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ignore from 'ignore';
import * as util from './util';
import Formatter from './formatter';

const nodepath = require('path');
const fs = require('fs');
const process = require('process');
const chalk = require('chalk');
Expand All @@ -17,6 +19,7 @@ export class BladeFormatter {
this.diffs = [];
this.outputs = [];
this.formattedFiles = [];
this.ignoreFile = '';
}

format(content, opts = {}) {
Expand All @@ -27,11 +30,24 @@ export class BladeFormatter {
}

async formatFromCLI() {
await this.readIgnoreFile();
this.printPreamble();
await this.processPaths();
this.printResults();
}

async readIgnoreFile() {
const ignoreFile = '.bladeignore';

try {
if (fs.existsSync(ignoreFile)) {
this.ignoreFile = fs.readFileSync(ignoreFile).toString();
}
} catch (err) {
// do nothing
}
}

async processPaths() {
await Promise.all(
_.map(this.paths, async (path) => this.processPath(path)),
Expand All @@ -40,6 +56,8 @@ export class BladeFormatter {

async processPath(path) {
await BladeFormatter.globFiles(path)
.then((paths) => _.map(paths, (target) => nodepath.relative('.', target)))
.then((paths) => this.filterFiles(paths))
.then(this.fulFillFiles)
.then((paths) => this.formatFiles(paths));
}
Expand All @@ -52,6 +70,25 @@ export class BladeFormatter {
});
}

async filterFiles(paths) {
if (this.ignoreFile === '') {
return paths;
}

const REGEX_FILES_NOT_IN_CURRENT_DIR = new RegExp(/^\.\.*/);
const filesOutsideTargetDir = _.filter(paths, (path) =>
REGEX_FILES_NOT_IN_CURRENT_DIR.test(nodepath.relative('.', path)),
);

const filesUnderTargetDir = _.xor(paths, filesOutsideTargetDir);

const filteredFiles = ignore()
.add(this.ignoreFile)
.filter(filesUnderTargetDir);

return _.concat(filesOutsideTargetDir, filteredFiles);
}

static fulFillFiles(paths) {
this.targetFiles.push(paths);

Expand Down

0 comments on commit 57e76b7

Please sign in to comment.