-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·111 lines (102 loc) · 2.85 KB
/
cli.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
#!/usr/bin/env node
/**
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this
* distribution, and is available at https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*/
// load tool version
const pkg = require('./package.json');
const version = pkg && pkg.version;
// load other functions
const path = require('path');
const { P } = require('./lib/common');
const { checkStandaloneLicense } = require('./lib/check-standalone');
const { checkHeaderLicense } = require('./lib/check-header');
// parse arguments
const yargs = require('yargs');
const argv = yargs
.version(version)
.scriptName('license-tool')
.usage('Usage: $0 [options] <folder|file>')
.option('L', {
alias: 'standalone',
default: path.join(__dirname, 'licenses/standalone.txt'),
description: 'license file stays in root folder',
})
.option('H', {
alias: 'header',
default: path.join(__dirname, 'licenses/header.txt'),
description: 'license attached to file header',
})
.option('X', {
alias: 'excludes',
description: 'exclude extra files/folders pattern',
})
.option('Y', {
alias: 'years',
description: 'each file license start year',
})
.option('f', {
alias: 'fix',
default: false,
description: 'if let the tool fixes the license errors',
type: 'boolean'
})
.option('v', {
alias: 'verbose',
default: false,
description: 'show more processing details',
type: 'boolean'
})
.demandCommand()
.help()
.alias('h', 'help')
.parse();
// init logger
const logger = require('./lib/logger')(argv.verbose ? 'debug' : 'info', 'check.log');
logger.info('=============================================================================================');
logger.debug('argv: %j', argv);
// validate folder argv
const folder = argv && argv._ && argv._[0];
if (!folder) {
yargs.showHelp();
process.exit(1);
}
// check started
logger.info('Check licenses for "%s" ...', folder);
logger.info('');
logger.debug(' - options.standalone: %s', argv.standalone);
logger.debug(' - options.header: %s', argv.header);
logger.debug('');
P()
.then(async() => {
if (!argv.standalone) {
logger.debug('skipped standalone license check.');
}
await checkStandaloneLicense(logger, folder, argv.standalone, argv.fix);
logger.info('');
})
.then(async() => {
if (!argv.header) {
logger.debug('skipped header license check.');
}
await checkHeaderLicense(logger, folder, argv.header, argv.fix, {
excludes: argv.excludes,
years: argv.years,
});
logger.info('');
})
.then(() => {
logger.info('done');
logger.info('');
})
.catch((err) => {
if (err.stack) {
logger.error('%s', err.stack);
} else {
logger.error('%s', err);
}
logger.info('');
});