Skip to content

Commit

Permalink
uses spawn instead of exec
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentmorneau committed Oct 17, 2018
1 parent 8d28fe8 commit 2209af8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 60 deletions.
78 changes: 44 additions & 34 deletions index.js
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const execSync = require('child_process').execSync;
const spawnSync = require('child_process').spawnSync;
const fs = require('fs');

module.exports = {
Expand Down Expand Up @@ -35,44 +35,54 @@ module.exports = {
throw new Error(`Directory ${opts.directory} is not a valid path.`);
}

// Execute the upload process
try {
switch (opts.destination.toLowerCase()) {
case 'theme':
console.log(`Uploading to ${opts.appID} - Theme Files...`);
break;
case 'workspace':
console.log(`Uploading to ${opts.appID} - Workspace Files...`);
break;
case 'plugin':
console.log(`Uploading to ${opts.appID} - ${opts.pluginName} - Plugin Files...`);
break;
default:
console.log(`Uploading to ${opts.appID} - Application Static Files...`);
}
const getAllFiles = dir =>
fs.readdirSync(dir).reduce((files, file) => {
const name = path.join(dir, file);
const isDirectory = fs.statSync(name).isDirectory();
return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name];
}, []);

const childProcess = execSync(
'"' + opts.sqlclPath + '"' + // Sqlcl path
' ' + opts.connectString + // Connect string (user/pass@server:port/sid)
' @"' + path.resolve(__dirname, 'lib/script') + '"' + // Sql to execute
' "' + path.resolve(__dirname, 'lib/distUpload.js') + '"' + // Param &1 (js to execute)
' "' + path.resolve(opts.directory) + '"' + // Param &2
' ' + opts.appID + // Param &3
' "' + opts.destination + '"' + // Param &4
' "' + opts.pluginName + '"' // Param &5
, {
encoding: 'utf8'
if (getAllFiles(opts.directory).length === 0) {
console.log(`Directory is empty.`);
} else {
// Execute the upload process
try {
switch (opts.destination.toLowerCase()) {
case 'theme':
console.log(`Uploading to ${opts.appID} - Theme Files...`);
break;
case 'workspace':
console.log(`Uploading to ${opts.appID} - Workspace Files...`);
break;
case 'plugin':
console.log(`Uploading to ${opts.appID} - ${opts.pluginName} - Plugin Files...`);
break;
default:
console.log(`Uploading to ${opts.appID} - Application Static Files...`);
}
);

console.log(childProcess);
const spawnOpts = [
opts.connectString, // Connect string (user/pass@server:port/sid)
'@' + path.resolve(__dirname, 'lib/script'), // Sql to execute
path.resolve(__dirname, 'lib/distUpload.js'), // Param &1 (js to execute)
path.resolve(opts.directory), // Param &2
opts.appID, // Param &3
opts.destination, // Param &4
opts.pluginName // Param &5
];

console.log('Files were uploaded successfully.');
const childProcess = spawnSync(
opts.sqlclPath, // Sqlcl path
spawnOpts, {
encoding: 'utf8'
}
);

return true;
} catch (err) {
console.error(err);
return false;
console.log(childProcess.stdout);
console.log('Files were uploaded successfully.');
} catch (err) {
console.error(err);
}
}
}
};
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -24,8 +24,8 @@
"url": "https://github.com/vincentmorneau/apex-publish-static-files/issues"
},
"devDependencies": {
"ava": "^0.24.0",
"xo": "^0.18.0"
"ava": "^0.25.0",
"xo": "^0.23.0"
},
"xo": {
"ignores": [
Expand Down
44 changes: 20 additions & 24 deletions test.js
Expand Up @@ -4,9 +4,9 @@ import _app from '.';
test('application', t => {
_app.publish({
sqlclPath: 'sql',
connectString: 'dev/dev@localhost:32122/orclpdb513.localdomain',
connectString: 'dev/dev@localhost:32122/orclpdb1810.localdomain',
directory: './demo/demo-working/',
appID: 101
appID: 105990
});

t.pass();
Expand All @@ -15,9 +15,9 @@ test('application', t => {
test('workspace', t => {
_app.publish({
sqlclPath: 'sql',
connectString: 'dev/dev@localhost:32122/orclpdb513.localdomain',
connectString: 'dev/dev@localhost:32122/orclpdb1810.localdomain',
directory: './demo/demo-working/',
appID: 101,
appID: 105990,
destination: 'workspace'
});

Expand All @@ -27,9 +27,9 @@ test('workspace', t => {
test('theme', t => {
_app.publish({
sqlclPath: 'sql',
connectString: 'dev/dev@localhost:32122/orclpdb513.localdomain',
connectString: 'dev/dev@localhost:32122/orclpdb1810.localdomain',
directory: './demo/demo-working/',
appID: 101,
appID: 105990,
destination: 'theme'
});

Expand All @@ -39,9 +39,9 @@ test('theme', t => {
test('alias', t => {
_app.publish({
sqlclPath: 'sql',
connectString: 'dev/dev@localhost:32122/orclpdb513.localdomain',
connectString: 'dev/dev@localhost:32122/orclpdb1810.localdomain',
directory: './demo/demo-working/',
appID: 'demo'
appID: 'DEMO'
});

t.pass();
Expand All @@ -50,9 +50,9 @@ test('alias', t => {
test('plugin', t => {
_app.publish({
sqlclPath: 'sql',
connectString: 'dev/dev@localhost:32122/orclpdb513.localdomain',
connectString: 'dev/dev@localhost:32122/orclpdb1810.localdomain',
directory: './demo/demo-working/',
appID: 101,
appID: 105990,
destination: 'plugin',
pluginName: 'ME.VMORNEAU.ANIMAPEX'
});
Expand All @@ -61,27 +61,23 @@ test('plugin', t => {
});

test('empty', t => {
try {
_app.publish({
sqlclPath: 'sql',
connectString: 'dev/dev@localhost:32122/orclpdb513.localdomain',
directory: './demo/demo-empty/',
appID: 101
});
} catch (err) {
if (err instanceof Error) {
t.pass();
}
}
_app.publish({
sqlclPath: 'sql',
connectString: 'dev/dev@localhost:32122/orclpdb1810.localdomain',
directory: './demo/demo-empty/',
appID: 105990
});

t.pass();
});

test('invalid-path', t => {
try {
_app.publish({
sqlclPath: 'sql',
connectString: 'dev/dev@localhost:32122/orclpdb513.localdomain',
connectString: 'dev/dev@localhost:32122/orclpdb1810.localdomain',
directory: './demo/demo-invalid/',
appID: 101
appID: 105990
});
} catch (err) {
if (err instanceof Error) {
Expand Down

0 comments on commit 2209af8

Please sign in to comment.