1 | #!/usr/bin/env node
|
2 |
|
3 | const args = require('minimist')(process.argv.slice(2));
|
4 | const ipc = require('node-ipc');
|
5 | const CONSTANTS = require("./constants");
|
6 |
|
7 | const BRACKET_END = CONSTANTS.BRACKET_END;
|
8 | const SERVER_PORT = CONSTANTS.SERVER_PORT;
|
9 | const APP_NAME = CONSTANTS.CLIENT_APP_NAME;
|
10 | const SERVER_APP_NAME = CONSTANTS.SERVER_APP_NAME;
|
11 |
|
12 | ipc.config.id = APP_NAME;
|
13 | ipc.config.encoding = "utf8";
|
14 | ipc.config.retry = 5000;
|
15 | ipc.config.logger = function() {};
|
16 |
|
17 | var stdinData = '';
|
18 | process.stdin.setEncoding('utf8');
|
19 | process.stdin.on('data', message => {
|
20 | stdinData += message;
|
21 | if (stdinData.endsWith(BRACKET_END)) {
|
22 | var splitted = stdinData.split(BRACKET_END);
|
23 | splitted = splitted[splitted.length - 2];
|
24 | writeTask(splitted);
|
25 | stdinData = "";
|
26 | }
|
27 | });
|
28 |
|
29 | function writeTask(data) {
|
30 | ipc.of.sfLibraryManager.emit(
|
31 | 'writeTask',
|
32 | data
|
33 | );
|
34 | }
|
35 |
|
36 | ipc.connectToNet(
|
37 | SERVER_APP_NAME,
|
38 | SERVER_PORT,
|
39 | function() {
|
40 | ipc.of.sfLibraryManager.on(
|
41 | 'connect',
|
42 | function() {
|
43 |
|
44 | if (args.task) {
|
45 | ipc.of.sfLibraryManager.emit(
|
46 | args.task, { libraryFolder: args.libraryFolder, name: args.name }
|
47 | );
|
48 | }
|
49 | else {
|
50 | ipc.of.sfLibraryManager.emit(
|
51 | 'init', {
|
52 | initialLibraryData: args.initialLibraryData,
|
53 | libraryFolder: args.libraryFolder,
|
54 | name: args.name
|
55 | }
|
56 | );
|
57 | }
|
58 | }
|
59 | );
|
60 | ipc.of.sfLibraryManager.on(
|
61 | 'read',
|
62 | function(data) {
|
63 | process.stdout.write(data);
|
64 | process.stdout.write("\r\n" + BRACKET_END);
|
65 | }
|
66 | );
|
67 | ipc.of.sfLibraryManager.on(
|
68 | 'error',
|
69 | function(data) {
|
70 | process.stderr.write(data.toString());
|
71 | }
|
72 | );
|
73 | }
|
74 | ); |
\ | No newline at end of file |