lib/support/browserScript.js
async function findAndParseScripts(root, category) {
  async function scriptsFromDirectory(dirpath) {
    const dir = (await readdir(dirpath))
      .map(name => toFullPath(name, dirpath))
      .filter(await filters.onlyFiles)
      .filter(filters.onlyWithExtension('.js'));
    const result = {};
    for (const filepath of dir) {
      const name = path.basename(filepath, '.js');
      const script = await readFile(filepath, 'utf8');
      result[name] = script;
    }
    return result;
  }

  async function parseDirectory(dirPath) {
    const categoryName = path.basename(dirPath);

    return scriptsFromDirectory(dirPath).then(scripts => {
      const categories = {};
      categories[categoryName] = scripts;
      return categories;
    });
  }

  return stat(root).then(stats => {
    if (stats.isFile()) {
      return readFile(root, 'utf8').then(content => {
        const name = path.basename(root, '.js');

        let scripts = {};
        scripts[name] = content;

        let categories = {};
        categories[category] = scripts;

        return categories;
      });
    } else {
      return parseDirectory(root);
    }
  });
}
async function findAndParseScripts(root, category) {
  async function scriptsFromDirectory(dirpath) {
    const dir = (await readdir(dirpath))
      .map(name => toFullPath(name, dirpath))
      .filter(await filters.onlyFiles)
      .filter(filters.onlyWithExtension('.js'));
    const result = {};
    for (const filepath of dir) {
      const name = path.basename(filepath, '.js');
      const script = await readFile(filepath, 'utf8');
      result[name] = script;
    }
    return result;
  }

  async function parseDirectory(dirPath) {
    const categoryName = path.basename(dirPath);

    return scriptsFromDirectory(dirPath).then(scripts => {
      const categories = {};
      categories[categoryName] = scripts;
      return categories;
    });
  }

  return stat(root).then(stats => {
    if (stats.isFile()) {
      return readFile(root, 'utf8').then(content => {
        const name = path.basename(root, '.js');

        let scripts = {};
        scripts[name] = content;

        let categories = {};
        categories[category] = scripts;

        return categories;
      });
    } else {
      return parseDirectory(root);
    }
  });
}
async function findAndParseScripts ( root , category ) { async function scriptsFromDirectory ( dirpath ) { const dir = ( await readdir ( dirpath ) ) . map ( name => toFullPath ( name , dirpath ) ) . filter ( await filters . onlyFiles ) . filter ( filters . onlyWithExtension ( '.js' ) ) ; const result = { } ; for ( const filepath of dir ) { const name = path . basename ( filepath , '.js' ) ; const script = await readFile ( filepath , 'utf8' ) ; result [ name ] = script ; } return result ; } async function parseDirectory ( dirPath ) { const categoryName = path . basename ( dirPath ) ; return scriptsFromDirectory ( dirPath ) . then ( scripts => { const categories = { } ; categories [ categoryName ] = scripts ; return categories ; } ) ; } return stat ( root ) . then ( stats => { if ( stats . isFile ( ) ) { return readFile ( root , 'utf8' ) . then ( content => { const name = path . basename ( root , '.js' ) ; let scripts = { } ; scripts [ name ] = content ; let categories = { } ; categories [ category ] = scripts ; return categories ; } ) ; } else { return parseDirectory ( root ) ; } } ) ; }
Parse a file or directory, and return an object representing that groups scripts by category.
Single js files will be put into the category 'custom', for directories the category name will
be taken from the directory name.

The resulting value looks like this:
<pre>
{
'category': {
'script': <contents of script.js>
}
}
</pre>
@param {string} root a path to a js file, alternatively a directory containing js files.
@param {string} category a category to apply if a file is passed
@returns {Promise.<Object>}
Parse a file or directory and return an object representing that groups scripts by category . Single js files will be put into the category custom for directories the category name will be taken from the directory name .