UNPKG

1.58 kBJavaScriptView Raw
1/**
2 * @license MIT
3 * Copyright (c) 2016 Craig Monro (cmroanirgo)
4 **/
5
6/*
7* This is the public api for 'init'
8* Returns a Promise
9*/
10"use strict";
11
12
13var l = require('ergo-utils').log.module('ergo-api-init');
14var _ = require('ergo-utils')._;
15var fs = require('ergo-utils').fs.extend(require('fs-extra'));
16var path = require('path');
17var Promise = require('bluebird');
18
19module.exports = function(dir, skeleton_dir, options) {
20 if (!_.isDefined(dir))
21 throw ('Command \'init\' is missing parameter \'dir\'');
22 if (!_.isDefined(skeleton_dir))
23 throw ('Command \'init\' is missing parameter \'skeleton_dir\'');
24 options = options || {};
25
26 l.log('Initialising \'' + dir + '\' ...')
27 return Promise.coroutine(function *() {
28 if (!options.force) {
29 // unless *forced* to, check that we have an empty dir first!
30 l.logd('walking: ' + dir)
31 var walkerIsEmpty = new Promise(function(resolve) {
32 var isEmpty = true;
33 fs.walk(dir)
34 .on('data', function (item) {
35 if (item.stats.isFile() || item.stats.isDirectory()) {
36 //l.log('found this in the destination dir: ' + item.path)
37 isEmpty = false;
38 }
39 })
40 .on('end', function () {
41 resolve(isEmpty)
42 })
43 .on('error', function(e) {
44 resolve(true);
45 })
46 });
47 ;
48 if (!(yield walkerIsEmpty)) {
49 throw new Error('Destination dir ('+dir+') is not empty');
50 }
51 }
52
53 l.vlog('Copying skeleton files to destination...')
54 var copy = Promise.promisify(fs.copy);
55 yield copy(skeleton_dir, dir, {clobber:(options.force||false)});
56 return true;
57 })();
58}
59