Browse Source

Add spawnSync

girish@cloudron.io 3 years ago
parent
commit
e0eedbedb2
3 changed files with 18 additions and 2 deletions
  1. 1 1
      README.md
  2. 7 1
      index.js
  3. 10 0
      test/test.js

+ 1 - 1
README.md

@@ -136,4 +136,4 @@ API
   * url.parse = returns null on error, object on success
   * require - returns null on error, module on success
   * child_process.execSync - returns null on error, stdio on success
-
+  * child_process.spawnSync - return null on error, object on success

+ 7 - 1
index.js

@@ -106,6 +106,11 @@ function execSync() {
     return safeCall(function () { return child_process.execSync.apply(child_process, args); }, null);
 }
 
+function spawnSync() {
+    var args = _argsArray(arguments);
+    return safeCall(function () { return child_process.spawnSync.apply(child_process, args); }, null);
+}
+
 // http://stackoverflow.com/questions/6491463
 // currently, '.' is assumed to be the separator
 function query(o, s, defaultValue) {
@@ -206,7 +211,8 @@ safeCall.fs = {
 };
 
 safeCall.child_process = {
-    execSync: execSync
+    execSync: execSync,
+    spawnSync: spawnSync
 };
 
 safeCall.require = safeRequire;

+ 10 - 0
test/test.js

@@ -271,4 +271,14 @@ describe('child_process', function () {
         expect(safe.child_process.execSync('true')).to.be.a(Buffer);
         expect(safe.child_process.execSync('cat', { input: 'cloudron' })).to.eql(new Buffer('cloudron', 'utf8'));
     });
+
+    it('returns null on invalid command', function () {
+        expect(safe.child_process.spawnSync('blah').error.code).to.be('ENOENT');
+        expect(safe.child_process.spawnSync('sudo ls', { timeout: 1 }).error.code).to.be('ENOENT');
+    });
+
+    it('return stdio on valid command', function () {
+        expect(safe.child_process.spawnSync('true').stdout.length).to.be(0);
+        expect(safe.child_process.spawnSync('cat', { input: 'cloudron' }).stdout).to.eql(new Buffer('cloudron', 'utf8'));
+    });
 });