UNPKG

3.18 kBJavaScriptView 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 ('commop/lib/minimist');
14
15var $global = dataflows.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
96var mainModule = dataflows.main ();
97
98project.on ('ready', function () {
99 var conf = project.config;
100
101 // load local modules
102 var requires = conf.requires || [ DEFAULT_REQUIRE ];
103 if (!Object.is('Array', requires)) {
104 requires = [ requires ];
105 }
106
107 requires.forEach(function (modName) {
108 var mod = project.require (modName, true);
109
110 // exporting everything to mainModule,
111 // be careful about name conflicts
112 if (mod) {
113 Object.keys(mod).forEach(function (key) {
114 mainModule[key] = mod[key];
115 });
116 } else {
117 // console.warn('Module %s not found', modName);
118 }
119 });
120
121 // now we can launch script;
122 // script require postponed until project get prepared
123
124 launchScript(conf);
125});
126
127project.on ('failed', function (err) {
128 // if (err === 'unpopulated variables')
129 // return;
130 // now we can launch script;
131 // script require postponed until project get prepared
132
133 launchScript (project.config, err);
134});
135