Sha256: 35fb22fb88745436602a3e23b54fa101498dce882b15500df611c85e9a3beba8

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

# encoding: utf-8

usage       'create_item [options] identifier'
summary     'create an item'
aliases     :ci
description <<-EOS
Create a new item in the current site. The first data source in the site
configuration will be used.
EOS

required :c, :vcs, 'specify the VCS to use'

run do |opts, args, cmd|
  Nanoc3::CLI::Commands::CreateItem.call(opts, args, cmd)
end

module Nanoc3::CLI::Commands

  class CreateItem < ::Nanoc3::CLI::Command

    def run
      # Check arguments
      if arguments.length != 1
        $stderr.puts "usage: #{command.usage}"
        exit 1
      end

      # Extract arguments and options
      identifier = arguments[0].cleaned_identifier

      # Make sure we are in a nanoc site directory
      self.require_site

      # Set VCS if possible
      self.set_vcs(options[:vcs])

      # Check whether item is unique
      if !self.site.items.find { |i| i.identifier == identifier }.nil?
        $stderr.puts "An item already exists at #{identifier}. Please " +
                     "pick a unique name for the item you are creating."
        exit 1
      end

      # Setup notifications
      Nanoc3::NotificationCenter.on(:file_created) do |file_path|
        Nanoc3::CLI::Logger.instance.file(:high, :create, file_path)
      end

      # Create item
      data_source = self.site.data_sources[0]
      data_source.create_item(
        "Hi, I'm a new item!\n",
        { :title => "A New Item" },
        identifier
      )

      puts "An item has been created at #{identifier}."
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nanoc3-3.2.4 lib/nanoc3/cli/commands/create_item.rb
nanoc3-3.2.3 lib/nanoc3/cli/commands/create_item.rb
nanoc3-3.2.2 lib/nanoc3/cli/commands/create_item.rb
nanoc3-3.2.1 lib/nanoc3/cli/commands/create_item.rb