Skip to content

Commit a5881c5

Browse files
authored
Merge pull request #7 from hlambda/release/0.0.13
Release/0.0.13
2 parents a49d296 + 76a23f6 commit a5881c5

File tree

15 files changed

+587
-38
lines changed

15 files changed

+587
-38
lines changed

CHANGELOG.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Release 0.0.13
2+
3+
- Ability to create new template files, for creating new routes by the best practices
4+
- Add support for entry point files, that do not export default router.
5+
- Ability to set default environment, useful for local development when running commands. (This is not the same as the default that you can set in config.yaml, this can be env specific, thus .env should contain that info)
6+
- Update docs
7+
- Fix security issues via npm audit
8+
- Ability to run server metadata reset
9+
- Clone command
10+
11+
# Release 0.0.12
12+
13+
- Bugfixes
14+
- General improvements
15+
116
# Release 0.0.11
217

318
- Bugfix for Node LTS version, ERR_UNKNOWN_BUILTIN_MODULE
@@ -13,17 +28,17 @@
1328
# Release 0.0.9
1429

1530
- Environments, and env management
16-
- Override values from yaml from .env using {{}} pattern
31+
- Override values from YAML from .env using {{}} pattern
1732
- Snippets, used for quick installation
18-
- ~~ Analytics~~ Replaced with feedback program (We belive in privacy by default)
19-
- Optimize node_modules deployment, such that we can deploy app without zip-ing dependencies
20-
- Check for new version and updates
33+
- ~~ Analytics~~ Replaced with feedback program (We believe in privacy by default)
34+
- Optimize node_modules deployment, such that we can deploy the app without zip-ing dependencies
35+
- Check for new versions and updates
2136
- Shell command execution on metadata apply as post apply script
2237

2338
# Release 0.0.8
2439

2540
- New initialization template
26-
- Init command flags, force and force-remove
41+
- Init command flags, force, and force-remove
2742
- Add new command aliases
2843
- Add changelog
2944
- Fix the issue with passing -c flag to the save

package-lock.json

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

package.json

Lines changed: 2 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.12",
4+
"version": "0.0.13",
55
"description": "CLI for hlambda server.",
66
"main": "src/index.js",
77
"bin": {
@@ -48,6 +48,7 @@
4848
"colors": "^1.4.0",
4949
"commander": "^9.0.0",
5050
"dotenv": "^16.0.0",
51+
"edit-dotenv": "^1.0.4",
5152
"figlet": "^1.5.2",
5253
"formdata-node": "^4.3.2",
5354
"hlambda": "^0.0.4",

src/commands/clone.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import path from 'path';
2+
import { writeFile, mkdir, access } from 'fs/promises';
3+
import rimraf from 'rimraf';
4+
5+
import { errors } from './../errors/index.js';
6+
import init from './init.js';
7+
import { metadataExport } from './metadata.js';
8+
9+
import CLIErrorHandler from './../utils/CLIErrorHandler.js';
10+
11+
export const clone = async (dirName, url, options, program) => {
12+
await (async () => {
13+
const cwd = path.resolve(process.cwd());
14+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
15+
16+
const cloneFilePath = path.resolve(cwd, dirName);
17+
console.log(`Trying to clone app in:`.green, `${cloneFilePath}`.yellow);
18+
19+
const { force, forceRemove } = options;
20+
const includeDemoApp = !options.clean; // Flip the clean flag.
21+
22+
const folderExists = await access(cloneFilePath)
23+
.then((result) => {
24+
return true;
25+
})
26+
.catch((error) => {
27+
// console.log(error);
28+
// throw new Error(errors.ERROR_FS_READ_ERROR);
29+
return false;
30+
});
31+
if (folderExists) {
32+
if (force) {
33+
if (forceRemove) {
34+
if (
35+
typeof cloneFilePath === 'string' &&
36+
cloneFilePath !== '' &&
37+
cloneFilePath !== '/' &&
38+
cloneFilePath !== '/*'
39+
) {
40+
// Sanity check !!!
41+
console.log(`Removing everything inside ${cloneFilePath}`.red);
42+
rimraf.sync(`${cloneFilePath}/*`); // Please be careful...
43+
} else {
44+
throw new Error(errors.ERROR_DANGEROUS_SANITY_CHECK_DID_NOT_PASS);
45+
}
46+
}
47+
} else {
48+
throw new Error(errors.ERROR_FOLDER_ALREADY_EXISTS);
49+
}
50+
}
51+
52+
// Call Init
53+
await init(dirName, options, program, true);
54+
55+
// Write configuration .env with endpoint and admin secret values
56+
const adminSecret = options?.adminSecret ?? '';
57+
58+
// !!! Important !!! Mutate options?.config to point inside hlapp
59+
// eslint-disable-next-line no-param-reassign
60+
options.config = `./${dirName}/`;
61+
62+
const envTemplate = `# Remove "#" to uncomment the env values.
63+
ENV_LOCAL_HLAMBDA_ENDPOINT="${url}"
64+
ENV_LOCAL_HLAMBDA_ADMIN_SECRET="${adminSecret}"
65+
66+
# ENV_DEV_HLAMBDA_ENDPOINT="http://dev-server:8081"
67+
# ENV_DEV_HLAMBDA_ADMIN_SECRET="demo-dev"
68+
69+
# ENV_DEFAULT_ENVIRONMENT="local"
70+
`;
71+
72+
await writeFile(`./${dirName}/.env`, envTemplate, 'utf-8')
73+
.then(() => {
74+
// console.log(`File write ${`./${dirName}/.env`} successfull!`.green);
75+
})
76+
.catch(() => {
77+
console.log(`File write ${`./${dirName}/.env`} failed`.red);
78+
});
79+
80+
// Call metadata export
81+
await metadataExport(options, program);
82+
83+
console.log(`Directory created.`.green);
84+
})()
85+
.then(() => {})
86+
.catch(CLIErrorHandler(program));
87+
};
88+
89+
export default clone;

src/commands/environment/add.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ export const addEnv = async (envName, options, program) => {
1414
const cwd = path.resolve(process.cwd());
1515
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
1616

17-
// Load yaml configuration
18-
const configuration = await loadConfigFromYAML(options);
19-
20-
const endpoint = configuration?.endpoint ?? 'http://localhost:8081';
21-
const adminSecret = options?.adminSecret ?? configuration?.admin_secret ?? '';
22-
23-
// Check if configuration has already the env name
24-
2517
// Create env
2618
const initEnvFilePath = path.resolve(cwd, options.config, 'environments', envName);
2719
console.log(`Trying to add new environment ${envName}:`.green, `${initEnvFilePath}`.yellow);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import path from 'path';
2+
import { writeFile, mkdir, access, readFile } from 'fs/promises';
3+
import editDotenv from 'edit-dotenv';
4+
5+
import { errors } from './../../errors/index.js';
6+
7+
import CLIErrorHandler from './../../utils/CLIErrorHandler.js';
8+
import { loadConfigFromYAML } from './../../utils/loadConfigFromYAML.js';
9+
10+
import { configEnvTemplate } from './../../templates/index.js';
11+
12+
export const defaultEnv = async (envName, options, program) => {
13+
await (async () => {
14+
const cwd = path.resolve(process.cwd());
15+
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
16+
17+
// Create env
18+
const initEnvFilePath = path.resolve(cwd, '.env');
19+
console.log(
20+
`Trying to update .env file and set default to environment ${envName}:`.green,
21+
`${initEnvFilePath}`.yellow
22+
);
23+
24+
const { force, forceRemove } = options;
25+
const includeDemoApp = !options.clean; // Flip the clean flag.
26+
27+
// We don't need to check for this because we presume that he knows what he is doing...
28+
const folderExists = await access(initEnvFilePath)
29+
.then((result) => {
30+
return true;
31+
})
32+
.catch((error) => {
33+
// console.log(error);
34+
// throw new Error(errors.ERROR_FS_READ_ERROR);
35+
return false;
36+
});
37+
38+
// Check if the in the current cwd there is .env file, because the .env we load from current dir.
39+
// envName
40+
41+
// Read .env value if exists
42+
const oldValueOfDotenvFile = await readFile(`${initEnvFilePath}`, 'utf-8')
43+
.then((data) => {
44+
// console.log(`File read ${initEnvFilePath} successfull!`.green);
45+
return data;
46+
})
47+
.catch(() => {
48+
console.log(`File read ${initEnvFilePath}failed`.red);
49+
});
50+
51+
// Edit dotenv with the changes provided to us
52+
const newValueOfDotenvFile = editDotenv(oldValueOfDotenvFile, { ENV_DEFAULT_ENVIRONMENT: `"${envName}"` });
53+
54+
// Save the new values to dotenv file to the local cwd
55+
// console.log(newValueOfDotenvFile);
56+
await writeFile(`${initEnvFilePath}`, newValueOfDotenvFile, 'utf-8')
57+
.then(() => {
58+
// console.log(`File write ${initEnvFilePath} successfull!`.green);
59+
})
60+
.catch(() => {
61+
console.log(`File write ${initEnvFilePath} failed`.red);
62+
});
63+
})()
64+
.then(() => {})
65+
.catch(CLIErrorHandler(program));
66+
};
67+
68+
export default defaultEnv;

src/commands/init.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717

1818
import CLIErrorHandler from './../utils/CLIErrorHandler.js';
1919

20-
export const init = async (dirName, options, program) => {
20+
export const init = async (dirName, options, program, silent = false) => {
2121
await (async () => {
2222
const cwd = path.resolve(process.cwd());
2323
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
@@ -166,10 +166,12 @@ export const init = async (dirName, options, program) => {
166166
});
167167
}
168168

169-
console.log(
170-
`Directory created. Execute the following commands to continue:`.green,
171-
`\n\n ${'cd'.green} ${dirName}\n ${'hlambda'.green} console\n`
172-
);
169+
if (!silent) {
170+
console.log(
171+
`Directory created. Execute the following commands to continue:`.green,
172+
`\n\n ${'cd'.green} ${dirName}\n ${'hlambda'.green} console\n`
173+
);
174+
}
173175
})()
174176
.then(() => {})
175177
.catch(CLIErrorHandler(program));

0 commit comments

Comments
 (0)