This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
70 lines (61 loc) · 1.68 KB
/
utils.js
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
63
64
65
66
67
68
69
70
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
/** @param {string} dir */
export function mkdirp(dir) {
try {
fs.mkdirSync(dir, { recursive: true });
} catch (e) {
if (/** @type {any} */ (e).code === 'EEXIST') return;
throw e;
}
}
/** @param {string} path */
export function rimraf(path) {
(fs.rmSync || fs.rmdirSync)(path, { recursive: true, force: true });
}
/**
* @template T
* @param {T} x
*/
function identity(x) {
return x;
}
/**
* @param {string} from
* @param {string} to
* @param {(basename: string) => string} rename
*/
export function copy(from, to, rename = identity) {
if (!fs.existsSync(from)) return;
const stats = fs.statSync(from);
if (stats.isDirectory()) {
fs.readdirSync(from).forEach((file) => {
copy(path.join(from, file), path.join(to, rename(file)));
});
} else {
mkdirp(path.dirname(to));
fs.copyFileSync(from, to);
}
}
/** @param {string} path */
export function dist(path) {
return fileURLToPath(new URL(`./dist/${path}`, import.meta.url).href);
}
/** @type {string} */
export const package_manager = get_package_manager() || 'npm';
/**
* Supports npm, pnpm, Yarn, cnpm, bun and any other package manager that sets the
* npm_config_user_agent env variable.
* Thanks to https://github.com/zkochan/packages/tree/main/which-pm-runs for this code!
*/
function get_package_manager() {
if (!process.env.npm_config_user_agent) {
return undefined;
}
const user_agent = process.env.npm_config_user_agent;
const pm_spec = user_agent.split(' ')[0];
const separator_pos = pm_spec.lastIndexOf('/');
const name = pm_spec.substring(0, separator_pos);
return name === 'npminstall' ? 'cnpm' : name;
}