@hackthedev/frontend-libs 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +99 -94
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,95 +1,100 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { execSync } from 'child_process';
|
|
4
|
-
|
|
5
|
-
export default class FrontendLibs {
|
|
6
|
-
static async install(packageSpec, pathToSave) {
|
|
7
|
-
try {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
fs.
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { execSync } from 'child_process';
|
|
4
|
+
|
|
5
|
+
export default class FrontendLibs {
|
|
6
|
+
static async install(packageSpec, pathToSave) {
|
|
7
|
+
try {
|
|
8
|
+
let packageName, version;
|
|
9
|
+
|
|
10
|
+
// parse package name and version from spec
|
|
11
|
+
const atIndex = packageSpec.lastIndexOf('@');
|
|
12
|
+
if (atIndex > 0) {
|
|
13
|
+
packageName = packageSpec.substring(0, atIndex);
|
|
14
|
+
version = packageSpec.substring(atIndex + 1);
|
|
15
|
+
} else {
|
|
16
|
+
packageName = packageSpec;
|
|
17
|
+
version = 'latest';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// extract clean folder name from scoped package
|
|
21
|
+
const folderName = packageName.includes('/')
|
|
22
|
+
? packageName.split('/').pop()
|
|
23
|
+
: packageName;
|
|
24
|
+
|
|
25
|
+
const targetPath = path.resolve(pathToSave, folderName);
|
|
26
|
+
|
|
27
|
+
if (fs.existsSync(targetPath)) {
|
|
28
|
+
console.log(`Removing old version of ${folderName}...`);
|
|
29
|
+
fs.rmSync(targetPath, { recursive: true, force: true });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const tempDir = path.join(process.cwd(), '.temp', `${Date.now()}`);
|
|
33
|
+
fs.mkdirSync(tempDir, { recursive: true });
|
|
34
|
+
|
|
35
|
+
const installSpec = version === 'latest'
|
|
36
|
+
? packageName
|
|
37
|
+
: `${packageName}@${version}`;
|
|
38
|
+
|
|
39
|
+
console.log(`Fetching ${installSpec}...`);
|
|
40
|
+
|
|
41
|
+
const tarballName = execSync(
|
|
42
|
+
`npm pack ${installSpec}`,
|
|
43
|
+
{ cwd: tempDir, encoding: 'utf8' }
|
|
44
|
+
).trim();
|
|
45
|
+
|
|
46
|
+
execSync(`tar -xzf ${tarballName}`, { cwd: tempDir });
|
|
47
|
+
|
|
48
|
+
const extractedPath = path.join(tempDir, 'package');
|
|
49
|
+
|
|
50
|
+
fs.mkdirSync(pathToSave, { recursive: true });
|
|
51
|
+
|
|
52
|
+
this._copyRecursive(extractedPath, targetPath);
|
|
53
|
+
|
|
54
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
success: true,
|
|
58
|
+
message: `Successfully installed ${packageName}@${version}`,
|
|
59
|
+
path: targetPath
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
} catch (error) {
|
|
63
|
+
return {
|
|
64
|
+
success: false,
|
|
65
|
+
message: `Error installing ${packageSpec}: ${error.message}`
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static async installMultiple(packages) {
|
|
71
|
+
const results = [];
|
|
72
|
+
for (const { package: pkg, path: pathToSave } of packages) {
|
|
73
|
+
const result = await this.install(pkg, pathToSave);
|
|
74
|
+
results.push({ package: pkg, ...result });
|
|
75
|
+
}
|
|
76
|
+
return results;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static _copyRecursive(src, dest) {
|
|
80
|
+
if (!fs.existsSync(src)) return;
|
|
81
|
+
|
|
82
|
+
if (fs.statSync(src).isDirectory()) {
|
|
83
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
84
|
+
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
85
|
+
|
|
86
|
+
for (const entry of entries) {
|
|
87
|
+
const srcPath = path.join(src, entry.name);
|
|
88
|
+
const destPath = path.join(dest, entry.name);
|
|
89
|
+
|
|
90
|
+
if (entry.isDirectory()) {
|
|
91
|
+
this._copyRecursive(srcPath, destPath);
|
|
92
|
+
} else {
|
|
93
|
+
fs.copyFileSync(srcPath, destPath);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
fs.copyFileSync(src, dest);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
95
100
|
}
|