Skip to content

Commit 14bf51f

Browse files
vladjercatoxik
authored andcommitted
ci: integrate public export checks
1 parent ec6994e commit 14bf51f

File tree

9 files changed

+344
-1526
lines changed

9 files changed

+344
-1526
lines changed

.build/check-public-exports/index.ts

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@ import {
1111
} from 'path';
1212
import * as ts from 'typescript';
1313

14+
import {
15+
getPullRequestHead,
16+
statusReporterFactory,
17+
} from '../common/github';
18+
19+
const reportStatus = statusReporterFactory('public-api-check');
20+
1421
const {
1522
red,
1623
cyan,
1724
green,
25+
grey,
1826
} = chalk.default;
1927

2028
const ROOT = resolve(__dirname, '../..');
2129
const BARREL = 'index.ts';
2230

2331
const resolveParentDir = (path) => dirname(resolve(ROOT, path));
24-
2532
interface IExportPair {
2633
exportLocation: string;
2734
exported: string;
@@ -39,6 +46,12 @@ interface IDecoratedError extends IDecoratedClass {
3946

4047
let isErrorFlagged = false;
4148

49+
const _handleUnexpectedError = (err?: string) => {
50+
if (err) { console.error(err); }
51+
52+
process.exit(1);
53+
};
54+
4255
const buildError = ({ className, decoratorName, exportLocation, rootPath }: IDecoratedError, message: string) => {
4356
return [
4457
red(`ERROR:`),
@@ -144,7 +157,7 @@ const resolveFileLocation = (exportLocation: string, path: string) => {
144157
return resolve(parentDir, fileName);
145158
};
146159

147-
const checkForUnamedDecoratedExports = (rootPath: string, path: string, exportList: IExportPair[]) =>
160+
const checkForUnnamedDecoratedExports = (rootPath: string, path: string, exportList: IExportPair[]) =>
148161
exportList
149162
.filter(e => !e.exported)
150163
.forEach(({ exportLocation }) => {
@@ -171,7 +184,7 @@ const checkForUnamedDecoratedExports = (rootPath: string, path: string, exportLi
171184
}),
172185
);
173186
} else {
174-
checkForUnamedDecoratedExports(rootPath, fileLocation, exportDeclarations);
187+
checkForUnnamedDecoratedExports(rootPath, fileLocation, exportDeclarations);
175188
}
176189
});
177190

@@ -229,22 +242,53 @@ const checkFile = (path: string) => {
229242
const sourceFile = readFileNode(path);
230243
const rootExports = getExportDeclarations(sourceFile);
231244

232-
checkForUnamedDecoratedExports(path, path, rootExports);
245+
console.log(`${grey('Checking')} ${cyan(sourceFile.fileName)} ${grey('for unnamed decorated exports...')}`);
246+
checkForUnnamedDecoratedExports(path, path, rootExports);
247+
248+
console.log(`${grey('Checking')} ${cyan(sourceFile.fileName)} ${grey('decorated exports hidden behind barrels...')}`);
233249
checkForBarreledDecoratedExports(path, path, rootExports);
234250
};
235251

236-
glob(
237-
'./projects/**/public_api.ts',
238-
{ root: ROOT },
239-
(err, files) => {
240-
if (err) { throw err; }
252+
const forEachApiFile = (callback: (string) => void) => {
253+
return new Promise((res, rej) => {
254+
const walker = new glob.Glob('./projects/**/public_api.ts', {
255+
root: ROOT,
256+
}, (err, files) => {
257+
if (err) {
258+
console.error(err);
259+
rej(err);
260+
}
241261

242-
files.forEach(file => {
243-
checkFile(file);
262+
files.forEach(callback);
244263
});
245264

246-
if (isErrorFlagged) {
247-
process.exit(1);
248-
}
249-
},
250-
);
265+
walker.on('end', res);
266+
});
267+
};
268+
269+
const run = async () => {
270+
const head = await getPullRequestHead();
271+
272+
await reportStatus('pending', head.sha, 'Checking Public API...')
273+
.catch(_handleUnexpectedError);
274+
275+
await forEachApiFile(checkFile);
276+
277+
if (isErrorFlagged) {
278+
console.error(red(`⛔ Something doesn't check out`));
279+
280+
await reportStatus('error', head.sha, 'npm run check:exports for more info...')
281+
.catch(_handleUnexpectedError);
282+
} else {
283+
console.log(green(`💯 Good to go!`));
284+
285+
await reportStatus('success', head.sha, '✔ Good to go!')
286+
.catch(_handleUnexpectedError);
287+
}
288+
};
289+
290+
(async () => {
291+
await run()
292+
.catch(_handleUnexpectedError);
293+
})();
294+

.build/commitlint/index.js

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,23 @@
11
/**
22
* Adapted from: https://github.com/fathyb/commitlint-circle
33
*/
4-
const Octokit = require('@octokit/rest')
54
const { lint, load } = require('@commitlint/core')
65
const path = require('path')
6+
const { red, yellow, blue, green } = require('chalk');
77

8-
const { variables } = require('../common')
9-
const { owner, repo, pull, token } = variables;
10-
11-
const github = new Octokit({
12-
auth: token,
13-
})
8+
const { getPullRequestHead, getCommits, statusReporterFactory } = require('../common/github');
149

1510
const _handleUnexpectedError = (err) => {
1611
console.error(err)
1712
process.exit(1)
1813
}
1914

20-
const getCommits = async () => {
21-
console.log('📡 Looking up commits for PR #%s...', pull)
22-
const response = await github.pulls.listCommits({
23-
owner,
24-
repo,
25-
pull_number: pull,
26-
per_page: 100
27-
})
28-
29-
return response.data
30-
}
31-
32-
const reportStatus = async (state, sha, description, target_url) => {
33-
await github.repos.createStatus({
34-
owner,
35-
repo,
36-
sha,
37-
state,
38-
description,
39-
context: 'commitlint',
40-
target_url,
41-
})
42-
}
43-
44-
const getPullRequestHead = async () => {
45-
const pr = await github.pullRequests.get({
46-
owner,
47-
repo,
48-
pull_number: pull,
49-
})
50-
51-
return pr.data.head
52-
}
15+
const reportStatus = statusReporterFactory('commitlint')
5316

5417
const run = async () => {
5518
const head = await getPullRequestHead()
5619

57-
console.log('👮‍ Calling Git Police...')
20+
console.log(blue('👮‍ Calling Git Police...'))
5821

5922
await reportStatus('pending', head.sha, 'Checking commits...')
6023

@@ -69,13 +32,11 @@ const run = async () => {
6932
const isConventionalCommitGuidelineRespected = lintResultList.every(result => result.valid)
7033
const isAnyFixupCommit = commits.some(({ commit }) => commit.message.startsWith('fixup!'))
7134

72-
console.log(isConventionalCommitGuidelineRespected)
73-
7435
if (
7536
isConventionalCommitGuidelineRespected &&
7637
!isAnyFixupCommit
7738
) {
78-
console.log(`💯 Good to go!`)
39+
console.log(green(`💯 Good to go!`))
7940

8041
await reportStatus('success', head.sha, '✔ Good to go!')
8142
.catch(_handleUnexpectedError)
@@ -84,15 +45,15 @@ const run = async () => {
8445
}
8546

8647
if (isAnyFixupCommit) {
87-
console.log(`😬 There are still some fixup commits.`)
48+
console.warn(yellow(`😬 There are still some fixup commits.`))
8849

8950
await reportStatus('error', head.sha, 'Please rebase and apply the fixup commits!')
9051
.catch(_handleUnexpectedError)
9152

9253
return;
9354
}
9455

95-
console.log(`⛔ Something doesn't check out`)
56+
console.error(red(`⛔ Something doesn't check out`))
9657

9758
await reportStatus('error', head.sha, 'We use conventional commits (╯°□°)╯︵ ┻━┻', 'https://www.conventionalcommits.org/en/v1.0.0-beta.4/')
9859
.catch(_handleUnexpectedError)

0 commit comments

Comments
 (0)