Skip to content

Commit

Permalink
refactor(commands/init): move tasks into dedicated lib modules for be…
Browse files Browse the repository at this point in the history
…tter testability
  • Loading branch information
0x-r4bbit committed Aug 10, 2018
1 parent b3d9da4 commit e4393d9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
40 changes: 11 additions & 29 deletions src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const path = require('path')
const fs = require('fs-extra')
const { installDeps } = require('../util')
const defaultAPMName = require('../helpers/default-apm')
import { checkProjectExists } from '../lib/check-project-exists';
import { prepareTemplate } from '../lib/prepare-template';

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

Expand Down Expand Up @@ -43,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/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.`)
}
}
16 changes: 16 additions & 0 deletions src/lib/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)
])
}

0 comments on commit e4393d9

Please sign in to comment.