Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index.js « move-file « node_modules - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 09e31acaace3ccddcf3f5086740956571f3042a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const path = require('path');
const fs = require('fs');
const pathExists = require('path-exists');

const fsP = fs.promises;

module.exports = async (source, destination, options) => {
	if (!source || !destination) {
		throw new TypeError('`source` and `destination` file required');
	}

	options = {
		overwrite: true,
		...options
	};

	if (!options.overwrite && await pathExists(destination)) {
		throw new Error(`The destination file exists: ${destination}`);
	}

	await fsP.mkdir(path.dirname(destination), {recursive: true});

	try {
		await fsP.rename(source, destination);
	} catch (error) {
		if (error.code === 'EXDEV') {
			await fsP.copyFile(source, destination);
			await fsP.unlink(source);
		} else {
			throw error;
		}
	}
};

module.exports.sync = (source, destination, options) => {
	if (!source || !destination) {
		throw new TypeError('`source` and `destination` file required');
	}

	options = {
		overwrite: true,
		...options
	};

	if (!options.overwrite && fs.existsSync(destination)) {
		throw new Error(`The destination file exists: ${destination}`);
	}

	fs.mkdirSync(path.dirname(destination), {recursive: true});

	try {
		fs.renameSync(source, destination);
	} catch (error) {
		if (error.code === 'EXDEV') {
			fs.copyFileSync(source, destination);
			fs.unlinkSync(source);
		} else {
			throw error;
		}
	}
};