-
Notifications
You must be signed in to change notification settings - Fork 149
Propose using --replace
for multiple input files
#257
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
Conversation
So in that sense, why not just use
Or better yet, we could just allow files,globs,dir as an array or a string then loop each file, instead of having users do multiple transforms. This should be easy enough using glob modules such as const path = require('path');
const globby = require('globby');
const basedir = process.cwd();
function normalizePath(globs) {
if (!globs) {
throw 'Files path is required';
}
if (typeof globs === 'string') {
globs = [globs];
}
if (!Array.isArray(globs)) {
throw `glob should be String or Array, not ${typeof globs}`;
}
return globs;
}
function resolveFilepath(filepath) {
if (path.isAbsolute(filepath)) {
return path.normalize(filepath);
}
return path.resolve(basedir, filepath);
}
function resolvePath(paths) {
let mod = '';
if (paths[0] === '!') {
mod = paths[0];
paths = paths.slice(1);
}
return mod + resolveFilepath(paths);
} A simple test /**
* Quick test
* Test environment:
* Root folder /home/project/globtest/
* Root files app.js, test1.js, test2.js
*
* @files {string|array} {a string or an array of files/globs}
* @return {array} { returns a list of files with their absolute paths }
*/
let files = '';
let globs;
// test1
// returns ['/home/project/globtest/app.js']
files = 'app.js';
globs = normalizePath(files);
globs = globs.map(resolvePath);
globby(globs).then(paths => {
console.log('test1', paths);
});
// test2
// returns ['/home/project/globtest/app.js','/home/project/globtest/test1.js','/home/project/globtest/test2.js']
files = '../globtest/*.js';
globs = normalizePath(files);
globs = globs.map(resolvePath);
globby(globs).then(paths => {
console.log('test2', paths);
});
// test3
// returns ['/home/project/globtest/app.js','/home/project/globtest/test1.js','/home/project/globtest/test2.js']
files = '*.js';
globs = normalizePath(files);
globs = globs.map(resolvePath);
globby(globs).then(paths => {
console.log('test3', paths);
});
// test4
// returns ['/home/project/globtest/test1.js','/home/project/globtest/test2.js']
files = ['test1.js', 'test2.js'];
globs = normalizePath(files);
globs = globs.map(resolvePath);
globby(globs).then(paths => {
console.log('test4', paths);
});
// test5
// returns []
files = ['file*.js', 'js/*.js'];
globs = normalizePath(files);
globs = globs.map(resolvePath);
globby(globs).then(paths => {
console.log('test5', paths);
});
// test6
// returns ['/home/project/globtest/test1.js','/home/project/globtest/test2.js']
files = ['*.js', '!app.js'];
globs = normalizePath(files);
globs = globs.map(resolvePath);
globby(globs).then(paths => {
console.log('test6', paths);
}); |
# reads file, writes to STDOUT
lebab -t let input.js
# reads STDIN, writes to STDOUT
cat input.js | lebab -t let
# reads STDIN, writes to file
cat input.js | lebab -t let -o output.js The One problem with lebab -t let --replace file1.js --replace file2.js That will actually lead to I think the lebab -t let --replace file1.js file2.js 'some*pattern.js` some-dir/ And then the error message of more than one input file in non-replace-mode can also be improved. |
Since Lebab is already using /**
* Runs the app
*/
run() {
if (this.options.replace) {
// Transform all files in a directory
glob.sync(this.normalizeGlob(this.options.replace)).forEach((file) => {
this.transformFile(file, file);
});
} else {
// Transform just a single file
this.transformFile(this.options.inFile, this.options.outFile);
}
}
normalizeGlob(globs) {
if (!globs || (typeof globs !== 'string' && !Array.isArray(globs))) {
throw `File/Patterns are required and should be a String or an Array`;
}
if (Array.isArray(globs)) {
globs = `{${globs.join(',')}}`;
}
return globs;
} |
Closing this old PR. Created an issue #277 to discuss (and possibly solve) this later. |
People are used to pass multiple input files. It was not obvious to me at first that you need to use a different option for that.
Hopefully an improved error message will help the next guy stumbling upon that 😉