|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const commander = require('commander'); |
| 4 | + inquirer = require('inquirer'); |
| 5 | + fs = require('fs'); |
| 6 | + |
| 7 | +const pkgJson = require('../package.json'); |
| 8 | + |
| 9 | +const GitHubInventoryFactory = require('../lib/github/inventory-factory'); |
| 10 | + //Commented out GitLab since we are not using it. |
| 11 | + //GitLabInventoryFactory = require('../lib/gitlab/inventory-factory'); |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +const createFactory = (config, endpoint) => { |
| 16 | + if (endpoint.type === 'github') { |
| 17 | + return new GitHubInventoryFactory(config, endpoint); |
| 18 | + } //else if (endpoint.type === 'gitlab') { |
| 19 | + //return new GitLabInventoryFactory(config, endpoint); |
| 20 | + else { |
| 21 | + throw new Error(`Unknown factory type '${endpoint.type}'\n`); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const combineInventories = (inventories) => { |
| 26 | + return inventories.reduce((combined, current) => { |
| 27 | + combined.releases = combined.releases.concat(current.releases); |
| 28 | + return combined; |
| 29 | + }); |
| 30 | +}; |
| 31 | + |
| 32 | +const createInventory = (config) => { |
| 33 | + let factories = config.endpoints.map((endpoint) => { |
| 34 | + return createFactory(config, endpoint); |
| 35 | + }); |
| 36 | + let promises = factories.map(factory => factory.inventory()); |
| 37 | + |
| 38 | + return Promise.all(promises).then((inventories) => { |
| 39 | + return combineInventories(inventories); |
| 40 | + }).then((inventory) => { |
| 41 | + inventory.releases.sort(releaseComparator); |
| 42 | + return inventory; |
| 43 | + }); |
| 44 | +}; |
| 45 | + |
| 46 | +const releaseComparator = (a, b) => { |
| 47 | + if (a.permissions.usageType === 'openSource' && |
| 48 | + b.permissions.usageType !== 'openSource') { |
| 49 | + return -1; |
| 50 | + } else if (b.permissions.usageType === 'openSource' && |
| 51 | + a.permissions.usageType !== 'openSource') { |
| 52 | + return 1; |
| 53 | + } |
| 54 | + |
| 55 | + // Both open source or both not, sort by name |
| 56 | + if (a.name < b.name) { |
| 57 | + return -1; |
| 58 | + } else if (b.name < a.name) { |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + // Same project, try sorting by date |
| 63 | + try { |
| 64 | + const aStamp = new Date(a.date.created).getTime(); |
| 65 | + const bStamp = new Date(b.date.created).getTime(); |
| 66 | + |
| 67 | + return aStamp - bStamp; |
| 68 | + } catch (e) { |
| 69 | + // Date sorting failed, just equal |
| 70 | + return 0; |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | + |
| 75 | +if (require.main === module) { |
| 76 | + commander |
| 77 | + .version(pkgJson.version) |
| 78 | + .option('-c, --configFile <file>', 'Configuration file') |
| 79 | + .parse(process.argv); |
| 80 | + |
| 81 | + |
| 82 | + Promise.resolve(commander.configFile).then((configFile) => { |
| 83 | + if (!configFile) { |
| 84 | + throw new Error('Configuration file not provided.'); |
| 85 | + } |
| 86 | + return configFile; |
| 87 | + }).catch((err) => { |
| 88 | + return inquirer.prompt([{ |
| 89 | + type: 'input', |
| 90 | + name: 'configFile', |
| 91 | + message: 'Please specify a configuration file' |
| 92 | + }]).then(answers => answers.configFile); |
| 93 | + }).then((configFile) => { |
| 94 | + if (!fs.existsSync(configFile)) { |
| 95 | + process.stderr.write(`Configuration file '${configFile}' not found.\n`); |
| 96 | + process.exit(-1); |
| 97 | + } |
| 98 | + |
| 99 | + return createInventory(JSON.parse(fs.readFileSync(configFile, 'UTF-8'))); |
| 100 | + }).then((inventory) => { |
| 101 | + process.stdout.write(JSON.stringify(inventory, null, 2) + '\n'); |
| 102 | + process.exit(0); |
| 103 | + }).catch((err) => { |
| 104 | + process.stderr.write(err.stack + '\n'); |
| 105 | + process.exit(-1); |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | +module.exports = createInventory; |
0 commit comments