Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@goldstack/utils-sh

Package Overview
Dependencies
Maintainers
1
Versions
98
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@goldstack/utils-sh - npm Package Compare versions

Comparing version 0.2.2 to 0.2.3

284

dist/utilsSh.js

@@ -21,38 +21,2 @@ "use strict";

};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -63,68 +27,43 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

exports.cd = exports.sh = exports.write = exports.read = exports.pwd = exports.exec = exports.tempDir = exports.unzip = exports.zip = exports.mkdir = exports.rm = exports.rmSafe = exports.cp = exports.copy = void 0;
var shelljs_1 = __importStar(require("shelljs"));
const shelljs_1 = __importStar(require("shelljs"));
exports.sh = shelljs_1.default;
var fs_1 = __importDefault(require("fs"));
var ncp_1 = __importDefault(require("ncp"));
var path_1 = __importDefault(require("path"));
var rimraf_1 = __importDefault(require("rimraf"));
var archiver_1 = __importDefault(require("archiver"));
var extract_zip_1 = __importDefault(require("extract-zip"));
exports.copy = function (source, dest) { return __awaiter(void 0, void 0, void 0, function () {
var sourceArr, _loop_1, _i, sourceArr_1, sourceEl;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (Array.isArray(source)) {
sourceArr = source;
const fs_1 = __importDefault(require("fs"));
const ncp_1 = __importDefault(require("ncp"));
const path_1 = __importDefault(require("path"));
const rimraf_1 = __importDefault(require("rimraf"));
const archiver_1 = __importDefault(require("archiver"));
const extract_zip_1 = __importDefault(require("extract-zip"));
exports.copy = async (source, dest) => {
let sourceArr;
if (Array.isArray(source)) {
sourceArr = source;
}
else {
sourceArr = [source];
}
for (const sourceEl of sourceArr) {
await new Promise((resolve, reject) => {
let destCorrected;
if (!fs_1.default.lstatSync(sourceEl).isDirectory()) {
destCorrected = dest + path_1.default.basename(sourceEl);
}
else {
destCorrected = dest;
}
ncp_1.default(sourceEl, destCorrected, { stopOnErr: true }, (err) => {
if (err) {
reject(err);
return;
}
else {
sourceArr = [source];
}
_loop_1 = function (sourceEl) {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
var destCorrected;
if (!fs_1.default.lstatSync(sourceEl).isDirectory()) {
destCorrected = dest + path_1.default.basename(sourceEl);
}
else {
destCorrected = dest;
}
ncp_1.default(sourceEl, destCorrected, { stopOnErr: true }, function (err) {
if (err) {
reject(err);
return;
}
resolve();
});
})];
case 1:
_a.sent();
return [2 /*return*/];
}
});
};
_i = 0, sourceArr_1 = sourceArr;
_a.label = 1;
case 1:
if (!(_i < sourceArr_1.length)) return [3 /*break*/, 4];
sourceEl = sourceArr_1[_i];
return [5 /*yield**/, _loop_1(sourceEl)];
case 2:
_a.sent();
_a.label = 3;
case 3:
_i++;
return [3 /*break*/, 1];
case 4: return [2 /*return*/];
}
});
}); };
exports.cp = function (options, source, dest) {
var res = shelljs_1.default.cp(options, source, dest);
resolve();
});
});
}
};
exports.cp = (options, source, dest) => {
const res = shelljs_1.default.cp(options, source, dest);
if (!res || res.code !== 0) {
shelljs_1.default.echo(res.stdout);
shelljs_1.default.echo(res.stderr);
throw new Error("Cannot copy " + source + " to " + dest + ".");
throw new Error(`Cannot copy ${source} to ${dest}.`);
}

@@ -135,56 +74,19 @@ };

*/
exports.rmSafe = function () {
var files = [];
for (var _i = 0; _i < arguments.length; _i++) {
files[_i] = arguments[_i];
exports.rmSafe = async (...files) => {
for (const file of files) {
await new Promise((resolve, reject) => {
rimraf_1.default(file, {
maxBusyTries: 10,
}, (e) => {
if (e) {
reject(e);
return;
}
resolve();
});
});
}
return __awaiter(void 0, void 0, void 0, function () {
var _loop_2, _a, files_1, file;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_loop_2 = function (file) {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
rimraf_1.default(file, {
maxBusyTries: 10,
}, function (e) {
if (e) {
reject(e);
return;
}
resolve();
});
})];
case 1:
_a.sent();
return [2 /*return*/];
}
});
};
_a = 0, files_1 = files;
_b.label = 1;
case 1:
if (!(_a < files_1.length)) return [3 /*break*/, 4];
file = files_1[_a];
return [5 /*yield**/, _loop_2(file)];
case 2:
_b.sent();
_b.label = 3;
case 3:
_a++;
return [3 /*break*/, 1];
case 4: return [2 /*return*/];
}
});
});
};
exports.rm = function (options) {
var files = [];
for (var _i = 1; _i < arguments.length; _i++) {
files[_i - 1] = arguments[_i];
}
for (var _a = 0, files_2 = files; _a < files_2.length; _a++) {
var file = files_2[_a];
exports.rm = (options, ...files) => {
for (const file of files) {
rimraf_1.default.sync(file); // sh.rm(options, files);

@@ -198,56 +100,38 @@ // if (!res || res.code !== 0) {

};
exports.mkdir = function (options) {
var dir = [];
for (var _i = 1; _i < arguments.length; _i++) {
dir[_i - 1] = arguments[_i];
}
var res = shelljs_1.default.mkdir(options, dir);
exports.mkdir = (options, ...dir) => {
const res = shelljs_1.default.mkdir(options, dir);
if (!res || res.code !== 0) {
shelljs_1.default.echo(res.stdout);
shelljs_1.default.echo(res.stderr);
throw new Error("Cannot create directory " + dir);
throw new Error(`Cannot create directory ${dir}`);
}
};
exports.zip = function (params) { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) {
var output = fs_1.default.createWriteStream(params.target);
var archive = archiver_1.default('zip', {
zlib: { level: 9 },
});
archive.on('warning', function (err) {
console.warn(err.message);
});
output.on('finish', function () {
resolve();
});
output.on('error', reject);
// pipe archive data to the file
archive.pipe(output);
archive.directory(params.directory, false);
archive.finalize();
})];
case 1:
_a.sent();
return [2 /*return*/];
}
exports.zip = async (params) => {
await new Promise((resolve, reject) => {
const output = fs_1.default.createWriteStream(params.target);
const archive = archiver_1.default('zip', {
zlib: { level: 9 },
});
archive.on('warning', function (err) {
console.warn(err.message);
});
output.on('finish', () => {
resolve();
});
output.on('error', reject);
// pipe archive data to the file
archive.pipe(output);
archive.directory(params.directory, false);
archive.finalize();
});
}); };
};
/**
* Unzips a zip file into directly into a directory.
*/
exports.unzip = function (params) { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, extract_zip_1.default(params.file, {
dir: path_1.default.resolve(params.targetDirectory),
})];
case 1:
_a.sent();
return [2 /*return*/];
}
exports.unzip = async (params) => {
await extract_zip_1.default(params.file, {
dir: path_1.default.resolve(params.targetDirectory),
});
}); };
exports.tempDir = function () {
};
exports.tempDir = () => {
if (process.env.GOLDSTACK_WORKDIR) {

@@ -258,8 +142,8 @@ return process.env.GOLDSTACK_WORKDIR;

};
var exec = function (cmd, params) {
var res = shelljs_1.default.exec(cmd, { silent: params === null || params === void 0 ? void 0 : params.silent });
const exec = (cmd, params) => {
const res = shelljs_1.default.exec(cmd, { silent: params === null || params === void 0 ? void 0 : params.silent });
if (!res || res.code != 0) {
shelljs_1.default.echo(res.stdout);
shelljs_1.default.echo(res.stderr);
throw new Error("Error running '" + cmd + "'");
throw new Error(`Error running '${cmd}'`);
}

@@ -269,14 +153,14 @@ return res.stdout;

exports.exec = exec;
var read = function (path) {
const read = (path) => {
return shelljs_1.default.cat(path).toString();
};
exports.read = read;
var write = function (content, path) {
const write = (content, path) => {
new shelljs_1.ShellString(content).to(path);
};
exports.write = write;
var pwd = function () { return shelljs_1.default.pwd().toString(); };
const pwd = () => shelljs_1.default.pwd().toString();
exports.pwd = pwd;
var cd = shelljs_1.default.cd;
const cd = shelljs_1.default.cd;
exports.cd = cd;
//# sourceMappingURL=utilsSh.js.map
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {

@@ -42,60 +6,35 @@ return (mod && mod.__esModule) ? mod : { "default": mod };

Object.defineProperty(exports, "__esModule", { value: true });
var fs_1 = __importDefault(require("fs"));
var utilsSh_1 = require("./utilsSh");
var assert_1 = __importDefault(require("assert"));
describe('Copy', function () {
beforeAll(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, utilsSh_1.rmSafe('./work')];
case 1:
_a.sent();
utilsSh_1.mkdir('-p', './work');
return [2 /*return*/];
}
});
}); });
it('Should copy all files (including hidden)', function () { return __awaiter(void 0, void 0, void 0, function () {
var testDir, destDir;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
testDir = './goldstackLocal/work/copyAll/';
utilsSh_1.mkdir('-p', testDir);
utilsSh_1.write('dummy', testDir + '.hidden');
utilsSh_1.write('dummy', testDir + 'normal.txt');
utilsSh_1.mkdir('-p', testDir + 'dir/');
utilsSh_1.write('dummy', testDir + 'dir/.hidden');
utilsSh_1.write('dummy', testDir + 'dir/normal.txt');
destDir = './goldstackLocal/work/copyAllDest/';
return [4 /*yield*/, utilsSh_1.copy(testDir, destDir)];
case 1:
_a.sent();
assert_1.default(fs_1.default.existsSync(destDir + 'normal.txt'), "Cannot find " + (destDir + 'normal.txt'));
assert_1.default(fs_1.default.existsSync(destDir + '.hidden'));
assert_1.default(fs_1.default.existsSync(destDir + 'dir/.hidden'));
assert_1.default(fs_1.default.existsSync(destDir + 'dir/normal.txt'));
return [2 /*return*/];
}
});
}); });
it('Should copy a single file', function () { return __awaiter(void 0, void 0, void 0, function () {
var testDir, destDir;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
testDir = './goldstackLocal/work/copySingle/';
utilsSh_1.mkdir('-p', testDir);
utilsSh_1.write('dummy', testDir + 'normal.txt');
destDir = './goldstackLocal/work/copySingleDest/';
utilsSh_1.mkdir('-p', destDir);
return [4 /*yield*/, utilsSh_1.copy(testDir + 'normal.txt', destDir)];
case 1:
_a.sent();
assert_1.default(fs_1.default.existsSync(destDir + 'normal.txt'));
return [2 /*return*/];
}
});
}); });
const fs_1 = __importDefault(require("fs"));
const utilsSh_1 = require("./utilsSh");
const assert_1 = __importDefault(require("assert"));
describe('Copy', () => {
beforeAll(async () => {
await utilsSh_1.rmSafe('./work');
utilsSh_1.mkdir('-p', './work');
});
it('Should copy all files (including hidden)', async () => {
const testDir = './goldstackLocal/work/copyAll/';
utilsSh_1.mkdir('-p', testDir);
utilsSh_1.write('dummy', testDir + '.hidden');
utilsSh_1.write('dummy', testDir + 'normal.txt');
utilsSh_1.mkdir('-p', testDir + 'dir/');
utilsSh_1.write('dummy', testDir + 'dir/.hidden');
utilsSh_1.write('dummy', testDir + 'dir/normal.txt');
const destDir = './goldstackLocal/work/copyAllDest/';
await utilsSh_1.copy(testDir, destDir);
assert_1.default(fs_1.default.existsSync(destDir + 'normal.txt'), `Cannot find ${destDir + 'normal.txt'}`);
assert_1.default(fs_1.default.existsSync(destDir + '.hidden'));
assert_1.default(fs_1.default.existsSync(destDir + 'dir/.hidden'));
assert_1.default(fs_1.default.existsSync(destDir + 'dir/normal.txt'));
});
it('Should copy a single file', async () => {
const testDir = './goldstackLocal/work/copySingle/';
utilsSh_1.mkdir('-p', testDir);
utilsSh_1.write('dummy', testDir + 'normal.txt');
const destDir = './goldstackLocal/work/copySingleDest/';
utilsSh_1.mkdir('-p', destDir);
await utilsSh_1.copy(testDir + 'normal.txt', destDir);
assert_1.default(fs_1.default.existsSync(destDir + 'normal.txt'));
});
});
//# sourceMappingURL=utilsSh.spec.js.map
{
"name": "@goldstack/utils-sh",
"version": "0.2.2",
"version": "0.2.3",
"license": "MIT",

@@ -5,0 +5,0 @@ "author": "Max Rohde",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc