UNPKG

1.96 kBJavaScriptView Raw
1const fs = require('fs');
2const Promise = require('bluebird');
3const path = require('path');
4
5const Analyzer = require('./analyzer');
6
7const clone = require('./analyzer/steps/clone');
8const build = require('./analyzer/steps/build');
9const push = require('./analyzer/steps/push');
10const packageJson = require('./analyzer/steps/package.json');
11const sendToSlack = require('./analyzer/steps/slack.notifier');
12
13
14const stages = require('./analyzer/header/stages');
15const stagesEnricher = require('./analyzer/enricher/stages');
16const codefreshYaml = require('./analyzer/transform/codefresh.yaml');
17
18function isValidContext(context){
19 const { credentials, repo: { repoOwner, repoName }} = context;
20 return credentials && repoOwner && repoName;
21}
22
23async function _defaultYaml(){
24 const defaultYaml = await Promise.fromCallback((cb) => fs.readFile(path.resolve(__dirname, 'default.yaml'), cb));
25 return defaultYaml.toString();
26}
27
28module.exports = {
29
30 'onboarding': async (context) => {
31
32 const analyzer = new Analyzer();
33
34 // if user logged in with google for example and havent repo info
35 if(!isValidContext(context)){
36 return _defaultYaml();
37 }
38 try{
39 await analyzer.init(context);
40 }
41 catch (e) {
42 console.error(e);
43 return _defaultYaml();
44 }
45 return analyzer
46 .step(clone)
47 .step(build)
48 .step(push)
49 .header(stages)
50 .enrich(stagesEnricher)
51 .resolve();
52 },
53
54 'api': async (context) => {
55 const analyzer = new Analyzer();
56
57 await analyzer.init(context);
58 return analyzer
59 .step(clone)
60 .step(build)
61 .step(push)
62 .step(packageJson)
63 .step(sendToSlack)
64 .header(stages)
65 .enrich(stagesEnricher)
66 .transform(codefreshYaml)
67 .resolve();
68 }
69
70};