Skip to content

Commit

Permalink
Release v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Mar 17, 2024
1 parent fbfbb0a commit 5fc834c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion dist/copy-file.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
//! copy-file-util v1.1.3 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
//! copy-file-util v1.2.0 ~~ https://github.com/center-key/copy-file-util ~~ MIT License

export type Settings = {
cd: string;
targetFile: string;
targetFolder: string;
fileExtension: string;
move: boolean;
overwrite: boolean;
};
export type Result = {
origin: string;
dest: string;
duration: number;
moved: boolean;
skipped: boolean;
};
declare const copyFile: {
cp(sourceFile: string, options?: Partial<Settings>): Result;
Expand Down
13 changes: 9 additions & 4 deletions dist/copy-file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! copy-file-util v1.1.3 ~~ https://github.com/center-key/copy-file-util ~~ MIT License
//! copy-file-util v1.2.0 ~~ https://github.com/center-key/copy-file-util ~~ MIT License

import chalk from 'chalk';
import fs from 'fs';
Expand All @@ -13,6 +13,7 @@ const copyFile = {
targetFolder: null,
fileExtension: null,
move: false,
overwrite: true,
};
const settings = { ...defaults, ...options };
const startTime = Date.now();
Expand All @@ -28,6 +29,8 @@ const copyFile = {
const targetFolder = targetPath ? normalize(startFolder + targetPath) : null;
const targetFile = settings.targetFile ?? settings.targetFolder + '/' + sourceFilename;
const target = normalize(startFolder + targetFile);
const targetExists = !missingTarget && fs.existsSync(target);
const skip = targetExists && !settings.overwrite;
if (targetFolder)
fs.mkdirSync(targetFolder, { recursive: true });
const badTargetFolder = !targetFolder || !fs.existsSync(targetFolder);
Expand All @@ -41,14 +44,15 @@ const copyFile = {
null;
if (errorMessage)
throw Error('[copy-file-util] ' + errorMessage);
if (settings.move)
if (!skip && settings.move)
fs.renameSync(source, target);
else
else if (!skip)
fs.copyFileSync(source, target);
return {
origin: source,
dest: target,
moved: settings.move,
skipped: skip,
duration: Date.now() - startTime,
};
},
Expand All @@ -57,7 +61,8 @@ const copyFile = {
const origin = chalk.blue.bold(result.origin);
const dest = chalk.magenta(result.dest);
const arrow = chalk.gray.bold('→');
const info = chalk.white(`(${result.duration}ms${result.moved ? ', move' : ''})`);
const status = result.skipped ? ', skip -- target exists' : result.moved ? ', move' : '';
const info = chalk.white(`(${result.duration}ms${status})`);
log(name, origin, arrow, dest, info);
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "copy-file-util",
"version": "1.1.3",
"version": "1.2.0",
"description": "Copy or rename a file with optional package version number (CLI tool designed for use in npm package.json scripts)",
"license": "MIT",
"type": "module",
Expand Down

0 comments on commit 5fc834c

Please sign in to comment.