UNPKG

scrypt-ts-transpiler

Version:

```bash npm i npx scryptlib download npm t ```

110 lines 4.79 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IndexerWriter = void 0; const path = __importStar(require("path")); const fs = __importStar(require("fs")); const indexerReader_1 = require("./indexerReader"); /** * @ignore */ class IndexerWriter { constructor(pathParam) { this.symbolPaths = new Map(); this.filePath = path.join(pathParam.tsconfigDir, indexerReader_1.INDEX_FILE_NAME); this.scryptBasePath = path.relative(path.dirname(this.filePath), pathParam.scryptOutDir); if (fs.existsSync(this.filePath) && process.env.NODE_ENV === 'test') { // load existing index file only under test envrionment, // for it may be unexpectedly overwritten for serveral times in a single test run. // for example, the command `mocha -r ts-node/register *.test.ts` behaves like so. // and it's advised to use a `pretest` hook to cleanup the index file before a single run. this.load(); } else { this.save(); } } save() { let content = { 'scryptBase': this.scryptBasePath, 'bindings': Array.from(this.symbolPaths.keys()).map(symbol => { return { symbol, path: this.symbolPaths.get(symbol) }; }) }; fs.writeFileSync(this.filePath, JSON.stringify(content)); } load() { if (!fs.existsSync(this.filePath)) { throw new Error(`index file not exist: ${this.filePath}`); } let content = JSON.parse(fs.readFileSync(this.filePath).toString()); if (!content.scryptBase) { throw new Error(`missing \`scryptBase\` in index file ${this.filePath}`); } this.scryptBasePath = content.scryptBase; if (!content.bindings) { throw new Error(`missing \`bindings\` in index file ${this.filePath}`); } content.bindings.forEach(binding => { if (binding.symbol && binding.path) { this.symbolPaths.set(binding.symbol, binding.path); } }); } addSymbols(symbolsWithRange, symbolPath) { symbolsWithRange.forEach(symbolWithRange => { const existPath = this.symbolPaths.get(symbolWithRange.name); if (existPath && existPath !== symbolPath) { const srcFileName = symbolWithRange.srcRange.fileName; const startLine = symbolWithRange.srcRange.start.line + 1; const startCol = symbolWithRange.srcRange.start.character + 1; const endLine = symbolWithRange.srcRange.end.line + 1; const endCol = symbolWithRange.srcRange.end.character + 1; console.log(`scrypt-ts ERROR - ${srcFileName}:${startLine}:${startCol}:${endLine}:${endCol} - symbol \`${symbolWithRange.name}\` already has been defined in \`${this.symbolPaths.get(symbolWithRange.name).replace('.scrypt', '.ts')}\` in ${this.filePath}\n`); } else { this.symbolPaths.set(symbolWithRange.name, symbolPath); } }); this.save(); } getRelativePath(symbol) { return this.symbolPaths.get(symbol); } getFullPath(symbol) { const relativePath = this.getRelativePath(symbol); if (!relativePath) { return undefined; } ; return path.join(path.dirname(this.filePath), this.scryptBasePath, relativePath); } query(symbol, includeBase = false) { const sPath = this.symbolPaths.get(symbol); return sPath && includeBase ? path.join(this.scryptBasePath, sPath) : sPath; } } exports.IndexerWriter = IndexerWriter; //# sourceMappingURL=indexerWriter.js.map