Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 404b0e3

Browse files
chore: Save package statistics to DB (#821)
* Add stats:save task * Save stats in CI * Address PR comments
1 parent 2d74195 commit 404b0e3

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

.circleci/config.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ jobs:
7474
command: yarn test:projects
7575
- run:
7676
name: Bundle Statistics
77-
command: yarn build:stats
77+
command: yarn stats:build
78+
- run:
79+
name: Save Statistics to DB
80+
command: |
81+
if [ -n "${STATS_URI}" ]; then
82+
yarn stats:save
83+
else
84+
echo "STATS_URI not set, skipping"
85+
fi
7886
- run:
7987
name: Danger JS
8088
command: |
@@ -84,7 +92,7 @@ jobs:
8492
- run:
8593
name: Publish npm package
8694
command: |
87-
if [ -n "${npm_TOKEN+x}" ] && [ "${CIRCLE_BRANCH}" == "master" ]; then
95+
if [ -n "${npm_TOKEN}" ] && [ "${CIRCLE_BRANCH}" == "master" ]; then
8896
echo "//registry.npmjs.org/:_authToken=${npm_TOKEN}" > ~/project/.npmrc
8997
npm version prerelease --preid="${CIRCLE_BUILD_NUM}.${CIRCLE_BRANCH}.`git rev-parse --short HEAD`" --no-git-tag-version
9098
npm publish --tag current

build/gulp/tasks/stats.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { task, parallel, series } from 'gulp'
33
import * as _ from 'lodash'
44
import * as webpack from 'webpack'
55
import * as stableStringify from 'json-stable-stringify-without-jsonify'
6+
import { argv } from 'yargs'
7+
import * as requestHttp from 'request-promise-native'
68

79
import config from '../../../config'
810

@@ -101,7 +103,7 @@ function updateStatsFile(filePath: string, currentBundleStats: any) {
101103
)
102104
}
103105

104-
task('build:stats:bundle', async () => {
106+
task('stats:build:bundle', async () => {
105107
process.env.NODE_ENV = 'build'
106108
const webpackStatsConfig = require('../../webpack.config.stats').default
107109

@@ -118,6 +120,39 @@ task(
118120
'stats',
119121
series(
120122
parallel(series('clean:dist:es', 'build:dist:es'), 'build:docs:component-info'),
121-
'build:stats:bundle',
123+
'stats:build:bundle',
122124
),
123125
)
126+
127+
task('stats:save', async () => {
128+
const commandLineArgs = _.pick(argv, ['sha', 'branch', 'tag', 'pr', 'build'])
129+
const bundleStats = require(paths.docsSrc('bundleStats.json'))[UNRELEASED_VERSION_STRING]
130+
131+
const statsPayload = {
132+
sha: process.env.CIRCLE_SHA1,
133+
branch: process.env.CIRCLE_BRANCH,
134+
tag: process.env.CIRCLE_TAG, // optional
135+
pr: process.env.CIRCLE_PULL_REQUEST, // optional
136+
build: process.env.CIRCLE_BUILD_NUM,
137+
...commandLineArgs, // allow command line overwrites
138+
bundleSize: bundleStats,
139+
ts: new Date(),
140+
}
141+
142+
// payload sanity check
143+
_.forEach(['sha', 'branch', 'build', 'bundleSize'], fieldName => {
144+
if (statsPayload[fieldName] === undefined) {
145+
throw `Required field '${fieldName}' not set in stats payload`
146+
}
147+
})
148+
149+
const options = {
150+
method: 'POST',
151+
uri: process.env.STATS_URI,
152+
body: statsPayload,
153+
json: true,
154+
}
155+
156+
const response = await requestHttp(options)
157+
console.log(response)
158+
})

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
],
1313
"scripts": {
1414
"build": "gulp build",
15-
"build:stats": "gulp stats",
1615
"build:docs": "gulp --series dll build:docs",
1716
"build:dist": "gulp --series build:dist",
1817
"ci": "yarn lint && yarn prettier && yarn test --strict",
@@ -34,6 +33,8 @@
3433
"release:patch": "yarn prerelease && ta-script npm/release patch && yarn postrelease",
3534
"prestart": "yarn satisfied --fix yarn",
3635
"start": "gulp --series dll docs",
36+
"stats:build": "gulp stats",
37+
"stats:save": "gulp stats:save",
3738
"satisfied": "satisfied --skip-invalid",
3839
"pretest": "yarn satisfied",
3940
"test": "gulp test",
@@ -147,6 +148,7 @@
147148
"react-router-dom": "^4.1.2",
148149
"react-source-render": "^2.0.0-beta.4",
149150
"react-test-renderer": "^16.0.0",
151+
"request-promise-native": "^1.0.5",
150152
"rimraf": "^2.6.1",
151153
"satisfied": "^1.1.1",
152154
"screener-runner": "^0.10.7",

0 commit comments

Comments
 (0)