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

feat: pass argument to main function #5

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion bin/create-webextension
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ if (!process.argv[2]) {
process.exit(1);
}

require("..").main(process.argv[2]);
require("..").main(process.argv[2])
.then(console.log)
.catch(err => console.log(err.message));
Copy link
Owner

Choose a reason for hiding this comment

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

@saintsebastian This is definitely better for being able to use the module as a library but we still need to make the following tweaks:

  • we want to be sure that the CLI command exits with an "error exit code" (e.g. using process.exit explicitly) when there we catch an error here
  • we want to be sure that we also produce a stack trace when the caught error is not expected (which currently
    means "anything but an 'EEXIST' error"), and we can do it by just re-throwing the caught error
  • The expected errors (currently only the one above) should not print a stack trace and they should be possibly be
    printed using the color red

To achieve these behaviors we can use the same strategy that we are using in web-ext:

  • define a custom UsageError class, and throw an instance of this class for the "expected" errors
  • then, in the bin wrapper, check for the errors that are instanceof the UsageError and change the behavior
    based on it
  • just re-throw the "unexpected" errors, so that they will be logged with the stack trace in the console

11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function getProjectManifest(projectDirName) {

exports.main = function main(dirPath) {
if (!dirPath) {
throw new Error("Project directory name is a compulsory argument");
throw new Error("Project directory name is a mandatory argument");
}

const projectPath = path.resolve(dirPath);
Expand All @@ -106,16 +106,13 @@ exports.main = function main(dirPath) {
.then(() => getProjectReadme(projectDirName))
.then(projectReadme => fs.writeFile(path.join(projectPath, "README.md"),
stripAnsi(projectReadme)))
.then(() => getProjectCreatedMessage(projectPath))
.then(console.log);
.then(() => getProjectCreatedMessage(projectPath));
Copy link
Owner

Choose a reason for hiding this comment

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

@saintsebastian we should probably return a more useful object to the caller, e.g. something like:

.then(() => {
  const projectCreatedMessage = getProjectCreatedMessage(projectPath);

  return {projectPath, projectCreatedMessage};
});

This should also make much more clear what line 16 of the bin/create-webextension is actually doing.

}, error => {
if (error.code === "EEXIST") {
const msg = `Unable to create a new WebExtension: ${chalk.bold.underline(projectPath)} dir already exist.`;
console.error(`${chalk.red(msg)}\n`);
process.exit(1);
throw new Error(msg);
}
}).catch((error) => {
console.error(error);
process.exit(1);
throw error;
Copy link
Owner

Choose a reason for hiding this comment

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

@saintsebastian we can omit this catch, it is basically doing what it is going to happen if we omit it.

});
};