-
Notifications
You must be signed in to change notification settings - Fork 0
/
configjoy.js
executable file
·137 lines (121 loc) · 5.91 KB
/
configjoy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /usr/bin/env node
'use strict';
const { program } = require('commander');
const { exec, spawn } = require('child_process');
const wellKnown = require('./well_known.json');
const path = require('path');
const fs = require('fs');
const fsExtra = require('fs-extra');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const { convert, Options } = require("./convert");
program
.name('string-util')
.version('0.1.8')
.description('A structured, type-safe editor for configurable data.')
.option('--files <files>')
.option('--schema <schema>')
.option('--unity-output-dir <unityOutputDir>', 'Directory to output generated Unity C# files')
.option('--generate-only', 'Generate files and terminate without running the interface.', false)
.option('--port', 'The port to listen on for the interface.', 9876)
program.parse();
async function run() {
if ((program.opts().files && program.args.length > 0) || (!program.opts().files && program.args.length === 0)) {
console.error('Error: please specify either --files or a file argument \n\te.g. "configjoy ./config-file.json" or "configjoy --files ./config-file.json"');
process.exit(1)
}
let filesDir = program.opts().files || program.args[0]
let schemaDir = program.opts().schema
if (!fs.existsSync(filesDir)) {
console.error(`Error: ${filesDir} doesn't exist.`)
process.exit(1);
}
if (!schemaDir) {
if (wellKnown[filesDir]) {
schemaDir = path.join(__dirname, 'schema', 'well_known', wellKnown[filesDir])
} else {
await new Promise((resolve, reject) => {
rl.question(`Schema for ${filesDir} is not built in and no --schema was provided.\nAuto generate a schema file for ${filesDir}?\n(y/n): `, function (generate) {
if (generate === 'y') {
rl.question(`Enter a directory to save ${filesDir}.proto:\n`, function (dir) {
const resolved = path.resolve(dir);
fsExtra.mkdirpSync(resolved);
schemaDir = path.join(resolved, path.basename(filesDir)) + '.proto';
const rootMessageName = path.basename(filesDir).slice(0, path.basename(filesDir).lastIndexOf('.')).replace('.', '');
console.log(`Generating schema file at ${schemaDir}...`);
try {
fs.writeFileSync(schemaDir, convert(fs.readFileSync(filesDir, 'utf-8'), new Options(rootMessageName, true, true, true)).success, { flag: 'wx' });
console.log(`Success! For subsequent runs use:\n\tconfigjoy --files ${filesDir} --schema ${path.join(dir, path.basename(filesDir) + '.proto')}`)
} catch (e) {
if (e.code === 'EEXIST') {
console.log("Error: a schema file at that location already exists, please remove it before generating a new schema.")
console.log(`Alternatively, you can use the existing schema with:\n\tconfigjoy --files ${filesDir} --schema ${path.join(dir, path.basename(filesDir) + '.proto')}`)
} else {
console.log(e);
}
process.exit(1);
}
resolve();
});
} else {
rl.close();
process.exit();
}
});
})
}
}
let protoPath = '';
let schemaFiles = '';
if (fs.lstatSync(schemaDir).isDirectory() == false) {
protoPath = path.resolve(schemaDir, '..');
schemaFiles = path.resolve(schemaDir, '.');
} else {
protoPath = schemaDir;
schemaFiles = path.join(schemaDir, '*.proto');
}
if (fs.lstatSync(filesDir).isDirectory() == false) {
filesDir = path.resolve(filesDir, '..')
}
await new Promise((resolve, reject) => {
exec(`${path.join(__dirname, '/protogen/bin/protoc')} \
--plugin=protoc-gen-ts=${path.join(__dirname, '/node_modules/.bin/protoc-gen-ts' + (process.platform === 'win32' ? '.cmd' : ''))} \
--ts_out=${path.join(__dirname, '/generated/typescript/src')} \
--plugin=protoc-gen-configjoy=${path.join(__dirname, '/protogen/scripts/protoc-gen-configjoy' + (process.platform === 'win32' ? '.cmd' : ''))} \
--configjoy_out=${path.join(__dirname, '/generated/')} \
--configjoy_opt=dataDirectory=${filesDir},port=${program.opts().port}${program.opts().unityOutputDir ? `,unityOutputDir=${program.opts().unityOutputDir}` : ''} \
${program.opts().unityOutputDir ? `--csharp_out=${program.opts().unityOutputDir}` : ''} \
--proto_path=${protoPath} \
${schemaFiles}`,
(error, stdout, stderr) => {
if (error) {
console.log(error, stderr);
reject();
} else {
resolve();
}
});
})
if (!program.opts().generateOnly) {
let printedWelcomeMessage = false;
const npm = spawn('npm', ['run', 'dev', '--', '-p', program.opts().port], { cwd: __dirname, shell: true });
npm.stdout.on('data', (data) => {
if (!printedWelcomeMessage) {
printedWelcomeMessage = true;
console.log(`Interface running at http://localhost:${program.opts().port}`)
}
});
npm.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
npm.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
} else {
process.exit(0);
}
}
run();