-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy-deps.mjs
75 lines (66 loc) · 2.62 KB
/
copy-deps.mjs
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
71
72
73
74
75
import fs from 'fs/promises';
import path from 'path';
import glob from 'glob';
import {createRequire} from 'module';
const require = createRequire(import.meta.url);
// const saxPath = require.resolve('sax-wasm/lib/sax-wasm.wasm');
//
// async function copy() {
// const tsConfigBuffer = await fs.readFile('./tsconfig.json');
// const {compilerOptions: {outDir}} = JSON.parse(tsConfigBuffer.toString());
// await fs.mkdir(outDir, {recursive: true});
// const {name, ext} = path.parse(saxPath)
// const dir = path.resolve(outDir, name + ext);
// await fs.copyFile(saxPath, dir);
// }
export async function findPackageJsonFrom(dependencyPath) {
const parts = dependencyPath.split(path.sep);
let i = parts.length;
while (i--) {
try {
const packageJsonRoot = parts.slice(0, i).join(path.sep);
const packageJsonBuffer = await fs.readFile(path.join(packageJsonRoot, 'package.json'));
const packageJson = JSON.parse(packageJsonBuffer.toString());
return { packageJsonRoot, packageJson };
} catch {
}
}
}
export async function copyDependencies(dependencyName, resolvedOutputPath) {
const require = createRequire(import.meta.url);
const resolvedPath = require.resolve(dependencyName);
const { packageJson: { files, name }, packageJsonRoot } = await findPackageJsonFrom(resolvedPath);
if (files) {
for (let i = 0; i < files.length; i++) {
const from = path.resolve(packageJsonRoot, files[i]);
const globbed = glob.sync(from, { nodir: true });
if (!globbed) {
continue;
}
for (let j = 0; j < globbed.length; j++) {
const [, relativePath] = globbed[j].replace(/[\/]/g, path.sep).split(packageJsonRoot + path.sep);
const to = path.join(resolvedOutputPath, name, relativePath);
const { dir: destinationDir } = path.parse(to);
await fs.mkdir(destinationDir, {recursive: true});
await fs.copyFile(globbed[j], to);
}
}
} else {
const { dir: destinationDir } = path.parse(path.resolve(resolvedOutputPath, name));
await fs.mkdir(destinationDir, {recursive: true});
await fs.copyFile(packageJsonRoot, path.resolve(resolvedOutputPath, name));
}
}
async function copyLocalDependencies() {
const packageJsonBuffer = await fs.readFile('./package.json');
const {dependencies} = JSON.parse(packageJsonBuffer.toString());
const keys = Object.keys(dependencies);
for (let i = 0; i < keys.length; i++) {
const dependencyName = keys[i];
if (dependencyName === 'fs-extra' || dependencyName === 'glob') {
continue;
}
await copyDependencies(dependencyName, 'lib');
}
}
copyLocalDependencies().then();