Class: ConvenientService::Examples::Rails::V1::Gemfile::Services::StripComments

Inherits:
Object
  • Object
show all
Includes:
RailsService::Config
Defined in:
lib/convenient_service/examples/rails/v1/gemfile/services/strip_comments.rb

Instance Method Summary collapse

Instance Method Details

#file_with_commentsObject



90
91
92
93
94
95
# File 'lib/convenient_service/examples/rails/v1/gemfile/services/strip_comments.rb', line 90

def file_with_comments
  ##
  # NOTE: Tempfile is used to avoid issues with escaping of quotes inside JS script.
  #
  @file_with_comments ||= ::Tempfile.new.tap { |tempfile| tempfile.write(content_with_comments) }.tap(&:close)
end

#file_without_commentsObject



97
98
99
100
101
102
# File 'lib/convenient_service/examples/rails/v1/gemfile/services/strip_comments.rb', line 97

def file_without_comments
  ##
  # NOTE: JS script tries to fill content of the file without comments.
  #
  @file_without_comments ||= ::Tempfile.new
end

#js_scriptObject

NOTE: When you have no time to do something well, delegate that task to someone who already works with it all the time. That is why strip-comments npm package is used to remove comments.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/convenient_service/examples/rails/v1/gemfile/services/strip_comments.rb', line 35

def js_script
  ##
  # NOTE: `main` function is created in order to have an ability to use early returns.
  #
  # IMPORTANT: Use only double quotes inside this JS script, since it is later consumed by `node -e` option wrapped by single quoutes.
  #
  # TODO: Consider to use WeakRef if memory is a concern.
  # https://ruby-doc.org/stdlib-2.5.1/libdoc/weakref/rdoc/WeakRef.html
  #
  # TODO: Jest tests for JS script.
  #
  @js_script ||= <<~JAVASCRIPT.gsub(/\n\s*/, " ")
    const main = () => {
      const process = require("process");
      const fs = require("fs");

      /**
       * TODO: try/catch when "strip-comments" is not available.
       */
      const strip = require("strip-comments");

      const fileWithCommentsPath = process.argv[1];
      const fileWithoutCommentsPath = process.argv[2];
      const language = process.argv[3] || "ruby";

      if (!fileWithCommentsPath) {
        console.log("Error: Input file path is NOT passed.");

        process.exit(1);
      }

      if (!fileWithoutCommentsPath) {
        console.log("Error: Output file path is NOT passed.");

        process.exit(1);
      }

      if (!["ruby", "javascript"].includes(language)) {
        console.log("Error: Language " + language + " is NOT supported.");

        process.exit(1);
      }

      const fileWithCommentsContent = fs.readFileSync(fileWithCommentsPath, { encoding: "utf8" });
      const fileWithoutCommentsContent = strip(fileWithCommentsContent, { language });

      fs.writeFileSync(fileWithoutCommentsPath, fileWithoutCommentsContent, { encoding: "utf8" });

      process.exit(0);
    };

    main();
  JAVASCRIPT
end

#resultObject



27
28
29
# File 'lib/convenient_service/examples/rails/v1/gemfile/services/strip_comments.rb', line 27

def result
  success(data: {content_without_comments: file_without_comments.read})
end