The goal of this homework is to create a Node.js application that separates TypeScript code from comments in a given source file.
The application must:
- Read an input TypeScript file line-by-line.
- Split each line into code and comment parts.
- Write two separate output files:
- One containing only the code.
- One containing only the comments.
- Support comments starting with
//(single-line comments).
Input file:
//abcd is a variable of type number
let abcd: number;
const lmn = 5; //constant lmn contains value 5Output code file:
let abcd: number;
const lmn = 5;Output comment file:
//abcd is a variable of type number
//constant lmn contains value 5Paths to the input and output files must be configured in the config module.
This project is a TypeScript Node.js application designed to parse source files and extract comments and code into separate files.
It uses asynchronous file reading with Node.js streams and the readline module for line-by-line processing.
The main modules are:
CommentParser— responsible for splitting code and comments from each line.FileHandler— responsible for reading input files and writing output files.config— stores paths for input and output files.
The project also includes unit tests with Jest to ensure the parsing logic works correctly.
This project helps to:
- Practice file handling and stream processing in Node.js.
- Learn line-by-line parsing using
readline. - Apply string manipulation and parsing logic in TypeScript.
- Set up a modern TypeScript project with ESM modules.
- Configure and run unit tests with
Jestandts-jest.
- The application reads the input file asynchronously.
- Each line is sent to
CommentParser.extractCodeAndComments():- Lines starting with
//are considered comments only. - Lines containing both code and comment are split at the first occurrence of
//. - Lines without
//are treated as code only.
- Lines starting with
- Extracted code lines and comment lines are collected in arrays.
FileHandlerwrites these arrays into separate files defined in the configuration.- Logging is performed for process tracking and error handling.
Input file:
const x = 5; // это переменная
let y = 10;
// комментарийGenerated output files:
- Code output file:
const x = 5;
let y = 10;- Comment output file:
// это переменная
// комментарий- Clone the repository:
git clone <repo-url>
cd HW2- Install dependencies:
npm install- Configure file paths in
config/default.json:
{
"inputFilePath": "./input.txt",
"outputCodeFilePath": "./output_code.txt",
"outputCommentFilePath": "./output_comments.txt"
}- Run the application:
npm start- Run tests:
npm testHW2/
│
├── src/
│ ├── app.ts # Main application entry point
│ ├── parser/
│ │ └── CommentParser.ts # Logic for extracting code and comments
│ └── utils/
│ └── FileHandler.ts # File reading/writing utilities
│
├── tests/
│ ├── CommentParser.test.ts
│ └── FileHandler.test.ts
│
├── config/
│ └── default.json # Configuration for input/output file paths
│
├── jest.config.cjs # Jest configuration for ts-jest with ESM
├── package.json
├── tsconfig.json
└── README.md
Unit tests verify:
- Correct splitting of code and comments in all scenarios:
- Comment-only lines
- Code-only lines
- Lines with code and inline comments
- Correct handling of empty lines
- Proper writing of output files
Run tests with:
npm test- Node.js 18+
- TypeScript
- Jest (
ts-jest)
This project demonstrates:
- Asynchronous file reading and writing in Node.js
- Line-by-line parsing and string manipulation in TypeScript
- Separation of code and comments for analysis or documentation purposes
- Unit testing with Jest in a TypeScript environment
Made with ❤️ and TypeScript by Sam-Shepsl Malikin