-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·112 lines (103 loc) · 4.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
'use strict';
const request = require('request');
const chalk = require('chalk');
const argv = require('yargs')
.usage('Usage: npm-package-checker -r [repositoryUrl] -t [versionTag] -d -c')
.demand(['repoUrl', 'tag'])
.alias('r', 'repoUrl')
.describe('r', 'Repository URL of component (Nexus, Artifactory...)')
.requiresArg('r')
.alias('t', 'tag')
.describe('t', 'NPM component version (package.json)')
.requiresArg('t')
.alias('d', 'debug')
.describe('d', 'Show detailed info about the progress')
.alias('c', 'colors')
.describe('c', 'Show color traces')
.help('h')
.alias('h', 'help')
.boolean('debug')
.boolean('colors')
.version(() => { return 'The current installed version of <npm-package-checker> is ' + require('./package.json').version })
.epilog('----------------------\n Sema García - 2016\n----------------------')
.showHelpOnFail(true, 'Specify --help for available options')
.recommendCommands()
.argv;
const LEVEL_LOG = { SUCCESS: 0, FAILURE: 1, INFO: 2 };
let colors = argv.colors;
let verboseMode = !!argv.debug;
let componentUrl = argv.repoUrl;
let componentVersion = argv.tag;
let red = (colors) ? chalk.bold.red : (traceMessage) => traceMessage;
let green = (colors) ? chalk.bold.green : (traceMessage) => traceMessage;
let blue = (colors) ? chalk.bold.blue : (traceMessage) => traceMessage;
/**
* Ends the execution with a success message (green color if -c options is enabled/specified).
* @param successMessage: print a successful trace and ends the process with OK.
*/
let endsSuccessfully = (successMessage) => {
process.stdout.write(green('[npm-package-checker] OK :: ' + successMessage + '\n'));
process.exitCode = 0; // Ok
};
/**
* Ends the execution with a failure message (red color if -c options is enabled/specified).
* @param errorMessage: print an error trace and ends the process with KO.
*/
let endsWithFailure = (errorMessage) => {
process.stderr.write(red('[npm-package-checker] Error :: ' + errorMessage + '\n'));
process.exitCode = 1; // Error
};
/**
* Print a trace if the debug flag is enabled and, moreover, it could be colorize the trace according to its type.
* @param level: [ SUCCESS | FAILURE | INFO ] The type trace: success (green), failure (red) and info (blue).
* @param message: trace to log.
*/
let trace = (level, message) => {
if(verboseMode) {
switch (level) {
case LEVEL_LOG.SUCCESS:
return green(message);
case LEVEL_LOG.FAILURE:
return red(message);
case LEVEL_LOG.INFO:
return blue(message);
default:
return message;
}
}
};
/**
* Main process
*/
let makeRequest = () => {
// Attention... async request, manage it with callback/promise
trace(LEVEL_LOG.INFO, `[npm-package-checker] :: Starting process...`);
request(componentUrl, (error, response, body) => {
trace(LEVEL_LOG.INFO, `[npm-package-checker] :: Request made to: ${componentUrl}`);
// Check the result (HTTP status code)
if (!error && response.statusCode == 200) {
trace(LEVEL_LOG.INFO, `[npm-package-checker] :: Request successful, searching component version <${componentVersion}>`);
// search tag version
let res = '';
try {
res = JSON.parse(body);
trace(LEVEL_LOG.SUCCESS, `[npm-package-checker] :: request completed!`);
if(res['versions'][componentVersion]) {
endsWithFailure(`The TAG <${componentVersion}> was found and there is a component published with that version.`);
} else {
endsSuccessfully(`The TAG <${componentVersion}> was not found.`);
}
} catch(err) {
trace(LEVEL_LOG.FAILURE, `[npm-package-checker] :: Error catched!`);
endsWithFailure(`Error parsing JSON from response. Error: ${err}`);
};
} else {
// print HTTP error and status code
let statusCode = (response) ? response.statusCode : '-';
endsWithFailure(`Error querying Nexus NPM repository. HTTP Error: ${statusCode}, Error: ${error}`);
}
});
};
// Let's go... Execute!
makeRequest();