UNPKG

apeman-scff

Version:
63 lines (52 loc) 1.53 kB
'use strict' const argx = require('argx') const fs = require('fs') const findout = require('findout') const types = require('./types') const co = require('co') const listScaffolds = require('./scaffolding/list_scaffolds') const askScaffold = require('./scaffolding/ask_scaffold') const applyScaffold = require('./scaffolding/apply_scaffold') /** @lends apemanScff */ function apemanScff (type, dest, options = {}) { let args = argx(arguments) options = args.pop('object') type = args.shift('string|object') dest = args.pop('string') return co(function * () { let listing = !type && !dest if (listing) { return yield listScaffolds(types) } if (!dest) { throw new Error('dest is required.') } let scff = apemanScff.scffWithType(type) if (!scff) { throw new Error(`type not found: "${type}"`) } let exists = yield new Promise((resolve) => fs.exists(dest, (exists) => resolve(exists))) let skip = exists && !options.force if (skip) { throw new Error(`${dest} is already exists. Use -f option to force.`) } let asked = yield askScaffold(scff, dest, { straight: options.straight }) return yield applyScaffold(scff, dest, asked, { silent: options.silent }) }) } apemanScff.scffWithType = function (type) { if (typeof type === 'string') { if (types[ type ]) { return types[ type ][ 'module' ] } else { return findout(type, { safe: true }) } } return type } module.exports = apemanScff