-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'contract new' command, which verifies a project workspace, and adds a new smart contract package in the workspace
- Loading branch information
Showing
30 changed files
with
556 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Args } from '@oclif/core'; | ||
|
||
import { sanitizeDirName } from '@/utils/sanitize'; | ||
|
||
const ContractArgumentDescription = 'Contract name'; | ||
|
||
/** | ||
* Contract name argument | ||
*/ | ||
export const contractNameRequired = Args.string({ | ||
required: true, | ||
parse: async val => sanitizeDirName(val), | ||
description: ContractArgumentDescription, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Flags } from '@oclif/core'; | ||
|
||
import { BaseCommand } from '@/lib/base'; | ||
import { darkGreen, green } from '@/utils/style'; | ||
import { contractNameRequired } from '@/arguments/contract'; | ||
import { templateWithPrompt } from '@/flags/template'; | ||
import { Config } from '@/domain/Config'; | ||
import { DEFAULT } from '@/config'; | ||
|
||
/** | ||
* Command 'contracts new' | ||
* Initializes a new smart contract from a template | ||
*/ | ||
export default class ContractsNew extends BaseCommand<typeof ContractsNew> { | ||
static summary = 'Scaffolds a new Smart Contract from a template'; | ||
static args = { | ||
contract: contractNameRequired, | ||
}; | ||
|
||
static flags = { | ||
template: templateWithPrompt(), | ||
}; | ||
|
||
/** | ||
* Override init function to show message before prompts are displayed | ||
*/ | ||
public async init(): Promise<void> { | ||
// Need to parse early to display the contract name on the starting message | ||
const { args } = await this.parse({ | ||
args: this.ctor.args, | ||
// Override template flag on this early parse, to avoid early prompting | ||
flags: { ...this.ctor.flags, template: Flags.string() }, | ||
}); | ||
|
||
this.log(`Creating new contract ${args.contract}...\n`); | ||
|
||
await super.init(); | ||
} | ||
|
||
/** | ||
* Runs the command. | ||
* | ||
* @returns Empty promise | ||
*/ | ||
public async run(): Promise<void> { | ||
const config = await Config.open(); | ||
|
||
await config.contractsInstance.assertValidWorkspace(); | ||
|
||
await config.contractsInstance.new(this.args.contract!, this.flags.template || DEFAULT.Template); | ||
|
||
this.success( | ||
`${darkGreen('Contract')} ${green(this.args.contract)} ${darkGreen('created from template')} ${green( | ||
this.flags.template || DEFAULT.Template | ||
)}` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.