UNPKG

3.8 kBPlain TextView Raw
1#!/usr/bin/env node
2
3var args = resolveNativeNodeArgumentsIfAny();
4if (args) {
5 runNewNode(args);
6} else {
7 preload(initialize);
8}
9
10function runNewNode (args) {
11
12 var spawn = require('child_process').spawn;
13 var path = require('path');
14 var proc = spawn(process.execPath, args, { stdio: 'inherit' });
15
16 proc.on('exit', function (code, signal) {
17 process.on('exit', function () {
18 if (signal) {
19 process.kill(process.pid, signal);
20 } else {
21 process.exit(code);
22 }
23 });
24 });
25
26 // terminate children.
27 process.on('SIGINT', function () {
28 proc.kill('SIGINT'); // calls runner.abort()
29 proc.kill('SIGTERM'); // if that didn't work, we're probably in an infinite loop, so make it die.
30 });
31}
32
33function resolveNativeNodeArgumentsIfAny () {
34 var path = require('path');
35 var args = [path.join(__dirname, 'atma')];
36 var isDebug = false;
37 var count = 0;
38
39 process.argv.slice(2).forEach(function (arg) {
40 var flag = arg.split('=')[0];
41 switch (flag) {
42 case '-d':
43 args.unshift('--debug');
44 break;
45 case 'debug':
46 args.unshift('--inspect');
47 isDebug = true;
48 break;
49 case '--debug':
50 case '--debug-brk':
51 case '--inspect':
52 case '--inspect-brk':
53 args.unshift(arg);
54 break;
55 case '-gc':
56 case '--expose-gc':
57 args.unshift('--expose-gc');
58 break;
59 case '--gc-global':
60 case '--es_staging':
61 case '--no-deprecation':
62 case '--no-warnings':
63 case '--prof':
64 case '--log-timer-events':
65 case '--throw-deprecation':
66 case '--trace-deprecation':
67 case '--trace-warnings':
68 case '--use_strict':
69 case '--allow-natives-syntax':
70 case '--perf-basic-prof':
71 case '--napi-modules':
72 args.unshift(arg);
73 break;
74 default:
75 if (arg.indexOf('--harmony') === 0) {
76 args.unshift(arg);
77 } else if (arg.indexOf('--trace') === 0) {
78 args.unshift(arg);
79 } else if (arg.indexOf('--icu-data-dir') === 0) {
80 args.unshift(arg);
81 } else if (arg.indexOf('--max-old-space-size') === 0) {
82 args.unshift(arg);
83 } else if (arg.indexOf('--preserve-symlinks') === 0) {
84 args.unshift(arg);
85 } else {
86 count++;
87 args.push(arg);
88 }
89 break;
90 }
91 });
92 if (args.length === count + 1) {
93 return null;
94 }
95 if (isDebug) {
96 args.push('--debugger');
97 }
98 return args;
99}
100
101
102function preload (onComplete) {
103
104 var isTest = process.argv.indexOf('test') !== -1;
105 if (isTest) {
106 process.env.TEST = true;
107 }
108
109 require('includejs');
110 require('atma-libs/globals-dev');
111
112 var _ = require('atma-utils');
113 global.atma = {};
114 global.ruta = require('ruta');
115 global.Class = require('atma-class');
116 global.mask = require('maskjs');
117
118 process
119 .mainModule
120 .paths
121 .push(process.cwd() + '/node_modules');
122
123
124 require('atma-logger/lib/global-dev');
125 require('atma-io');
126
127
128 var base = io.env.applicationDir.toString();
129
130 include
131 .cfg({
132 path: base
133 })
134 .routes({
135 handler: base + 'src/handler/{0}.js',
136 parser: base + 'src/parser/{0}.js',
137 action: base + 'src/action/{0}.js',
138 script: base + 'src/{0}.js',
139 helper: base + 'src/helper/{0}.js',
140 server: base + 'src/server/{0}.js',
141 atma: base + '{0}.js'
142 })
143 .js({
144 atma: [
145 'src/utils/exports'
146 ]
147 })
148 .done(function() {
149
150 require('./src/app.js').done(initialize);
151 });
152}
153
154
155function initialize(app) {
156
157 include
158 .js({
159 script: [
160 'helper/extensions',
161 ],
162 })
163 .done(function(resp) {
164
165 logger(99)
166 .log('> process:'.cyan, process)
167 .log('> config:'.cyan, app.config);
168
169 if (app.config.tasks == null) {
170 logger.error('<config:invalid> Tasks are not defined', app.config.toJSON());
171 return 0;
172 }
173 app
174 .run()
175 .fail(function(error) {
176 logger.error(error);
177 process.exit(1);
178 })
179 .always(function() {
180 if (app.errors.length) {
181 logger.error(app.errors);
182 process.exit(1);
183 }
184 logger.log('<done>'.green);
185 });
186
187 return 1;
188
189 });
190}
\No newline at end of file