Skip to content

Commit

Permalink
feat: added support for --template param (#902)
Browse files Browse the repository at this point in the history
* feat: added support for --template param

* feat: added .zip to the regexp
  • Loading branch information
cazala authored Nov 29, 2022
1 parent bbd43d8 commit 71e37f9
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/commands/init/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { getProjectTypes } from './utils'
export const args = arg({
'--help': Boolean,
'--project': String,
'--template': String,
'--skip-install': Boolean,
'-h': '--help',
'-p': '--project'
'-p': '--project',
'-t': '--template'
})

export const help = () => `
Expand All @@ -31,4 +33,5 @@ export const help = () => `
${chalk.green('$ dcl init --project scene')}
--skip-install Skip installing dependencies
--template The URL to a template. It must be under the decentraland or decentraland-scenes GitHub organization.
`
19 changes: 16 additions & 3 deletions src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { downloadRepoZip } from '../../utils/download'
import { fail, ErrorType } from '../../utils/errors'
import { isEmptyDirectory } from '../../utils/filesystem'
import * as spinner from '../../utils/spinner'
import { getInitOption, getRepositoryUrl } from './utils'
import { getInitOption, getRepositoryUrl, isValidTemplateUrl } from './utils'
import { args } from './help'
import { InitOption } from './types'

export { help } from './help'

Expand All @@ -26,8 +27,20 @@ export async function main() {
return
}
const projectArg = args['--project']
const choice = await getInitOption(projectArg)
const url = getRepositoryUrl(choice)
const templateArg = args['--template']

let url: string | void
let choice: InitOption | void
if (templateArg && isValidTemplateUrl(templateArg)) {
choice = {
type: 'scene',
value: templateArg
}
url = templateArg
} else {
choice = await getInitOption(projectArg)
url = getRepositoryUrl(choice)
}

if (!url) {
fail(ErrorType.INIT_ERROR, 'Cannot get a choice')
Expand Down
4 changes: 4 additions & 0 deletions src/commands/init/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,7 @@ export function getRepositoryUrl(choice: InitOption): string | void {
return choice.value
}
}

export function isValidTemplateUrl(url: string) {
return /^https:\/\/github\.com\/decentraland(-scenes)?\/(.)+\.zip/.test(url)
}
1 change: 1 addition & 0 deletions test/e2e/snapshots/init.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Generated by [AVA](https://avajs.dev).
$ dcl init --project scene␊
--skip-install Skip installing dependencies␊
--template The URL to a template. It must be under the decentraland or decentraland-scenes GitHub organization.␊
`
Binary file modified test/e2e/snapshots/init.test.ts.snap
Binary file not shown.
Binary file modified test/e2e/snapshots/start.test.ts.snap
Binary file not shown.

0 comments on commit 71e37f9

Please sign in to comment.