Module: Vuesfc2js

Defined in:
lib/vuesfc2js.rb,
lib/vuesfc2js/version.rb,
lib/vuesfc2js/conversion/base.rb,
lib/vuesfc2js/error/not_exist_error.rb,
lib/vuesfc2js/error/not_implemented_error.rb,
lib/vuesfc2js/conversion/script_extraction.rb,
lib/vuesfc2js/conversion/replace_dot_vue2js.rb,
lib/vuesfc2js/conversion/replace_path_alias.rb,
lib/vuesfc2js/conversion/replace_filename_dot_vue2js.rb

Defined Under Namespace

Modules: Conversion, Error

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.convert_directory(dir, path_alias: nil, extnames: %w[.vue .js],, replace: false) ⇒ Object

Convert a directory.

Parameters:

  • dir (String)
  • path_alias (nil, Hash) (defaults to: nil)
  • extnames (Array<String>) (defaults to: %w[.vue .js],)
  • replace (FalseClass, TrueClass) (defaults to: false)

    if replace .vue file to .js file, set true.

Raises:



61
62
63
64
65
66
67
# File 'lib/vuesfc2js.rb', line 61

def convert_directory(dir, path_alias: nil, extnames: %w[.vue .js], replace: false)
  raise Error::NotExistError, "#{dir} is not existed." unless Dir.exist?(dir)

  Dir.glob((Pathname.new(dir) + Pathname.new("**/*")).to_s).each do |filename|
    convert_file(filename, path_alias: path_alias, extnames: extnames, replace: replace)
  end
end

.convert_file(filename, path_alias: nil, extnames: %w[.vue .js],, replace: false) ⇒ Object

Convert a file.

Parameters:

  • filename (String)
  • path_alias (nil, Hash) (defaults to: nil)
  • extnames (Array<String>) (defaults to: %w[.vue .js],)
  • replace (FalseClass, TrueClass) (defaults to: false)

    if replace .vue file to .js file, set true.

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vuesfc2js.rb', line 39

def convert_file(filename, path_alias: nil, extnames: %w[.vue .js], replace: false)
  raise "Only .vue or .js is permitted" if (extnames - %w[.vue .js]).size >= 1

  extname = File.extname(filename)
  return unless extnames.include?(extname)

  raise Error::NotExistError, "#{filename} is not existed." unless File.exist?(filename)

  str = File.read(filename)
  str = convert_str(str, path_alias: path_alias, extname: extname)

  File.write(filename(filename), str)

  File.delete(filename) if replace && File.extname(filename) == ".vue"
  filename
end

.convert_str(str, path_alias: nil, extname: ".vue") ⇒ String

Convert code like SFC to js.

Parameters:

  • str (String)
  • path_alias (nil, Hash) (defaults to: nil)
  • extname (String) (defaults to: ".vue")

Returns:

  • (String)


21
22
23
24
25
# File 'lib/vuesfc2js.rb', line 21

def convert_str(str, path_alias: nil, extname: ".vue")
  str = extname == ".vue" ? Conversion::ScriptExtraction.call(str) : str
  str = Conversion::ReplaceDotVue2Js.call(str)
  path_alias.nil? ? str : Conversion::ReplacePathAlias.call(str, path_alias)
end

.convert_vue_project(src, dst, src_path_alias: nil, extnames: %w[.vue .js],, replace: false) ⇒ Object

Parameters:

  • src (String)
  • dst (String)
  • src_path_alias (nil, Hash) (defaults to: nil)
  • extnames (Array<String>) (defaults to: %w[.vue .js],)
  • replace (FalseClass, TrueClass) (defaults to: false)

    if replace .vue file to .js file, set true.



74
75
76
77
78
79
80
81
82
83
# File 'lib/vuesfc2js.rb', line 74

def convert_vue_project(src, dst, src_path_alias: nil, extnames: %w[.vue .js], replace: false)
  check_params(src, dst)
  src_pathname = Pathname.new(src).expand_path
  dst_pathname = Pathname.new(dst)
  new_src_pathname = dst_pathname + src_pathname.basename
  path_alias = shift_path_alias(src_path_alias, new_src_pathname, src_pathname)
  p "Converting #{src_pathname} to #{new_src_pathname}..."
  cp_r(src_pathname, dst_pathname)
  convert_directory(new_src_pathname.to_s, path_alias: path_alias, extnames: extnames, replace: replace)
end

.filename(filename) ⇒ String

Get filename having js extname from vue extname file.

Parameters:

  • filename (String)

Returns:

  • (String)


30
31
32
# File 'lib/vuesfc2js.rb', line 30

def filename(filename)
  Conversion::ReplaceFilenameDotVue2Js.call(filename)
end