Skip to content

Commit 811ac42

Browse files
committed
refactor: Move all code to typescript
1 parent 964fbac commit 811ac42

23 files changed

+2636
-912
lines changed

.circleci/config.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ jobs:
1515
- ./node_modules
1616
- run: npm run build
1717
- run: npm run coverage
18-
- run: npm run check-coverage
19-
- run: npm run report
2018

2119
build-latest: &latest-build
2220
docker:
@@ -33,7 +31,7 @@ jobs:
3331
- ./node_modules
3432
- run: yarn run test
3533
- run: yarn run lint
36-
- run: yarn run coverage-lcov
34+
- run: npm run coverage
3735
- run: yarn run codacy
3836

3937
build-node_8:
@@ -52,10 +50,15 @@ jobs:
5250
- image: node:10
5351

5452
build-node_11:
55-
<<: *latest-build
53+
<<: *common-build
5654
docker:
5755
- image: node:11
5856

57+
build-node_12:
58+
<<: *latest-build
59+
docker:
60+
- image: node:12
61+
5962
workflows:
6063
version: 2
6164
build:
@@ -64,3 +67,4 @@ workflows:
6467
- build-node_9
6568
- build-node_10
6669
- build-node_11
70+
- build-node_12

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Skip bin and coverage folders
22
bin/**
33
coverage/**
4-
built/**
4+
build/**

.eslintrc.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"parser": "@typescript-eslint/parser",
23
"parserOptions": {
34
"ecmaVersion": 6,
45
"ecmaFeatures": {
@@ -22,13 +23,17 @@
2223
},
2324
"plugins": [
2425
"standard",
25-
"promise"
26+
"promise",
27+
"@typescript-eslint"
2628
],
2729
"globals": {
2830
"document": false,
2931
"navigator": false,
3032
"window": false
3133
},
34+
"extends": [
35+
"plugin:@typescript-eslint/recommended"
36+
],
3237
"rules": {
3338
"accessor-pairs": 2,
3439
"arrow-spacing": [
@@ -341,4 +346,4 @@
341346
"unix"
342347
]
343348
}
344-
}
349+
}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ coverage/
2828
bower_components/
2929

3030
# TypeScript
31-
built/
31+
build/

CONTRIBUTING.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
* After creating your pull request make sure the build is passing on [CircleCI](https://circleci.com/gh/rtfpessoa/diff2html-cli)
1818
and that [Codacy](https://www.codacy.com/app/Codacy/diff2html-cli) is also confident in the code quality.
1919

20-
* In your pull request, do not commit the `dist` or `build` folder if you needed to build the release files.
21-
2220
### Commit Style
2321

2422
Writing good commit logs is important. A commit log should describe what changed and why.

bin/diff2html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env node
22

3-
require("../src/main.js");
3+
require("../build/main.js");

jest.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
'verbose': true,
3+
'preset': 'ts-jest',
4+
'testEnvironment': 'node',
5+
'coverageDirectory': './coverage',
6+
'coverageReporters': ['lcov', 'text', 'html'],
7+
'coverageThreshold': {
8+
'global': {
9+
'statements': 40,
10+
'branches': 15,
11+
'functions': 33,
12+
'lines': 40
13+
}
14+
}
15+
};

package.json

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,21 @@
3636
},
3737
"preferGlobal": true,
3838
"scripts": {
39+
"prepare": "yarn run build",
3940
"build": "tsc",
4041
"lint": "eslint .",
4142
"style": "yarn run lint",
42-
"test": "yarn build && yarn run coverage",
43-
"coverage": "nyc mocha",
44-
"coverage-html": "nyc report --reporter=html && open ./coverage/index.html",
45-
"check-coverage": "nyc check-coverage --statements 35 --functions 35 --branches 0 --lines 35",
46-
"report": "nyc report",
47-
"coverage-lcov": "nyc report --reporter=lcov",
43+
"test": "jest",
44+
"coverage": "jest --collectCoverage",
45+
"coverage-html": "yarn run coverage && open ./coverage/index.html",
4846
"codacy": "cat ./coverage/lcov.info | codacy-coverage",
4947
"preversion": "yarn run test",
5048
"postversion": "git push && git push --tags"
5149
},
5250
"bin": {
5351
"diff2html": "./bin/diff2html"
5452
},
55-
"main": "./src/main.js",
53+
"main": "./build/main.js",
5654
"dependencies": {
5755
"clipboardy": "^2.1.0",
5856
"diff2html": "^2.11.2",
@@ -62,20 +60,24 @@
6260
"yargs": "^14.0.0"
6361
},
6462
"devDependencies": {
63+
"@types/diff2html": "0.0.5",
64+
"@types/jest": "24.0.18",
65+
"@types/node": "^11.9.4",
66+
"@types/request": "2.48.2",
67+
"@typescript-eslint/eslint-plugin": "2.0.0",
68+
"@typescript-eslint/parser": "2.0.0",
6569
"codacy-coverage": "^3.4.0",
66-
"eslint": "^6.2.1",
70+
"eslint": "6.2.1",
6771
"eslint-plugin-promise": "^4.2.1",
6872
"eslint-plugin-standard": "^4.0.1",
69-
"mocha": "^6.2.0",
70-
"nyc": "^14.1.1",
71-
"sinon": "^7.4.1",
72-
"@types/node": "^11.9.4",
73+
"jest": "24.9.0",
74+
"ts-jest": "24.0.2",
7375
"typescript": "^3.3.3"
7476
},
7577
"license": "MIT",
7678
"files": [
7779
"bin",
78-
"src",
79-
"dist"
80+
"build",
81+
"template.html"
8082
]
81-
}
83+
}

src/__tests__/cli-tests.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as cli from '../cli';
2+
import * as http from '../http-utils';
3+
import * as utils from '../utils';
4+
5+
beforeEach(() => jest.clearAllMocks());
6+
7+
describe('cli', () => {
8+
describe('getInput', () => {
9+
test('should readFile when inputType is `file`', () => {
10+
jest.spyOn(utils, 'readFile').mockImplementationOnce(() => { });
11+
12+
cli.getInput('file', ['lol', 'foo'], [], 'callback');
13+
14+
expect(utils.readFile).toBeCalledTimes(1);
15+
expect(utils.readFile).toBeCalledWith('lol', 'callback');
16+
});
17+
18+
test('should readStdin when inputType is `stdin`', () => {
19+
jest.spyOn(utils, 'readStdin').mockImplementationOnce(() => { });
20+
21+
cli.getInput('stdin', ['lol'], [], 'callback');
22+
23+
expect(utils.readStdin).toBeCalledTimes(1);
24+
expect(utils.readStdin).toBeCalledWith('callback');
25+
});
26+
27+
test('should readStdin when inputType is `command`', () => {
28+
jest.spyOn(cli, 'runGitDiff').mockImplementationOnce(() => { });
29+
30+
cli.getInput('command', ['lol', 'foo'], [], 'callback');
31+
32+
expect(cli.runGitDiff).toBeCalledTimes(1);
33+
expect(cli.runGitDiff).toBeCalledWith(['lol', 'foo'], [], 'callback');
34+
});
35+
});
36+
37+
describe('preview', () => {
38+
test('should call `utils.writeFile`', () => {
39+
jest.spyOn(utils, 'writeFile').mockImplementationOnce(() => { });
40+
41+
cli.preview('a', 'b');
42+
43+
expect(utils.writeFile).toBeCalledTimes(1);
44+
});
45+
});
46+
47+
describe('postToDiffy', () => {
48+
test('should call `http.put`', () => {
49+
jest.spyOn(http, 'put').mockImplementationOnce(() => Promise.resolve('http://example.com'));
50+
51+
cli.postToDiffy('a', 'print');
52+
53+
expect(http.put).toBeCalledTimes(1);
54+
expect(http.put).toBeCalledWith('https://diffy.org/api/diff/', { diff: 'a' });
55+
});
56+
});
57+
});

src/__tests__/utils-tests.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as utils from '../utils';
2+
3+
beforeEach(() => jest.clearAllMocks());
4+
5+
describe('utils', () => {
6+
describe('io', () => {
7+
var stringToCompare = 'This is a rand0m preTTy string to write and read a file';
8+
9+
test('should write and read file synchronously', () => {
10+
utils.writeFile('/tmp/file.test', stringToCompare);
11+
12+
var contentRead = utils.readFileSync('/tmp/file.test');
13+
14+
expect(stringToCompare).toBe(contentRead);
15+
});
16+
17+
test('should write synchronously and read file asynchronously', (done) => {
18+
utils.writeFile('/tmp/file.test', stringToCompare);
19+
20+
utils.readFile('/tmp/file.test', function (err, content) {
21+
expect(err).toBe(null);
22+
expect(stringToCompare).toBe(content);
23+
done();
24+
});
25+
});
26+
});
27+
});

0 commit comments

Comments
 (0)