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

Commit

Permalink
Validate init params (#1148)
Browse files Browse the repository at this point in the history
Validates that name is not empty, and that version is semver compatible. Only on interactive command.

Fixes #1142
  • Loading branch information
spalladino authored Jul 22, 2019
1 parent 6095845 commit fb42ffb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
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";
}

0 comments on commit fb42ffb

Please sign in to comment.