8000 Propose using `--replace` for multiple input files by andywer · Pull Request #257 · lebab/lebab · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

andywer
Copy link
Contributor
@andywer andywer commented Aug 7, 2017

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 😉

@uniibu
Copy link
Contributor
uniibu commented Aug 7, 2017

--replace is used for a single glob pattern, directory, and single file. So two specific file won't work.

So in that sense, why not just use --replace as the default since according to notes,

transform all *.js files in a directory

can also be a single file or a glob pattern`

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 globby

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);
});

@nene
Copy link
Collaborator
nene commented Aug 8, 2017

--replace as default would break expectations of how command line tools usually work. That is, they read STDIN and write to STDOUT unless specified otherwi 8000 se:

# 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 --replace option is explicitly named so to make it clear that it's going to be destructive. Being destructive by default will lead to unpleasant surprises.

One problem with --replace currently is that you can't use it to transform two separate files. You can't even do:

lebab -t let --replace file1.js --replace file2.js

That will actually lead to file2.js being transformed and file1.js being ignored completely, without any warnings about that.

I think the --replace option could be improved so that one could use it with multiple files, dirs and patterns:

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.

@uniibu
Copy link
Contributor
uniibu commented Aug 8, 2017

Since Lebab is already using glob module, why not take advantage of it. You can use {pattern,pattern,pattern} for multiple globs/files. So code on lebab/src/Cli.js should be similar to:

  /**
   * 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;
  }

@nene
Copy link
Collaborator
nene commented Sep 7, 2018

Closing this old PR. Created an issue #277 to discuss (and possibly solve) this later.

< 6E90 /span>

@nene nene closed this Sep 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0