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
Instance Method Details
#file_with_comments ⇒ Object
89 90 91 92 93 94 |
# File 'lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb', line 89 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
96 97 98 99 100 101 |
# File 'lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb', line 96 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.
34 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 |
# File 'lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb', line 34 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
26 27 28 |
# File 'lib/convenient_service/examples/rails/gemfile/services/strip_comments.rb', line 26 def result success(content_without_comments: file_without_comments.read) end |