UNPKG

3.14 kBPlain TextView Raw
1#!/usr/bin/env node
2
3var MODULE_NAME = 'dataflo.ws';
4var INITIATOR_PATH = 'initiator';
5var DEFAULT_REQUIRE = 'main';
6
7var dataflows = require('../');
8var common = dataflows.common;
9var paint = dataflows.color;
10
11var path = require('path');
12
13var minimist = require ('minimist');
14
15var $global = common.$global;
16
17var Project = require (MODULE_NAME + '/project');
18
19var project;
20
21common.getProject = function (rootPath) {
22 if (!project) {
23 project = new Project(rootPath);
24 }
25 return project;
26};
27
28$global.project = common.getProject(); // used by initiators
29
30function launchScript (conf, err) {
31 var scriptName = process.argv[2];
32
33 if (!scriptName)
34 scriptName = "help";
35
36 if (err) {
37 if (err === "no project config" && !scriptName.match (/^(help|init)$/)) {
38 console.error (
39 'no', paint.dataflows(),
40 'project config found. please run',
41 paint.path ('dataflows help'), 'or', paint.path ('dataflows init')
42 );
43 }
44 if (conf && scriptName !== "config") {
45 conf = null;
46 }
47 }
48
49 var scriptClass;
50 try {
51 scriptClass = require (project.root.fileIO ('bin', scriptName).path);
52 } catch (e) {
53 try {
54 // console.log (path.join (MODULE_NAME, 'script', scriptName));
55 scriptClass = require (path.join (MODULE_NAME, 'bin', scriptName));
56 } catch (e) {
57 console.error (e);
58 }
59 }
60
61 if (!scriptClass) {
62 // TODO: list all available scripts with descriptions
63 console.error('sorry, there is no such script "%s"', scriptName);
64 process.exit();
65 }
66
67 var scriptMethod = 'launch';
68 var launchContext;
69
70 if (typeof scriptClass.launchContext === 'function') {
71 launchContext = scriptClass.launchContext() || {};
72 if (launchContext.method) {
73 scriptMethod = launchContext.method;
74 }
75 }
76
77 scriptClass.command = launchContext.command || process.argv[2];
78 scriptClass.args = launchContext.args || minimist(process.argv.slice(3));
79
80 // scriptClass.args =
81
82 if (!err && typeof scriptClass[scriptMethod] === 'function') {
83 scriptClass[scriptMethod](conf, project);
84 } else if (typeof scriptClass[scriptMethod + 'Anyway'] === 'function') {
85 scriptClass[scriptMethod + 'Anyway'](conf, project);
86 } else {
87 console.error(
88 'missing method "%s" for script "%s"',
89 scriptMethod, scriptName
90 );
91 process.exit();
92 }
93
94}
95
96project.on ('ready', function () {
97 var conf = project.config;
98
99 // load local modules
100 var requires = conf.requires || [ DEFAULT_REQUIRE ];
101 if (!Object.is('Array', requires)) {
102 requires = [ requires ];
103 }
104
105 requires.forEach(function (modName) {
106 var mod = project.require(modName, true);
107
108 // exporting everything to mainModule,
109 // be careful about name conflicts
110 if (mod) {
111 Object.keys(mod).forEach(function (key) {
112 $global.$mainModule.exports[key] = mod[key];
113 });
114 } else {
115 // console.warn('Module %s not found', modName);
116 }
117 });
118
119 // now we can launch script;
120 // script require postponed until project get prepared
121
122 launchScript(conf);
123});
124
125project.on ('error', function (err) {
126 // if (err === 'unpopulated variables')
127 // return;
128 // now we can launch script;
129 // script require postponed until project get prepared
130
131 launchScript (project.config, err);
132});
133