-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
144 lines (110 loc) · 3.65 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
var argv = require('minimist')(process.argv.slice(2));
const yaml = require('./lib/yaml');
const template = require('./lib/template');
const style = require('./lib/style');
const async = require('async');
var userHome = require('user-home');
const datJson = require('./lib/datJson')
var Dat = require('dat-node');
// process.title = 'archetype';
// argv.loc = argv.loc || path.join(userHome, '/Sites/' + folder );
// argv.input = argv.input || path.join(userHome, '/Sites/' + folder + "/" + initFile);
// argv.lockfile = argv.lockfile || path.join(userHome, '/Sites/' + folder + "/" + lockFile);
// console.log(argv.loc);
const args = (arg) => {
console.log(JSON.stringify(arg));
fs.writeFile(argv.loc + 'dat.json', arg, function(err){console.log(err);});
}
const init = (cb) => {
console.log("inicializando");
async.parallel([initInput, initLockfile], cb);
function initInput(cb) {
fs.readFile(argv.input, 'utf8', (err, data) => {
if (err && err.code == 'ENOENT') {
console.log("create");
createFolders(createIndexAndSubDir);
return
}
cb(err); // Si hay error pasar el err a function callback
});
}
function initLockfile(cb) {
fs.readFile(argv.lockfile, 'utf8', (err, data) => {
if (err && err.code == 'ENOENT') { //ENOENT = no such file or dir
return yaml.writeLockfile(argv.lockfile, { sites: [] }, cb);
}
cb(err); // Si hay error pasar el err a function callback
});
}
}
function createFolders(cb){
fs.mkdir(argv.loc, function(err){console.log(err);});
console.log('home folder ok√');
fs.mkdir(argv.loc + '/assets', function(err){console.log(err);});
console.log('home Assets ok√');
cb();
}
// function createFiles() {
//
// createIndex(createSubDirFiles);
// console.log('creating Index');
// }
function createIndexAndSubDir() {
fs.writeFile(argv.loc + '/index.html', template.html,function(err){console.log(err);});
console.log('index ok√');
fs.mkdir(argv.loc + '/assets/css', function(err){console.log(err);});
console.log('home assets css ok√');
fs.mkdir(argv.loc + '/assets/js', function(err){console.log(err);});
console.log('home js Js ok√');
createSubDirFiles();
}
function createSubDirFiles() {
fs.writeFile(argv.loc + '/assets/css/styles.css', style.css, function(err){console.log(err);});
console.log("css ok √");
// fs.writeFile(argv.loc + '/assets/js/scripts.js', 'scripts');
}
const share = () => {
Dat(argv.loc, function (err, dat) {
// use dat
console.log(dat);
console.log(dat.key.toString('hex'));
hex = dat.key.toString('hex');
})
exec('cd ' + argv.loc + ' && echo' + datJson + ' > dat.json ' + ' dat create' , (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log('creating');
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
}
const getHex = () => {
exec('cd ' + argv.loc + ' && ' + ' dat status ', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(stdout);
// console.log(`stderr: ${stderr}`);
});
// console.log(hex);
}
function err() {
console.log('something went wrong');
}
function exit(err) {
if (err) {
var msg = err.message;
console.log(msg);
process.exit(1); // proccess failed
}
process.exit(0); // If code is omitted, exit uses either the 'success' code 0
}
module.exports = {init, share, getHex, args }