interpret-dubbo2js
Version:
apache dubbo & dubbo-js interpret java-jar file to typescript files
101 lines (100 loc) • 3.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const commander_1 = __importDefault(require("commander"));
const debug_1 = __importDefault(require("debug"));
const fs_extra_1 = require("fs-extra");
const klaw_1 = __importDefault(require("klaw"));
const prettier_1 = __importDefault(require("prettier"));
const config_1 = __importDefault(require("./config"));
const ext_1 = require("./ext");
const request_1 = require("./request");
const to_1 = require("./to");
const log = debug_1.default('j2t:cli');
commander_1.default
.version('0.0.1')
.usage('-c dubbo.json')
.option('-c, --config [value]', 'specify interpret Config ')
.parse(process.argv);
(async () => {
const { res: dubboConfig, err: configErr } = await config_1.default.fromConfigPath(commander_1.default.config);
if (configErr) {
console.error('Error reading configuration file');
console.log(configErr);
log(configErr);
return;
}
const { res: extInfo, err: extError } = await to_1.to(ext_1.extra(dubboConfig));
if (extError) {
console.error('Failed to extract ast from java class');
console.log(extError);
log(extError);
return;
}
//setup jar ast path
console.log('read jar ast file', extInfo.jarInfo);
dubboConfig.jarInfo = extInfo.jarInfo;
log(`parse config->${JSON.stringify(dubboConfig, null, 2)}`);
await new request_1.Request(dubboConfig).work();
await formatSourceDir(dubboConfig.output);
console.log('Translation completed');
})();
/**
* Format the source code
* @param srcDir
* @returns {Promise}
*/
async function formatSourceDir(srcDir) {
log(`Format the source code:${srcDir}`);
return new Promise((resolve, reject) => {
klaw_1.default(srcDir)
.on('data', async (item) => {
if (item.path.endsWith('.ts')) {
try {
let fileContent = await fs_extra_1.readFile(item.path);
await fs_extra_1.writeFile(item.path, prettier_1.default.format(fileContent.toString(), {
parser: 'typescript',
singleQuote: true,
bracketSpacing: false,
trailingComma: 'all',
semi: true,
}));
log(`Format the source code successfully:${item.path}`);
}
catch (err) {
log(`Failed to format the source code:${item.path} ${err}`);
console.warn(`Failed to format the source code:${item.path} ${err}`);
reject(err);
}
}
})
.on('end', () => {
resolve();
});
});
}
process.on('uncaughtException', err => {
console.log(err);
});
process.on('unhandledRejection', err => {
console.log(err);
});