Skip to content

Commit

Permalink
Merge pull request #168 from PascalPrecht/feat/test-setup
Browse files Browse the repository at this point in the history
refactor: move init command functions into lib and introduce tests
  • Loading branch information
kernelwhisperer authored Oct 15, 2018
2 parents 06e2f1c + 0c5aa30 commit 525b1de
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 29 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@
"ava": {
"require": [
"@babel/register"
],
"files": [
"test/**/*.js"
]
},
"aragon": {
Expand Down
39 changes: 10 additions & 29 deletions src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path')
const fs = require('fs-extra')
const { installDeps } = require('../util')
const defaultAPMName = require('../helpers/default-apm')
import { checkProjectExists, prepareTemplate } from '../lib/init';

exports.command = 'init <name> [template]'

Expand Down Expand Up @@ -44,52 +45,32 @@ exports.builder = (yargs) => {

exports.handler = function ({ reporter, name, template }) {
name = defaultAPMName(name)

const basename = name.split('.')[0]

const tasks = new TaskList([
{
title: 'Checking project existence',
title: 'Preparing initialization',
task: async (ctx, task) => {
const projectPath = path.resolve(process.cwd(), basename)
const exists = await fs.pathExists(projectPath)
if (exists) {
throw new Error(`Couldn't initialize project. Project with name ${basename} already exists in ${projectPath}. Use different <name> or rename existing project folder.`)
}
task.output = 'Checking if project folder already exists...'
await checkProjectExists(basename)
}
},
{
title: 'Clone template',
title: 'Cloning app template',
task: async (ctx, task) => {
task.output = `Cloning ${template} into ${basename}...`

const repo = await clone(template, basename, { shallow: true })
await clone(template, basename, { shallow: true })
}
},
{
title: 'Preparing template',
task: async (ctx, task) => {
// Set `appName` in arapp
const arappPath = path.resolve(
basename,
'arapp.json'
)
const arapp = await fs.readJson(arappPath)
arapp.appName = name

// Delete .git folder
const gitFolderPath = path.resolve(
basename,
'.git'
)

return Promise.all([
fs.writeJson(arappPath, arapp, { spaces: 2 }),
fs.remove(gitFolderPath)
])
task.output = 'Initiliazing arapp.json and removing Git repository';
await prepareTemplate(basename, name)
}
},
{
title: 'Install package dependencies',
title: 'Installing package dependencies',
task: async (ctx, task) => (await installDeps(basename, task)),
}
])
Expand Down
11 changes: 11 additions & 0 deletions src/lib/init/check-project-exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from 'path';
import fs from 'fs-extra';

export async function checkProjectExists(basename) {
const projectPath = path.resolve(process.cwd(), basename)
const exists = await fs.pathExists(projectPath)

if (exists) {
throw new Error(`Couldn't initialize project. Project with name ${basename} already exists in ${projectPath}. Use different <name> or rename existing project folder.`)
}
}
2 changes: 2 additions & 0 deletions src/lib/init/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './check-project-exists';
export * from './prepare-template';
16 changes: 16 additions & 0 deletions src/lib/init/prepare-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'path';
import fs from 'fs-extra';

export async function prepareTemplate(basename, appName) {
const arappPath = path.resolve(basename, 'arapp.json')
const arapp = await fs.readJson(arappPath)

arapp.appName = appName

const gitFolderPath = path.resolve(basename, '.git')

return Promise.all([
fs.writeJson(arappPath, arapp, { spaces: 2 }),
fs.remove(gitFolderPath)
])
}
43 changes: 43 additions & 0 deletions test/commands/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import test from 'ava';
import fs from 'fs-extra';

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

const projectPath = './.tmp/aragon-app'

test.beforeEach(t => {
fs.ensureDirSync(projectPath)
})

test.afterEach(t => {
fs.removeSync(projectPath)
})

test('check if project folder already exists', async t => {
try {
await checkProjectExists(projectPath)
t.fail()
} catch (err) {
t.pass()
}
})

test('prepare project template', async t => {

const repoPath = `${projectPath}/.git`
const arappPath = `${projectPath}/arapp.json`
const appName = 'TestApp'

await fs.ensureDir(repoPath)
await fs.ensureFile(arappPath)
await fs.writeJson(arappPath, {
appName: 'boilerplate-placeholder'
})

await prepareTemplate(projectPath, appName)
const project = await fs.readJson(arappPath)

t.falsy(await fs.pathExists(repoPath))
t.is(appName, project.appName)
})

0 comments on commit 525b1de

Please sign in to comment.