ソースを参照

sync/await for file reading

Alexander Rose 7 年 前
コミット
b90480cb2b
2 ファイル変更24 行追加36 行削除
  1. 2 1
      package.json
  2. 22 35
      src/script.ts

+ 2 - 1
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": {}
 }

+ 22 - 35
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();