Skip to content

Commit

Permalink
Merge pull request #135 from xtyrrell/feature/include-hidden
Browse files Browse the repository at this point in the history
feat: add --include-hidden flag
  • Loading branch information
jonycheung authored Jun 28, 2021
2 parents 497f26c + 8488a7d commit 97e2028
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ node_modules
.vscode/
./less-watch-compiler.config.json
build/
dist/
dist/
tests/examples/with-hidden-variables-file/css/main.css
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ less-watch-compiler
"watchFolder": "<input_folder>",
"outputFolder": "<output_folder>",
"mainFile": "<main-file>",
"includeHidden": false,
"sourceMap": false,
"plugins": "plugin1,plugin2",
"lessArgs": "option1=1,option2=2",
Expand All @@ -110,6 +111,7 @@ less-watch-compiler
--main-file <file> Specify <file> as the file to always re-compile e.g. '--main-file style.less'.
--config <file> Custom configuration file path. (default: "less-watch-compiler.config.json")
--run-once Run the compiler once without waiting for additional changes.
--include-hidden Don't ignore files beginning with a '.' or a '_'
--enable-js Less.js Option: To enable inline JavaScript in less files.
--source-map Less.js Option: To generate source map for css files.
--plugins <plugin-a>,<plugin-b> Less.js Option: To specify plugins separated by commas.
Expand All @@ -119,7 +121,7 @@ less-watch-compiler
* By default, "minified" is turned on to always compress/minify output. You can set the minification to false by adding `"minified":false` in the config file.
* By default, "sourceMap" is turned off. You can generating sourcemap to true by adding `"sourceMap":true` in the config file.
* By default, this script only compiles files with `.less` extension. More file extensions can be added by modifying the `allowedExtensions` array in `config.json`.
* Files that start with underscores `_style.css` or period `.style.css` are ignored. This behavior can be changed in the `filterFiles()` function.
* Files that start with underscores `_style.css` or period `.style.css` are ignored. This behavior can be changed by adding `"includeHidden:true` in the config file.
* When `--run-once` used, compilation will fail on first error

### Using the source files
Expand Down
1 change: 1 addition & 0 deletions less-watch-compiler.sample.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"allowedExtensions":[".less"],
"enableJs": true,
"minified": true,
"includeHidden": true,
"sourceMap": true,
"plugins": "plugin1,plugin2",
"watchFolder": "tests/less",
Expand Down
11 changes: 10 additions & 1 deletion src/less-watch-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cmdr
.option('--main-file <file>', "Specify <file> as the file to always re-compile e.g. '--main-file style.less'.")
.option('--config <file>', 'Custom configuration file path.', 'less-watch-compiler.config.json')
.option('--run-once', 'Run the compiler once without waiting for additional changes.')
.option('--include-hidden', "Don't ignore files beginning with a '.' or a '_'")
//Less Options
.option('--enable-js', 'Less.js Option: To enable inline JavaScript in less files.')
.option('--source-map', "Less.js Option: To generate source map for css files.")
Expand Down Expand Up @@ -65,9 +66,12 @@ function init(){
if (program.sourceMap) lessWatchCompilerUtils.config.sourceMap = program.sourceMap;
if (program.plugins) lessWatchCompilerUtils.config.plugins = program.plugins;
if (program.runOnce) lessWatchCompilerUtils.config.runOnce = program.runOnce;
if (program.inludeHidden) lessWatchCompilerUtils.config.includeHidden = program.includeHidden;
if (program.enableJs) lessWatchCompilerUtils.config.enableJs = program.enableJs;
if (program.lessArgs) lessWatchCompilerUtils.config.lessArgs = program.lessArgs;

lessWatchCompilerUtils.config = Object.assign({}, lessWatchCompilerUtils.config, program.opts())

/*
3rd parameter is optional, but once you define it, then we will just compile
the main and generate as "{main_file_name}.css". All the files that has been
Expand Down Expand Up @@ -120,7 +124,12 @@ function init(){
console.log('Watching directory for file changes.');
lessWatchCompilerUtils.watchTree(
lessWatchCompilerUtils.config.watchFolder,
{interval: 200, ignoreDotFiles: true, filter:lessWatchCompilerUtils.filterFiles},
{
interval: 200,
// If we've set --include-hidden, don't ignore dotfiles
ignoreDotFiles: !lessWatchCompilerUtils.config.includeHidden,
filter: lessWatchCompilerUtils.filterFiles
},
function (f, curr, prev, fileimportlist) {
if (typeof f == 'object' && prev === null && curr === null) {
// Finished walking the tree
Expand Down
6 changes: 6 additions & 0 deletions src/lib/filesearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ if (typeof define !== 'function') {

define(function (require){
const fs = require('fs');
const path = require('path')

const filesearch = {
findLessImportsInFile: function(f){
if (fs.statSync(f) && fs.statSync(f).isFile() === false)
Expand All @@ -20,6 +22,10 @@ define(function (require){
}
return files;
}
},
isHiddenFile: function (filename) {
filename = path.basename(filename)
return filename.substr(0, 1) === '_' || filename.substr(0, 1) === '.';
}
}
return filesearch;
Expand Down
24 changes: 14 additions & 10 deletions src/lib/lessWatchCompilerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ define(function (require) {

var outputFilePath = this.resolveOutputPath(file);

// As a rule, we don't compile hidden files for now. If we encounter one,
// just return.
if (fileSearch.isHiddenFile(outputFilePath)) return

var enableJsFlag = lessWatchCompilerUtilsModule.config.enableJs ? ' --js' : '';
var minifiedFlag = lessWatchCompilerUtilsModule.config.minified ? ' -x' : '';
var sourceMap = (lessWatchCompilerUtilsModule.config.sourceMap) ? ' --source-map' : '';
Expand Down Expand Up @@ -149,19 +153,19 @@ define(function (require) {

return JSON.stringify(shortPath);
},
// This is the function we use to filter the files to watch.
// We build the function to filter the files to watch.
// Returning true marks a file to be ignored.
filterFiles: function (f) {
var filename = path.basename(f);
var extension = path.extname(f),
allowedExtensions = lessWatchCompilerUtilsModule.config.allowedExtensions || defaultAllowedExtensions;
if (filename.substr(0, 1) == '_' ||
filename.substr(0, 1) == '.' ||
filename == '' ||
allowedExtensions.indexOf(extension) == -1
)
return true;
else {
return false;
allowedExtensions = lessWatchCompilerUtilsModule.config.allowedExtensions || defaultAllowedExtensions;
if (filename == '' || allowedExtensions.indexOf(extension) == -1) {
return true;
} else {
// If we're including hidden files then don't ignore this file
if (lessWatchCompilerUtilsModule.config.includeHidden) return false;
// Otherwise, do ignore this file if it's a hidden file
else return fileSearch.isHiddenFile(filename)
}
},
getDateTime: function () {
Expand Down
4 changes: 4 additions & 0 deletions tests/examples/with-hidden-variables-file/css/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.primary {
color: blue;
margin: 10px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@default-spacing: 10px;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@primary-colour: blue;
7 changes: 7 additions & 0 deletions tests/examples/with-hidden-variables-file/less/main.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import '_variables.less';
@import '.other-variables.less';

.primary {
color: @primary-colour;
margin: @default-spacing;
}
12 changes: 10 additions & 2 deletions tests/filesearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('filesearch Module', function () {

it('should search through a file and find LESS @import statements ', function (done) {
var file = "./tests/less/test.less";
var result = ['lvl1.less', 'lvl2/lvl2.less', 'lvl2/lvl3/lvl3.less'],
var result = ['lvl1.less', 'lvl2/lvl2.less', 'lvl2/lvl3/lvl3.less', 'hidden/_hidden.less', 'hidden/.hidden2.less'],
filesearchresult = filesearch.findLessImportsInFile(file);
assert.equal(result.toString(), filesearchresult.toString());
done();
Expand All @@ -29,6 +29,14 @@ describe('filesearch Module', function () {
done();
});
})

describe('isHiddenFile()', function () {
it('should return `true` on hidden files', function () {
assert.equal(filesearch.isHiddenFile(".hidden.less"), true);
assert.equal(filesearch.isHiddenFile("_hidden.less"), true);
})
it('should return `false` on non-hidden files', function () {
assert.equal(filesearch.isHiddenFile("non-hidden.less"), false);
})
});
})
})
58 changes: 58 additions & 0 deletions tests/less-watch-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const assert = require('assert');
const child_process = require('child_process');
const fs = require('fs');
const sh = require('shelljs');

const cwd = sh.pwd().toString();

function runLessWatchCompiler(argumentsAndOptions, callback) {
return child_process.exec(cwd + '/dist/less-watch-compiler.js ' + argumentsAndOptions, callback)
}

describe('Using the CLI in example projects', function () {
describe('with-hidden-variables-file', function () {
const lessDir = cwd + '/tests/examples/with-hidden-variables-file/less'
const cssDir = cwd + '/tests/examples/with-hidden-variables-file/css'

it('should compile main.css correctly', function (done) {
// Make sure we're testing against the main.css file compiled by this test run
fs.rmSync(cssDir + '/main.css', { force: true })

runLessWatchCompiler(lessDir + ' ' + cssDir + " --include-hidden")

// Wait for compilation to happen
setTimeout(function () {
const contents = fs.readFileSync(cssDir + '/main.css')
const contentsExpected = fs.readFileSync(cssDir + '/expected.css')

assert.ok(contents.equals(contentsExpected))

fs.rmSync(cssDir + '/main.css', { force: true })

done()
}, 500)
})

it('should not compile the hidden variables files', function (done) {
const compiledVariablesPath = cssDir + '/_variables.css'
const compiledOtherVariablesPath = cssDir + '/.other-variables.css'

// Make sure we don't detect compiled variables files left over from other runs
fs.rmSync(compiledVariablesPath, { force: true })
fs.rmSync(compiledOtherVariablesPath, { force: true })

runLessWatchCompiler(lessDir + ' ' + cssDir + " --include-hidden")

// Wait for compilation to happen
setTimeout(function () {
console.log("ls css:" + child_process.execSync("ls " + cssDir))

const variablesFilesWereNotCompiled = !fs.existsSync(compiledVariablesPath) &&
!fs.existsSync(compiledOtherVariablesPath)

assert.ok(variablesFilesWereNotCompiled)
done()
}, 500)
})
})
})
3 changes: 3 additions & 0 deletions tests/less/hidden/.hidden2.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hidden2 {
color: transparent;
}
3 changes: 3 additions & 0 deletions tests/less/hidden/_hidden.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hidden {
color: black;
}
2 changes: 2 additions & 0 deletions tests/less/lvl2/lvl3/reverse_dirtree.less
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ Test Comments
@import '../../lvl1.less';
@import "../lvl2.less";
@import "lvl3.less";
@import "../../hidden/_hidden.less";
@import "../../hidden/.hidden2.less";
2 changes: 2 additions & 0 deletions tests/less/test.less
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ Test Comments
@import 'lvl1.less';
@import "lvl2/lvl2.less";
@import "lvl2/lvl3/lvl3.less";
@import "hidden/_hidden.less";
@import "hidden/.hidden2.less";
13 changes: 13 additions & 0 deletions tests/lessWatchCompilerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,25 @@ describe('lessWatchCompilerUtils Module API', function () {
assert.equal("function", typeof (lessWatchCompilerUtils.filterFiles));
});
it('filterFiles() function should return "false" for allowed files:' + JSON.stringify(lessWatchCompilerUtils.config), function () {
assert.equal(false, lessWatchCompilerUtils.filterFiles("file.less"));

lessWatchCompilerUtils.config.allowedExtensions = [".css"]
assert.equal(false, lessWatchCompilerUtils.filterFiles("file.css"));
lessWatchCompilerUtils.config = {}
});
it('filterFiles() function should return "true" for non-allowed files' + JSON.stringify(lessWatchCompilerUtils.config), function () {
assert.equal(true, lessWatchCompilerUtils.filterFiles("file.js"));
});
it('filterFiles() function should return "true" for hidden files' + JSON.stringify(lessWatchCompilerUtils.config), function () {
assert.equal(true, lessWatchCompilerUtils.filterFiles("_file.less"));
assert.equal(true, lessWatchCompilerUtils.filterFiles(".file.less"));
});
it('filterFiles() function should return "false" for hidden files with includeHidden flag' + JSON.stringify(lessWatchCompilerUtils.config), function () {
lessWatchCompilerUtils.config.includeHidden = true
assert.equal(false, lessWatchCompilerUtils.filterFiles("_file.less"));
assert.equal(false, lessWatchCompilerUtils.filterFiles(".file.less"));
lessWatchCompilerUtils.config = {}
});

})
describe('getDateTime()', function () {
Expand Down

0 comments on commit 97e2028

Please # to comment.