Skip to content

Commit

Permalink
Use yargs more. Fix audit failure. Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
firecow committed Mar 21, 2020
1 parent 165c4c2 commit 2884c2f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 44 deletions.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ It lets you simulate a CI pipeline on your local machine.
* [Scripts](#scripts)
* [Build binaries](#build-binaries)
* [TODO](#todo)
* [Known Bugs](#known-bugs)
* [Features](#features)
* [Unsupported tags, will be implemented in order](#unsupported-tags-will-be-implemented-in-order)
* [Docker specfic tags. (Only shell working now)](#docker-specfic-tags-only-shell-working-now)
* [Gitlab CI only, will not be used by gitlab-ci-local](#gitlab-ci-only-will-not-be-used-by-gitlab-ci-local)
* [Will not be implemented](#will-not-be-implemented)
* [Undecided](#undecided)

# Installation
Expand Down Expand Up @@ -57,7 +55,30 @@ TODO: Fill this
### Bash alias
$ echo "alias gcl='gitlab-ci-local'" >> ~/.bashrc
### Bash completion
TODO: Fill this

Add this to `~/.bashrc`
```
_yargs_completions()
{
local cur_word args type_list
cur_word="${COMP_WORDS[COMP_CWORD]}"
args=("${COMP_WORDS[@]}")
# ask yargs to generate completions.
type_list=$(/usr/local/bin/gitlab-ci-local --get-yargs-completions "${args[@]}")
COMPREPLY=( $(compgen -W "${type_list}" -- ${cur_word}) )
# if no match was found, fall back to filename completion
if [ ${#COMPREPLY[@]} -eq 0 ]; then
COMPREPLY=()
fi
return 0
}
complete -o default -F _yargs_completions gitlab-ci-local
```

## Quirks
### Artifacts
Expand All @@ -82,6 +103,7 @@ Artifacts works right now, as along as you don't overwrite tracked files and as

## Unsupported tags, will be implemented in order
- rules
- environment
- when:always
- when:on_failure
- when:delayed
Expand All @@ -97,12 +119,11 @@ Artifacts works right now, as along as you don't overwrite tracked files and as
- services
- image

## Gitlab CI only, will not be used by gitlab-ci-local
## Will not be implemented
- cache
- pages
- resource_group
- interruptible
- environment

## Undecided
- artifacts (reset/restore/uploads "files" from job to job)
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 32 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ Array.prototype.first = function() {

yargs
.alias("v", "version")
.version("3.0.6");
.version("3.0.6")
.command("exec [name]", "Run a single job")
.command("manual [names..]", "Run manual jobs during the pipeline")
.command("list", "List all jobs, with detailed info")
.options("cwd", {type: "string", description: "Path to a gitlab-ci.yml", requiresArg: true});

const argv = yargs.argv;
const argv: any = yargs.argv;
const cwd = String(argv.cwd || process.cwd());
const m: any = argv.m;
const manualArgs: string[] = [].concat(m || []);
const manualArgs: string[] = [].concat(argv.names || []);

const firstArg = argv._[0] ?? "pipeline";

if (firstArg === "manual") {
for (let i = 1; i < argv._.length; i += 1) {
manualArgs.push(argv._[i]);
}
}

const parser = new Parser(cwd);

const runJobs = async () => {
Expand Down Expand Up @@ -112,8 +108,20 @@ const runJobs = async () => {
const printReport = (jobs: Job[]) => {
console.log('');
console.log(`<<<<< ------- ${c.magenta('report')} ------- >>>>>`);

const stageNames = Array.from(parser.getStages().values()).map((s) => s.name);
jobs.sort((a, b) => {
const whenPrio = ["never"];
if (a.stage !== b.stage) {
return stageNames.indexOf(a.stage) - stageNames.indexOf(b.stage);
}
return whenPrio.indexOf(b.when) - whenPrio.indexOf(a.when);
});

for (const job of jobs) {
if (job.getPrescriptsExitCode() === 0) {
if (!job.isStarted()) {
console.log(`${job.getJobNameString()} not started`);
} else if (job.getPrescriptsExitCode() === 0) {
console.log(`${job.getJobNameString()} ${c.green('successful')}`);
} else if (job.allowFailure) {
console.log(`${job.getJobNameString()} ${c.yellowBright(`warning with code ${job.getPrescriptsExitCode()}`)}`);
Expand All @@ -125,21 +133,20 @@ const printReport = (jobs: Job[]) => {

const runExecJobs = async () => {
const jobs = [];
for (let i = 1; i < argv._.length; i += 1) {
const jobName = argv._[i];
const job = parser.getJobs().get(argv._[i]);
if (!job) {
console.error(`${c.blueBright(`${jobName}`)} ${c.red(" could not be found")}`);
process.exit(1);
}
const jobName: string = String(argv.name);
const job = parser.getJobs().get(jobName);
if (!job) {
console.error(`${c.blueBright(`${jobName}`)} ${c.red(" could not be found")}`);
process.exit(1);
}

jobs.push(job);
jobs.push(job);

/* tslint:disable */
// noinspection ES6MissingAwait
job.start();
/* tslint:enabled */

/* tslint:disable */
// noinspection ES6MissingAwait
job.start();
/* tslint:enabled */
}

while (jobs.filter((j) => j.isRunning()).length > 0) {
await new Promise((r) => setTimeout(r, 50));
Expand Down
16 changes: 10 additions & 6 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ export class Job {
public readonly stage: string;
public readonly allowFailure: boolean;
public readonly when: string;
public readonly maxJobNameLength: number;

private readonly afterScripts: string[] = [];
private readonly beforeScripts: string[] = [];
private readonly cwd: any;

private readonly globals: any;
public readonly maxJobNameLength: number;

private readonly scripts: string[] = [];
private readonly variables: { [key: string]: string };

private prescriptsExitCode = 0;
private afterScriptsExitCode = 0;

private started = false;
private finished = false;
private running = false;
private success = true;
Expand Down Expand Up @@ -101,11 +100,15 @@ export class Job {
return this.finished;
}

public isManual(): boolean {
public isStarted() {
return this.started;
}

public isManual() {
return this.when === "manual";
}

public isNever(): boolean {
public isNever() {
return this.when === "never";
}

Expand All @@ -130,6 +133,7 @@ export class Job {
const startTime = process.hrtime();
const prescripts = this.beforeScripts.concat(this.scripts);
this.prescriptsExitCode = await this.exec(prescripts.join(" && "));
this.started = true;
if (this.afterScripts.length === 0 && this.prescriptsExitCode > 0 && !this.allowFailure) {
console.error(this.getExitedString(startTime, this.prescriptsExitCode, false));
this.running = false;
Expand Down Expand Up @@ -181,7 +185,7 @@ export class Job {
}

private async exec(script: string): Promise<number> {
return new Promise<any>((resolve, reject) => {
return new Promise<number>((resolve, reject) => {
const jobNameStr = this.getJobNameString();
const outputFilesPath = this.getOutputFilesPath();

Expand Down

0 comments on commit 2884c2f

Please sign in to comment.