index.js
function entrifyFromPkg(dir, options) {

  // Find package.json files.
  const pkgPaths = glob.sync('**/package.json', { cwd: dir, nodir: true, absolute: true })

  pkgPaths.forEach((pkgPath) => {
    console.log('[entrify]', 'Found:', pkgPath)
    const pkg = require(pkgPath)

    // A package.json file should have a main entry.
    if (!pkg.main) {
      console.warn('[entrify]', pkgPath, 'does not have a main entry.')
      return
    }

    // The main entry should not point to an index.js file.
    if (pkg.main === 'index.js' || pkg.main === './index.js') {
      console.warn('[entrify]', pkgPath, 'main entry is index.js.')
      return
    }

    // Ensure the index.js file to create doesn't already exist.
    const pkgDir = path.dirname(pkgPath)
    const indexPath = `${pkgDir}/index.js`

    if (fs.existsSync(indexPath)) {
      console.warn('[entrify]', indexPath, 'already exists.')
      return
    }

    // Create an index.js file alongside the package.json.
    const name = camelize(path.basename(pkgDir))
    const main = pkg.main.startsWith('./') ? pkg.main : `./${pkg.main}`

    fs.writeFileSync(indexPath, indexTemplate(options.format, { name, main }))
    console.log('[entrify]', indexPath, 'created!')

    // Delete the package.json file.
    fs.unlinkSync(pkgPath)
    console.log('[entrify]', pkgPath, 'deleted!')
  })
}
function entrifyFromPkg(dir, options) {

  // Find package.json files.
  const pkgPaths = glob.sync('**/package.json', { cwd: dir, nodir: true, absolute: true })

  pkgPaths.forEach((pkgPath) => {
    console.log('[entrify]', 'Found:', pkgPath)
    const pkg = require(pkgPath)

    // A package.json file should have a main entry.
    if (!pkg.main) {
      console.warn('[entrify]', pkgPath, 'does not have a main entry.')
      return
    }

    // The main entry should not point to an index.js file.
    if (pkg.main === 'index.js' || pkg.main === './index.js') {
      console.warn('[entrify]', pkgPath, 'main entry is index.js.')
      return
    }

    // Ensure the index.js file to create doesn't already exist.
    const pkgDir = path.dirname(pkgPath)
    const indexPath = `${pkgDir}/index.js`

    if (fs.existsSync(indexPath)) {
      console.warn('[entrify]', indexPath, 'already exists.')
      return
    }

    // Create an index.js file alongside the package.json.
    const name = camelize(path.basename(pkgDir))
    const main = pkg.main.startsWith('./') ? pkg.main : `./${pkg.main}`

    fs.writeFileSync(indexPath, indexTemplate(options.format, { name, main }))
    console.log('[entrify]', indexPath, 'created!')

    // Delete the package.json file.
    fs.unlinkSync(pkgPath)
    console.log('[entrify]', pkgPath, 'deleted!')
  })
}
function entrifyFromPkg ( dir , options ) { // Find package.json files. const pkgPaths = glob . sync ( '**/package.json' , { cwd : dir , nodir : true , absolute : true } ) pkgPaths . forEach ( ( pkgPath ) => { console . log ( '[entrify]' , 'Found:' , pkgPath ) const pkg = require ( pkgPath ) // A package.json file should have a main entry. if ( ! pkg . main ) { console . warn ( '[entrify]' , pkgPath , 'does not have a main entry.' ) return } // The main entry should not point to an index.js file. if ( pkg . main === 'index.js' || pkg . main === './index.js' ) { console . warn ( '[entrify]' , pkgPath , 'main entry is index.js.' ) return } // Ensure the index.js file to create doesn't already exist. const pkgDir = path . dirname ( pkgPath ) const indexPath = ` ${ pkgDir } ` if ( fs . existsSync ( indexPath ) ) { console . warn ( '[entrify]' , indexPath , 'already exists.' ) return } // Create an index.js file alongside the package.json. const name = camelize ( path . basename ( pkgDir ) ) const main = pkg . main . startsWith ( './' ) ? pkg . main : ` ${ pkg . main } ` fs . writeFileSync ( indexPath , indexTemplate ( options . format , { name , main } ) ) console . log ( '[entrify]' , indexPath , 'created!' ) // Delete the package.json file. fs . unlinkSync ( pkgPath ) console . log ( '[entrify]' , pkgPath , 'deleted!' ) } ) }
Entrify a directory by creating an index.js file when a valid package.json is found.
@param {String} dir The directory to entrify.
@param {Object} options Hash of options.
Entrify a directory by creating an index . js file when a valid package . json is found .