Skip to content

Commit

Permalink
feat(init): add optional folder parameter to init
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Feb 7, 2022
1 parent d71bfe8 commit ad00bea
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 29 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.4",
"version": "4.0.0-alpha.5",
"description": "aeternity smart contract testing tool",
"main": "src/lib/index.js",
"bin": {
Expand Down
8 changes: 5 additions & 3 deletions src/cli/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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;
Expand All @@ -10,9 +11,10 @@ const addInitOption = (program) => {
program
.command('init')
.description('Initialize AEproject')
.option('--update [update]', 'Update project files')
.action(async (option) => {
await init.run(option.update);
.argument('[folder]', 'project name for folder to be created', constants.artifactsDest)
.option('--update', 'update project files')
.action(async (folder, option) => {
await init.run(folder, option.update);
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/init/artifacts/package.json
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.4",
"@aeternity/aeproject": "^4.0.0-alpha.5",
"chai": "^4.3.4",
"mocha": "^9.1.3"
}
Expand Down
2 changes: 1 addition & 1 deletion src/init/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"@aeternity/aepp-sdk@10"
],
"devDependencies": [
"@aeternity/[email protected].4",
"@aeternity/[email protected].5",
"chai@4",
"mocha@9"
],
Expand Down
48 changes: 29 additions & 19 deletions src/init/init.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
const fs = require('fs');
const path = require('path');

const { exec } = require('promisify-child-process');

const constants = require('./constants.json');
const { print } = require('../utils/utils');

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

async function run(update) {
async function run(folder, update) {
checkNodeVersion();
createFolder(folder);

if (update) {
await updateAEprojectProjectLibraries(update);
await updateAEprojectProjectLibraries(folder, update);
} else {
await createAEprojectProjectStructure();
await createAEprojectProjectStructure(folder);
}
}

Expand All @@ -22,28 +26,35 @@ const checkNodeVersion = () => {
}
};

const createAEprojectProjectStructure = async () => {
const createFolder = (folder) => {
if (folder !== constants.artifactsDest) {
print(`creating project folder ${folder}`);
fs.mkdirSync(folder);
}
};

const createAEprojectProjectStructure = async (folder) => {
print('===== initializing aeproject =====');

await setupArtifacts();
await installDependencies();
await setupArtifacts(folder);
await installDependencies(folder);

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

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

await updateArtifacts();
await installDependencies(update);
await updateArtifacts(folder);
await installDependencies(folder, 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 (update = false) => {
if (fileExists('./package.json')) {
const installDependencies = async (folder, update = false) => {
if (fs.existsSync(path.join(process.cwd(), folder, 'package.json'))) {
print('===== installing dependencies =====');
const npm = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
const installPromises = [`${npm} install`];
Expand All @@ -58,31 +69,30 @@ const installDependencies = async (update = false) => {
await installPromises.reduce(async (promiseAcc, command) => {
await promiseAcc;
print(command);
await exec(command);
await exec(command, { cwd: path.join(process.cwd(), folder) });
}, Promise.resolve());
}
};

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

await copyFolderRecursiveSync(`${__dirname}${constants.updateArtifactsDir}`, constants.artifactsDest);
await copyFolderRecursiveSync(`${__dirname}${constants.artifactsDir}`, constants.artifactsDest);
await copyFolderRecursiveSync(`${__dirname}${constants.updateArtifactsDir}`, path.join(constants.artifactsDest, folder));
await copyFolderRecursiveSync(`${__dirname}${constants.artifactsDir}`, path.join(constants.artifactsDest, folder));
};

const updateArtifacts = async () => {
const updateArtifacts = async (folder) => {
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);
await copyFolderRecursiveSync(fileSource, folder);
};

module.exports = {
Expand Down
3 changes: 0 additions & 3 deletions src/utils/fs-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ async function deleteWithPrompt(target) {
}
}

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

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

0 comments on commit ad00bea

Please sign in to comment.