Class: ConvenientService::Examples::Rails::Gemfile::Services::StripComments
- Inherits:
-
Object
- Object
- ConvenientService::Examples::Rails::Gemfile::Services::StripComments
- Includes:
- RailsService::Config
- Defined in:
- lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb
Instance Method Summary collapse
- #file_with_comments ⇒ Object
- #file_without_comments ⇒ Object
-
#js_script ⇒ Object
NOTE: When you have no time to do something well, delegate that task to someone who already works with it all the time.
- #result ⇒ Object
Methods included from Config
Instance Method Details
#file_with_comments ⇒ Object
94 95 96 97 98 99 |
# File 'lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb', line 94 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_comments ⇒ Object
101 102 103 104 105 106 |
# File 'lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb', line 101 def file_without_comments ## # NOTE: JS script tries to fill content of the file without comments. # @file_without_comments ||= ::Tempfile.new end |
#js_script ⇒ Object
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.
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 89 90 91 92 |
# File 'lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb', line 39 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 |
#result ⇒ Object
31 32 33 |
# File 'lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb', line 31 def result success(content_without_comments: file_without_comments.read) end |