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

refactor: move init command functions into lib and introduce tests #168

Merged
merged 1 commit into from
Oct 15, 2018
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
"ava": {
"require": [
"@babel/register"
],
"files": [
"test/**/*.js"
]
}
}
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 @@ -43,52 +44,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',
Copy link
Contributor Author

@0x-r4bbit 0x-r4bbit Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically went back to having the tasks defined inline, as the actual functionality is moved into lib modules now. That way, we can easily move away from tasklist at some point, without having to worry about touch test imports.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm working on #98, is ok to refactor my implementation of the new command aragon start?

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)
})