Skip to content

Commit 077cf12

Browse files
committed
feat: release v0.0.8
1 parent d7b6158 commit 077cf12

File tree

15 files changed

+449
-82
lines changed

15 files changed

+449
-82
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Release 0.0.8
2+
3+
- New initialization template
4+
- Init command flags, force and force-remove
5+
- Add new command aliases
6+
- Add changelog
7+
- Fix the issue with passing -c flag to the save
8+
- Colorize the output
9+
- No color output flag --no-color
10+
- Handle request connection error
11+
- Add aliases

package-lock.json

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

package.json

Lines changed: 1 addition & 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.7",
4+
"version": "0.0.8",
55
"description": "CLI for hlambda server.",
66
"main": "src/index.js",
77
"bin": {

src/commands/config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { loadConfigFromYAML } from './../utils/loadConfigFromYAML.js';
1111
export const config = async (options, program) => {
1212
await (async () => {
1313
const cwd = path.resolve(process.cwd());
14-
console.log('Executing in cwd:', cwd);
14+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
1515

1616
// Load yaml configuration
1717
const configuration = await loadConfigFromYAML(options);
@@ -21,13 +21,14 @@ export const config = async (options, program) => {
2121

2222
const configurationFilePath = path.resolve(cwd, options.config, 'config.yaml');
2323

24+
// Read the raw content of the config file...
2425
const data = await readFile(configurationFilePath, 'utf8')
2526
.then((fileData) => {
26-
console.log(`Config`.green, `${configurationFilePath}`.yellow, `successfully loaded...`.green);
27+
// console.log(`Config`.green, `${configurationFilePath}`.yellow, `successfully loaded...`.green);
2728
return fileData;
2829
})
2930
.catch((error) => {
30-
console.error(`Config`.red, `${configurationFilePath.yellow}`, `errored out...`.red);
31+
// console.error(`Config`.red, `${configurationFilePath.yellow}`, `errored out...`.red);
3132
// console.error(error);
3233
return undefined;
3334
});

src/commands/console.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,26 @@ import open from 'open';
77
import { errors } from './../errors/index.js';
88

99
import CLIErrorHandler from './../utils/CLIErrorHandler.js';
10+
import { loadConfigFromYAML } from './../utils/loadConfigFromYAML.js';
1011

1112
export const startConsole = async (options, program) => {
1213
await (async () => {
1314
const cwd = path.resolve(process.cwd());
14-
console.log('Executing in cwd:', cwd);
15-
16-
// TODO: Maybe check for both .yaml and .yml in future...
17-
const configurationFilePath = path.resolve(cwd, options.config, 'config.yaml');
18-
const configuration = await readFile(configurationFilePath, 'utf8')
19-
.then((fileData) => {
20-
const result = YAML.parse(fileData);
21-
console.log(
22-
`[configuration loader] Config`.green,
23-
`${configurationFilePath}`.yellow,
24-
`successfully loaded...`.green
25-
);
26-
return result;
27-
})
28-
.catch((error) => {
29-
console.error(`[configuration loader] Config`.red, `${configurationFilePath.yellow}`, `errored out...`.red);
30-
// console.error(error);
31-
return undefined;
32-
});
15+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
16+
17+
// Load yaml configuration
18+
const configuration = await loadConfigFromYAML(options);
19+
3320
if (typeof configuration === 'undefined') {
3421
throw Error(errors.ERROR_CONFIGURATION_FILE_IS_MISSING);
3522
}
3623

37-
const endpoint = configuration?.endpoint ?? 'http://localhost:8081';
24+
const endpoint = options?.endpoint ?? configuration?.endpoint ?? 'http://localhost:8081';
3825
const adminSecret = options?.adminSecret ?? configuration?.admin_secret ?? '';
3926

40-
console.log('Starting browser...');
41-
4227
// Sanity check to not execute open on evilCalc.exe :) , we only open URLS that start with `http://` or `https://`
4328
if (`${endpoint}/console`.match(/^https?:\/\//g)) {
29+
console.log('Starting browser...'.green);
4430
open(`${endpoint}/console`);
4531
} else {
4632
throw new Error(errors.ERROR_INVALID_ENDPOINT_URL);

src/commands/init.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path';
22
import { writeFile, mkdir, access } from 'fs/promises';
3+
import rimraf from 'rimraf';
34

45
import { errors } from './../errors/index.js';
56
import {
@@ -9,18 +10,23 @@ import {
910
routerDemoJsTemplate,
1011
errorTemplate,
1112
hlambdaYamlTemplate,
13+
hlambdaREADMETemplate,
1214
} from './../templates/index.js';
1315

1416
import CLIErrorHandler from './../utils/CLIErrorHandler.js';
1517

1618
export const init = async (dirName, options, program) => {
1719
await (async () => {
1820
const cwd = path.resolve(process.cwd());
19-
console.log('Executing in cwd:', cwd);
21+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
2022

23+
const initFilePath = path.resolve(cwd, dirName);
24+
console.log(`Trying to initialize app in:`.green, `${initFilePath}`.yellow);
25+
26+
const { force, forceRemove } = options;
2127
const includeDemoApp = !options.clean; // Flip the clean flag.
2228

23-
const folderExists = await access(`./${dirName}`)
29+
const folderExists = await access(initFilePath)
2430
.then((result) => {
2531
return true;
2632
})
@@ -30,7 +36,24 @@ export const init = async (dirName, options, program) => {
3036
return false;
3137
});
3238
if (folderExists) {
33-
throw new Error(errors.ERROR_FOLDER_ALREADY_EXISTS);
39+
if (force) {
40+
if (forceRemove) {
41+
if (
42+
typeof initFilePath === 'string' &&
43+
initFilePath !== '' &&
44+
initFilePath !== '/' &&
45+
initFilePath !== '/*'
46+
) {
47+
// Sanity check !!!
48+
console.log(`Removing everything inside ${initFilePath}`.red);
49+
rimraf.sync(`${initFilePath}/*`); // Please be careful...
50+
} else {
51+
throw new Error(errors.ERROR_DANGEROUS_SANITY_CHECK_DID_NOT_PASS);
52+
}
53+
}
54+
} else {
55+
throw new Error(errors.ERROR_FOLDER_ALREADY_EXISTS);
56+
}
3457
}
3558

3659
await mkdir(`./${dirName}`, { recursive: true })
@@ -59,6 +82,14 @@ export const init = async (dirName, options, program) => {
5982
});
6083

6184
if (includeDemoApp) {
85+
await writeFile(`./${dirName}/README.md`, hlambdaREADMETemplate, 'utf-8')
86+
.then(() => {
87+
// console.log(`File write ${`./${dirName}/README.md`} successfull!`.green);
88+
})
89+
.catch(() => {
90+
console.log(`File write ${`./${dirName}/README.md`} failed`.red);
91+
});
92+
6293
await writeFile(`./${dirName}/metadata/package.json`, packageJsonTemplate, 'utf-8')
6394
.then(() => {
6495
// console.log(`File write ${`./${dirName}/metadata/package.json`} successfull!`.green);
@@ -109,7 +140,8 @@ export const init = async (dirName, options, program) => {
109140
}
110141

111142
console.log(
112-
`directory created. execute the following commands to continue:\n\n cd ${dirName}\n hlambda console\n`
143+
`Directory created. Execute the following commands to continue:`.green,
144+
`\n\n ${'cd'.green} ${dirName}\n ${'hlambda'.green} console\n`
113145
);
114146
})()
115147
.then(() => {})

src/commands/metadata.js

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
2-
import { readFile } from 'fs/promises';
3-
import YAML from 'yaml';
2+
// import { readFile } from 'fs/promises';
3+
// import YAML from 'yaml';
44
import fetch from 'node-fetch';
55
// import FormData from 'form-data';
66
import { FormData, File } from 'formdata-node';
@@ -15,7 +15,7 @@ import { loadConfigFromYAML } from './../utils/loadConfigFromYAML.js';
1515
export const serverReload = async (options, program) => {
1616
await (async () => {
1717
const cwd = path.resolve(process.cwd());
18-
console.log('Executing in cwd:', cwd);
18+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
1919

2020
// Load yaml configuration
2121
const configuration = await loadConfigFromYAML(options);
@@ -32,7 +32,9 @@ export const serverReload = async (options, program) => {
3232
headers,
3333
});
3434

35-
console.log('Metadata reloaded!');
35+
if (response.status === 200) {
36+
console.log('Metadata reloaded!'.green);
37+
}
3638
console.log(response.status);
3739
})()
3840
.then(() => {})
@@ -42,7 +44,7 @@ export const serverReload = async (options, program) => {
4244
export const serverClearMetadata = async (options, program) => {
4345
await (async () => {
4446
const cwd = path.resolve(process.cwd());
45-
console.log('Executing in cwd:', cwd);
47+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
4648

4749
// Load yaml configuration
4850
const configuration = await loadConfigFromYAML(options);
@@ -59,8 +61,27 @@ export const serverClearMetadata = async (options, program) => {
5961
headers,
6062
});
6163

62-
console.log('Metadata cleared!');
64+
if (response.status === 200) {
65+
console.log('Metadata cleared!'.green);
66+
}
6367
console.log(response.status);
68+
69+
if (response.status !== 200) {
70+
throw new Error(errors.ERROR_INVALID_HLAMBDA_ADMIN_SECRET);
71+
}
72+
73+
// This is magic from commander, the real flag was --no-auto-reload but we get positive logic transformation to autoReload
74+
if (options?.autoReload) {
75+
const responseRestart = await fetch(`${endpoint}/console/api/v1/trigger-restart`, {
76+
method: 'GET',
77+
// body: formData,
78+
headers,
79+
});
80+
if (responseRestart.status === 200) {
81+
console.log('Metadata reloaded after clearing!'.green);
82+
}
83+
console.log(responseRestart.status);
84+
}
6485
})()
6586
.then(() => {})
6687
.catch(CLIErrorHandler(program));
@@ -69,7 +90,7 @@ export const serverClearMetadata = async (options, program) => {
6990
export const metadataApply = async (options, program) => {
7091
await (async () => {
7192
const cwd = path.resolve(process.cwd());
72-
console.log('Executing in cwd:', cwd);
93+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
7394

7495
// console.log(options);
7596

@@ -83,7 +104,7 @@ export const metadataApply = async (options, program) => {
83104
// ZIP the metadata
84105
const zip = new AdmZip();
85106
zip.addLocalFolder(path.resolve(cwd, options.config, metadataDirectory));
86-
console.log('Creating zip from the metadata');
107+
console.log('Creating zip from the metadata'.yellow);
87108

88109
// POST metadata to the API
89110
const payloadAsBuffer = zip.toBuffer();
@@ -115,7 +136,9 @@ export const metadataApply = async (options, program) => {
115136
headers,
116137
});
117138

118-
console.log('Metadata applied!');
139+
if (response.status === 200) {
140+
console.log('Metadata applied!'.green);
141+
}
119142
console.log(response.status);
120143

121144
// This is magic from commander, the real flag was --no-auto-reload but we get positive logic transformation to autoReload
@@ -125,7 +148,7 @@ export const metadataApply = async (options, program) => {
125148
// body: formData,
126149
headers,
127150
});
128-
console.log('Metadata reloaded!');
151+
console.log('Metadata reloaded!'.green);
129152
console.log(responseRestart.status);
130153
}
131154
})()
@@ -136,7 +159,7 @@ export const metadataApply = async (options, program) => {
136159
export const metadataExport = async (options, program) => {
137160
(async () => {
138161
const cwd = path.resolve(process.cwd());
139-
console.log('Executing in cwd:', cwd);
162+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
140163

141164
// console.log(options);
142165

@@ -171,14 +194,26 @@ export const metadataExport = async (options, program) => {
171194

172195
const metadataFilePath = path.resolve(cwd, options.config, metadataDirectory);
173196

174-
console.log(`Removing all the local metadata... ${metadataFilePath}`);
175-
176197
// Dangerous when you think about it... small mistake here and it can delete a lot of things, please be careful!
177-
rimraf.sync(metadataFilePath);
198+
if (
199+
typeof metadataFilePath === 'string' &&
200+
metadataFilePath !== '' &&
201+
metadataFilePath !== '/' &&
202+
metadataFilePath !== '/*'
203+
) {
204+
// Sanity check !!!
205+
console.log(`Removing all the local metadata... ${metadataFilePath}`.yellow);
206+
rimraf.sync(metadataFilePath); // Please be careful...
207+
} else {
208+
throw new Error(errors.ERROR_DANGEROUS_SANITY_CHECK_DID_NOT_PASS);
209+
}
178210

179211
zip.extractAllTo(metadataFilePath, true);
180212

181-
console.log('Metadata exported!');
213+
if (response.status === 200) {
214+
console.log('Metadata exported!'.green);
215+
}
216+
console.log(response.status);
182217
})()
183218
.then(() => {})
184219
.catch(CLIErrorHandler(program));

src/commands/requests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { loadConfigFromYAML } from './../utils/loadConfigFromYAML.js';
1010
export const requests = (method) => async (route, options, program) => {
1111
await (async () => {
1212
const cwd = path.resolve(process.cwd());
13-
console.log('Executing in cwd:', cwd);
13+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
1414

1515
// Load yaml configuration
1616
const configuration = await loadConfigFromYAML(options);

src/commands/save.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { loadConfigFromYAML } from './../utils/loadConfigFromYAML.js';
1111
export const save = async (options, program) => {
1212
await (async () => {
1313
const cwd = path.resolve(process.cwd());
14-
console.log('Executing in cwd:', cwd);
14+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
1515

1616
// Load yaml configuration
1717
const configuration = await loadConfigFromYAML(options);

0 commit comments

Comments
 (0)