<commit_before>path = require 'path'

optimist = require 'optimist'

Command = require './command'
fs = require './fs'

module.exports =
class Generator extends Command
  @commandNames: ['init']

  parseOptions: (argv) ->
    options = optimist(argv)

    options.usage """
      Usage: apm init -p <package-name>

      Generates code scaffolding for either a theme or package depending
      on option selected.
    """
    options.alias('p', 'package').describe('package', 'Generates a basic package')
    options.alias('h', 'help').describe('help', 'Print this usage message')

  showHelp: (argv) -> @parseOptions(argv).showHelp()

  run: (options) ->
    {callback} = options
    options = @parseOptions(options.commandArgs)
    if options.argv.package?
      packagePath = path.join(process.cwd(), options.argv.package)
      @generatePackage(packagePath)

    callback()

  generatePackage: (packagePath) ->
    templatePath = path.resolve(__dirname, '..', 'templates', 'package')
    packageName = path.basename(packagePath)

    fs.mkdir(packagePath)

    for childPath in fs.listRecursive(templatePath)
      templateChildPath = path.resolve(__dirname, '..', 'templates', 'package', childPath)
      relativePath = templateChildPath.replace(templatePath, "")
      relativePath = relativePath.replace(/^\//, '')
      relativePath = relativePath.replace(/\.template$/, '')
      relativePath = @replacePackageNamePlaceholders(relativePath, packageName)

      sourcePath = path.join(packagePath, relativePath)
      if fs.isDirectory(templateChildPath)
        fs.mkdir(sourcePath)
      else if fs.isFile(templateChildPath)
        fs.mkdir(path.dirname(sourcePath))
        contents = fs.readFileSync(templateChildPath).toString()
        content = @replacePackageNamePlaceholders(contents, packageName)
        fs.writeFileSync(sourcePath, content)

  replacePackageNamePlaceholders: (string, packageName) ->
    placeholderRegex = /__(?:(package-name)|([pP]ackageName)|(package_name))__/g<commit_msg>Throw an error if a type isn't specified<commit_after>path = require 'path'

optimist = require 'optimist'

Command = require './command'
fs = require './fs'

module.exports =
class Generator extends Command
  @commandNames: ['init']

  parseOptions: (argv) ->
    options = optimist(argv)

    options.usage """
      Usage: apm init -p <package-name>

      Generates code scaffolding for either a theme or package depending
      on option selected.
    """
    options.alias('p', 'package').describe('package', 'Generates a basic package')
    options.alias('h', 'help').describe('help', 'Print this usage message')

  showHelp: (argv) -> @parseOptions(argv).showHelp()

  run: (options) ->
    {callback} = options
    options = @parseOptions(options.commandArgs)
    if options.argv.package?
      packagePath = path.join(process.cwd(), options.argv.package)
      @generatePackage(packagePath)
      callback()
    else
      callback('Error: You must specify either --project or --theme to `apm init`')

  generatePackage: (packagePath) ->
    templatePath = path.resolve(__dirname, '..', 'templates', 'package')
    packageName = path.basename(packagePath)

    fs.mkdir(packagePath)

    for childPath in fs.listRecursive(templatePath)
      templateChildPath = path.resolve(__dirname, '..', 'templates', 'package', childPath)
      relativePath = templateChildPath.replace(templatePath, "")
      relativePath = relativePath.replace(/^\//, '')
      relativePath = relativePath.replace(/\.template$/, '')
      relativePath = @replacePackageNamePlaceholders(relativePath, packageName)

      sourcePath = path.join(packagePath, relativePath)
      if fs.isDirectory(templateChildPath)
        fs.mkdir(sourcePath)
      else if fs.isFile(templateChildPath)
        fs.mkdir(path.dirname(sourcePath))
        contents = fs.readFileSync(templateChildPath).toString()
        content = @replacePackageNamePlaceholders(contents, packageName)
        fs.writeFileSync(sourcePath, content)

  replacePackageNamePlaceholders: (string, packageName) ->
    placeholderRegex = /__(?:(package-name)|([pP]ackageName)|(package_name))__/g
{'fraction_non_alphanumeric': 0.1301518438177874, 'fraction_numerical': 0.0, 'mean_word_length': 3.8766157461809634, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '3255412', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1147, 'n_words': 256}