UNPKG

3.35 kBJavaScriptView Raw
1var dataflows = require ('dataflo.ws');
2var common = dataflows.common;
3var paint = dataflows.color;
4var fs = require ('fs');
5var path = require ('path');
6
7var project = require ('dataflo.ws/project');
8
9module.exports = {
10 launchContext: function () {
11 },
12 defaultConfig: {
13 debug: "<#false for production>",
14 daemon: {
15 http: {
16 initiator: ['http']
17 }
18 },
19 initiator: {
20 token: {
21 flows: {
22
23 }
24 },
25 http: {
26 host: "<#default:127.0.0.1>",
27 domain: "<#host.local>",
28 port: "<#50100>",
29 session: {
30 secret: "<#please generate unique string>",
31 cookieTemplate: {
32 name: "session",
33 domain: "<$initiator.http.domain>",
34 path: "/",
35 expirePeriod: "+172800000",
36 httpOnly: true
37 }
38 },
39
40 static: {
41 root: "www",
42 index: "index.html",
43 headers: {}
44 },
45 prepare: {
46 post: {
47 tasks: [{
48 $class: "post",
49 request: "{$request}",
50 $set: "request.body"
51 }]
52 }
53 },
54 flows: []
55 }
56 }
57
58 },
59 launch: function (conf, project) {
60
61 if (!conf) {
62 if (this.args._.length <= 1) {
63 var projectPath = path.resolve (this.args._[0] || '.');
64 console.log (paint.dataflows(), 'initalizing project in ', paint.path (this.args._[0] || '.', '('+projectPath+')'));
65
66 var confDir = path.resolve (projectPath, '.dataflows');
67 var instanceName = project.prototype.generatedInstance ();
68 var confFixup = path.resolve (confDir, instanceName);
69
70 if (!fs.existsSync (confDir))
71 fs.mkdirSync (confDir);
72 // TODO: add detection of stub variables
73 if (!fs.existsSync (path.resolve (confDir, 'project')) && !fs.existsSync (path.resolve (confDir, 'project.json')))
74 fs.writeFileSync (path.resolve (confDir, 'project.json'),
75 JSON.stringify (this.defaultConfig, null, "\t"), {flag: "wx"}
76 );
77
78 if (!fs.existsSync (path.resolve (confDir, 'instance')))
79 fs.writeFileSync (
80 path.resolve (confDir, 'instance'),
81 instanceName,
82 {flag: "wx"}
83 );
84
85 if (!fs.existsSync (confFixup))
86 fs.mkdirSync (confFixup);
87
88 } else {
89
90 }
91
92 // console.log ('no dataflo.ws project found within current dir. please run `dataflows init` within project dir');
93 return;
94 } else if (conf && !project.instance) {
95 var confDir = project.configDir;
96 var instanceName = project.prototype.generatedInstance ();
97 var confFixup = path.resolve (confDir, instanceName);
98
99 fs.writeFileSync (
100 path.resolve (confDir, 'instance'),
101 instanceName,
102 {flag: "wx"}
103 );
104
105 if (!fs.existsSync (confFixup))
106 fs.mkdirSync (confFixup);
107
108 } else {
109 console.log (paint.dataflows(), 'project already initialized');
110 process.kill();
111 }
112
113 var project = common.getProject ();
114
115 // 1) check for legacy project dir
116 if (project.legacy) {
117 console.error (paint.error ('project has legacy configuration layout. you can migrate by running those commands:'));
118 console.error ("\n\tcd "+project.root.path);
119 console.error ("\tmv etc .dataflows");
120 if (project.instance)
121 console.error ("\tmv var/instance .dataflows/");
122 console.error();
123 }
124 // 2) check for instance
125 if (!project.instance) {
126 // TODO
127 }
128 // 3) check for config errors
129 // TODO
130 }
131};
132
133// in case of unreadable dataflows project config
134module.exports.launchAnyway = module.exports.launch;