Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Release 0.0.13

- Ability to create new template files, for creating new routes by the best practices
- Add support for entry point files, that do not export default router.
- 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)
- Update docs
- Fix security issues via npm audit
- Ability to run server metadata reset
- Clone command

# Release 0.0.12

- Bugfixes
- General improvements

# Release 0.0.11

- Bugfix for Node LTS version, ERR_UNKNOWN_BUILTIN_MODULE
Expand All @@ -13,17 +28,17 @@
# Release 0.0.9

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

# Release 0.0.8

- New initialization template
- Init command flags, force and force-remove
- Init command flags, force, and force-remove
- Add new command aliases
- Add changelog
- Fix the issue with passing -c flag to the save
Expand Down
63 changes: 55 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hlambda-cli",
"type": "module",
"version": "0.0.12",
"version": "0.0.13",
"description": "CLI for hlambda server.",
"main": "src/index.js",
"bin": {
Expand Down Expand Up @@ -48,6 +48,7 @@
"colors": "^1.4.0",
"commander": "^9.0.0",
"dotenv": "^16.0.0",
"edit-dotenv": "^1.0.4",
"figlet": "^1.5.2",
"formdata-node": "^4.3.2",
"hlambda": "^0.0.4",
Expand Down
89 changes: 89 additions & 0 deletions src/commands/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import path from 'path';
import { writeFile, mkdir, access } from 'fs/promises';
import rimraf from 'rimraf';

import { errors } from './../errors/index.js';
import init from './init.js';
import { metadataExport } from './metadata.js';

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

export const clone = async (dirName, url, options, program) => {
await (async () => {
const cwd = path.resolve(process.cwd());
console.log('Executing in cwd:'.green, `${cwd}`.yellow);

const cloneFilePath = path.resolve(cwd, dirName);
console.log(`Trying to clone app in:`.green, `${cloneFilePath}`.yellow);

const { force, forceRemove } = options;
const includeDemoApp = !options.clean; // Flip the clean flag.

const folderExists = await access(cloneFilePath)
.then((result) => {
return true;
})
.catch((error) => {
// console.log(error);
// throw new Error(errors.ERROR_FS_READ_ERROR);
return false;
});
if (folderExists) {
if (force) {
if (forceRemove) {
if (
typeof cloneFilePath === 'string' &&
cloneFilePath !== '' &&
cloneFilePath !== '/' &&
cloneFilePath !== '/*'
) {
// Sanity check !!!
console.log(`Removing everything inside ${cloneFilePath}`.red);
rimraf.sync(`${cloneFilePath}/*`); // Please be careful...
} else {
throw new Error(errors.ERROR_DANGEROUS_SANITY_CHECK_DID_NOT_PASS);
}
}
} else {
throw new Error(errors.ERROR_FOLDER_ALREADY_EXISTS);
}
}

// Call Init
await init(dirName, options, program, true);

// Write configuration .env with endpoint and admin secret values
const adminSecret = options?.adminSecret ?? '';

// !!! Important !!! Mutate options?.config to point inside hlapp
// eslint-disable-next-line no-param-reassign
options.config = `./${dirName}/`;

const envTemplate = `# Remove "#" to uncomment the env values.
ENV_LOCAL_HLAMBDA_ENDPOINT="${url}"
ENV_LOCAL_HLAMBDA_ADMIN_SECRET="${adminSecret}"

# ENV_DEV_HLAMBDA_ENDPOINT="http://dev-server:8081"
# ENV_DEV_HLAMBDA_ADMIN_SECRET="demo-dev"

# ENV_DEFAULT_ENVIRONMENT="local"
`;

await writeFile(`./${dirName}/.env`, envTemplate, 'utf-8')
.then(() => {
// console.log(`File write ${`./${dirName}/.env`} successfull!`.green);
})
.catch(() => {
console.log(`File write ${`./${dirName}/.env`} failed`.red);
});

// Call metadata export
await metadataExport(options, program);

console.log(`Directory created.`.green);
})()
.then(() => {})
.catch(CLIErrorHandler(program));
};

export default clone;
8 changes: 0 additions & 8 deletions src/commands/environment/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ export const addEnv = async (envName, options, program) => {
const cwd = path.resolve(process.cwd());
console.log('Executing in cwd:'.green, `${cwd}`.yellow);

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

const endpoint = configuration?.endpoint ?? 'http://localhost:8081';
const adminSecret = options?.adminSecret ?? configuration?.admin_secret ?? '';

// Check if configuration has already the env name

// Create env
const initEnvFilePath = path.resolve(cwd, options.config, 'environments', envName);
console.log(`Trying to add new environment ${envName}:`.green, `${initEnvFilePath}`.yellow);
Expand Down
68 changes: 68 additions & 0 deletions src/commands/environment/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import path from 'path';
import { writeFile, mkdir, access, readFile } from 'fs/promises';
import editDotenv from 'edit-dotenv';

import { errors } from './../../errors/index.js';

import CLIErrorHandler from './../../utils/CLIErrorHandler.js';
import { loadConfigFromYAML } from './../../utils/loadConfigFromYAML.js';

import { configEnvTemplate } from './../../templates/index.js';

export const defaultEnv = async (envName, options, program) => {
await (async () => {
const cwd = path.resolve(process.cwd());
console.log('Executing in cwd:'.green, `${cwd}`.yellow);

// Create env
const initEnvFilePath = path.resolve(cwd, '.env');
console.log(
`Trying to update .env file and set default to environment ${envName}:`.green,
`${initEnvFilePath}`.yellow
);

const { force, forceRemove } = options;
const includeDemoApp = !options.clean; // Flip the clean flag.

// We don't need to check for this because we presume that he knows what he is doing...
const folderExists = await access(initEnvFilePath)
.then((result) => {
return true;
})
.catch((error) => {
// console.log(error);
// throw new Error(errors.ERROR_FS_READ_ERROR);
return false;
});

// Check if the in the current cwd there is .env file, because the .env we load from current dir.
// envName

// Read .env value if exists
const oldValueOfDotenvFile = await readFile(`${initEnvFilePath}`, 'utf-8')
.then((data) => {
// console.log(`File read ${initEnvFilePath} successfull!`.green);
return data;
})
.catch(() => {
console.log(`File read ${initEnvFilePath}failed`.red);
});

// Edit dotenv with the changes provided to us
const newValueOfDotenvFile = editDotenv(oldValueOfDotenvFile, { ENV_DEFAULT_ENVIRONMENT: `"${envName}"` });

// Save the new values to dotenv file to the local cwd
// console.log(newValueOfDotenvFile);
await writeFile(`${initEnvFilePath}`, newValueOfDotenvFile, 'utf-8')
.then(() => {
// console.log(`File write ${initEnvFilePath} successfull!`.green);
})
.catch(() => {
console.log(`File write ${initEnvFilePath} failed`.red);
});
})()
.then(() => {})
.catch(CLIErrorHandler(program));
};

export default defaultEnv;
12 changes: 7 additions & 5 deletions src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

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

export const init = async (dirName, options, program) => {
export const init = async (dirName, options, program, silent = false) => {
await (async () => {
const cwd = path.resolve(process.cwd());
console.log('Executing in cwd:'.green, `${cwd}`.yellow);
Expand Down Expand Up @@ -166,10 +166,12 @@ export const init = async (dirName, options, program) => {
});
}

console.log(
`Directory created. Execute the following commands to continue:`.green,
`\n\n ${'cd'.green} ${dirName}\n ${'hlambda'.green} console\n`
);
if (!silent) {
console.log(
`Directory created. Execute the following commands to continue:`.green,
`\n\n ${'cd'.green} ${dirName}\n ${'hlambda'.green} console\n`
);
}
})()
.then(() => {})
.catch(CLIErrorHandler(program));
Expand Down
Loading