@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.
Files changed (2) hide show
  1. package/index.mjs +99 -94
  2. 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
- const [packageName, version] = packageSpec.includes('@') && !packageSpec.startsWith('@')
9
- ? packageSpec.split('@')
10
- : [packageSpec, 'latest'];
11
-
12
- const targetDirName = version === 'latest'
13
- ? packageName
14
- : `${packageName}_${version}`;
15
-
16
- const targetPath = path.resolve(pathToSave, targetDirName);
17
-
18
- if (fs.existsSync(targetPath)) {
19
- return {
20
- success: true,
21
- message: `Package ${packageName}@${version} already exists. Skipped.`,
22
- path: targetPath
23
- };
24
- }
25
-
26
- const tempDir = path.join(process.cwd(), '.temp', `${Date.now()}`);
27
- fs.mkdirSync(tempDir, { recursive: true });
28
-
29
- const fullPackageName = `@hackthedev/${packageName}`;
30
- const installSpec = version === 'latest'
31
- ? fullPackageName
32
- : `${fullPackageName}@${version}`;
33
-
34
- console.log(`Fetching ${installSpec}...`);
35
-
36
- const tarballName = execSync(
37
- `npm pack ${installSpec}`,
38
- { cwd: tempDir, encoding: 'utf8' }
39
- ).trim();
40
-
41
- execSync(`tar -xzf ${tarballName}`, { cwd: tempDir });
42
-
43
- const extractedPath = path.join(tempDir, 'package');
44
-
45
- fs.mkdirSync(pathToSave, { recursive: true });
46
-
47
- this._copyRecursive(extractedPath, targetPath);
48
-
49
- fs.rmSync(tempDir, { recursive: true, force: true });
50
-
51
- return {
52
- success: true,
53
- message: `Successfully installed ${packageName}@${version}`,
54
- path: targetPath
55
- };
56
-
57
- } catch (error) {
58
- return {
59
- success: false,
60
- message: `Error installing ${packageSpec}: ${error.message}`
61
- };
62
- }
63
- }
64
-
65
- static async installMultiple(packages) {
66
- const results = [];
67
- for (const { package: pkg, path: pathToSave } of packages) {
68
- const result = await this.install(pkg, pathToSave);
69
- results.push({ package: pkg, ...result });
70
- }
71
- return results;
72
- }
73
-
74
- static _copyRecursive(src, dest) {
75
- if (!fs.existsSync(src)) return;
76
-
77
- if (fs.statSync(src).isDirectory()) {
78
- fs.mkdirSync(dest, { recursive: true });
79
- const entries = fs.readdirSync(src, { withFileTypes: true });
80
-
81
- for (const entry of entries) {
82
- const srcPath = path.join(src, entry.name);
83
- const destPath = path.join(dest, entry.name);
84
-
85
- if (entry.isDirectory()) {
86
- this._copyRecursive(srcPath, destPath);
87
- } else {
88
- fs.copyFileSync(srcPath, destPath);
89
- }
90
- }
91
- } else {
92
- fs.copyFileSync(src, dest);
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackthedev/frontend-libs",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Simple Frontend Lib installer",
5
5
  "license": "ISC",
6
6
  "author": "",