nh-library 1.0.2 → 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/cli/index.cjs CHANGED
@@ -3,82 +3,56 @@
3
3
  const fs = require("fs");
4
4
  const path = require("path");
5
5
 
6
- // 1. Setup Paths
7
- // __dirname is /node_modules/nh-library/cli
8
- // We need to go up one level to reach /node_modules/nh-library/
9
- const libraryRoot = path.join(__dirname, "..");
10
- const projectRoot = process.cwd();
11
-
12
- // 2. Get the component name from CLI arguments
6
+ const RAW_BASE_URL = "https://raw.githubusercontent.com/NirmataHub/NH-Library/main/";
13
7
  const componentName = process.argv[2];
8
+ const libraryRoot = path.join(__dirname, "..");
14
9
 
15
- if (!componentName) {
16
- console.error("\x1b[31m%s\x1b[0m", "Error: Please specify a component name.");
17
- console.log("Usage: npx nh-library <ComponentName>");
18
- process.exit(1);
19
- }
20
-
21
- // 3. Load the registry
10
+ // 1. Load Registry
22
11
  const registryPath = path.join(libraryRoot, "registry", "components.json");
23
-
24
12
  if (!fs.existsSync(registryPath)) {
25
- console.error("\x1b[31m%s\x1b[0m", "Error: Registry file not found in the library.");
13
+ console.error(" Registry not found.");
26
14
  process.exit(1);
27
15
  }
28
-
29
16
  const registry = require(registryPath);
30
17
 
31
- const componentConfig = registry[componentName];
32
-
33
- if (!componentConfig) {
34
- console.error("\x1b[31m%s\x1b[0m", `Error: Component "${componentName}" not found in registry.`);
35
- console.log("Available components:", Object.keys(registry).join(", "));
18
+ if (!componentName || !registry[componentName]) {
19
+ console.error(`❌ Component "${componentName || ''}" not found.`);
20
+ console.log("Available:", Object.keys(registry).join(", "));
36
21
  process.exit(1);
37
22
  }
38
23
 
39
- // 4. Define Destination (Standardizing on src/components/ui)
40
- const destFolder = path.join(projectRoot, "src", "components", "ui", componentName);
24
+ async function downloadComponent() {
25
+ const componentConfig = registry[componentName];
26
+ const projectRoot = process.cwd();
27
+
28
+ // Smart pathing: if user is in 'src', don't add another 'src'
29
+ const baseFolder = path.basename(projectRoot) === "src" ? "" : "src";
30
+ const destFolder = path.join(projectRoot, baseFolder, "components", "ui", componentName);
41
31
 
42
- // 5. Recursive Copy Function
43
- function copyRecursive(src, dest) {
44
- const exists = fs.existsSync(src);
45
- const stats = exists && fs.statSync(src);
46
- const isDirectory = exists && stats.isDirectory();
32
+ if (!fs.existsSync(destFolder)) {
33
+ fs.mkdirSync(destFolder, { recursive: true });
34
+ }
47
35
 
48
- if (isDirectory) {
49
- if (!fs.existsSync(dest)) {
50
- fs.mkdirSync(dest, { recursive: true });
36
+ console.log(`\n🚀 Installing ${componentName}...`);
37
+
38
+ for (const fileRelativePath of componentConfig.files) {
39
+ const url = `${RAW_BASE_URL}${fileRelativePath}`;
40
+ const fileName = path.basename(fileRelativePath);
41
+ const destPath = path.join(destFolder, fileName);
42
+
43
+ try {
44
+ const response = await fetch(url);
45
+ if (!response.ok) throw new Error(`${response.statusText}`);
46
+
47
+ const content = await response.text();
48
+ fs.writeFileSync(destPath, content);
49
+ console.log(` ✅ Downloaded: ${fileName}`);
50
+ } catch (err) {
51
+ console.error(` ❌ Failed to download ${fileName}: ${err.message}`);
51
52
  }
52
- fs.readdirSync(src).forEach((childItemName) => {
53
- copyRecursive(
54
- path.join(src, childItemName),
55
- path.join(dest, childItemName)
56
- );
57
- });
58
- } else {
59
- fs.copyFileSync(src, dest);
60
- console.log(` \x1b[32m%s\x1b[0m`, `ADDED: ${path.relative(projectRoot, dest)}`);
61
53
  }
62
- }
63
-
64
- // 6. Execution
65
- console.log(`\nInstalling ${componentName} into your project...`);
66
54
 
67
- try {
68
- componentConfig.files.forEach((fileRelativePath) => {
69
- // Construct the absolute path to the source in node_modules
70
- const sourcePath = path.join(libraryRoot, fileRelativePath);
71
-
72
- if (fs.existsSync(sourcePath)) {
73
- copyRecursive(sourcePath, destFolder);
74
- } else {
75
- console.warn(`\x1b[33m%s\x1b[0m`, `Warning: Source path not found: ${fileRelativePath}`);
76
- }
77
- });
55
+ console.log(`\n✨ Success! ${componentName} is now in /src/components/ui/${componentName}\n`);
56
+ }
78
57
 
79
- console.log(`\n\x1b[32m%s\x1b[0m`, `Success! ${componentName} is ready to use.`);
80
- console.log(`Location: /src/components/ui/${componentName}\n`);
81
- } catch (error) {
82
- console.error("\x1b[31m%s\x1b[0m", "An error occurred during installation:");
83
- console.error(error);
84
- }
58
+ downloadComponent();
package/package.json CHANGED
@@ -1,17 +1,12 @@
1
1
  {
2
2
  "name": "nh-library",
3
- "version": "1.0.2",
4
- "description": "CLI-based React component library",
5
- "main": "cli/index.cjs",
3
+ "version": "1.0.4",
6
4
  "bin": {
7
- "nh-lib": "./cli/index.cjs"
5
+ "nh-library": "./cli/index.cjs"
8
6
  },
9
7
  "files": [
10
8
  "cli",
11
- "registry",
12
- "Components"
9
+ "registry"
13
10
  ],
14
- "type": "commonjs",
15
- "author": "Your Name",
16
- "license": "MIT"
11
+ "type": "commonjs"
17
12
  }
@@ -1,5 +1,20 @@
1
1
  {
2
- "ComponentTestOne": { "files": ["Components/ComponentTestOne"] },
3
- "ComponentTestTwo": { "files": ["Components/ComponentTestTwo"] },
4
- "ComponentTestTree": { "files": ["Components/ComponentTestTree"] }
2
+ "ComponentTestOne": {
3
+ "files": [
4
+ "Components/ComponentTestOne/ComponentTest.css",
5
+ "Components/ComponentTestOne/ComponentTest.jsx"
6
+ ]
7
+ },
8
+ "ComponentTestTwo": {
9
+ "files": [
10
+ "Components/ComponentTestTwo/ComponentTest.css",
11
+ "Components/ComponentTestTwo/ComponentTestTwo.jsx"
12
+ ]
13
+ },
14
+ "ComponentTestTree": {
15
+ "files": [
16
+ "Components/ComponentTestTree/ComponentTest.css",
17
+ "Components/ComponentTestTree/ComponentTestTree.jsx"
18
+ ]
19
+ }
5
20
  }
@@ -1,3 +0,0 @@
1
- h1{
2
- color: cyan;
3
- }
@@ -1,11 +0,0 @@
1
- import "./ComponentTest.css"
2
-
3
- const ComponentTest = () => {
4
- return (
5
- <div>
6
- <h1>NH-Lib is on Git! </h1>
7
- </div>
8
- )
9
- }
10
-
11
- export default ComponentTest
@@ -1,3 +0,0 @@
1
- h1{
2
- color: cyan;
3
- }
@@ -1,11 +0,0 @@
1
- import "./ComponentTest.css"
2
-
3
- const ComponentTestThree = () => {
4
- return (
5
- <div>
6
- <h1>Nirmata is here 3333! </h1>
7
- </div>
8
- )
9
- }
10
-
11
- export default ComponentTestThree
@@ -1,3 +0,0 @@
1
- h1{
2
- color: cyan;
3
- }
@@ -1,11 +0,0 @@
1
- import "./ComponentTest.css"
2
-
3
- const ComponentTestTwo = () => {
4
- return (
5
- <div>
6
- <h1>Nirmata is here 222! </h1>
7
- </div>
8
- )
9
- }
10
-
11
- export default ComponentTestTwo