Skip to content

Commit

Permalink
refactor: wrap main script with IIFE
Browse files Browse the repository at this point in the history
  • Loading branch information
madbence committed Oct 10, 2020
1 parent e05a1d1 commit 2431e2c
Showing 1 changed file with 46 additions and 41 deletions.
87 changes: 46 additions & 41 deletions lint.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env node

const { version } = require('./package.json');
const argv = require('minimist')(process.argv.slice(2), {
const minimist = require('minimist');
const watch = require('./watch');
const run = require('./runner');
const logProblems = require('./log-problems');

const argv = minimist(process.argv.slice(2), {
boolean: ['watch'],
default: {
format: 'vscode',
},
});
const watch = require('./watch');
const run = require('./runner');
const logProblems = require('./log-problems');

const usageMessage = `Usage:
tekton-lint <path-to-yaml-files>
Expand All @@ -36,47 +39,49 @@ $ tekton-lint path/to/my/pipeline.yaml 'path/to/my/tasks/*.yaml'
$ tekton-lint --watch '**/*.yaml'
`;

if (argv.version) {
return console.log(`Version: ${version}`);
}

if (argv.help) {
return console.log(usageMessage);
}
(() => {
if (argv.version) {
return console.log(`Version: ${version}`);
}

if (argv._.length === 0) {
return console.log(usageMessage);
}
if (argv.help) {
return console.log(usageMessage);
}

if (+process.version.slice(1).split('.')[0] < 12) {
process.exitCode = 1;
return console.log(`The current node version is ${process.version}, but at least v12.0.0 is required`);
}
if (argv._.length === 0) {
return console.log(usageMessage);
}

let maxWarnings = -1;
if (argv['max-warnings'] !== undefined) {
if (typeof argv['max-warnings'] !== 'number') {
if (+process.version.slice(1).split('.')[0] < 12) {
process.exitCode = 1;
return console.log(`Invalid value for 'max-warnings' option. Expected a number, received value: ${argv['max-warnings']}.`);
return console.log(`The current node version is ${process.version}, but at least v12.0.0 is required`);
}
maxWarnings = argv['max-warnings'];
}

if (argv.watch) {
watch(argv._);
} else {
run(argv._)
.then((problems) => {
logProblems(argv, problems);
let maxWarnings = -1;
if (argv['max-warnings'] !== undefined) {
if (typeof argv['max-warnings'] !== 'number') {
process.exitCode = 1;
return console.log(`Invalid value for 'max-warnings' option. Expected a number, received value: ${argv['max-warnings']}.`);
}
maxWarnings = argv['max-warnings'];
}

if (argv.watch) {
watch(argv._);
} else {
run(argv._)
.then((problems) => {
logProblems(argv, problems);

const hasError = problems.some(p => p.level === 'error');
const warningCount = problems.filter(p => p.level === 'warning').length;
const tooManyWarnings = maxWarnings >= 0 && warningCount > maxWarnings;
// eslint-disable-next-line no-process-env
if ((hasError || tooManyWarnings) && process.env.NODE_ENV !== 'test') {
process.exitCode = 1;
}
}, (error) => {
console.error(error);
});
}
const hasError = problems.some(p => p.level === 'error');
const warningCount = problems.filter(p => p.level === 'warning').length;
const tooManyWarnings = maxWarnings >= 0 && warningCount > maxWarnings;
// eslint-disable-next-line no-process-env
if ((hasError || tooManyWarnings) && process.env.NODE_ENV !== 'test') {
process.exitCode = 1;
}
}, (error) => {
console.error(error);
});
}
})();

0 comments on commit 2431e2c

Please sign in to comment.