Skip to content

Commit

Permalink
Merge pull request #1 from tonkolviktor/master
Browse files Browse the repository at this point in the history
Wildcard and ignore prefix features
  • Loading branch information
DannyMoerkerke authored Sep 5, 2016
2 parents 9278032 + 1f2ea4b commit b64f022
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"commander": "^2.9.0",
"faucet": "0.0.1",
"replacestream": "^4.0.0",
"tape": "^4.5.1"
"tape": "^4.5.1",
"glob": "^7.0.0"
}
}
44 changes: 33 additions & 11 deletions postbuild
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const fs = require('fs');
const path = require('path');
const replaceStream = require('replacestream');
const program = require('commander');
const glob = require('glob');

let cssFiles;
let jsFiles;
Expand All @@ -27,13 +28,18 @@ const getFullPath = (file) => {
return file.substring(0, 1) !== '/' ? `${process.cwd()}/${file}` : `${process.cwd()}${file}`;
};

const removePrefix = (file, prefix) => {
return prefix ? file.substr(prefix.length) : file;
}

program
.version('1.0.0')
.option('-i, --input <input>', 'Input file')
.option('-o, --output <output>', 'Output file (defaults to input when omitted)')
.option('-c, --css <css>', 'css file(s) to inject (file or directory)')
.option('-j, --js <js>', 'js file(s) to inject (file or directory)')
.option('-c, --css <css>', 'css file(s) to inject (file or directory). Wildcards can be used with quotation: \'**/*.css\'')
.option('-j, --js <js>', 'js file(s) to inject (file or directory). Wildcards can be used with quotation: \'**/*.js\'')
.option('-r, --remove <remove>', 'Remove condition')
.option('-g, --ignore <path>', 'Prefix to remove from the injected filenames')
.parse(process.argv);

if(!program.input) {
Expand Down Expand Up @@ -61,14 +67,22 @@ if(program.js) {
const js = program.js;

try {
if(fs.lstatSync(js).isDirectory()) {
jsFiles = fs.readdirSync(js)
.filter(file => file.substr(-3) === '.js')
.map(file => `<script src="${js}/${file}"></script>`)
.join('\n');
if(glob.hasMagic(js)) {
jsFiles = glob.sync(js)
.map(file => removePrefix(file, program.ignore))
.map(file => `<script src="${file}"></script>`)
.join('\n');
}
else if(fs.lstatSync(js).isDirectory()) {
jsFiles = fs.readdirSync(js)
.filter(file => file.substr(-3) === '.js')
.map(file => removePrefix(`${js}/${file}`, program.ignore))
.map(file => `<script src="${file}"></script>`)
.join('\n');
}
else if(fs.lstatSync(js).isFile()) {
jsFiles = `<script src="${js}"></script>`;
let file = removePrefix(js, program.ignore);
jsFiles = `<script src="${file}"></script>`;
}
}
catch(e) {
Expand All @@ -80,14 +94,22 @@ if(program.css) {
const css = program.css;

try {
if(fs.lstatSync(css).isDirectory()) {
if(glob.hasMagic(css)) {
cssFiles = glob.sync(css)
.map(file => removePrefix(file, program.ignore))
.map(file => `<link rel="stylesheet" href="${file}">`)
.join('\n');
}
else if(fs.lstatSync(css).isDirectory()) {
cssFiles = fs.readdirSync(css)
.filter(file => file.substr(-4) === '.css')
.map(file => `<link rel="stylesheet" href="${css}/${file}">`)
.map(file => removePrefix(`${css}/${file}`, program.ignore))
.map(file => `<link rel="stylesheet" href="${file}">`)
.join('\n');
}
else if(fs.lstatSync(css).isFile()) {
cssFiles = `<link rel="stylesheet" href="${css}">`;
let file = removePrefix(css, program.ignore);
cssFiles = `<link rel="stylesheet" href="${file}">`;
}
}
catch(e) {
Expand Down
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ This way you can replace the regular files used in a development environment wit
-V, --version output the version number
-i, --input <input> Input file
-o, --output <output> Output file (defaults to input when omitted)
-c, --css <css> css file(s) to inject (file or directory)
-j, --js <js> js file(s) to inject (file or directory)
-c, --css <css> css file(s) to inject (file or directory). Wildcards can be used with quotation: '**/*.css'
-j, --js <js> js file(s) to inject (file or directory). Wildcards can be used with quotation: '**/*.js'
-r, --remove <remove> Remove condition
-g, --ignore <path> Prefix to remove from the injected filenames

### Examples

Expand Down
102 changes: 102 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const inputFile = `${tmpDir}/index.html`;
const outputFile = `${tmpDir}/output.html`;
const cssFiles = ['styles1.css', 'styles2.css', 'styles3.css'];
const jsFiles = ['script1.js', 'script2.js', 'script3.js'];
const jsFilesWildcard = '**/*.js';
const cssFilesWildcard = '**/*.css';

const setup = () => {

Expand Down Expand Up @@ -127,6 +129,106 @@ test('test injection of single javascript', (t) => {
});
});

test('test injection of stylesheets with wildcard', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -c '${cssFilesWildcard}'`, (err) => {

fs.readFile(`${outputFile}`, (err, data) => {
cssFiles.forEach((file) => {
t.equal(true, data.indexOf(`${tmpDir}/${file}`) !== -1, `expect ${file} to be injected`);
});

t.end();
});
});
});

test('test injection of javascripts with wildcard', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -j '${jsFilesWildcard}'`, (err) => {

fs.readFile(`${outputFile}`, (err, data) => {
jsFiles.forEach((file) => {
t.equal(true, data.indexOf(`${tmpDir}/${file}`) !== -1, `expect ${file} to be injected`);
});

t.end();
});
});
});

test('test injection of all stylesheets in directory with ignore', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -c ${tmpDir} -g ${tmpDir}/`, (err) => {

fs.readFile(`${outputFile}`, (err, data) => {
cssFiles.forEach((file) => {
t.equal(true, data.indexOf(`\"${file}\"`) !== -1, `expect ${file} to be injected`);
});

t.end();
});
});
});

test('test injection of all javascripts in directory with ignore', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -j ${tmpDir} -g ${tmpDir}/`, (err) => {

fs.readFile(`${outputFile}`, (err, data) => {
jsFiles.forEach((file) => {
t.equal(true, data.indexOf(`\"${file}\"`) !== -1, `expect ${file} to be injected`);
});

t.end();
});
});
});

test('test injection of single stylesheet with ignore', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -c ${tmpDir}/${cssFiles[0]} -g ${tmpDir}/`, (err) => {

fs.readFile(`${outputFile}`, (err, data) => {
t.equal(true, data.indexOf(`\"${cssFiles[0]}\"`) !== -1, `expect ${cssFiles[0]} to be injected`);

t.end();
});
});
});

test('test injection of single javascript with ignore', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -j ${tmpDir}/${jsFiles[0]} -g ${tmpDir}/`, (err) => {

fs.readFile(`${outputFile}`, (err, data) => {
t.equal(true, data.indexOf(`\"${jsFiles[0]}\"`) !== -1, `expect ${jsFiles[0]} to be injected`);

t.end();
});
});
});

test('test injection of stylesheets with wildcard with ignore', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -c '${cssFilesWildcard}' -g ${tmpDir}/`, (err) => {

fs.readFile(`${outputFile}`, (err, data) => {
cssFiles.forEach((file) => {
t.equal(true, data.indexOf(`\"${file}\"`) !== -1, `expect ${file} to be injected`);
});

t.end();
});
});
});

test('test injection of javascripts with wildcard with ignore', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -j '${jsFilesWildcard}' -g ${tmpDir}/`, (err) => {

fs.readFile(`${outputFile}`, (err, data) => {
jsFiles.forEach((file) => {
t.equal(true, data.indexOf(`\"${file}\"`) !== -1, `expect ${file} to be injected`);
});

t.end();
});
});
});

test('test removal of development code', (t) => {
exec(`./postbuild -i ${inputFile} -o ${outputFile} -r development`, (err) => {
const devRegex = new RegExp('(<!\\-\\- remove:development \\-\\->)([\\s\\S]*?)(<!\\-\\- endremove \\-\\->)');
Expand Down

0 comments on commit b64f022

Please sign in to comment.