Skip to content
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

Add CLI defaults for input and output directory #7966

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

iansu
Copy link

@iansu iansu commented May 17, 2018

Q                       A
Fixed Issues? Fixes #7237
Patch: Bug Fix? No
Major: Breaking Change? No
Minor: New Feature? Yes
Tests Added + Pass? No
Documentation PR No
Any Dependency Changes? No
License MIT

When running babel with no arguments input directory will default to src and output directory will default to:

  1. Directory specified in main field in package.json
  2. lib if it exists
  3. dist if it exists
  4. lib

Work in progress. To do:

  • Add and fix tests
  • Add documentation
  • Update CLI help

@hzoo hzoo added the PR: Polish 💅 A type of pull request used for our changelog categories label May 17, 2018
Copy link
Member

@hzoo hzoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! looks good to me so far 👍

@xtuc
Copy link
Member

xtuc commented May 19, 2018

I saw on some projects that lib is used for vendored libraries and that would be not good to add our js files in it.

I think the rules should be: if directory x exists pick another one. Note that the build destination directories are usually not committed and thus not exisinting.

@iansu
Copy link
Author

iansu commented May 19, 2018

That's a good point about lib existing but being used for another purpose.

I don't think the rule you're proposing will work either though. The first time I build lib won't exist so it will be used. If I build again lib will exist so dist will be used. Then if I build again both will exist and the build will fail?

Maybe we should just use the path from the main field in package.json and if that field doesn't exist show an error and exit?

Copy link
Member

@loganfsmyth loganfsmyth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments. I think this is a great start, but there are definitely concerns I have.


outDirOptions.some(function(outDir) {
if (fs.existsSync(outDir)) {
return outDir;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return isn't going to do what you want because it is inside of the .some callback, not inside getOutDir.

const packageJson = JSON.parse(fs.readFileSync("package.json"));

if (packageJson && packageJson.main) {
return path.dirname(packageJson.main);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's valid for a package.json#main to actually be a folder, so this will have issues, I'd suggest that we do

const basedir = process.cwd();
const filepath = require.resolve(path.resolve(basedir, packageJson.main));
return path.dirname(filepath);

so that we resolve the specific file first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we may want to skip here if the dirname that is returned is the root of the package and not a subfolder.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this second comment are you saying if dirname is the same as the basepath then we should just default to something like lib or dist (instead of writing the output to the root of the package)?

@@ -124,6 +125,7 @@ commander.option(
commander.option(
"-d, --out-dir [out]",
"Compile an input directory of modules into an output directory",
getOutDir(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to break the check at https://github.com/iansu/babel/blob/3ec44323aa3477de09dc5b836a0f7233fc6b2967/packages/babel-cli/src/babel/index.js#L9 because there will always be a dir. We should invert that so that if the user specifies --out-file, it uses the file approach.

This is going to break Babel's ability to write to stdout:

process.stdout.write(result.code + "\n");
because there will always be a file. We'll want to allow --out-file - to say "output to stdout".

@@ -152,6 +154,10 @@ export default function parseArgv(args: Array<string>) {

const errors = [];

if (commander.args.length === 0) {
commander.args.push("src");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to break

because there will always be a filename. We'll want to allow

babel - ...

where the - in place of a filename means it will read from stdin.

}
}

outDirOptions.some(function(outDir) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think skipping the lib and dist and only using package.json might be reasonable, given the concern about lib, but it definitely has risks either way.

@iansu
Copy link
Author

iansu commented May 23, 2018

@loganfsmyth Thanks for the review. I will try to address those comments as well as some of the previous discussion later today. The stdin/stdout thing is definitely an issue. That's why a bunch of tests are currently failing. Using - to indicate stdin/stdout seems like a good solution to me.

@billyjanitsch
Copy link

Using main from package.json wouldn't work well for packages using esm, wherein main points to a single file which activates the loader and points to the ESM entry point. Something like:

// index.js
module.exports = require('esm')(module)('./lib')

+1 for @loganfsmyth's suggestion of skipping main if it resolves to the root package directory, which would alleviate this problem.

@dhruvdutt
Copy link

@iansu Any update on this?

@iansu
Copy link
Author

iansu commented Aug 22, 2018

Yes! I made the suggested changes but haven't had a chance to test them yet. It kind of fell off my radar. Thanks for the reminder. I will try to push an update soon.

@dhruvdutt
Copy link

@iansu Let me know if I can lend a hand. Still looking for my first contribution to babel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: cli PR: Polish 💅 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@babel/cli: default the src/out directory
7 participants