1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const os = require("os");
|
4 | const path = require("path");
|
5 | const chalk_1 = require("chalk");
|
6 | const bunyan = require("bunyan");
|
7 | const minimist = require("minimist");
|
8 | const errors_1 = require("./errors");
|
9 | const env_1 = require("./lib/env");
|
10 | const registry_1 = require("./lib/registry");
|
11 | const index_1 = require("./jobs/clean-up/index");
|
12 | const index_2 = require("./jobs/fetch-updates/index");
|
13 | const index_3 = require("./jobs/build-app/index");
|
14 | const index_4 = require("./jobs/build-android/index");
|
15 | const registry = new registry_1.JobRegistry();
|
16 | registry.register('clean-up', index_1.run);
|
17 | registry.register('fetch-updates', index_2.run);
|
18 | registry.register('build-app', index_3.run);
|
19 | registry.register('build-android', index_4.run);
|
20 | function getJobId() {
|
21 | const id = Number(env_1.env.require('IONIC_JOB_ID'));
|
22 | if (isNaN(id)) {
|
23 | throw new Error('IONIC_JOB_ID invalid');
|
24 | }
|
25 | return id;
|
26 | }
|
27 | async function createJobContext({ name, log, id }) {
|
28 | const bindir = env_1.env.require('IONIC_JOBS_BIN');
|
29 | const joblog = log.child({ job: name });
|
30 | const serverlog = joblog.child({ type: 'server' });
|
31 | const userlog = joblog.child({ type: 'user' });
|
32 | return {
|
33 | id,
|
34 | argv: process.argv,
|
35 | env: env_1.env,
|
36 | c: new chalk_1.default.constructor({ enabled: true }),
|
37 | serverlog,
|
38 | userlog,
|
39 | bindir: path.resolve(bindir),
|
40 | codedir: process.cwd(),
|
41 | };
|
42 | }
|
43 | async function execjob({ name, log, id }) {
|
44 | const job = registry.get(name);
|
45 | const ctx = await createJobContext({ name, log, id });
|
46 | ctx.userlog.info(ctx.c.cyan(`Running Stage ${name} for Job: ${id}`));
|
47 | try {
|
48 | await job(ctx);
|
49 | }
|
50 | catch (e) {
|
51 | if (e instanceof errors_1.JobException && e.type === 'user') {
|
52 | ctx.userlog.error(ctx.c.red(e.msg));
|
53 | }
|
54 | throw e;
|
55 | }
|
56 | }
|
57 | async function run() {
|
58 | const logfile = env_1.env.get('IONIC_JOBS_LOG', path.resolve(os.homedir(), 'logs', 'jobs.log'));
|
59 | const id = getJobId();
|
60 | const rootlog = bunyan.createLogger({
|
61 | name: 'rootlog',
|
62 | type: 'root',
|
63 | job_id: id,
|
64 | app_id: env_1.env.require('APP_ID'),
|
65 | streams: [
|
66 | {
|
67 | stream: process.stdout,
|
68 | level: bunyan.DEBUG,
|
69 | },
|
70 | {
|
71 | type: 'rotating-file',
|
72 | path: logfile,
|
73 | count: 28,
|
74 | },
|
75 | ],
|
76 | });
|
77 | const args = minimist(process.argv.slice(2));
|
78 | const jobName = args._[0];
|
79 | try {
|
80 | if (!jobName) {
|
81 | throw new Error(`Must provide job type.`);
|
82 | }
|
83 | rootlog.info(`Job start: ${jobName}.`);
|
84 | await execjob({ name: jobName, log: rootlog, id });
|
85 | rootlog.info(`Job succeeded: ${jobName}.`);
|
86 | }
|
87 | catch (e) {
|
88 | rootlog.error(e);
|
89 | rootlog.error(`Job failed: ${jobName}.`);
|
90 | process.exitCode = 1;
|
91 | }
|
92 | }
|
93 | exports.run = run;
|