Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use node fs module instead of execSync where possible #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const fs = require('fs');
const moment = require('moment');
const mongodb = require('mongodb');
const options = require('./src/options');
const path = require('path');
const prettyjson = require('prettyjson');
const printHelp = require('./src/printHelp');
const spawn = require('child_process').spawn;
Expand All @@ -38,7 +39,7 @@ function* run() {
}

const options = {};
const rcfile = isWin ? `${process.cwd()}\\.run-rs.rc` : `${process.cwd()}/.run-rs.rc`;
const rcfile = path.join(process.cwd(), '.run-rs.rc');
if (fs.existsSync(rcfile)) {
Object.assign(options, JSON.parse(fs.readFileSync(rcfile, 'utf8')));
}
Expand Down Expand Up @@ -89,32 +90,40 @@ function* run() {
dbPath = `${commander.dbpath}` ;
}
else {
dbPath = isWin ? `${process.cwd()}\\data` : `${process.cwd()}/data`;
dbPath = path.join(process.cwd(), 'data');
}

if (!fs.existsSync(`${dbPath}`)) {
execSync(isWin ? `md ${dbPath}` : `mkdir -p ${dbPath}`);
fs.mkdirSync(dbPath, { recursive: true });
}
if (commander.keep) {
console.log(chalk.blue('Skipping purge'));
} else {
console.log(chalk.blue('Purging database...'));
execSync(isWin ? `del /S /Q ${dbPath}\\*` : `rm -rf ${dbPath}/*`);
const dir = fs.opendirSync(dbPath);

let file;
while (file = dir.readSync()) {
fs.rmSync(path.join(dbPath, file.name), {
force: true,
recursive: true
});
}
}

ports.forEach((port) => {
const portDBPath = isWin ? `${dbPath}\\${port}` : `${dbPath}/${port}`;
const portDBPath = path.join(dbPath, port.toString());;
if (!fs.existsSync(portDBPath)) {
execSync(isWin ? `md ${dbPath}\\${port}` : `mkdir -p ${dbPath}/${port}`);
fs.mkdirSync(portDBPath);
}
});

console.log(`Running '${mongod}'`, ports);
const rs = new ReplSet(mongod,
ports.map(port => {
const options = {
port: port,
dbpath: isWin ? `${dbPath}\\${port}` : `${dbPath}/${port}`,
port,
dbpath: path.join(dbPath, port.toString()),
bind_ip: hostname
};
if (commander.bind_ip_all) {
Expand Down
24 changes: 14 additions & 10 deletions src/download.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const childProcess = require('child_process');
const fs = require('fs');
const path = require('path');

module.exports = function download(version, systemLinux, os) {
Expand Down Expand Up @@ -68,19 +69,22 @@ module.exports = function download(version, systemLinux, os) {
execSync('powershell.exe -nologo -noprofile -command "&{' +
'Add-Type -AssemblyName System.IO.Compression.FileSystem;' +
`(New-Object Net.WebClient).DownloadFile('${url}', '${filename}');` +
`[System.IO.Compression.ZipFile]::ExtractToDirectory('${filename}','.');` +
`mv './${dirname}/bin' '${mainScriptDir}/${version}';` +
`rd -r './${dirname}';` +
`rm './${filename}';` +
'}"'
);
`[System.IO.Compression.ZipFile]::ExtractToDirectory('${filename}', '.');` +
'}"');
} else {
execSync(`curl -OL ${url}`);
execSync(`tar -zxvf ${filename}`);
execSync(`mv ./${dirname}/bin ${mainScriptDir}/${version}`);
execSync(`rm -rf ./${dirname}`);
execSync(`rm ./${filename}`);
}

return { path: `${mainScriptDir}/${version}`, url: url };
const targetDir = path.join(mainScriptDir, version);

fs.rmSync(targetDir, { force: true, recursive: true })
fs.renameSync(
path.join(process.cwd(), dirname, 'bin'),
targetDir
);
fs.rmSync(path.join(process.cwd(), dirname), { force: true, recursive: true });
fs.rmSync(path.join(process.cwd(), filename));

return { path: path.join(mainScriptDir, version), url };
};
6 changes: 3 additions & 3 deletions test/download.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ describe('download', function() {

it('basic download', function() {
let { url } = download('4.0.6', 'ubuntu1604', 'linux');
assert.equal(url, 'http://downloads.mongodb.org/linux/mongodb-linux-x86_64-4.0.6.tgz');
assert.equal(url, 'https://downloads.mongodb.org/linux/mongodb-linux-x86_64-4.0.6.tgz');

({ url } = download('4.2.0', 'ubuntu1604', 'linux'));
assert.equal(url, 'http://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.2.0.tgz');
assert.equal(url, 'https://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1604-4.2.0.tgz');
});

it('osx 4.2.0', function() {
Expand All @@ -30,6 +30,6 @@ describe('download', function() {

it('osx < 4.2.0', function() {
let { url } = download('4.0.6', null, 'darwin');
assert.equal(url, 'http://downloads.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.6.tgz');
assert.equal(url, 'https://downloads.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.6.tgz');
});
});