UNPKG

@sisyphus.js/cli

Version:

Protoc cli for sisyphus project on js platform

233 lines 13.2 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.main = void 0; var compiler_1 = require("@sisyphus.js/compiler"); var protoc_1 = __importDefault(require("@sisyphus.js/protoc/lib/protoc")); var runtime_proto_1 = require("@sisyphus.js/runtime.proto"); var commander_1 = require("commander"); var fs = __importStar(require("fs")); var promises_1 = __importDefault(require("fs/promises")); var npmlog_1 = __importDefault(require("npmlog")); var path = __importStar(require("path")); var process = __importStar(require("process")); var config_1 = require("./config"); var plugins_1 = require("./plugins"); var root_1 = require("./root"); var source_1 = require("./source"); npmlog_1.default.heading = 'sisyphus'; function collect(value, previous) { return previous.concat(value); } function main(args) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var packageInfo, currentPackage, currentPackageInfo, currentModule, _i, _e, child, targetProtoDir, targetProtos, protos, descOutput, protocArgs, result, usedPlugins, _f, usedPlugins_1, usedPlugin, plugin, desc, fileDescSet, files, info, _g, files_1, file, filename; return __generator(this, function (_h) { switch (_h.label) { case 0: return [4 /*yield*/, (0, config_1.readConfigJson)(path.join(__dirname, '../package.json'))]; case 1: packageInfo = (_h.sent()); commander_1.program.version(packageInfo.version); commander_1.program.requiredOption('-O --output <output>', 'Folder to output ts files.') .parse(args); commander_1.program.opts(); npmlog_1.default.info('cli', "Sisygen v".concat(packageInfo.version)); currentPackage = path.join(process.cwd(), 'package.json'); if (!fs.existsSync(currentPackage)) { npmlog_1.default.error('cli', "Sisygen command must run under a valid package: ".concat(currentPackage, " not exists.")); return [2 /*return*/, -1]; } currentPackageInfo = require(currentPackage); npmlog_1.default.info('cli', "Generating for package: ".concat(currentPackageInfo.name)); for (_i = 0, _e = module.children; _i < _e.length; _i++) { child = _e[_i]; if (child.path == process.cwd()) { currentModule = child; } } if (currentModule === undefined) { npmlog_1.default.error('cli', "Failed to load module ".concat(currentPackageInfo.name, ".")); return [2 /*return*/, -1]; } return [4 /*yield*/, (0, root_1.discoverRootsInModule)(currentPackageInfo, currentModule.paths)]; case 2: _h.sent(); targetProtoDir = path.join(process.cwd(), (_b = (_a = currentPackageInfo.directories) === null || _a === void 0 ? void 0 : _a.proto) !== null && _b !== void 0 ? _b : 'proto'); targetProtos = []; if (!fs.existsSync(targetProtoDir)) return [3 /*break*/, 4]; return [4 /*yield*/, (0, source_1.discoverSourceInDir)(targetProtoDir)]; case 3: protos = _h.sent(); npmlog_1.default.info('cli', "Found ".concat(protos.length, " proto source files from work package.")); targetProtos.push.apply(targetProtos, protos); _h.label = 4; case 4: return [4 /*yield*/, (0, source_1.discoverSourceInModule)(currentPackageInfo, currentModule.paths, targetProtos)]; case 5: _h.sent(); if (targetProtos.length == 0) { npmlog_1.default.info('cli', "No protos need to generate, put .proto files into '".concat(targetProtoDir, "' directory")); return [2 /*return*/, 0]; } else { npmlog_1.default.info('cli', "".concat(targetProtos.length, " proto files picked to generate.")); } targetProtos = Array.from(new Set(targetProtos)); return [4 /*yield*/, (0, plugins_1.discoverPluginsInGlobal)()]; case 6: _h.sent(); return [4 /*yield*/, (0, plugins_1.discoverPluginsInModule)(currentPackageInfo, currentModule.paths)]; case 7: _h.sent(); descOutput = path.join(process.cwd(), '.gen.desc.pb'); protocArgs = __spreadArray(__spreadArray(__spreadArray([ "-o".concat(descOutput) ], root_1.protoRoots.map(function (it) { return "-I".concat(it); }), true), [ "-I".concat(targetProtoDir), '--include_imports', '--include_source_info' ], false), targetProtos, true); if ((_c = packageInfo.protobuf) === null || _c === void 0 ? void 0 : _c.version) { protocArgs.unshift("--version", packageInfo.protobuf.version); } npmlog_1.default.info('cli', "Run protoc ".concat(protocArgs.join(' '))); return [4 /*yield*/, protoc_1.default.apply(void 0, protocArgs)]; case 8: result = _h.sent(); if (result !== 0) { npmlog_1.default.error('cli', "Protoc return ".concat(result, " on exit.")); return [2 /*return*/, -1]; } usedPlugins = []; if (((_d = currentPackageInfo.protobuf) === null || _d === void 0 ? void 0 : _d.plugins) != undefined) { if (currentPackageInfo.protobuf.plugins.length > 0) { npmlog_1.default.info('cli', "Plugins ".concat(currentPackageInfo.protobuf.plugins.join(', '), " picked by package.json.")); usedPlugins.push.apply(usedPlugins, currentPackageInfo.protobuf.plugins); } } if (usedPlugins.length == 0) { npmlog_1.default.error('cli', "No generating plugin."); return [2 /*return*/, -1]; } for (_f = 0, usedPlugins_1 = usedPlugins; _f < usedPlugins_1.length; _f++) { usedPlugin = usedPlugins_1[_f]; plugin = plugins_1.registeredPlugins[usedPlugin]; if (plugin == undefined) { npmlog_1.default.error('cli', "Plugin ".concat(usedPlugin, " not registered.")); return [2 /*return*/, -1]; } require(plugin); } return [4 /*yield*/, promises_1.default.readFile(descOutput)]; case 9: desc = _h.sent(); return [4 /*yield*/, promises_1.default.unlink(descOutput)]; case 10: _h.sent(); fileDescSet = runtime_proto_1.FileDescriptorSet.parse(desc); files = []; advance({ kind: 'files', parent: undefined, descriptor: new compiler_1.FileDescriptorSet(fileDescSet), target: files, src: targetProtos, lib: root_1.generatedModules }); info = { version: '1.0', generatedFiles: [] }; for (_g = 0, files_1 = files; _g < files_1.length; _g++) { file = files_1[_g]; filename = path.join(process.cwd(), commander_1.program.opts()['output'], file.name); info.generatedFiles.push(file.name); fs.mkdirSync(path.dirname(filename), { recursive: true }); fs.writeFileSync(filename, file.content); } npmlog_1.default.info('cli', "".concat(files.length, " files generated, listed in sisyphus-dist.json")); fs.writeFileSync(path.join(process.cwd(), 'sisyphus-dist.json'), JSON.stringify(info, undefined, 2)); return [2 /*return*/, 0]; } }); }); } exports.main = main; main(process.argv).then(function (ret) { process.exit(ret); }).catch(function (err) { console.error(err); process.exit(1); }); //# sourceMappingURL=index.js.map