@hackthedev/frontend-libs 1.0.1 → 1.0.4
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 +115 -94
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1,95 +1,116 @@
|
|
|
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
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
fs.mkdirSync(
|
|
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
|
+
const versionFile = path.join(targetPath, '.version');
|
|
27
|
+
|
|
28
|
+
// check if already installed with same version
|
|
29
|
+
if (fs.existsSync(targetPath) && fs.existsSync(versionFile)) {
|
|
30
|
+
const installedVersion = fs.readFileSync(versionFile, 'utf8').trim();
|
|
31
|
+
if (installedVersion === version) {
|
|
32
|
+
return {
|
|
33
|
+
success: true,
|
|
34
|
+
message: `Package ${packageName}@${version} already installed. Skipped.`,
|
|
35
|
+
path: targetPath,
|
|
36
|
+
skipped: true
|
|
37
|
+
};
|
|
38
|
+
} else {
|
|
39
|
+
console.log(`Removing old version ${installedVersion} of ${folderName}...`);
|
|
40
|
+
fs.rmSync(targetPath, { recursive: true, force: true });
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const tempDir = path.join(process.cwd(), '.temp', `${Date.now()}`);
|
|
45
|
+
fs.mkdirSync(tempDir, { recursive: true });
|
|
46
|
+
|
|
47
|
+
const installSpec = version === 'latest'
|
|
48
|
+
? packageName
|
|
49
|
+
: `${packageName}@${version}`;
|
|
50
|
+
|
|
51
|
+
console.log(`Fetching ${installSpec}...`);
|
|
52
|
+
|
|
53
|
+
const tarballName = execSync(
|
|
54
|
+
`npm pack ${installSpec}`,
|
|
55
|
+
{ cwd: tempDir, encoding: 'utf8' }
|
|
56
|
+
).trim();
|
|
57
|
+
|
|
58
|
+
execSync(`tar -xzf ${tarballName}`, { cwd: tempDir });
|
|
59
|
+
|
|
60
|
+
const extractedPath = path.join(tempDir, 'package');
|
|
61
|
+
|
|
62
|
+
fs.mkdirSync(pathToSave, { recursive: true });
|
|
63
|
+
|
|
64
|
+
this._copyRecursive(extractedPath, targetPath);
|
|
65
|
+
|
|
66
|
+
// write version file
|
|
67
|
+
fs.writeFileSync(versionFile, version, 'utf8');
|
|
68
|
+
|
|
69
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
success: true,
|
|
73
|
+
message: `Successfully installed ${packageName}@${version}`,
|
|
74
|
+
path: targetPath,
|
|
75
|
+
skipped: false
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return {
|
|
80
|
+
success: false,
|
|
81
|
+
message: `Error installing ${packageSpec}: ${error.message}`
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static async installMultiple(packages) {
|
|
87
|
+
const results = [];
|
|
88
|
+
for (const { package: pkg, path: pathToSave } of packages) {
|
|
89
|
+
const result = await this.install(pkg, pathToSave);
|
|
90
|
+
results.push({ package: pkg, ...result });
|
|
91
|
+
}
|
|
92
|
+
return results;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
static _copyRecursive(src, dest) {
|
|
96
|
+
if (!fs.existsSync(src)) return;
|
|
97
|
+
|
|
98
|
+
if (fs.statSync(src).isDirectory()) {
|
|
99
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
100
|
+
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
101
|
+
|
|
102
|
+
for (const entry of entries) {
|
|
103
|
+
const srcPath = path.join(src, entry.name);
|
|
104
|
+
const destPath = path.join(dest, entry.name);
|
|
105
|
+
|
|
106
|
+
if (entry.isDirectory()) {
|
|
107
|
+
this._copyRecursive(srcPath, destPath);
|
|
108
|
+
} else {
|
|
109
|
+
fs.copyFileSync(srcPath, destPath);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
fs.copyFileSync(src, dest);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
95
116
|
}
|