Commit 38469208 authored by rockyl's avatar rockyl

cli wrapper

parent 735bba59
/**
* Created by rockyl on 2019-04-29.
*/
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const {generateDeclareMap} = require('../src/index');
export async function execute(program){
if(!program.input || !program.output){
console.warn('parameter is incorrect');
process.exit(1);
}
const componentsPath = program.projectPath || path.join(os.homedir(), '.scilla', 'components');
const componentsSrcPath = path.join(componentsPath, 'src');
const tsconfig = fs.readJsonSync(path.join(componentsPath, 'tsconfig.json'));
const inputFile = program.input;
const inputFileFolder = path.dirname(inputFile);
const projectPath = process.cwd();
function declareFilter(source) {
return source.fileName.indexOf(inputFileFolder) >= 0;
}
const result = generateDeclareMap(
tsconfig,
program.input,
componentsSrcPath,
projectPath,
null,
declareFilter
);
if(program.verbose){
console.log(result);
}
if(Object.keys(result.missingDependencies).length > 0){
console.warn(JSON.stringify(result.missingDependencies, null, '\t'));
process.exit(2);
}
fs.ensureDirSync(path.dirname(program.output));
fs.writeJsonSync(program.output, result.declareMap);
}
...@@ -4,56 +4,20 @@ ...@@ -4,56 +4,20 @@
* Created by rockyl on 2019-04-26. * Created by rockyl on 2019-04-26.
*/ */
const os = require('os'); import {execute} from "./cli-wrapper";
const path = require('path');
const fs = require('fs-extra');
const program = require('commander'); const program = require('commander');
const {generateDeclareMap} = require('../src/index');
program program
.version('1.0.3') .version('1.0.3')
.description('scilla declare generator') .description('scilla declare generator')
.option('-i, --input [string]', 'input file') .option('-i, --input [string]', 'input file')
.option('-o, --output [string]', 'output file') .option('-o, --output [string]', 'output file')
.option('-p, --pathofcomponents [string]', 'path of components') .option('-p, --projectPath [string]', 'path of components')
.option('-v, --verbose [boolean]', 'verbose mode', false) .option('-v, --verbose [boolean]', 'verbose mode', false)
.parse(process.argv); .parse(process.argv);
if(!program.input || !program.output){ execute(program).catch(e=>{
console.warn('parameter is incorrect'); console.error(e);
process.exit(1);
}
const componentsPath = program.pathofcomponents || path.join(os.homedir(), '.scilla', 'components');
const componentsSrcPath = path.join(componentsPath, 'src');
const tsconfig = fs.readJsonSync(path.join(componentsPath, 'tsconfig.json'));
const inputFile = program.input;
const inputFileFolder = path.dirname(inputFile);
const projectPath = process.cwd();
function declareFilter(source) {
return source.fileName.indexOf(inputFileFolder) >= 0;
}
const result = generateDeclareMap(
tsconfig,
program.input,
componentsSrcPath,
projectPath,
null,
declareFilter
);
if(program.verbose){
console.log(result);
}
if(Object.keys(result.missingDependencies).length > 0){
console.warn(JSON.stringify(result.missingDependencies, null, '\t'));
process.exit(2); process.exit(2);
} });
fs.ensureDirSync(path.dirname(program.output));
fs.writeJsonSync(program.output, result.declareMap);
{ {
"name": "scilla-declare-generator", "name": "scilla-declare-generator",
"version": "1.0.2", "version": "1.0.3",
"main": "src/index.js", "main": "src/index.js",
"bin": "bin/cli.js", "bin": "bin/cli.js",
"license": "MIT", "license": "MIT",
......
...@@ -35,6 +35,7 @@ exports.generateDeclareMap = function(tsconfig, file, componentsPath, projectPat ...@@ -35,6 +35,7 @@ exports.generateDeclareMap = function(tsconfig, file, componentsPath, projectPat
if(showLog) console.timeEnd('getTypeChecker'); if(showLog) console.timeEnd('getTypeChecker');
if(showLog) console.time('delint'); if(showLog) console.time('delint');
const dependencies = [];
const missingDependencies = {}; const missingDependencies = {};
for(let sourceFile of sourceFiles){ for(let sourceFile of sourceFiles){
if (sourceFile.fileName.indexOf('.d.ts') < 0) { if (sourceFile.fileName.indexOf('.d.ts') < 0) {
...@@ -42,6 +43,9 @@ exports.generateDeclareMap = function(tsconfig, file, componentsPath, projectPat ...@@ -42,6 +43,9 @@ exports.generateDeclareMap = function(tsconfig, file, componentsPath, projectPat
const fileName = sourceFile.fileName; const fileName = sourceFile.fileName;
if(sourceFile.resolvedModules){ if(sourceFile.resolvedModules){
sourceFile.resolvedModules.forEach((item, key)=>{ sourceFile.resolvedModules.forEach((item, key)=>{
if(!dependencies.includes(key)){
dependencies.push(key);
}
if(!item){ if(!item){
let t = missingDependencies[fileName]; let t = missingDependencies[fileName];
if(!t){ if(!t){
...@@ -61,6 +65,7 @@ exports.generateDeclareMap = function(tsconfig, file, componentsPath, projectPat ...@@ -61,6 +65,7 @@ exports.generateDeclareMap = function(tsconfig, file, componentsPath, projectPat
return { return {
declareMap, declareMap,
dependencies,
missingDependencies, missingDependencies,
}; };
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment