Comparing version 0.2.3 to 0.2.4
@@ -7,2 +7,3 @@ "use strict"; | ||
const errors_1 = require("./errors"); | ||
const logger_1 = require("./logger"); | ||
const { yellow } = chalk_1.default; | ||
@@ -26,4 +27,3 @@ function parse(abi) { | ||
if (checkForOverloads(constants, constantFunctions, functions, abiPiece.name)) { | ||
// tslint:disable-next-line | ||
console.log(yellow(`Detected overloaded constant function ${abiPiece.name} skipping...`)); | ||
logger_1.logger.log(yellow(`Detected overloaded constant function ${abiPiece.name} skipping...`)); | ||
return; | ||
@@ -45,4 +45,3 @@ } | ||
if (eventAbi.anonymous) { | ||
// tslint:disable-next-line | ||
console.log(yellow("Skipping anonymous event...")); | ||
logger_1.logger.log(yellow("Skipping anonymous event...")); | ||
return; | ||
@@ -49,0 +48,0 @@ } |
@@ -12,81 +12,16 @@ #!/usr/bin/env node | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_1 = require("fs"); | ||
const chalk_1 = require("chalk"); | ||
const path_1 = require("path"); | ||
const fs_extra_1 = require("fs-extra"); | ||
const glob = require("glob"); | ||
const prettier = require("prettier"); | ||
const generateSource_1 = require("./generateSource"); | ||
const parseArgs_1 = require("./parseArgs"); | ||
const copyRuntime_1 = require("./copyRuntime"); | ||
const abiParser_1 = require("./abiParser"); | ||
const { blue, red, green, yellow } = chalk_1.default; | ||
const cwd = process.cwd(); | ||
const generateTypeChainWrappers_1 = require("./generateTypeChainWrappers"); | ||
const logger_1 = require("./logger"); | ||
function main() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
global.IS_CLI = true; | ||
const options = parseArgs_1.parseArgs(); | ||
const matches = glob.sync(options.glob, { ignore: "node_modules/**", absolute: true }); | ||
if (matches.length === 0) { | ||
// tslint:disable-next-line | ||
console.log(red(`Found ${matches.length} ABIs.`)); | ||
process.exit(0); | ||
} | ||
// tslint:disable-next-line | ||
console.log(green(`Found ${matches.length} ABIs.`)); | ||
const prettierConfig = yield prettier.resolveConfig(path_1.dirname(matches[0])); | ||
if (prettierConfig) { | ||
// tslint:disable-next-line | ||
console.log("Found prettier config file"); | ||
} | ||
// tslint:disable-next-line | ||
console.log("Generating typings..."); | ||
// copy runtime in directory of first typing (@todo it should be customizable) | ||
const runtimeFilename = "typechain-runtime.ts"; | ||
const runtimePath = path_1.join(options.outDir || path_1.dirname(matches[0]), runtimeFilename); | ||
copyRuntime_1.copyRuntime(runtimePath); | ||
// tslint:disable-next-line | ||
console.log(blue(`${runtimeFilename} => ${runtimePath}`)); | ||
// generate wrappers | ||
matches.forEach(p => processFile(p, options.force, runtimePath, Object.assign({}, (prettierConfig || {}), { parser: "typescript" }), options.outDir)); | ||
yield generateTypeChainWrappers_1.generateTypeChainWrappers(options); | ||
}); | ||
} | ||
function processFile(absPath, forceOverwrite, runtimeAbsPath, prettierConfig, fixedOutputDir) { | ||
const relativeInputPath = path_1.relative(cwd, absPath); | ||
const parsedInputPath = path_1.parse(absPath); | ||
const filenameWithoutAnyExtensions = getFilenameWithoutAnyExtensions(parsedInputPath.name); | ||
const outputDir = fixedOutputDir || parsedInputPath.dir; | ||
const outputPath = path_1.join(outputDir, filenameWithoutAnyExtensions + ".ts"); | ||
const relativeOutputPath = path_1.relative(cwd, outputPath); | ||
const runtimeRelativePath = getRelativeModulePath(outputDir, runtimeAbsPath); | ||
// tslint:disable-next-line | ||
console.log(blue(`${relativeInputPath} => ${relativeOutputPath}`)); | ||
if (fs_extra_1.pathExistsSync(outputPath) && !forceOverwrite) { | ||
// tslint:disable-next-line | ||
console.log(red("File exists, skipping")); | ||
return; | ||
} | ||
const abiString = fs_1.readFileSync(absPath).toString(); | ||
const rawAbi = abiParser_1.extractAbi(abiString); | ||
if (rawAbi.length === 0) { | ||
// tslint:disable-next-line | ||
console.log(yellow("ABI is empty, skipping")); | ||
return; | ||
} | ||
const typescriptSourceFile = generateSource_1.generateSource(rawAbi, { | ||
fileName: filenameWithoutAnyExtensions, | ||
relativeRuntimePath: runtimeRelativePath, | ||
}); | ||
fs_1.writeFileSync(outputPath, prettier.format(typescriptSourceFile, prettierConfig)); | ||
} | ||
function getFilenameWithoutAnyExtensions(filePath) { | ||
const endPosition = filePath.indexOf("."); | ||
return filePath.slice(0, endPosition !== -1 ? endPosition : filePath.length); | ||
} | ||
function getRelativeModulePath(from, to) { | ||
return ("./" + path_1.relative(from, to)).replace(".ts", ""); // @note: this is probably not the best way to find relative path for modules | ||
} | ||
main().catch(e => { | ||
// tslint:disable-next-line | ||
console.error(red("Error occured: ", e.message)); | ||
logger_1.logger.error("Error occured: ", e.message); | ||
process.exit(1); | ||
}); |
@@ -5,3 +5,4 @@ export interface IOptions { | ||
outDir?: string; | ||
cwd?: string; | ||
} | ||
export declare function parseArgs(): IOptions; |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"license": "MIT", | ||
@@ -71,6 +71,7 @@ "repository": "https://github.com/Neufund/Typechain", | ||
"test:integration": "NODE_ENV=test mocha --require ts-node/register.js --require test/setup-chai.ts 'test/integration/web3.ts' 'test/integration/**/*.spec.ts'", | ||
"test:generateContracts": "NODE_ENV=development ts-node ./lib/cli.ts --force", | ||
"test:generateContracts": "NODE_ENV=development ts-node ./scripts/generate", | ||
"test:generateContractsOutDir": "NODE_ENV=development ts-node ./lib/cli.ts --force --outDir ./test-tmp/" | ||
}, | ||
"main": "./dist/generateTypeChainWrappers.js", | ||
"bin": "./dist/cli.js" | ||
} |
@@ -211,2 +211,19 @@ <p align="center"> | ||
### Use as API | ||
You might also use TypeChain as api: | ||
```typescript | ||
import { generateTypeChainWrappers } from "../lib/generateTypeChainWrappers"; | ||
async function main() { | ||
await generateTypeChainWrappers({ | ||
glob: "**/*.abi", | ||
force: true, | ||
}); | ||
} | ||
main().catch(console.error); | ||
``` | ||
## Roadmap 🛣️ | ||
@@ -213,0 +230,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
187517
30
844
248