From 7d62586afe7ff7ee91063013a2bd681ade257a05 Mon Sep 17 00:00:00 2001
From: khai96_ <hvksmr1996@gmail.com>
Date: Fri, 8 May 2020 13:06:16 +0700
Subject: [PATCH] Complete basic

---
 src/index.ts         | 12 ++++++++++++
 src/install/index.ts |  9 +++------
 src/install/run.ts   | 16 +++++++++++-----
 3 files changed, 26 insertions(+), 11 deletions(-)
 create mode 100644 src/index.ts

diff --git a/src/index.ts b/src/index.ts
new file mode 100644
index 0000000..40947ef
--- /dev/null
+++ b/src/index.ts
@@ -0,0 +1,12 @@
+import { setFailed } from '@actions/core'
+import getInputs from './inputs'
+import install from './install'
+
+const inputs = getInputs()
+
+install(inputs).then(() => {
+  console.log('Installation Completed!')
+}).catch(error => {
+  console.error(error)
+  setFailed(error)
+})
diff --git a/src/install/index.ts b/src/install/index.ts
index 4f14810..da85a61 100644
--- a/src/install/index.ts
+++ b/src/install/index.ts
@@ -1,14 +1,11 @@
 import { setFailed } from '@actions/core'
-import getInputs from '../inputs'
+import { Inputs } from '../inputs'
 import runSelfInstaller from './run'
 
 export { runSelfInstaller }
 
-export async function install() {
-  const { error, status } = await runSelfInstaller(getInputs())
-
-  if (error) return setFailed(error)
-
+export async function install(inputs: Inputs) {
+  const status = await runSelfInstaller(inputs)
   if (status) {
     return setFailed(`Something does wrong, self-installer exits with code ${status}`)
   }
diff --git a/src/install/run.ts b/src/install/run.ts
index cf17f11..1312b6f 100644
--- a/src/install/run.ts
+++ b/src/install/run.ts
@@ -1,17 +1,23 @@
-import { spawnSync } from 'child_process'
+import { spawn } from 'child_process'
 import { downloadSelfInstaller } from '../self-installer'
 import { Inputs } from '../inputs'
 
-export async function runSelfInstaller(inputs: Inputs) {
-  return spawnSync('node', {
+export function runSelfInstaller(inputs: Inputs): Promise<number> {
+  const cp = spawn('node', {
     env: {
       PNPM_VERSION: inputs.version,
       PNPM_DEST: inputs.dest,
       PNPM_BIN_DEST: inputs.binDest,
       PNPM_REGISTRY: inputs.registry,
     },
-    input: await downloadSelfInstaller(),
-    stdio: 'inherit',
+    stdio: ['pipe', 'inherit', 'inherit'],
+  })
+
+  downloadSelfInstaller().pipe(cp.stdin)
+
+  return new Promise((resolve, reject) => {
+    cp.on('error', reject)
+    cp.on('close', resolve)
   })
 }