Skip to content

Commit

Permalink
refactor!: improve update from previous aeproject version
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Feb 7, 2022
1 parent 66edcf4 commit d71bfe8
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aeternity/aeproject",
"version": "4.0.0-alpha.3",
"version": "4.0.0-alpha.4",
"description": "aeternity smart contract testing tool",
"main": "src/lib/index.js",
"bin": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@aeternity/aepp-sdk": "^10.0.0"
},
"devDependencies": {
"@aeternity/aeproject": "^4.0.0-alpha.2",
"@aeternity/aeproject": "^4.0.0-alpha.4",
"chai": "^4.3.4",
"mocha": "^9.1.3"
}
Expand Down
27 changes: 22 additions & 5 deletions src/init/constants.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
{
"artifactsDest": "./",
"artifactsDir": "/artifacts",
"updateArtifactsDir": "/update-artifacts",
"aeNodeTag": "master",
"aeCompilerTag": "v6.1.2"
"artifactsDest": "./",
"artifactsDir": "/artifacts",
"updateArtifactsDir": "/update-artifacts",
"dependencies": [
"@aeternity/aepp-sdk@10"
],
"devDependencies": [
"@aeternity/[email protected]",
"chai@4",
"mocha@9"
],
"uninstallDependencies": [
"aeproject",
"aeproject-lib"
],
"deleteArtifacts": [
"./config/",
"./docker/",
"./utils/",
"./deployment/",
"./docker-compose.compiler.yml"
]
}
36 changes: 29 additions & 7 deletions src/init/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const { exec } = require('promisify-child-process');
const constants = require('./constants.json');
const { print } = require('../utils/utils');

const { copyFolderRecursiveSync, fileExists } = require('../utils/fs-utils');
const { copyFolderRecursiveSync, fileExists, deleteWithPrompt } = require('../utils/fs-utils');

async function run(update) {
checkNodeVersion();

if (update) {
await updateAEprojectProjectLibraries();
await updateAEprojectProjectLibraries(update);
} else {
await createAEprojectProjectStructure();
}
Expand All @@ -29,21 +29,37 @@ const createAEprojectProjectStructure = async () => {
await installDependencies();

print('===== aeproject successfully initialized =====');
print('test/exampleTest.js and contract/ExampleContract.aes have been added as example how to use aeproject');
};

const updateAEprojectProjectLibraries = async () => {
const updateAEprojectProjectLibraries = async (update) => {
print('===== updating aeproject =====');

await updateArtifacts();
await installDependencies();
await installDependencies(update);

print('===== aeproject sucessfully initalized =====');
print('test/exampleTest.js and contract/ExampleContract.aes have been added as example how to use aeproject');
};

const installDependencies = async () => {
const installDependencies = async (update = false) => {
if (fileExists('./package.json')) {
print('===== installing dependencies =====');
await exec(/^win/.test(process.platform) ? 'npm.cmd install' : 'npm install');
const npm = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
const installPromises = [`${npm} install`];

if (update) {
constants.dependencies.map((dependency) => installPromises.push(`${npm} install ${dependency}`));
constants.devDependencies.map((dependency) => installPromises.push(`${npm} install --save-dev ${dependency}`));
constants.uninstallDependencies.map((dependency) => installPromises.push(`${npm} uninstall ${dependency}`));
installPromises.push('npx npm-add-script -k "test" -v "mocha ./test/**/*.js --timeout 0 --exit" --force');
}

await installPromises.reduce(async (promiseAcc, command) => {
await promiseAcc;
print(command);
await exec(command);
}, Promise.resolve());
}
};

Expand All @@ -55,11 +71,17 @@ const setupArtifacts = async () => {
};

const updateArtifacts = async () => {
print('===== creating project file and directory structure =====');
print('===== updating project file and directory structure =====');

const fileSource = `${__dirname}${constants.updateArtifactsDir}`;
const destination = constants.artifactsDest;

await constants.deleteArtifacts
.reduce(async (promiseAcc, artifact) => {
await promiseAcc;
await deleteWithPrompt(artifact);
}, Promise.resolve());

await copyFolderRecursiveSync(fileSource, destination);
};

Expand Down
9 changes: 3 additions & 6 deletions src/init/update-artifacts/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
version: '3.6'
services:

node:
aeproject_node:
image: aeternity/aeternity:${NODE_TAG}-bundle
hostname: node
container_name: aeproject_node
environment:
AETERNITY_CONFIG: /home/aeternity/aeternity.yaml
volumes:
- './docker/aeternity.yaml:/home/aeternity/aeternity.yaml'
- './docker/accounts.json:/home/aeternity/node/data/aecore/.genesis/accounts_test.json'

compiler:
aeproject_compiler:
image: aeternity/aesophia_http:${COMPILER_TAG}
hostname: compiler
container_name: aeproject_compiler
ports:
- '3080:3080'

proxy:
aeproject_proxy:
image: nginx:latest
hostname: proxy
container_name: aeproject_proxy
ports:
- '3001:3001'
volumes:
Expand Down
File renamed without changes.
15 changes: 12 additions & 3 deletions src/utils/fs-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const fs = require('fs');
const path = require('path');
const prompts = require('prompts');

async function promptOverwrite(target) {
async function prompt(action, target) {
const response = await prompts({
type: 'text',
name: 'value',
message: `Do you want to overwrite '${target}'? (y/N):`,
message: `Do you want to ${action} '${target}'? (y/N):`,
});

const input = response.value.trim();
Expand All @@ -31,15 +31,24 @@ async function copyFolderRecursiveSync(srcDir, dstDir) {
await copyFolderRecursiveSync(src, dst);
} else if (!fs.existsSync(dst)) {
fs.writeFileSync(dst, fs.readFileSync(src));
} else if (await promptOverwrite(dst)) {
} else if (await prompt('overwrite', dst)) {
fs.writeFileSync(dst, fs.readFileSync(src));
}
}, Promise.resolve());
}

async function deleteWithPrompt(target) {
if (fs.existsSync(target)) {
if (await prompt('delete', target)) {
fs.rmSync(target, { recursive: true });
}
}
}

const fileExists = (relativePath) => fs.existsSync(path.resolve(process.cwd(), relativePath));

module.exports = {
fileExists,
copyFolderRecursiveSync,
deleteWithPrompt,
};

0 comments on commit d71bfe8

Please sign in to comment.