UNPKG

@json-schema-tools/semantic-release-transpiler

Version:
174 lines (173 loc) 7.84 kB
"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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepare = exports.verifyConditions = void 0; const path = __importStar(require("path")); const checkExists_1 = __importDefault(require("./checkExists")); const semanticReleaseError_1 = __importDefault(require("./semanticReleaseError")); const transpiler_1 = __importDefault(require("@json-schema-tools/transpiler")); const lodash_1 = require("lodash"); const fs = __importStar(require("fs")); const util_1 = require("util"); const dereferencer_1 = __importDefault(require("@json-schema-tools/dereferencer")); const toml_1 = __importDefault(require("@iarna/toml")); const readFile = (0, util_1.promisify)(fs.readFile); const writeFile = (0, util_1.promisify)(fs.writeFile); const mkdir = (0, util_1.promisify)(fs.mkdir); const tsc = require("node-typescript-compiler"); // eslint-disable-line let verified = false; const verifyConditions = (pluginConfig) => __awaiter(void 0, void 0, void 0, function* () { const cwd = process.cwd(); if (pluginConfig.schemaLocation === undefined) { throw new semanticReleaseError_1.default("You must provide a schema location", "123321", "json-schema-tools transpiler requires a schema"); } const sPath = path.resolve(cwd, pluginConfig.schemaLocation); const exists = yield (0, checkExists_1.default)(sPath); if (!exists) { throw new semanticReleaseError_1.default("Cannot find schema", "404", `please check that your schemaLocation is properly set. received value was: ${sPath}`); } verified = true; return verified; }); exports.verifyConditions = verifyConditions; const generateTs = (transpiler, schema, outpath) => __awaiter(void 0, void 0, void 0, function* () { const indexTS = `${outpath}/src/index.ts`; const regularName = (0, lodash_1.camelCase)(schema.title); const ts = [ `export const ${regularName} = ${JSON.stringify(schema)};`, `export default ${regularName}` ].join("\n"); yield writeFile(indexTS, ts); yield tsc.compile({ "target": "es6", "module": "commonjs", "lib": [ "es2015", ], "declaration": true, "outDir": `${outpath}`, "strict": true, "esModuleInterop": true, "resolveJsonModule": true, }, [ indexTS, `${outpath}/src/schema.json` ]); yield writeFile(`${outpath}/index.d.ts`, transpiler.toTs()); return true; }); const generateGo = (transpiler, schema, outpath) => __awaiter(void 0, void 0, void 0, function* () { const packageName = (0, lodash_1.snakeCase)(schema.title); const exportName = `Raw${(0, lodash_1.upperFirst)(packageName)}`; const escapedS = JSON.stringify(schema).replace(/"/g, "\\\""); const go = [ `package ${packageName}`, "", "", transpiler.toGo(), "", `const ${exportName} = "${escapedS}"`, ].join("\n"); yield writeFile(`${outpath}/${packageName}.go`, go); return true; }); const generateRs = (transpiler, schema, outpath, version) => __awaiter(void 0, void 0, void 0, function* () { const crateName = (0, lodash_1.snakeCase)(schema.title); let cargotoml; try { cargotoml = toml_1.default.parse(yield readFile("${outpath}/Cargo.toml", "utf8")); cargotoml.version = version; } catch (e) { cargotoml = { package: { name: crateName, version, description: "Generated types based on the JSON-Schema for " + crateName, license: "Apache-2.0" }, dependencies: { serde: { version: "1.0", features: ["derive"] }, serde_json: "1.0", derive_builder: "0.10" // eslint-disable-line } }; } yield writeFile(`${outpath}/Cargo.toml`, toml_1.default.stringify(cargotoml)); yield writeFile(`${outpath}/src/lib.rs`, transpiler.toRs()); return true; }); const prepare = (pluginConfig, context) => __awaiter(void 0, void 0, void 0, function* () { if (!verified) { throw new semanticReleaseError_1.default("Not verified", "ENOTVERIFIED", "Something went wrong and the schemas were not able to be verified."); //tslint:disable-line } if (!context || !context.nextRelease || !context.nextRelease.version) { throw new semanticReleaseError_1.default("No nextRelease version", "ENOVERSION", "Something went wrong and there is no next release version"); //tslint:disable-line } const outpath = pluginConfig.outpath || process.cwd(); yield mkdir(`./${outpath}/src`, { recursive: true }); const schemaPath = path.resolve(process.cwd(), pluginConfig.schemaLocation); const schemaString = yield readFile(schemaPath, "utf8"); const schema = JSON.parse(schemaString); yield writeFile(`${outpath}/src/schema.json`, schemaString); if (!schema.title) { throw new semanticReleaseError_1.default("The schema must have a title", "ENOTITLE", "Schema requires a title"); } let dereffedSchema; try { const dereffer = new dereferencer_1.default(JSON.parse(JSON.stringify(schema))); dereffedSchema = yield dereffer.resolve(); } catch (e) { throw new semanticReleaseError_1.default(e.message); } const transpiler = new transpiler_1.default(dereffedSchema); if (!pluginConfig.languages || pluginConfig.languages.ts) { yield generateTs(transpiler, schema, outpath); } if (!pluginConfig.languages || pluginConfig.languages.go) { yield generateGo(transpiler, schema, outpath); } if (!pluginConfig.languages || pluginConfig.languages.rs) { yield generateRs(transpiler, schema, outpath, context.nextRelease.version); } if (!pluginConfig.languages || pluginConfig.languages.py) { yield writeFile(`${outpath}/index.py`, transpiler.toPy()); } return true; }); exports.prepare = prepare;