Commit 23c636b8 authored by rockyl's avatar rockyl

修复组件依赖关系

增加文件夹遍历执行
parent a59dc6c9
......@@ -5,8 +5,36 @@
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const semver = require('semver');
const {generateDeclareMap} = require('../src/index');
function getAllDependencies(dependencies, missingDependencies, componentName, version, componentsSrcPath){
let temp;
try {
temp = fs.readJsonSync(path.join(componentsSrcPath, componentName, version, 'dep.json'));
}catch (e) {}
if(temp){
for(let key in temp){
if(!dependencies[key]){
let componentName = key.replace('components/', '');
let componentPath = path.join(componentsSrcPath, componentName);
let versions = fs.readdirSync(componentPath);
versions = versions.filter(item=>item.match(/^\d+\.\d+\.\d+$/));
let maxVersion = semver.maxSatisfying(versions, temp[key]);
if(maxVersion){
dependencies[key] = maxVersion;
getAllDependencies(dependencies, missingDependencies, componentName, maxVersion, componentsSrcPath);
}else{
missingDependencies[key] = temp[key];
}
}
}
}
}
exports.execute = async function (program) {
if (!program.input) {
console.warn('parameter is incorrect');
......@@ -22,11 +50,15 @@ exports.execute = async function (program) {
const inputFileFolder = path.dirname(inputFile);
const outputFile = path.join(inputFileFolder, 'dec.json');
let dependencies;
try {
dependencies = fs.readJsonSync(path.join(inputFileFolder, 'dep.json'));
}catch (e) {
let componentPath = path.relative(componentsSrcPath, inputFileFolder);
let componentName = componentPath.substring(0, componentPath.lastIndexOf('/'));
let componentVersion = componentPath.substr(componentPath.lastIndexOf('/') + 1);
let dependencies = {}, missingDependenciesPre = {};
getAllDependencies(dependencies, missingDependenciesPre, componentName, componentVersion, componentsSrcPath);
let result = dealMissingDependencies(missingDependenciesPre);
if(result > 0){
return result;
}
const projectPath = process.cwd();
......@@ -46,13 +78,20 @@ exports.execute = async function (program) {
);
if (program.verbose) {
console.log(result);
console.log(declareMap, missingDependencies);
}
if (Object.keys(missingDependencies).length > 0) {
console.warn(JSON.stringify(missingDependencies, null, '\t'));
return 2;
result = dealMissingDependencies(missingDependencies);
if(result > 0){
return result;
}
fs.writeJsonSync(outputFile, declareMap);
};
function dealMissingDependencies(missingDependencies){
if (Object.keys(missingDependencies).length > 0) {
console.warn(JSON.stringify(missingDependencies, null, '\t'));
return 2;
}
}
......@@ -4,7 +4,6 @@
const ts = require('typescript');
const fs = require('fs-extra');
const semver = require('semver');
let declareMap;
let _componentsPath;
......@@ -30,14 +29,7 @@ exports.generateDeclareMap = function(tsconfig, dependencies, file, componentsPa
for(let key in dependencies){
let componentSrcPath = key.replace('components', 'src');
let versions = fs.readdirSync(componentSrcPath);
versions = versions.filter(item=>item.match(/^\d+\.\d+\.\d+$/));
let maxVersion = semver.maxSatisfying(versions, dependencies[key]);
if(maxVersion){
config.options.paths[key] = [componentSrcPath + '/' + maxVersion + '/index'];
}else{
missingDependencies.push(key);
}
config.options.paths[key] = [componentSrcPath + '/' + dependencies[key] + '/index'];
}
if(missingDependencies.length > 0){
......
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