|
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 |
4 | 2 |
|
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. |
6 | 4 |
|
7 | 5 | ## Code Guardian |
8 | | -[](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:  |
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 | +[](https://github.com/aicore/template-nodejs/actions/workflows/build_verify.yml) |
33 | 7 |
|
34 | 8 | # Commands available |
35 | 9 |
|
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: |
38 | 12 | ```shell |
39 | 13 | > 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 | | - |
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  |
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 |
168 | 16 |
|
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. |
172 | 18 |
|
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 |
177 | 20 |
|
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: |
180 | 22 |
|
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(); |
0 commit comments