Skip to content

Commit

Permalink
refactor: improve env command
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Sep 20, 2023
1 parent 78be83d commit 18022a4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 44 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"lint:ci": "eslint --ext .js --max-warnings=0 .",
"test": "mocha ./tests/**/*.js --timeout 0 --exit",
"prepublishOnly": "npm run build",
"copy-files": "cp -r ./src/init/artifacts ./.build/cjs/src/init/ && cp -r ./src/init/artifacts ./.build/esm/src/init/ && cp -r ./src/init/update-artifacts ./.build/cjs/src/init/ && cp -r ./src/init/update-artifacts ./.build/esm/src/init/"
"copy-files": "cp -r ./src/init/artifacts ./.build/cjs/src/init/ && cp -r ./src/init/artifacts ./.build/esm/src/init/ && cp -r ./src/init/update-artifacts ./.build/cjs/src/init/ && cp -r ./src/init/update-artifacts ./.build/esm/src/init/",
"link:local": "npm run build && npm link"
},
"license": "ISC",
"engines": {
Expand Down
13 changes: 5 additions & 8 deletions src/cli/commands.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const init = require('../init/init');
const testConfig = require('../test/test');
const env = require('../env/env');
const config = require('../config/node-config.json');
const constants = require('../init/constants.json');

const nodeConfig = config.nodeConfiguration;
const compilerConfig = config.compilerConfiguration;

const addInitOption = (program) => {
program
.command('init')
Expand All @@ -31,10 +27,11 @@ const addEnvOption = (program) => {
program
.command('env')
.description('Running a local network. Without any argument started with default configuration')
.option('--stop', 'Stop the node')
.option('--info', 'Displays information about your current node status if any, and absolute path where it has been started from')
.option('--nodeVersion [nodeVersion]', `Specify node version, default is ${nodeConfig.imageVersion}`, nodeConfig.imageVersion)
.option('--compilerVersion [compilerVersion]', `Specify compiler version, default is ${compilerConfig.imageVersion}`, compilerConfig.imageVersion)
.option('--stop', 'Stop the development environment')
.option('--restart', 'Restart the development environment')
.option('--info', 'Displays information about your current development environment status')
.option('--nodeVersion [nodeVersion]', 'Specify node version, default is whatever is locally configured in docker-compose.yml')
.option('--compilerVersion [compilerVersion]', 'Specify compiler version, default is whatever is locally configured in docker-compose.yml')
.action(async (options) => {
await env.run(options);
});
Expand Down
14 changes: 0 additions & 14 deletions src/config/node-config.json

This file was deleted.

62 changes: 41 additions & 21 deletions src/env/env.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { spawn, exec } = require('promisify-child-process');

const { print, printError, ensureNodeAndCompilerAvailable } = require('../utils/utils');
const { nodeConfiguration, compilerConfiguration, proxyConfiguration } = require('../config/node-config.json');

let dockerComposeCmd = 'docker compose';

Expand All @@ -22,9 +21,9 @@ async function isEnvRunning(cwd = './') {

if (info) {
const containers = [
nodeConfiguration.containerName,
compilerConfiguration.containerName,
proxyConfiguration.containerName,
'aeproject_node',
'aeproject_compiler',
'aeproject_proxy',
];
return containers.some((containerName) => {
const line = info.split('\n').find((l) => l.includes(containerName));
Expand All @@ -36,9 +35,6 @@ async function isEnvRunning(cwd = './') {
}

async function run(option) {
const nodeVersion = option.nodeVersion || nodeConfiguration.imageVersion;
const compilerVersion = option.compilerVersion || compilerConfiguration.imageVersion;

const running = await isEnvRunning();

if (option.info) {
Expand All @@ -50,8 +46,12 @@ async function run(option) {
await stopEnv(running);
return;
}
if (option.restart) {
await restartEnv(running);
return;
}

await startEnv(nodeVersion, compilerVersion);
await startEnv(option);
}

async function stopEnv(running) {
Expand All @@ -68,38 +68,58 @@ async function stopEnv(running) {
print('===== Env was successfully stopped! =====');
}

async function startEnv(nodeVersion, compilerVersion) {
async function restartEnv(running) {
if (!running) {
printError('===== Env is not running! =====');
return;
}

print('===== restarting env =====');

await getDockerCompose();
await exec(`${dockerComposeCmd} restart`);

print('===== env was successfully restarted! =====');
}

async function startEnv(option) {
if (await isEnvRunning()) {
printError('===== Compiler or Node is already running! =====');
print('===== env already running, updating env =====');
} else {
print('===== starting env =====');
}

const versionTags = `${option.nodeVersion ? `NODE_TAG=${option.nodeVersion}` : ''} ${option.compilerVersion ? `COMPILER_TAG=${option.compilerVersion}` : ''}`;
if (versionTags.trim() !== '') print(`using versions as specified: ${versionTags}`);
else print('using versions from docker-compose.yml');

await getDockerCompose();
await exec(`NODE_TAG=${nodeVersion} COMPILER_TAG=${compilerVersion} ${dockerComposeCmd} pull`);
await exec(`NODE_TAG=${nodeVersion} COMPILER_TAG=${compilerVersion} ${dockerComposeCmd} up -d --wait`);
await getDockerCompose();
await exec(`${versionTags} ${dockerComposeCmd} pull`);
await exec(`${versionTags} ${dockerComposeCmd} up -d --wait`);

await ensureNodeAndCompilerAvailable();
await ensureNodeAndCompilerAvailable();

print('===== Env was successfully started! =====');
}
const isRunning = await isEnvRunning();
await printInfo(isRunning, true);
if (isRunning) print('===== env was successfully started =====');
}

async function printInfo(running) {
async function printInfo(running, imagesOnly = false) {
if (!running) {
printError('===== Compiler or Node is not running! ===== \n===== Please run the relevant command for your image! =====');
printError('===== compiler or node is not running ===== \n===== run \'aeproject env\' to start the development setup =====');
return;
}

print(await getInfo());
print(await getInfo(undefined, imagesOnly));
}

async function getInfo(cwd) {
async function getInfo(cwd = './', imagesOnly = false) {
await getDockerCompose();
const ps = await exec(`${dockerComposeCmd} ps`, { cwd });
const images = await exec(`${dockerComposeCmd} images`, { cwd });

if (ps && images && ps.stdout && images.stdout) {
return `${ps.stdout}\n${images.stdout}`;
return imagesOnly ? images.stdout : `${ps.stdout}\n${images.stdout}`;
}

return null;
Expand Down
3 changes: 3 additions & 0 deletions src/init/update-artifacts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ services:
- '3001:3001'
volumes:
- './docker/nginx.conf:/etc/nginx/conf.d/default.conf'
depends_on:
- aeproject_compiler
- aeproject_node
13 changes: 13 additions & 0 deletions tests/happy-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ describe('Happy Path', () => {
const res = await exec('aeproject env', { cwd });
assert.equal(res.code, 0);
assert.isTrue(await isEnvRunning(cwd));

const resSecond = await exec('aeproject env --nodeVersion v6.10.0 --compilerVersion v7.4.0', { cwd });
assert.include(resSecond.stdout, 'v6.10.0');
assert.include(resSecond.stdout, 'v7.4.0');

const resThird = await exec('aeproject env', { cwd });
assert.include(resThird.stdout, 'updating');
});

it('env --restart', async () => {
const res = await exec('aeproject env --restart', { cwd });
assert.include(res.stdout, 'restarting');
assert.isTrue(await isEnvRunning(cwd));
});

it('test', async () => {
Expand Down

0 comments on commit 18022a4

Please sign in to comment.