|
| 1 | +/* |
| 2 | + * MetaCall Command Line Interface by Parra Studios |
| 3 | + * Copyright (C) 2016 - 2020 Vicente Eduardo Ferrer Garcia <[email protected]> |
| 4 | + * |
| 5 | + * A command line interface example as metacall wrapper. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +#ifndef METACALL_CLI_PACKAGE_MANAGER_NPM_HPP |
| 10 | +#define METACALL_CLI_PACKAGE_MANAGER_NPM_HPP 1 |
| 11 | + |
| 12 | +/* -- Namespace -- */ |
| 13 | + |
| 14 | +namespace metacallcli { |
| 15 | + |
| 16 | +namespace package_manager { |
| 17 | + |
| 18 | +static const char npm[] = |
| 19 | +"#!/usr/bin/env node\n" |
| 20 | +"\n" |
| 21 | +"/* This has been ripped off from NPM and adapted to be callable instead of invoked by exec */\n" |
| 22 | +"\n" |
| 23 | +"function package_manager(args) {\n" |
| 24 | +" // windows: running npm blah in this folder will invoke WSH, not node.\n" |
| 25 | +" /* global WScript */\n" |
| 26 | +" if (typeof WScript !== 'undefined') {\n" |
| 27 | +" WScript.echo(\n" |
| 28 | +" 'npm does not work when run\\n' +\n" |
| 29 | +" 'with the Windows Scripting Host\\n\\n' +\n" |
| 30 | +" \"'cd' to a different directory,\\n\" +\n" |
| 31 | +" \"or type 'npm.cmd <args>',\\n\" +\n" |
| 32 | +" \"or type 'node npm <args>'.\"\n" |
| 33 | +" )\n" |
| 34 | +" WScript.quit(1)\n" |
| 35 | +" return\n" |
| 36 | +" }\n" |
| 37 | +"\n" |
| 38 | +" var unsupported = require('npm/lib/utils/unsupported.js')\n" |
| 39 | +" unsupported.checkForBrokenNode()\n" |
| 40 | +"\n" |
| 41 | +" var log = require('npm/node_modules/npmlog')\n" |
| 42 | +" log.pause() // will be unpaused when config is loaded.\n" |
| 43 | +" log.info('it worked if it ends with', 'ok')\n" |
| 44 | +"\n" |
| 45 | +" unsupported.checkForUnsupportedNode()\n" |
| 46 | +"\n" |
| 47 | +" var path = require('path')\n" |
| 48 | +" var npm = require('npm/lib/npm.js')\n" |
| 49 | +" var npmconf = require('npm/lib/config/core.js')\n" |
| 50 | +" var errorHandler = require('npm/lib/utils/error-handler.js')\n" |
| 51 | +"\n" |
| 52 | +" var configDefs = npmconf.defs\n" |
| 53 | +" var shorthands = configDefs.shorthands\n" |
| 54 | +" var types = configDefs.types\n" |
| 55 | +" var nopt = require('npm/node_modules/nopt')\n" |
| 56 | +"\n" |
| 57 | +" // Overwrite process args\n" |
| 58 | +" process.argv = [ 'node', 'npm', ...args ];\n" |
| 59 | +"\n" |
| 60 | +" log.verbose('cli', process.argv)\n" |
| 61 | +"\n" |
| 62 | +" var conf = nopt(types, shorthands)\n" |
| 63 | +" npm.argv = conf.argv.remain\n" |
| 64 | +" if (npm.deref(npm.argv[0])) npm.command = npm.argv.shift()\n" |
| 65 | +" else conf.usage = true\n" |
| 66 | +"\n" |
| 67 | +" if (conf.version) {\n" |
| 68 | +" console.log(npm.version)\n" |
| 69 | +" return errorHandler.exit(0)\n" |
| 70 | +" }\n" |
| 71 | +"\n" |
| 72 | +" if (conf.versions) {\n" |
| 73 | +" npm.command = 'version'\n" |
| 74 | +" conf.usage = false\n" |
| 75 | +" npm.argv = []\n" |
| 76 | +" }\n" |
| 77 | +"\n" |
| 78 | +" log.info('using', 'npm@%s', npm.version)\n" |
| 79 | +" log.info('using', 'node@%s', process.version)\n" |
| 80 | +"\n" |
| 81 | +" process.on('uncaughtException', errorHandler)\n" |
| 82 | +"\n" |
| 83 | +" if (conf.usage && npm.command !== 'help') {\n" |
| 84 | +" npm.argv.unshift(npm.command)\n" |
| 85 | +" npm.command = 'help'\n" |
| 86 | +" }\n" |
| 87 | +"\n" |
| 88 | +" var isGlobalNpmUpdate = conf.global && ['install', 'update'].includes(npm.command) && npm.argv.includes('npm')\n" |
| 89 | +"\n" |
| 90 | +" // now actually fire up npm and run the command.\n" |
| 91 | +" // this is how to use npm programmatically:\n" |
| 92 | +" conf._exit = true\n" |
| 93 | +" npm.load(conf, function (er) {\n" |
| 94 | +" if (er) return errorHandler(er)\n" |
| 95 | +" if (\n" |
| 96 | +" !isGlobalNpmUpdate &&\n" |
| 97 | +" npm.config.get('update-notifier') &&\n" |
| 98 | +" !unsupported.checkVersion(process.version).unsupported\n" |
| 99 | +" ) {\n" |
| 100 | +" const pkg = require('npm/package.json')\n" |
| 101 | +" let notifier = require('npm/node_modules/update-notifier')({pkg})\n" |
| 102 | +" const isCI = require('npm/node_modules/ci-info').isCI\n" |
| 103 | +" if (\n" |
| 104 | +" notifier.update &&\n" |
| 105 | +" notifier.update.latest !== pkg.version &&\n" |
| 106 | +" !isCI\n" |
| 107 | +" ) {\n" |
| 108 | +" const color = require('ansicolors')\n" |
| 109 | +" const useColor = npm.config.get('color')\n" |
| 110 | +" const useUnicode = npm.config.get('unicode')\n" |
| 111 | +" const old = notifier.update.current\n" |
| 112 | +" const latest = notifier.update.latest\n" |
| 113 | +" let type = notifier.update.type\n" |
| 114 | +" if (useColor) {\n" |
| 115 | +" switch (type) {\n" |
| 116 | +" case 'major':\n" |
| 117 | +" type = color.red(type)\n" |
| 118 | +" break\n" |
| 119 | +" case 'minor':\n" |
| 120 | +" type = color.yellow(type)\n" |
| 121 | +" break\n" |
| 122 | +" case 'patch':\n" |
| 123 | +" type = color.green(type)\n" |
| 124 | +" break\n" |
| 125 | +" }\n" |
| 126 | +" }\n" |
| 127 | +" const changelog = `https://github.com/npm/cli/releases/tag/v${latest}`\n" |
| 128 | +" notifier.notify({\n" |
| 129 | +" message: `New ${type} version of ${pkg.name} available! ${\n" |
| 130 | +" useColor ? color.red(old) : old\n" |
| 131 | +" } ${useUnicode ? '→' : '->'} ${\n" |
| 132 | +" useColor ? color.green(latest) : latest\n" |
| 133 | +" }\\n` +\n" |
| 134 | +" `${\n" |
| 135 | +" useColor ? color.yellow('Changelog:') : 'Changelog:'\n" |
| 136 | +" } ${\n" |
| 137 | +" useColor ? color.cyan(changelog) : changelog\n" |
| 138 | +" }\\n` +\n" |
| 139 | +" `Run ${\n" |
| 140 | +" useColor\n" |
| 141 | +" ? color.green(`npm install -g ${pkg.name}`)\n" |
| 142 | +" : `npm i -g ${pkg.name}`\n" |
| 143 | +" } to update!`\n" |
| 144 | +" })\n" |
| 145 | +" }\n" |
| 146 | +" }\n" |
| 147 | +" npm.commands[npm.command](npm.argv, function (err) {\n" |
| 148 | +" // https://genius.com/Lin-manuel-miranda-your-obedient-servant-lyrics\n" |
| 149 | +" if (\n" |
| 150 | +" !err &&\n" |
| 151 | +" npm.config.get('ham-it-up') &&\n" |
| 152 | +" !npm.config.get('json') &&\n" |
| 153 | +" !npm.config.get('parseable') &&\n" |
| 154 | +" npm.command !== 'completion'\n" |
| 155 | +" ) {\n" |
| 156 | +" console.error(\n" |
| 157 | +" `\\n ${\n" |
| 158 | +" npm.config.get('unicode') ? '🎵 ' : ''\n" |
| 159 | +" } I Have the Honour to Be Your Obedient Servant,${\n" |
| 160 | +" npm.config.get('unicode') ? '🎵 ' : ''\n" |
| 161 | +" } ~ npm ${\n" |
| 162 | +" npm.config.get('unicode') ? '📜🖋 ' : ''\n" |
| 163 | +" }\\n`\n" |
| 164 | +" )\n" |
| 165 | +" }\n" |
| 166 | +" errorHandler.apply(this, arguments)\n" |
| 167 | +" })\n" |
| 168 | +" })\n" |
| 169 | +"}\n" |
| 170 | +"\n" |
| 171 | +"module.exports = {\n" |
| 172 | +" package_manager,\n" |
| 173 | +"};\n"; |
| 174 | + |
| 175 | +} /* namespace package_manager */ |
| 176 | + |
| 177 | +} /* namespace metacallcli */ |
| 178 | + |
| 179 | +#endif /* METACALL_CLI_PACKAGE_MANAGER_NPM_HPP */ |
0 commit comments