<commit_before>/* @flow */

import colors from 'colors/safe';
import {determineFlowVersion} from './npmProjectUtils';
import {findPackageJsonPath} from './npmProjectUtils';
import {format} from 'util';
import {fs} from './node';
import globAsync from 'glob';
import {getPackageJsonData} from './npmProjectUtils';
import {getPackageJsonDependencies} from './npmProjectUtils';
import {mkdirp} from './fileUtils';
import {path} from './node';
import {signCode} from './codeSign';
import {verifySignedCode} from './codeSign';
import {versionToString} from './semver';

export function glob(pattern: string, options: Object): Promise<Array<string>> {
  return new Promise((resolve, reject) =>
    globAsync(pattern, options, (err, files) => {
      if (err) {
        reject(err);
      } else {
        resolve(files);
      }
    })
  );
}

async function resolvePkgDirPath(
  pkgName: string,
  pkgJsonDirPath: string,
): Promise<null | string> {
  let prevNodeModulesDirPath;
  let nodeModulesDirPath = path.resolve(pkgJsonDirPath, 'node_modules');
  while (true) {
    const pkgDirPath = path.resolve(nodeModulesDirPath, pkgName);
    if (await fs.exists(pkgDirPath)) {
      return pkgDirPath;
    }

    prevNodeModulesDirPath = nodeModulesDirPath;
    nodeModulesDirPath = path.resolve(
      nodeModulesDirPath,
      '..',
      '..',
      'node_modules',
    );
    if (prevNodeModulesDirPath === nodeModulesDirPath) {
      break;
    }
  }
  throw new Error(
    'Unable to find `node_modules/' + pkgName + '/` install directory! ' +
    'Did you forget to run `npm install` before running `flow-typed install`?'
  );
}

const moduleStubTemplate = (`
declare module '%s' {
  declare module.exports: any;
}`).trim();
const aliasTemplate = (`
declare module '%s%s' {
  declare module.exports: $Exports<'%s'>;
}`).trim();

function stubFor(moduleName: string, fileExt?: string): string {
  const moduleStub = format(moduleStubTemplate, moduleName);
  if (fileExt !== undefined) {
    const aliasStub = format(aliasTemplate, moduleName, fileExt, moduleName);
    return `${moduleStub}\n${aliasStub}`;
  }
  return moduleStub;
}

async function writeStub(
  projectRoot: string,
  packageName: string,
  packageVersion: string,
  overwrite: boolean,
  files: Array<string>,
): Promise<string> {
  let output = [
    '/**',
    ' * This is an autogenerated libdef stub for:',
    ' *',
    ` *   '${packageName}'`,
    ' *',
    ' * Fill this stub out by replacing all the `any` types.',
    ' *',
    ' * Once filled out, we encourage you to share your work with the ',
    ' * community by sending a pull request to: ',
    ' * https://github.com/flowtype/flow-typed',
    ' */\n\n'
  ].join('\n');

  output += stubFor(packageName);

  if (files.length > 0) {<commit_msg>Generates stubs without trailing whitespace (#687)<commit_after>/* @flow */

import colors from 'colors/safe';
import {determineFlowVersion} from './npmProjectUtils';
import {findPackageJsonPath} from './npmProjectUtils';
import {format} from 'util';
import {fs} from './node';
import globAsync from 'glob';
import {getPackageJsonData} from './npmProjectUtils';
import {getPackageJsonDependencies} from './npmProjectUtils';
import {mkdirp} from './fileUtils';
import {path} from './node';
import {signCode} from './codeSign';
import {verifySignedCode} from './codeSign';
import {versionToString} from './semver';

export function glob(pattern: string, options: Object): Promise<Array<string>> {
  return new Promise((resolve, reject) =>
    globAsync(pattern, options, (err, files) => {
      if (err) {
        reject(err);
      } else {
        resolve(files);
      }
    })
  );
}

async function resolvePkgDirPath(
  pkgName: string,
  pkgJsonDirPath: string,
): Promise<null | string> {
  let prevNodeModulesDirPath;
  let nodeModulesDirPath = path.resolve(pkgJsonDirPath, 'node_modules');
  while (true) {
    const pkgDirPath = path.resolve(nodeModulesDirPath, pkgName);
    if (await fs.exists(pkgDirPath)) {
      return pkgDirPath;
    }

    prevNodeModulesDirPath = nodeModulesDirPath;
    nodeModulesDirPath = path.resolve(
      nodeModulesDirPath,
      '..',
      '..',
      'node_modules',
    );
    if (prevNodeModulesDirPath === nodeModulesDirPath) {
      break;
    }
  }
  throw new Error(
    'Unable to find `node_modules/' + pkgName + '/` install directory! ' +
    'Did you forget to run `npm install` before running `flow-typed install`?'
  );
}

const moduleStubTemplate = (`
declare module '%s' {
  declare module.exports: any;
}`).trim();
const aliasTemplate = (`
declare module '%s%s' {
  declare module.exports: $Exports<'%s'>;
}`).trim();

function stubFor(moduleName: string, fileExt?: string): string {
  const moduleStub = format(moduleStubTemplate, moduleName);
  if (fileExt !== undefined) {
    const aliasStub = format(aliasTemplate, moduleName, fileExt, moduleName);
    return `${moduleStub}\n${aliasStub}`;
  }
  return moduleStub;
}

async function writeStub(
  projectRoot: string,
  packageName: string,
  packageVersion: string,
  overwrite: boolean,
  files: Array<string>,
): Promise<string> {
  let output = [
    '/**',
    ' * This is an autogenerated libdef stub for:',
    ' *',
    ` *   '${packageName}'`,
    ' *',
    ' * Fill this stub out by replacing all the `any` types.',
    ' *',
    ' * Once filled out, we encourage you to share your work with the',
    ' * community by sending a pull request to:',
    ' * https://github.com/flowtype/flow-typed',
    ' */\n\n'
  ].join('\n');

  output += stubFor(packageName);

  if (files.length > 0) {
{'fraction_non_alphanumeric': 0.14653784219001612, 'fraction_numerical': 0.0008946144211844695, 'mean_word_length': 3.6895973154362416, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1647184', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1751, 'n_words': 471}