bulk-curd-files
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -54,42 +54,49 @@ "use strict"; | ||
var writeFile = util_1.default.promisify(fs.writeFile); | ||
var readdir = util_1.default.promisify(fs.readdir); | ||
var lstat = util_1.default.promisify(fs.lstat); | ||
var exists = util_1.default.promisify(fs.exists); | ||
function bulkCopy(dir, originFileName, targetFileName, alsoReplaceExistFile) { | ||
if (alsoReplaceExistFile === void 0) { alsoReplaceExistFile = false; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var inputPath, files; | ||
var files; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
inputPath = path.resolve(__dirname, dir); | ||
files = fs.readdirSync(inputPath); | ||
case 0: return [4 /*yield*/, readdir(dir)]; | ||
case 1: | ||
files = _a.sent(); | ||
return [4 /*yield*/, Promise.all(files.map(function (filePath) { return __awaiter(_this, void 0, void 0, function () { | ||
var fileOrFolderPath, isFolder, content; | ||
var fileOrFolderPath, isFolder, fileExist, content; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
fileOrFolderPath = path.resolve(inputPath, filePath); | ||
isFolder = fs.lstatSync(fileOrFolderPath).isDirectory(); | ||
if (!isFolder) return [3 /*break*/, 2]; | ||
fileOrFolderPath = path.resolve(dir, filePath); | ||
return [4 /*yield*/, lstat(fileOrFolderPath)]; | ||
case 1: | ||
isFolder = (_a.sent()).isDirectory(); | ||
if (!isFolder) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, bulkCopy(fileOrFolderPath, originFileName, targetFileName, alsoReplaceExistFile) | ||
// if the file you want to copy exist | ||
// && (if ingnore the existing file || if the file exist, don't cover it) | ||
// && (if ignore the existing file || if the file exist, don't cover it) | ||
]; | ||
case 1: | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 5]; | ||
case 2: | ||
return [3 /*break*/, 7]; | ||
case 3: return [4 /*yield*/, exists(path.resolve(dir, targetFileName))]; | ||
case 4: | ||
fileExist = _a.sent(); | ||
if (!(new RegExp(originFileName).test(filePath.toString()) && | ||
(alsoReplaceExistFile || !fs.existsSync(path.resolve(inputPath, targetFileName))))) return [3 /*break*/, 5]; | ||
(alsoReplaceExistFile || !fileExist))) return [3 /*break*/, 7]; | ||
return [4 /*yield*/, readFile(fileOrFolderPath, 'utf8')]; | ||
case 3: | ||
case 5: | ||
content = _a.sent(); | ||
return [4 /*yield*/, writeFile(inputPath + "/" + targetFileName, content)]; | ||
case 4: | ||
return [4 /*yield*/, writeFile(dir + "/" + targetFileName, content)]; | ||
case 6: | ||
_a.sent(); | ||
_a.label = 5; | ||
case 5: return [2 /*return*/]; | ||
_a.label = 7; | ||
case 7: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }))]; | ||
case 1: | ||
case 2: | ||
_a.sent(); | ||
@@ -102,1 +109,2 @@ return [2 /*return*/]; | ||
exports.default = bulkCopy; | ||
// bulkCopy('./', 'originFileName.js', 'targetFileName.js') |
@@ -53,2 +53,4 @@ "use strict"; | ||
var unlink = util_1.default.promisify(fs.unlink); | ||
var readdir = util_1.default.promisify(fs.readdir); | ||
var lstat = util_1.default.promisify(fs.lstat); | ||
function bulkDelete(dir, targetFileName, ignoreFolder) { | ||
@@ -60,4 +62,5 @@ return __awaiter(this, void 0, void 0, function () { | ||
switch (_a.label) { | ||
case 0: | ||
files = fs.readdirSync(dir); | ||
case 0: return [4 /*yield*/, readdir(dir)]; | ||
case 1: | ||
files = _a.sent(); | ||
return [4 /*yield*/, Promise.all(files.map(function (filePath) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -69,21 +72,23 @@ var fileOrFolderPath, isFolder; | ||
fileOrFolderPath = path.resolve(dir, filePath); | ||
isFolder = fs.lstatSync(fileOrFolderPath).isDirectory(); | ||
if (!(isFolder && ignoreFolder !== fileOrFolderPath.toString())) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, lstat(fileOrFolderPath)]; | ||
case 1: | ||
isFolder = (_a.sent()).isDirectory(); | ||
if (!(isFolder && ignoreFolder !== fileOrFolderPath.toString())) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, bulkDelete(fileOrFolderPath, targetFileName, ignoreFolder) | ||
// if the file you want to delete exist | ||
]; | ||
case 1: | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 2: | ||
if (!new RegExp(targetFileName).test(filePath.toString())) return [3 /*break*/, 4]; | ||
return [3 /*break*/, 5]; | ||
case 3: | ||
if (!new RegExp(targetFileName).test(filePath.toString())) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, unlink(path.resolve(dir, filePath))]; | ||
case 3: | ||
case 4: | ||
_a.sent(); | ||
_a.label = 4; | ||
case 4: return [2 /*return*/]; | ||
_a.label = 5; | ||
case 5: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }))]; | ||
case 1: | ||
case 2: | ||
_a.sent(); | ||
@@ -90,0 +95,0 @@ return [2 /*return*/]; |
{ | ||
"name": "bulk-curd-files", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A files bulk copy lib", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
{ | ||
"name": "bulk-curd-files", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A files bulk curd lib", | ||
@@ -34,2 +34,3 @@ "main": "lib/index.js", | ||
"@types/jest": "^24.0.18", | ||
"babel-node": "0.0.1-security", | ||
"commitizen": "^4.0.3", | ||
@@ -36,0 +37,0 @@ "cz-conventional-changelog": "^3.0.2", |
@@ -7,2 +7,5 @@ import * as fs from 'fs' | ||
const writeFile = util.promisify(fs.writeFile); | ||
const readdir = util.promisify(fs.readdir); | ||
const lstat = util.promisify(fs.lstat); | ||
const exists = util.promisify(fs.exists); | ||
@@ -15,19 +18,21 @@ export default async function bulkCopy( | ||
) { | ||
const inputPath = path.resolve(__dirname, dir); | ||
const files = fs.readdirSync(inputPath) | ||
const files = await readdir(dir) | ||
await Promise.all( | ||
files.map(async filePath => { | ||
const fileOrFolderPath = path.resolve(inputPath, filePath) | ||
const fileOrFolderPath = path.resolve(dir, filePath) | ||
// if this is a folder, Recursive call it to handle the files from this folder; | ||
const isFolder = fs.lstatSync(fileOrFolderPath).isDirectory() | ||
const isFolder = (await lstat(fileOrFolderPath)).isDirectory() | ||
if (isFolder) { | ||
await bulkCopy(fileOrFolderPath, originFileName, targetFileName, alsoReplaceExistFile) | ||
// if the file you want to copy exist | ||
// && (if ingnore the existing file || if the file exist, don't cover it) | ||
} else if ( | ||
// && (if ignore the existing file || if the file exist, don't cover it) | ||
} else { | ||
const fileExist = await exists(path.resolve(dir, targetFileName)); | ||
if ( | ||
new RegExp(originFileName).test(filePath.toString()) && | ||
(alsoReplaceExistFile || !fs.existsSync(path.resolve(inputPath, targetFileName))) | ||
) { | ||
const content = await readFile(fileOrFolderPath, 'utf8') | ||
await writeFile(`${inputPath}/${targetFileName}`, content) | ||
(alsoReplaceExistFile || !fileExist) | ||
) { | ||
const content = await readFile(fileOrFolderPath, 'utf8') | ||
await writeFile(`${dir}/${targetFileName}`, content) | ||
} | ||
} | ||
@@ -37,1 +42,3 @@ }), | ||
} | ||
// bulkCopy('./', 'originFileName.js', 'targetFileName.js') |
@@ -6,5 +6,7 @@ import * as fs from 'fs' | ||
const unlink = util.promisify(fs.unlink); | ||
const readdir = util.promisify(fs.readdir); | ||
const lstat = util.promisify(fs.lstat); | ||
export default async function bulkDelete(dir: string, targetFileName: string, ignoreFolder?: string) { | ||
const files = fs.readdirSync(dir) | ||
const files = await readdir(dir) | ||
await Promise.all( | ||
@@ -14,3 +16,3 @@ files.map(async filePath => { | ||
const fileOrFolderPath = path.resolve(dir, filePath) | ||
const isFolder = fs.lstatSync(fileOrFolderPath).isDirectory() | ||
const isFolder = (await lstat(fileOrFolderPath)).isDirectory() | ||
if (isFolder && ignoreFolder !== fileOrFolderPath.toString()) { | ||
@@ -17,0 +19,0 @@ await bulkDelete(fileOrFolderPath, targetFileName, ignoreFolder) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24545
368
10