Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Validate init params #1148

Merged
merged 1 commit into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import push from './push';
import init from '../scripts/init';
import semver from 'semver';
import { promptIfNeeded, InquirerQuestions } from '../prompts/prompt';
import { FileSystem } from '@openzeppelin/upgrades';
import ProjectFile from '../models/files/ProjectFile';
import { notEmpty } from '../prompts/validators';

const name = 'init';
const signature = `${name} [project-name] [version]`;
Expand All @@ -26,7 +28,7 @@ async function action(projectName: string, version: string, options: any): Promi

const args = { name: projectName, version };
const props = getCommandProps();
const defaults = FileSystem.parseJsonIfExists('package.json') || {};
const defaults = FileSystem.parseJsonIfExists('package.json') || { version: '1.0.0' };
const prompted = await promptIfNeeded({ args, defaults, props }, interactive);

const dependencies = link ? link.split(',') : [];
Expand All @@ -50,10 +52,15 @@ function getCommandProps(): InquirerQuestions {
name: {
message: 'Welcome to the OpenZeppelin SDK! Choose a name for your project',
type: 'input',
validate: notEmpty
},
version: {
message: 'Initial project version',
type: 'input',
validate: input => {
if (semver.parse(input)) return true;
return `Invalid semantic version: ${input}`;
}
},
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface InquirerQuestion {
when?: (answers: { [key: string]: any }) => boolean;
transformer?: (value: string, answers: { [key: string]: any }) => string;
normalize?: (input?: any) => any;
validate?: (input?: any) => boolean | string;
}

interface InquirerAnswer {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/prompts/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function notEmpty(input) {
if (input && input.length > 0) return true;
return "Please enter a non-empty value";
}