Skip to content

Commit 746186f

Browse files
committed
feat: create init sub-command, add codebase for metadata sub-command
1 parent c4863e7 commit 746186f

11 files changed

Lines changed: 367 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules/
22
examples/
33
temp/
4+
tmp/
45

56
.DS_*
67
*.sublime*

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules/
22
examples/
33
temp/
4+
tmp/
45

56
.DS_*
67
*.sublime*

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,38 @@ $ yarn add hlambda-cli
1818

1919
## Example
2020

21+
````shell
22+
$ hlambda version
23+
$ hlambda --version
24+
````
25+
26+
````shell
27+
$ hlambda init my-first-hlambda-app
28+
````
29+
30+
````shell
31+
$ cd my-first-hlambda-app
32+
````
33+
Change the admin_secret in the config.yaml and just run
34+
2135
````shell
2236
$ hlambda metadata apply
2337
````
2438

39+
or run the hlambda console with option --admin-secret <your_secret>
40+
2541
````shell
26-
$ hlambda version
27-
$ hlambda --version
42+
$ hlambda metadata apply --admin-secret <your_secret>
43+
````
44+
45+
to export existing data from the hlambda server run
46+
47+
````shell
48+
$ hlambda metadata export
2849
````
50+
51+
## Notice
52+
53+
```
54+
This CLI is still in development. Main functionality may be missing, any contributions are greatly appreciated.
55+
```

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hlambda-cli",
33
"type": "module",
4-
"version": "0.0.1",
4+
"version": "0.0.2",
55
"description": "Hlambda cli.",
66
"main": "src/index.js",
77
"bin": {
@@ -41,6 +41,8 @@
4141
"colors": "^1.4.0",
4242
"commander": "^8.3.0",
4343
"dotenv": "^16.0.0",
44+
"figlet": "^1.5.2",
45+
"hlambda": "^0.0.4",
4446
"node-fetch": "^3.2.0",
4547
"rimraf": "^3.0.2",
4648
"yaml": "^1.10.2"

src/commands/console.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import path from 'path';
2+
3+
import { errors } from './../errors/index.js';
4+
5+
export const startConsole = async () => {
6+
(async () => {
7+
const cwd = path.resolve(process.cwd());
8+
console.log('Executing in cwd:', cwd);
9+
10+
console.log('Starting browser...');
11+
// TODO: Start the server and open browser
12+
13+
})()
14+
.then(() => {})
15+
.catch((error) => { console.log('[Error]'.red, `${error.message}`.red); })
16+
};
17+

src/commands/init.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import path from 'path';
2+
import { writeFile, mkdir, access } from 'fs/promises';
3+
4+
import { errors } from './../errors/index.js';
5+
import { configTemplate, packageJsonTemplate, gitignoreTemplate, routerDemoJsTemplate, errorTemplate, hlambdaYamlTemplate } from './../templates/index.js';
6+
7+
export const init = async (dirName, options) => {
8+
(async () => {
9+
const cwd = path.resolve(process.cwd());
10+
console.log('Executing in cwd:', cwd);
11+
12+
const includeDemoApp = !options.clean; // Flip the clean flag.
13+
14+
const folderExists = await access(`./${dirName}`)
15+
.then((result) => {
16+
return true;
17+
})
18+
.catch((error) => {
19+
// console.log(error);
20+
//throw new Error(errors.ERROR_FS_READ_ERROR);
21+
return false;
22+
});
23+
if (folderExists){
24+
throw new Error(errors.ERROR_FOLDER_ALREADY_EXISTS);
25+
}
26+
27+
await mkdir(`./${dirName}`, { recursive: true })
28+
.then(() => {
29+
// console.log(`Created folder!`.green);
30+
})
31+
.catch((error) => {
32+
console.log(`Create folder failed`.red);
33+
});
34+
35+
// Create metadata folder
36+
await mkdir(`./${dirName}/metadata`, { recursive: true })
37+
.then(() => {
38+
// console.log(`Created folder!`.green);
39+
})
40+
.catch(() => {
41+
console.log(`Create folder failed`.red);
42+
});
43+
44+
await writeFile(`./${dirName}/config.yaml`, configTemplate, 'utf-8')
45+
.then(() => {
46+
// console.log(`File write ${`./${dirName}/config.yaml`} successfull!`.green);
47+
})
48+
.catch(() => {
49+
console.log(`File write ${`./${dirName}/config.yaml`} failed`.red);
50+
});
51+
52+
if (includeDemoApp) {
53+
await writeFile(`./${dirName}/metadata/package.json`, packageJsonTemplate, 'utf-8')
54+
.then(() => {
55+
// console.log(`File write ${`./${dirName}/metadata/package.json`} successfull!`.green);
56+
})
57+
.catch(() => {
58+
console.log(`File write ${`./${dirName}/metadata/package.json`} failed`.red);
59+
});
60+
61+
await writeFile(`./${dirName}/metadata/.gitignore`, gitignoreTemplate, 'utf-8')
62+
.then(() => {
63+
// console.log(`File write ${`./${dirName}/metadata/.gitignore`} successfull!`.green);
64+
})
65+
.catch(() => {
66+
console.log(`File write ${`./${dirName}/metadata/.gitignore`} failed`.red);
67+
});
68+
69+
await mkdir(`./${dirName}/metadata/apps/example_demo_app`, { recursive: true })
70+
.then(() => {
71+
// console.log(`Created folder!`.green);
72+
})
73+
.catch(() => {
74+
console.log(`Create folder failed`.red);
75+
});
76+
77+
await writeFile(`./${dirName}/metadata/apps/example_demo_app/router.demo.js`, routerDemoJsTemplate, 'utf-8')
78+
.then(() => {
79+
// console.log(`File write ${`./${dirName}/metadata/apps/example_demo_app/router.demo.js`} successfull!`.green);
80+
})
81+
.catch(() => {
82+
console.log(`File write ${`./${dirName}/metadata/apps/example_demo_app/router.demo.js`} failed`.red);
83+
});
84+
85+
await writeFile(`./${dirName}/metadata/apps/example_demo_app/errors.demo.js`, errorTemplate, 'utf-8')
86+
.then(() => {
87+
// console.log(`File write ${`./${dirName}/metadata/apps/example_demo_app/errors.demo.js`} successfull!`.green);
88+
})
89+
.catch(() => {
90+
console.log(`File write ${`./${dirName}/metadata/apps/example_demo_app/errors.demo.js`} failed`.red);
91+
});
92+
93+
await writeFile(`./${dirName}/metadata/apps/example_demo_app/hlambda-config.yaml`, hlambdaYamlTemplate, 'utf-8')
94+
.then(() => {
95+
// console.log(`File write ${`./${dirName}/metadata/apps/example_demo_app/hlambda-config.yaml`} successfull!`.green);
96+
})
97+
.catch(() => {
98+
// console.log(`File write ${`./${dirName}/metadata/apps/example_demo_app/hlambda-config.yaml`} failed`.red);
99+
});
100+
}
101+
102+
console.log(
103+
`directory created. execute the following commands to continue:\n\n cd ${dirName}\n hlambda console\n`);
104+
})()
105+
.then(() => {})
106+
.catch((error) => { console.log('[Error]'.red, `${error.message}`.red); })
107+
};
108+

src/commands/metadata.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
export const metadataApply = () => {
2-
console.log('Command still in development!');
1+
import path from 'path';
2+
3+
import { errors } from './../errors/index.js';
4+
5+
export const metadataApply = async () => {
6+
(async () => {
7+
const cwd = path.resolve(process.cwd());
8+
// console.log('Executing in cwd:', cwd);
9+
10+
console.log('Command still in development!');
11+
console.log('Apply metadata!');
12+
// TODO: Start the server and open browser
13+
14+
})()
15+
.then(() => {})
16+
.catch((error) => { console.log('[Error]'.red, `${error.message}`.red); })
317
};
418

5-
export const metadataExport = () => {
6-
console.log('Export metadata!');
19+
export const metadataExport = async () => {
20+
(async () => {
21+
const cwd = path.resolve(process.cwd());
22+
// console.log('Executing in cwd:', cwd);
23+
24+
console.log('Command still in development!');
25+
console.log('Export metadata!');
26+
27+
})()
28+
.then(() => {})
29+
.catch((error) => { console.log('[Error]'.red, `${error.message}`.red); })
730
};

src/errors/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { createErrorDescriptor } from 'hlambda';
2+
3+
// --- START SAFE TO EDIT ---
4+
5+
export const errorsGroupName = 'hlambda-cli';
6+
7+
export const errors = {
8+
FUNCTIONALITY_NOT_IMPLEMENTED: {
9+
message:
10+
'Specific functionality is still in development. (It should be available soon, thank you for understanding.)',
11+
},
12+
ERROR_PAGE_NOT_FOUND: {
13+
message: 'Requested endpoint does not exist.',
14+
},
15+
16+
// Hlambda
17+
ERROR_HLAMBDA_ADMIN_SECRET_DISABLED: {
18+
message: 'Admin secret is disabled.',
19+
},
20+
ERROR_INVALID_HLAMBDA_ADMIN_SECRET: {
21+
message: `Invalida hlambda admin secret! Check 'x-hlambda-admin-secret'.`,
22+
},
23+
24+
// CLI errors
25+
ERROR_FS_READ_ERROR: {
26+
message: 'File system read error!',
27+
},
28+
ERROR_FOLDER_ALREADY_EXISTS: {
29+
message: 'Folder already exists!',
30+
},
31+
32+
// Special errors
33+
UNKNOWN_ERROR: {
34+
message: 'Unknown server error.',
35+
handler: () => {
36+
console.error(
37+
'[ERROR]: DANGER! Unknown error was detected! Please define all known error states and handle them accordingly!'
38+
);
39+
},
40+
},
41+
};
42+
43+
// --- STOP SAFE TO EDIT ---
44+
45+
export const ed = createErrorDescriptor(errors, errorsGroupName, false);
46+
47+
export default errors;

0 commit comments

Comments
 (0)