1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | "use strict";
|
11 |
|
12 |
|
13 | var l = require('ergo-utils').log.module('ergo-api-init');
|
14 | var _ = require('ergo-utils')._;
|
15 | var fs = require('ergo-utils').fs.extend(require('fs-extra'));
|
16 | var path = require('path');
|
17 | var Promise = require('bluebird');
|
18 |
|
19 | module.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 |
|
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 |
|
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 |
|