diff --git a/package.json b/package.json index b3007f25fe60b54257781b813d9ce22153a9e83f..ae74e31e7843cfcee96df6862a932c6d13e567e9 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,8 @@ "ts-jest": "^21.1.2", "tslint": "^5.7.0", "typescript": "^2.5.3", - "uglify-js": "^3.1.3" + "uglify-js": "^3.1.3", + "util.promisify": "^1.0.0" }, "dependencies": {} } diff --git a/src/script.ts b/src/script.ts index 7bcdd3cec863d564957a97785de8dba2d104deb1..07040421f2f4556d4e65b6d1bbb732743dc1af60 100644 --- a/src/script.ts +++ b/src/script.ts @@ -8,6 +8,9 @@ import * as util from 'util' import * as fs from 'fs' +require('util.promisify').shim(); +const readFileAsync = util.promisify(fs.readFile); + import Gro from './reader/gro/parser' import CIF from './reader/cif/index' @@ -76,13 +79,9 @@ async function runGro(input: string) { console.log(residueNumber.length, residueNumber[0], residueNumber[residueNumber.length - 1]) } -export function _gro() { - fs.readFile(`./examples/${file}`, 'utf8', function (err, input) { - if (err) { - return console.log(err); - } - runGro(input) - }); +export async function _gro() { + const input = await readFileAsync(`./examples/${file}`, 'utf8') + runGro(input) } // _gro() @@ -110,30 +109,22 @@ async function runCIF(input: string | Uint8Array) { console.log(mmcif.pdbx_struct_oper_list.matrix.value(0)); } -export function _cif() { +export async function _cif() { let path = `./examples/1cbs_updated.cif`; path = '../test/3j3q.cif' // lets have a relative path for big test files - fs.readFile(path, 'utf8', function (err, input) { - if (err) { - return console.log(err); - } - console.log('------------------'); - console.log('Text CIF:'); - runCIF(input); - }); + const input = await readFileAsync(path, 'utf8') + console.log('------------------'); + console.log('Text CIF:'); + runCIF(input); path = `./examples/1cbs_full.bcif`; // const path = 'c:/test/quick/3j3q.cif'; - fs.readFile(path, function (err, input) { - if (err) { - return console.log(err); - } - console.log('------------------'); - console.log('BinaryCIF:'); - const data = new Uint8Array(input.byteLength); - for (let i = 0; i < input.byteLength; i++) data[i] = input[i]; - runCIF(input); - }); + const input2 = await readFileAsync(path) + console.log('------------------'); + console.log('BinaryCIF:'); + const data = new Uint8Array(input2.byteLength); + for (let i = 0; i < input2.byteLength; i++) data[i] = input2[i]; + runCIF(input2); } // _cif(); @@ -155,16 +146,12 @@ async function runDic(input: string | Uint8Array) { // console.log(util.inspect(Object.keys(schema).length, {showHidden: false, depth: 1})) } -export function _dic() { +export async function _dic() { let path = './build/dics/mmcif_pdbx_v50.dic' - fs.readFile(path, 'utf8', function (err, input) { - if (err) { - return console.log(err); - } - console.log('------------------'); - console.log('Text DIC:'); - runDic(input); - }); + const input = await readFileAsync(path, 'utf8') + console.log('------------------'); + console.log('Text DIC:'); + runDic(input); } _dic();