How to use the clipanion.UsageError function in clipanion

To help you get started, we’ve selected a few clipanion examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yarnpkg / berry / packages / plugin-version / sources / commands / version / check.tsx View on Github external
// it may return long paths even when the cwd uses short paths, and we have no
  // way to detect it from Node (not even realpath).

  let match: PortablePath | null = null;

  let cwd: PortablePath;
  let nextCwd = initialCwd;
  do {
    cwd = nextCwd;
    if (await xfs.existsPromise(ppath.join(cwd, `.git` as Filename)))
      match = cwd;
    nextCwd = ppath.dirname(cwd);
  } while (match === null && nextCwd !== cwd);

  if (match === null)
    throw new UsageError(`This command can only be run from within a Git repository`);

  return match;
}
github yarnpkg / berry / packages / plugin-version / sources / commands / version / check.ts View on Github external
// it may return long paths even when the cwd uses short paths, and we have no
  // way to detect it from Node (not even realpath).

  let match: PortablePath | null = null;

  let cwd: PortablePath;
  let nextCwd = initialCwd;
  do {
    cwd = nextCwd;
    if (await xfs.existsPromise(ppath.join(cwd, `.git` as Filename)))
      match = cwd;
    nextCwd = ppath.dirname(cwd);
  } while (match === null && nextCwd !== cwd);

  if (match === null)
    throw new UsageError(`This command can only be run from within a Git repository`);

  return match;
}
github yarnpkg / berry / packages / yarnpkg-core / sources / Configuration.ts View on Github external
// The plugins have already been loaded at this point
      if (key === `plugins`)
        continue;

      // Some environment variables should be ignored when applying the configuration
      if (source === `` && IGNORED_ENV_VARIABLES.has(key))
        continue;

      // It wouldn't make much sense, would it?
      if (key === `rcFilename`)
        throw new UsageError(`The rcFilename settings can only be set via ${`${ENVIRONMENT_PREFIX}RC_FILENAME`.toUpperCase()}, not via a rc file`);

      const definition = this.settings.get(key);
      if (!definition) {
        if (strict) {
          throw new UsageError(`Unrecognized or legacy configuration settings found: ${key} - run "yarn config -v" to see the list of settings supported in Yarn`);
        } else {
          this.invalid.set(key, source);
          continue;
        }
      }

      if (this.sources.has(key) && !overwrite)
        continue;

      this.values.set(key, parseValue(this, key, data[key], definition, folder));
      this.sources.set(key, source);

      if (key === `packageExtensions`) {
        this.refreshPackageExtensions();
      }
    }
github arcanis / sherlock / sources / commands / entry.ts View on Github external
body = readFileSync(this.issue, `utf8`);
        } else {
            const packageJson = JSON.parse(readFileSync(pkgPath, `utf8`));
            if (!packageJson.repository || packageJson.repository.type !== `git` || !packageJson.repository.url)
                throw new UsageError(`This command must be run from within a package linked to a repository`);

            const {owner, name: repo} = gitUrlParse(packageJson.repository.url);
            if (!owner || !repo)
                throw new UsageError(`This command must be run from within a package linked to a GitHub repository`);

            const octokit = new Octokit({
                auth: process.env.GITHUB_TOKEN,
            });

            if (!process.env.GITHUB_TOKEN && process.env.GITHUB_ACTIONS)
                throw new UsageError(`Missing GitHub token in the environment`);

            let issue: number;

            const githubMatch = this.issue.match(GITHUB_REGEXP);
            if (githubMatch) {
                issue = parseInt(githubMatch[3], 10);
            } else {
                issue = parseInt(this.issue);
            }

            body = (await octokit.issues.get({
                owner,
                repo,
                issue_number: issue,
            })).data.body;
        }
github yarnpkg / berry / packages / plugin-pnp / sources / PnpLinker.ts View on Github external
async findPackageLocator(location: PortablePath, opts: LinkOptions) {
    const pnpPath = getPnpPath(opts.project);
    if (!xfs.existsSync(pnpPath))
      throw new UsageError(`The project in ${opts.project.cwd}/package.json doesn't seem to have been installed - running an install there might help`);

    const physicalPath = npath.fromPortablePath(pnpPath);
    const pnpFile = miscUtils.dynamicRequire(physicalPath);
    delete require.cache[physicalPath];

    const locator = pnpFile.findPackageLocator(npath.fromPortablePath(location));
    if (!locator)
      return null;

    return structUtils.makeLocator(structUtils.parseIdent(locator.name), locator.reference);
  }
github yarnpkg / berry / packages / plugin-stage / sources / commands / stage.ts View on Github external
async function findDriver(cwd: PortablePath) {
  let driver = null;
  let root: PortablePath | null = null;

  for (const candidate of ALL_DRIVERS) {
    if ((root = await candidate.findRoot(cwd)) !== null) {
      driver = candidate;
      break;
    }
  }

  if (driver === null || root === null)
    throw new UsageError(`No stage driver has been found for your current project`);

  return {driver, root};
}
github yarnpkg / berry / packages / plugin-init / sources / commands / init.ts View on Github external
async executeProxy(configuration: Configuration) {
    if (configuration.get(`yarnPath`) !== null)
      throw new UsageError(`Cannot use the --install flag when the current directory already uses yarnPath (from ${configuration.sources.get(`yarnPath`)})`);

    if (configuration.projectCwd !== null)
      throw new UsageError(`Cannot use the --install flag when the current directory is already part of a project`);

    if (!xfs.existsSync(this.context.cwd))
      await xfs.mkdirpPromise(this.context.cwd);

    const lockfilePath = ppath.join(this.context.cwd, configuration.get(`lockfileFilename`));
    if (!xfs.existsSync(lockfilePath))
      await xfs.writeFilePromise(lockfilePath, ``);

    const versionExitCode = await this.cli.run([`set`, `version`, this.install!]);
    if (versionExitCode !== 0)
      return versionExitCode;

    this.context.stdout.write(`\n`);

    const args: Array = [];
    if (this.private)
github yarnpkg / berry / packages / plugin-node-modules / sources / NodeModulesLinker.ts View on Github external
async findPackageLocator(location: PortablePath, opts: LinkOptions) {
    const pnpPath = getPnpPath(opts.project);
    if (!xfs.existsSync(pnpPath))
      throw new UsageError(`The project in ${opts.project.cwd}/package.json doesn't seem to have been installed - running an install there might help`);

    const physicalPath = npath.fromPortablePath(pnpPath);
    const pnpFile = miscUtils.dynamicRequire(physicalPath);
    delete require.cache[physicalPath];

    const locator = pnpFile.findPackageLocator(npath.fromPortablePath(location));
    if (!locator)
      return null;

    return structUtils.makeLocator(structUtils.parseIdent(locator.name), locator.reference);
  }
github yarnpkg / berry / packages / yarnpkg-core / sources / Configuration.ts View on Github external
const rcPath = ppath.join(currentCwd, rcFilename as PortablePath);

      if (xfs.existsSync(rcPath)) {
        const content = await xfs.readFilePromise(rcPath, `utf8`);

        let data;
        try {
          data = parseSyml(content) as any;
        } catch (error) {
          let tip = ``;

          if (content.match(/^\s+(?!-)[^:]+\s+\S+/m))
            tip = ` (in particular, make sure you list the colons after each key name)`;

          throw new UsageError(`Parse error when loading ${rcPath}; please check it's proper Yaml${tip}`);
        }

        rcFiles.push({path: rcPath, cwd: currentCwd, data});
      }

      nextCwd = ppath.dirname(currentCwd);
    }

    return rcFiles;
  }
github yarnpkg / berry / packages / plugin-pnp / sources / PnpLinker.ts View on Github external
async findPackageLocation(locator: Locator, opts: LinkOptions) {
    const pnpPath = getPnpPath(opts.project);
    if (!xfs.existsSync(pnpPath))
      throw new UsageError(`The project in ${opts.project.cwd}/package.json doesn't seem to have been installed - running an install there might help`);

    const pnpFile = miscUtils.dynamicRequireNoCache(pnpPath);

    const packageLocator = {name: structUtils.requirableIdent(locator), reference: locator.reference};
    const packageInformation = pnpFile.getPackageInformation(packageLocator);

    if (!packageInformation)
      throw new UsageError(`Couldn't find ${structUtils.prettyLocator(opts.project.configuration, locator)} in the currently installed PnP map - running an install might help`);

    return npath.toPortablePath(packageInformation.packageLocation);
  }

clipanion

Type-safe CLI library / framework with no runtime dependencies

MIT
Latest version published 4 months ago

Package Health Score

79 / 100
Full package analysis