Skip to content

Commit e1f8b41

Browse files
committed
feat: added support for sendWatchChangeRequest, jsxClosingTag, getLinkedEditingRange, braceCommand, braceCompletion, getSpanOfEnclosingComment,
1 parent c2829c2 commit e1f8b41

File tree

7 files changed

+319
-173
lines changed

7 files changed

+319
-173
lines changed

README.md

Lines changed: 12 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,24 @@
1-
# template-nodejs
2-
A template project for nodejs. Has integrated linting, testing,
3-
coverage, reporting, GitHub actions for publishing to npm repository, dependency updates and other goodies.
1+
# TypeScript Server Instance Project
42

5-
Easily use this template to quick start a production ready nodejs project template.
3+
A robust project for managing and interacting with a TypeScript Server (TSServer). It includes an extensive array of functionalities such as file manipulation, code analysis, formatting, refactoring, and more, making it an ideal starting point for any TypeScript project requiring direct interaction with TSServer.
64

75
## Code Guardian
8-
[![<app> build verification](https://github.com/aicore/template-nodejs/actions/workflows/build_verify.yml/badge.svg)](https://github.com/aicore/template-nodejs/actions/workflows/build_verify.yml)
9-
10-
<a href="https://sonarcloud.io/summary/new_code?id=aicore_template-nodejs-ts">
11-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=alert_status" alt="Sonar code quality check" />
12-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=security_rating" alt="Security rating" />
13-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=vulnerabilities" alt="vulnerabilities" />
14-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=coverage" alt="Code Coverage" />
15-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=bugs" alt="Code Bugs" />
16-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=reliability_rating" alt="Reliability Rating" />
17-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=sqale_rating" alt="Maintainability Rating" />
18-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=ncloc" alt="Lines of Code" />
19-
<img src="https://sonarcloud.io/api/project_badges/measure?project=aicore_template-nodejs-ts&metric=sqale_index" alt="Technical debt" />
20-
</a>
21-
22-
23-
# TODOs after template use
24-
## !!!Please see all issues in the generated repository as an issue will be generated tracking the fix of each of the below items.
25-
1. Update package.json with your app defaults
26-
2. Check Build actions on pull requests.
27-
3. create a home page in wiki by going to wiki link https://github.com/<your_org>/<your_repo>/wiki
28-
4. Goto github `repository` > `settings`> and uncheck `Allow merge commits`. this is usually automatically done by code guardian bots in core.ai org. so you may just need to verify it.
29-
5. In sonar cloud, enable Automatic analysis from `Administration
30-
Analysis Method` for the first time before a pull request is raised: ![image](https://user-images.githubusercontent.com/5336369/148695840-65585d04-5e59-450b-8794-54ca3c62b9fe.png)
31-
6. Check codacy runs on pull requests, set codacy defaults. You may remove codacy if sonar cloud is only needed.
32-
7. Update the above Code Guardian badges; change all `id=aicore_template-nodejs-ts` to the sonar id of your project fields. see this PR: https://github.com/aicore/libcache/pull/13
6+
[![TSServer Instance build verification](https://github.com/aicore/template-nodejs/actions/workflows/build_verify.yml/badge.svg)](https://github.com/aicore/template-nodejs/actions/workflows/build_verify.yml)
337

348
# Commands available
359

36-
## Building
37-
Since this is a pure JS template project, build command just runs test with coverage.
10+
## Initialization
11+
To initialize the TypeScript Server:
3812
```shell
3913
> npm install // do this only once.
40-
> npm run build
41-
```
42-
43-
## Linting
44-
To lint the files in the project, run the following command:
45-
```shell
46-
> npm run lint
47-
```
48-
To Automatically fix lint errors:
49-
```shell
50-
> npm run lint:fix
51-
```
52-
53-
## Testing
54-
To run all tests:
55-
```shell
56-
> npm run test
57-
Hello world Tests
58-
✔ should return Hello World
59-
#indexOf()
60-
✔ should return -1 when the value is not present
61-
```
62-
63-
Additionally, to run unit/integration tests only, use the commands:
64-
```shell
65-
> npm run test:unit
66-
> npm run test:integ
67-
```
68-
69-
## Coverage Reports
70-
To run all tests with coverage:
71-
72-
```shell
73-
> npm run cover
74-
Hello world Tests
75-
✔ should return Hello World
76-
#indexOf()
77-
✔ should return -1 when the value is not present
78-
79-
80-
2 passing (6ms)
81-
82-
----------|---------|----------|---------|---------|-------------------
83-
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
84-
----------|---------|----------|---------|---------|-------------------
85-
All files | 100 | 100 | 100 | 100 |
86-
index.js | 100 | 100 | 100 | 100 |
87-
----------|---------|----------|---------|---------|-------------------
88-
89-
=============================== Coverage summary ===============================
90-
Statements : 100% ( 5/5 )
91-
Branches : 100% ( 2/2 )
92-
Functions : 100% ( 1/1 )
93-
Lines : 100% ( 5/5 )
94-
================================================================================
95-
Detailed unit test coverage report: file:///template-nodejs/coverage-unit/index.html
96-
Detailed integration test coverage report: file:///template-nodejs/coverage-integration/index.html
97-
```
98-
After running coverage, detailed reports can be found in the coverage folder listed in the output of coverage command.
99-
Open the file in browser to view detailed reports.
100-
101-
To run unit/integration tests only with coverage
102-
```shell
103-
> npm run cover:unit
104-
> npm run cover:integ
105-
```
106-
107-
Sample coverage report:
108-
![image](https://user-images.githubusercontent.com/5336369/148687351-6d6c12a2-a232-433d-ab62-2cf5d39c96bd.png)
109-
110-
### Unit and Integration coverage configs
111-
Unit and integration test coverage settings can be updated by configs `.nycrc.unit.json` and `.nycrc.integration.json`.
112-
113-
See https://github.com/istanbuljs/nyc for config options.
114-
115-
# Publishing packages to NPM
116-
117-
## Preparing for release
118-
Please run `npm run release` on the `main` branch and push the changes to main. The release command will bump the npm version.
119-
120-
!NB: NPM publish will faill if there is another release with the same version.
121-
## Publishing
122-
To publish a package to npm, push contents to `npm` branch in
123-
this repository.
124-
125-
## Publishing `@aicore/package*`
126-
If you are looking to publish to package owned by core.ai, you will need access to the GitHub Organization secret `NPM_TOKEN`.
127-
128-
For repos managed by [aicore](https://github.com/aicore) org in GitHub, Please contact your Admin to get access to core.ai's NPM tokens.
129-
130-
131-
## Publishing to your own npm account
132-
Alternatively, if you want to publish the package to your own npm account, please follow these docs:
133-
1. Create an automation access token by following this [link](https://docs.npmjs.com/creating-and-viewing-access-tokens).
134-
2. Add NPM_TOKEN to your repository secret by following this [link](https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow)
135-
136-
To edit the publishing workflow, please see file: `.github/workflows/npm-publish.yml`
137-
138-
139-
# Dependency updates
140-
We use Rennovate for dependency updates: https://blog.logrocket.com/renovate-dependency-updates-on-steroids/
141-
* By default, dep updates happen on sunday every week.
142-
* The status of dependency updates can be viewed here if you have this repo permissions in github: https://app.renovatebot.com/dashboard#github/aicore/template-nodejs
143-
* To edit rennovate options, edit the rennovate.json file in root, see https://docs.renovatebot.com/configuration-options/
144-
Refer
145-
146-
# Code Guardian
147-
Several automated workflows that check code integrity are integrated into this template.
148-
These include:
149-
1. GitHub actions that runs build/test/coverage flows when a contributor raises a pull request
150-
2. [Sonar cloud](https://sonarcloud.io/) integration using `.sonarcloud.properties`
151-
1. In sonar cloud, enable Automatic analysis from `Administration
152-
Analysis Method` for the first time ![image](https://user-images.githubusercontent.com/5336369/148695840-65585d04-5e59-450b-8794-54ca3c62b9fe.png)
153-
154-
## IDE setup
155-
SonarLint is currently available as a free plugin for jetbrains, eclipse, vscode and visual studio IDEs.
156-
Use sonarLint plugin for webstorm or any of the available
157-
IDEs from this link before raising a pull request: https://www.sonarlint.org/ .
158-
159-
SonarLint static code analysis checker is not yet available as a Brackets
160-
extension.
161-
162-
## Internals
163-
### Testing framework: Mocha , assertion style: chai
164-
See https://mochajs.org/#getting-started on how to write tests
165-
Use chai for BDD style assertions (expect, should etc..). See move here: https://www.chaijs.com/guide/styles/#expect
166-
167-
### Mocks and spies:
14+
> node -e 'require("./path-to-createTSServerInstance").init()'
15+
# Usage and Examples
16816

169-
Since it is not that straight forward to mock es6 module imports, use the follow pull request as reference to mock
170-
imported libs:
171-
17+
This TypeScript Server (TSServer) Instance project provides a wide range of functionalities to interact with TypeScript Server. Below are examples of how you can use these functionalities.
17218

173-
* sample pull request: https://github.com/aicore/libcache/pull/6/files
174-
* [setting up mocks](https://github.com/aicore/libcache/blob/485b1b6244f7022eb0a83d9f72d897fe712badbe/test/unit/setup-mocks.js)
175-
* [using the mocks](https://github.com/aicore/libcache/pull/6/files#diff-8ea7ccf28b28a0ae7b43e468abd3e9a8bb411bb329ad5cb45eb9a93709ed8dc5R2)
176-
ensure to import `setup-mocks.js` as the first import of all files in tests.
19+
## Initialization
17720

178-
#### using sinon lib if the above method doesn't fit your case
179-
if you want to mock/spy on fn() for unit tests, use sinon. refer docs: https://sinonjs.org/
21+
Before you start, make sure to initialize the TypeScript Server:
18022

181-
### Note on coverage suite used here:
182-
we use c8 for coverage https://github.com/bcoe/c8. Its reporting is based on nyc, so detailed docs can be found
183-
here: https://github.com/istanbuljs/nyc ; We didn't use nyc as it do not yet have ES module support
184-
see: https://github.com/digitalbazaar/bedrock-test/issues/16 . c8 is drop replacement for nyc coverage reporting tool
23+
```javascript
24+
const tsServer = require('./path-to-createTSServerInstance').init();

src/exp.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,25 @@ for (let file of FILES) {
189189
await tsServer.openFile(file.prepareCallHierarchy.filePath);
190190
const provideCallHierarchyOutgoingCallsResponse = await tsServer.provideCallHierarchyOutgoingCalls(file.prepareCallHierarchy.filePath, file.prepareCallHierarchy.line, file.prepareCallHierarchy.offset);
191191
console.log('provideCallHierarchyOutgoingCallsResponse', JSON.stringify(provideCallHierarchyOutgoingCallsResponse));
192+
193+
await tsServer.openFile(file.getLinkedEditingRange.filePath);
194+
const getLinkedEditingRangeResponse = await tsServer.getLinkedEditingRange(file.getLinkedEditingRange.filePath, file.getLinkedEditingRange.line, file.getLinkedEditingRange.offset);
195+
console.log('getLinkedEditingRangeResponse', JSON.stringify(getLinkedEditingRangeResponse));
196+
197+
await tsServer.openFile(file.braceCommand.filePath);
198+
const braceCommandResponse = await tsServer.braceCommand(file.braceCommand.filePath, file.braceCommand.line, file.braceCommand.offset);
199+
console.log('braceCommandResponse', JSON.stringify(braceCommandResponse));
200+
201+
// TODO: see working and non-working use case
202+
await tsServer.openFile(file.braceCommand.filePath);
203+
const braceCompletionResponse = await tsServer.braceCompletion(file.braceCommand.filePath, file.braceCommand.line + 1, file.braceCommand.offset, 'a');
204+
console.log('braceCompletionResponse', JSON.stringify(braceCompletionResponse));
205+
206+
await tsServer.openFile(file.braceCommand.filePath);
207+
const getSpanOfEnclosingCommentResponse = await tsServer.getSpanOfEnclosingComment(file.getSpanOfEnclosingComment.filePath, file.getSpanOfEnclosingComment.line, file.getSpanOfEnclosingComment.offset, file.getSpanOfEnclosingComment.onlyMultiLine);
208+
console.log('getSpanOfEnclosingCommentResponse', JSON.stringify(getSpanOfEnclosingCommentResponse));
192209
}
193-
//tsServer.exitServer();
210+
/*
211+
tsServer.exitServer();
212+
tsServer.killServer();
213+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
3+
const SampleComponent = () => {
4+
return (
5+
<div>
6+
<header>
7+
<h1>Welcome to My App</h1>
8+
</header>
9+
<main>
10+
<section>
11+
<p>This is a paragraph in a section.</p>
12+
</section>
13+
<article>
14+
<p>This is an article.</p>
15+
</article>
16+
</main>
17+
<footer>
18+
<p>Footer content goes here.</p>
19+
</footer>
20+
</div>
21+
);
22+
};
23+
24+
export default SampleComponent;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
3+
const SampleComponent = () => {
4+
return (
5+
<div>
6+
<h1>Welcome to the Sample Component</h1>
7+
<p>This is a paragraph within the sample component.</p>
8+
<input type="text" placeholder="Enter text here" />
9+
<button>Click Me</button>
10+
</div>
11+
);
12+
};
13+
14+
export default SampleComponent;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5", // Specify ECMAScript target version
4+
"lib": [
5+
"dom",
6+
"dom.iterable",
7+
"esnext"
8+
], // List of library files to be included in the compilation
9+
"allowJs": true, // Allow JavaScript files to be compiled
10+
"skipLibCheck": true, // Skip type checking of all declaration files
11+
"esModuleInterop": true, // Disables namespace imports (import * as fs from "fs") and enables CJS/AMD/UMD style imports (import fs from "fs")
12+
"allowSyntheticDefaultImports": true, // Allow default imports from modules with no default export
13+
"strict": true, // Enable all strict type checking options
14+
"forceConsistentCasingInFileNames": true, // Disallow inconsistently-cased references to the same file.
15+
"module": "esnext", // Specify module code generation
16+
"moduleResolution": "node", // Resolve modules using Node.js style
17+
"isolatedModules": true, // Unconditionally emit imports for unresolved files
18+
"resolveJsonModule": true, // Include modules imported with .json extension
19+
"noEmit": true, // Do not emit output (meaning do not compile code, only perform type checking)
20+
"jsx": "react", // Support JSX in .tsx files
21+
"sourceMap": true, // Generate corrresponding .map file
22+
"declaration": true, // Generate corresponding .d.ts file
23+
"noUnusedLocals": true, // Report errors on unused locals
24+
"noUnusedParameters": true, // Report errors on unused parameters
25+
"incremental": true, // Enable incremental compilation by reading/writing information from prior compilations to a file on disk
26+
"noFallthroughCasesInSwitch": true // Report errors for fallthrough cases in switch statement
27+
},
28+
"include": [
29+
"src/**/*" // *** The files TypeScript should type check ***
30+
],
31+
"exclude": ["node_modules", "build"] // *** The files to not type check ***
32+
}

0 commit comments

Comments
 (0)