Skip to content

Commit 8e2e6ff

Browse files
committed
Added log file output, instead of completely spamming cli if you have hundred of jobs. Ensure "hidden" .gitlab-ci-local dir for various files generated. Logs etc. etc.
1 parent 29c5d5d commit 8e2e6ff

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

package-lock.json

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"js-yaml": "3.x",
2424
"pretty-hrtime": "1.x",
2525
"shelljs": "0.x",
26-
"yargs": "15.x"
26+
"yargs": "15.x",
27+
"fs-extra": "8.x"
2728
},
2829
"pkg": {
2930
"targets": [
@@ -34,12 +35,13 @@
3435
"license": "ISC",
3536
"devDependencies": {
3637
"@types/deep-extend": "0.x",
38+
"@types/fs-extra": "8.x",
3739
"@types/js-yaml": "3.x",
3840
"@types/pretty-hrtime": "1.x",
3941
"@types/shelljs": "0.x",
4042
"@types/yaml": "1.x",
4143
"@types/yargs": "15.x",
42-
"npm-check-updates": "4.0.1",
44+
"npm-check-updates": "4.x",
4345
"pkg": "4.x",
4446
"source-map-support": "0.x",
4547
"tslint": "5.x",

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as c from "ansi-colors";
2+
import * as fs from "fs-extra";
3+
import * as yaml from "js-yaml";
24
import * as yargs from "yargs";
35

46
import { Parser } from "./parser";
@@ -28,10 +30,9 @@ const makeid = (length: number): string => {
2830

2931
return result;
3032
};
31-
process.env.CI_PIPELINE_ID = makeid(10);
3233

3334
const argv = yargs.argv;
34-
const cwd = argv.cwd || process.cwd();
35+
const cwd = String(argv.cwd) || process.cwd();
3536
const m: any = argv.m;
3637
const manualArgs: string[] = [].concat(m || []);
3738

@@ -117,6 +118,17 @@ process.on("uncaughtException", (err) => {
117118
});
118119

119120
if (["pipeline", "manual"].includes(firstArg)) {
121+
const pipelinesPath = `${cwd}/.gitlab-ci-local/pipelines.yml`;
122+
const configPath = `${cwd}/.gitlab-ci-local/config.yml`;
123+
fs.ensureFileSync(configPath);
124+
fs.ensureFileSync(pipelinesPath);
125+
let config: any = yaml.safeLoad(fs.readFileSync(pipelinesPath, "utf8"));
126+
if (!config) {
127+
config = {};
128+
}
129+
config.pipelineId = config.pipelineId !== undefined ? Number(config.pipelineId) + 1 : 0;
130+
fs.writeFileSync(pipelinesPath, yaml.safeDump(config), {});
131+
process.env.CI_PIPELINE_ID = config.pipelineId;
120132
runJobs().catch();
121133
} else if (firstArg === "exec") {
122134
runExecJobs().catch();

src/job.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as c from "ansi-colors";
22
import * as deepExtend from "deep-extend";
3+
import * as fs from "fs-extra";
34
import * as glob from "glob";
45
import * as prettyHrtime from "pretty-hrtime";
56
import * as shelljs from "shelljs";
@@ -130,6 +131,10 @@ export class Job {
130131
}
131132

132133
private async exec(script: string): Promise<number> {
134+
const outputFilePath = `${this.cwd}/.gitlab-ci-local/pipe_logs/${this.getEnvs().CI_PIPELINE_ID}/${this.name}.log`;
135+
fs.ensureFileSync(outputFilePath);
136+
fs.truncateSync(outputFilePath);
137+
133138
return new Promise<any>((resolve, reject) => {
134139
const child = shelljs.exec(`${script}`, {
135140
cwd: this.cwd,
@@ -145,25 +150,12 @@ export class Job {
145150

146151
if (child.stdout) {
147152
child.stdout.on("data", (buf) => {
148-
const lines = `${buf}`.split(/\r?\n/);
149-
lines.forEach((l) => {
150-
if (!l) {
151-
return;
152-
}
153-
process.stdout.write(`${c.blueBright(`${this.name}`)} ${c.greenBright(">")} ${l}\n`);
154-
});
153+
fs.appendFileSync(outputFilePath, `${buf}`);
155154
});
156155
}
157-
158156
if (child.stderr) {
159157
child.stderr.on("data", (buf) => {
160-
const lines = `${buf}`.split(/\r?\n/);
161-
lines.forEach((l) => {
162-
if (!l) {
163-
return;
164-
}
165-
process.stderr.write(`${c.blueBright(`${this.name}`)} ${c.redBright(">")} ${c.red(`${l}`)}\n`);
166-
});
158+
fs.appendFileSync(outputFilePath, `${c.red(`${buf}`)}`);
167159
});
168160
}
169161

src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as c from "ansi-colors";
22
import * as deepExtend from "deep-extend";
3-
import * as fs from "fs";
3+
import * as fs from "fs-extra";
44
import * as yaml from "js-yaml";
55

66
import { Job } from "./job";

0 commit comments

Comments
 (0)