diff --git a/commands/dev-deploy.js b/commands/dev-deploy.js index 38d23743..962ab959 100644 --- a/commands/dev-deploy.js +++ b/commands/dev-deploy.js @@ -1,8 +1,10 @@ const { KeyPair } = require('near-api-js'); const exitOnError = require('../utils/exit-on-error'); const connect = require('../utils/connect'); -const { readFile, writeFile } = require('fs').promises; +const { readFile, writeFile, mkdir } = require('fs').promises; +const { existsSync } = require('fs'); const eventtracking = require('../utils/eventtracking'); +const { PROJECT_KEY_DIR } = require('../middleware/key-store'); module.exports = { command: 'dev-deploy [wasmFile]', @@ -51,8 +53,8 @@ async function devDeploy(options) { async function createDevAccountIfNeeded({ near, keyStore, networkId, init }) { // TODO: once examples and create-near-app use the dev-account.env file, we can remove the creation of dev-account // https://github.com/nearprotocol/near-shell/issues/287 - const accountFilePath = 'neardev/dev-account'; - const accountFilePathEnv = 'neardev/dev-account.env'; + const accountFilePath = `${PROJECT_KEY_DIR}/dev-account`; + const accountFilePathEnv = `${PROJECT_KEY_DIR}/dev-account.env`; if (!init) { try { // throws if either file is missing @@ -63,7 +65,10 @@ async function createDevAccountIfNeeded({ near, keyStore, networkId, init }) { } } catch (e) { if (e.code === 'ENOENT') { - // Ignore as it means new account needs to be created, which happens below + // Create neardev directory, new account will be created below + if (!existsSync(PROJECT_KEY_DIR)) { + await mkdir(PROJECT_KEY_DIR); + } } else { throw e; } diff --git a/middleware/key-store.js b/middleware/key-store.js index f199647d..8fa03d75 100644 --- a/middleware/key-store.js +++ b/middleware/key-store.js @@ -5,6 +5,7 @@ const MergeKeyStore = keyStores.MergeKeyStore; const UnencryptedFileSystemKeyStore = keyStores.UnencryptedFileSystemKeyStore; const CREDENTIALS_DIR = '.near-credentials'; +const PROJECT_KEY_DIR = './neardev'; module.exports = async function createKeyStore() { // ./neardev is an old way of storing keys under project folder. We want to fallback there for backwards compatibility @@ -13,7 +14,9 @@ module.exports = async function createKeyStore() { const credentialsPath = path.join(homedir, CREDENTIALS_DIR); const keyStores = [ new UnencryptedFileSystemKeyStore(credentialsPath), - new UnencryptedFileSystemKeyStore('./neardev') + new UnencryptedFileSystemKeyStore(PROJECT_KEY_DIR) ]; return { keyStore: new MergeKeyStore(keyStores) }; }; + +module.exports.PROJECT_KEY_DIR = PROJECT_KEY_DIR; \ No newline at end of file diff --git a/test_environment.js b/test_environment.js index 5670c4eb..dd5699f6 100644 --- a/test_environment.js +++ b/test_environment.js @@ -2,6 +2,8 @@ const NodeEnvironment = require('jest-environment-node'); const nearAPI = require('near-api-js'); const fs = require('fs'); +const { PROJECT_KEY_DIR } = require('./middleware/key-store'); + const INITIAL_BALANCE = '500000000000000000000000000'; const testAccountName = 'test.near'; @@ -20,7 +22,7 @@ class LocalTestEnvironment extends NodeEnvironment { contractName: 'test' + Date.now(), accountId: 'test' + Date.now() }); - const keyStore = new nearAPI.keyStores.UnencryptedFileSystemKeyStore('./neardev'); + const keyStore = new nearAPI.keyStores.UnencryptedFileSystemKeyStore(PROJECT_KEY_DIR); config.deps = Object.assign(config.deps || {}, { storage: this.createFakeStorage(), keyStore,