UNPKG

6.01 kBJavaScriptView Raw
1var fs = require('fs');
2var path = require('path');
3var _ = require('lodash');
4var getSize = require('get-folder-size');
5var gzipSize = require('gzip-size');
6var validateMessage = require('validate-commit-msg');
7
8//simple regex matcher to detect usage of helper function and its type signature
9var hotMatch = /\bhot\(/gi;
10var hotSignatureMatch = /\bimport \{.*?hot.*?\} from '.*?\/helpers\/marble-testing'/gi;
11
12var coldMatch = /\bcold\(/gi;
13var coldSignatureMatch = /\bimport \{.*?cold.*?\} from '.*?\/helpers\/marble-testing'/gi;
14
15var errorCount = 0;
16
17// Warn when PR size is large
18var bigPRThreshold = 600;
19if (danger.github.pr.additions + danger.github.pr.deletions > bigPRThreshold) {
20 warn(':exclamation: Big PR (' + ++errorCount + ')');
21 markdown('> (' + errorCount + ') : Pull Request size seems relatively large. If Pull Request contains multiple changes, split each into separate PR will helps faster, easier review.');
22}
23
24// Check test exclusion (.only) is included
25var modifiedSpecFiles = danger.git.modified_files.filter(function (filePath) {
26 return filePath.match(/-spec.(js|jsx|ts|tsx)$/gi);
27});
28
29var testFilesIncludeExclusion = modifiedSpecFiles.reduce(function (acc, value) {
30 var content = fs.readFileSync(value).toString();
31 var invalid = _.includes(content, 'it.only') || _.includes(content, 'describe.only');
32 if (invalid) {
33 acc.push(path.basename(value));
34 }
35 return acc;
36}, []);
37
38if (testFilesIncludeExclusion.length > 0) {
39 fail('an \`only\` was left in tests (' + testFilesIncludeExclusion + ')');
40}
41
42// Check test cases missing type signature import for test marble helper functions
43var testFilesMissingTypes = modifiedSpecFiles.reduce(function (acc, value) {
44 var content = fs.readFileSync(value).toString();
45
46 var hotFnMatchesWithoutTypes = content.match(hotMatch) && !content.match(hotSignatureMatch);
47 var coldFnMatchesWithoutTypes = content.match(coldMatch) && !content.match(coldSignatureMatch);
48
49 if (hotFnMatchesWithoutTypes || coldFnMatchesWithoutTypes) {
50 acc.push(path.basename(value));
51 }
52
53 return acc;
54}, []);
55
56if (testFilesMissingTypes.length > 0) {
57 fail('missing type definition import in tests (' + testFilesMissingTypes + ') (' + ++errorCount + ')');
58 markdown('> (' + errorCount + ') : It seems updated test cases uses test scheduler interface `hot`, `cold` but miss to import type signature for those.');
59}
60
61//validate commit message in PR if it conforms conventional change log, notify if it doesn't.
62var messageConventionValid = danger.git.commits.reduce(function (acc, value) {
63 var valid = validateMessage(value.message);
64 return valid && acc;
65}, true);
66
67if (!messageConventionValid) {
68 warn('commit message does not follows conventional change log (' + ++errorCount + ')');
69 markdown('> (' + errorCount + ') : RxJS uses conventional change log to generate changelog automatically. It seems some of commit messages are not following those, please check [contributing guideline](https://github.com/ReactiveX/rxjs/blob/master/CONTRIBUTING.md#commit-message-format) and update commit messages.');
70}
71
72// TODO(benlesh): update script to run against proper global files
73// The name has changed to `rxjs.umd.js` from `Rx.js`
74
75// function getKB(size) {
76// return (size / 1024).toFixed(1);
77// }
78
79// function getFormattedKB(size) {
80// if (size < 0) {
81// return '-' + size.toString();
82// } else if (size > 0) {
83// return '+' + size.toString();
84// }
85// return size.toString();
86// }
87
88// var globalFile = 'Rx.js';
89// var minFile = 'Rx.min.js';
90
91// function sizeDiffBadge(name, value) {
92// var color = 'lightgrey';
93// if (value > 0) {
94// color = 'red';
95// } else if (value < 0) {
96// color = 'green';
97// }
98// return 'https://img.shields.io/badge/' + name + '-' + getFormattedKB(getKB(value)) + 'KB-' + color + '.svg?style=flat-square';
99// }
100
101// //post size of build
102// schedule(new Promise(function (res) {
103// getSize('./dist/cjs', function (e, result) {
104// var localGlobalFile = path.resolve('./dist/global', globalFile);
105// var localMinFile = path.resolve('./dist/global', minFile);
106
107// //get sizes of PR build
108// var global = fs.statSync(localGlobalFile);
109// var global_gzip = gzipSize.sync(fs.readFileSync(localGlobalFile, 'utf8'));
110// var min = fs.statSync(localMinFile);
111// var min_gzip = gzipSize.sync(fs.readFileSync(localMinFile, 'utf8'));
112
113// //resolve path to release build
114// var releasePath = path.dirname(require.resolve(require.resolve('rxjs')));
115// var bundlePath = path.resolve(releasePath, 'bundles');
116// var bundleGlobalFile = path.resolve(bundlePath, globalFile);
117// var bundleMinFile = path.resolve(bundlePath, minFile);
118
119// var packagePath = path.resolve(releasePath, 'package.json');
120// var releaseVersion = require(packagePath).version;
121
122// //get sizes of release build
123// var bundleGlobal = fs.statSync(bundleGlobalFile);
124// var bundle_global_gzip = gzipSize.sync(fs.readFileSync(bundleGlobalFile, 'utf8'));
125// var bundleMin = fs.statSync(bundleMinFile);
126// var bundle_min_gzip = gzipSize.sync(fs.readFileSync(bundleMinFile, 'utf8'));
127
128// var sizeMessage = '<img src="https://img.shields.io/badge/Size%20Diff%20%28' + releaseVersion + '%29--lightgrey.svg?style=flat-square"/> ';
129// sizeMessage += '<img src="' + sizeDiffBadge('Global', global.size - bundleGlobal.size) + '"/> ';
130// sizeMessage += '<img src="' + sizeDiffBadge('Global(gzip)', global_gzip - bundle_global_gzip) + '"/> ';
131// sizeMessage += '<img src="' + sizeDiffBadge('Min', min.size - bundleMin.size) + '"/> ';
132// sizeMessage += '<img src="' + sizeDiffBadge('Min (gzip)', min_gzip - bundle_min_gzip) + '"/> ';
133// message(sizeMessage);
134
135// markdown('> CJS: **' + getKB(result) +
136// '**KB, global: **' + getKB(global.size) +
137// '**KB (gzipped: **' + getKB(global_gzip) +
138// '**KB), min: **' + getKB(min.size) +
139// '**KB (gzipped: **' + getKB(min_gzip) + '**KB)');
140
141// res();
142// });
143// }));