diff --git a/package-lock.json b/package-lock.json index 27a6ef8d..0021f0a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@aeternity/aeproject", - "version": "4.0.0-alpha.4", + "version": "4.0.0-alpha.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7ab6ef3d..8720a67e 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/cli/commands.js b/src/cli/commands.js index c5beb3fd..23ea95b1 100644 --- a/src/cli/commands.js +++ b/src/cli/commands.js @@ -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; @@ -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); }); }; diff --git a/src/init/artifacts/package.json b/src/init/artifacts/package.json index 60c3747d..de143d72 100644 --- a/src/init/artifacts/package.json +++ b/src/init/artifacts/package.json @@ -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" } diff --git a/src/init/constants.json b/src/init/constants.json index 28a07e58..111f4820 100644 --- a/src/init/constants.json +++ b/src/init/constants.json @@ -6,7 +6,7 @@ "@aeternity/aepp-sdk@10" ], "devDependencies": [ - "@aeternity/aeproject@4.0.0-alpha.4", + "@aeternity/aeproject@4.0.0-alpha.5", "chai@4", "mocha@9" ], diff --git a/src/init/init.js b/src/init/init.js index e75b7da0..56f0831c 100644 --- a/src/init/init.js +++ b/src/init/init.js @@ -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); } } @@ -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`]; @@ -58,23 +69,22 @@ 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) => { @@ -82,7 +92,7 @@ const updateArtifacts = async () => { await deleteWithPrompt(artifact); }, Promise.resolve()); - await copyFolderRecursiveSync(fileSource, destination); + await copyFolderRecursiveSync(fileSource, folder); }; module.exports = { diff --git a/src/utils/fs-utils.js b/src/utils/fs-utils.js index cbfe583f..3958dd49 100644 --- a/src/utils/fs-utils.js +++ b/src/utils/fs-utils.js @@ -45,10 +45,7 @@ async function deleteWithPrompt(target) { } } -const fileExists = (relativePath) => fs.existsSync(path.resolve(process.cwd(), relativePath)); - module.exports = { - fileExists, copyFolderRecursiveSync, deleteWithPrompt, };