Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project name validation #635

Merged
merged 6 commits into from
Jul 19, 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
10 changes: 9 additions & 1 deletion packages/aragon-cli/src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { checkProjectExists, prepareTemplate } from '../lib/init'
const { promisify } = require('util')
const clone = promisify(require('git-clone'))
const TaskList = require('listr')
const { installDeps } = require('../util')
const { installDeps, isValidAragonId } = require('../util')
const defaultAPMName = require('../helpers/default-apm')
const listrOpts = require('../helpers/listr-options')

Expand Down Expand Up @@ -55,6 +55,14 @@ exports.handler = function({ reporter, name, template, silent, debug }) {
title: 'Preparing initialization',
task: async (ctx, task) => {
task.output = 'Checking if project folder already exists...'
if (!isValidAragonId(basename)) {
throw new Error(
reporter.error(
'Invalid project name. Please only use lowercase alphanumeric and hyphen characters.'
)
)
}

await checkProjectExists(basename)
},
},
Expand Down
10 changes: 10 additions & 0 deletions packages/aragon-cli/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ const parseStringIfPossible = target => {
return target
}

/**
* Validates an Aragon Id
* @param {string} aragonId Aragon Id
* @returns {boolean} `true` if valid
*/
function isValidAragonId(aragonId) {
return /^[a-z0-9-]+$/.test(aragonId)
}

module.exports = {
parseStringIfPossible,
debugLogger,
Expand All @@ -314,6 +323,7 @@ module.exports = {
getLocalBinary,
getGlobalBinary,
getContract,
isValidAragonId,
ANY_ENTITY,
NO_MANAGER,
ZERO_ADDRESS,
Expand Down
14 changes: 12 additions & 2 deletions packages/aragon-cli/test/commands/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava'
import fs from 'fs-extra'

import { checkProjectExists, prepareTemplate } from '../../src/lib/init'
import { isValidAragonId } from '../../src/util'

import defaultAPMName from '../../src/helpers/default-apm'

Expand All @@ -15,7 +16,7 @@ test.afterEach(t => {
fs.removeSync(projectPath)
})

test('check if project folder already exists', async t => {
test.serial('check if project folder already exists', async t => {
try {
await checkProjectExists(projectPath)
t.fail()
Expand All @@ -24,7 +25,16 @@ test('check if project folder already exists', async t => {
}
})

test('prepare project template', async t => {
test('project name validation', t => {
t.is(isValidAragonId('testproject'), true)
t.is(isValidAragonId('project2'), true)
t.is(isValidAragonId('test-project'), true)

t.is(isValidAragonId('testProject'), false)
t.is(isValidAragonId('test_project'), false)
})

test.serial('prepare project template', async t => {
const repoPath = `${projectPath}/.git`
const arappPath = `${projectPath}/arapp.json`
const packageJsonPath = `${projectPath}/package.json`
Expand Down
10 changes: 9 additions & 1 deletion packages/create-aragon-app/src/commands/create-aragon-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { checkProjectExists, prepareTemplate } from '../lib'
const { promisify } = require('util')
const clone = promisify(require('git-clone'))
const TaskList = require('listr')
const { installDeps } = require('../util')
const { installDeps, isValidAragonId } = require('../util')
const defaultAPMName = require('../helpers/default-apm')
const listrOpts = require('../helpers/listr-options')

Expand Down Expand Up @@ -55,6 +55,14 @@ exports.handler = function({ reporter, name, template, silent, debug }) {
title: 'Preparing initialization',
task: async (ctx, task) => {
task.output = 'Checking if project folder already exists...'
if (!isValidAragonId(basename)) {
throw new Error(
reporter.error(
'Invalid project name. Please only use lowercase alphanumeric and hyphen characters.'
)
)
}

await checkProjectExists(basename)
},
},
Expand Down
10 changes: 10 additions & 0 deletions packages/create-aragon-app/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ const installDeps = (cwd, task) => {
})
}

/**
* Validates an Aragon Id
* @param {string} aragonId Aragon Id
* @returns {boolean} `true` if valid
*/
function isValidAragonId(aragonId) {
return /^[a-z0-9-]+$/.test(aragonId)
}

module.exports = {
installDeps,
isValidAragonId,
getNodePackageManager,
}
14 changes: 12 additions & 2 deletions packages/create-aragon-app/test/create-aragon-app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava'
import fs from 'fs-extra'

import { checkProjectExists, prepareTemplate } from '../src/lib'
import { isValidAragonId } from '../src/util'

import defaultAPMName from '../src/helpers/default-apm'

Expand All @@ -15,7 +16,7 @@ test.afterEach(t => {
fs.removeSync(projectPath)
})

test('check if project folder already exists', async t => {
test.serial('check if project folder already exists', async t => {
try {
await checkProjectExists(projectPath)
t.fail()
Expand All @@ -24,7 +25,16 @@ test('check if project folder already exists', async t => {
}
})

test('prepare project template', async t => {
test('project name validation', t => {
t.is(isValidAragonId('testproject'), true)
t.is(isValidAragonId('project2'), true)
t.is(isValidAragonId('test-project'), true)

t.is(isValidAragonId('testProject'), false)
t.is(isValidAragonId('test_project'), false)
})

test.serial('prepare project template', async t => {
const repoPath = `${projectPath}/.git`
const arappPath = `${projectPath}/arapp.json`
const packageJsonPath = `${projectPath}/package.json`
Expand Down