Commit 23c636b8 authored by rockyl's avatar rockyl

修复组件依赖关系

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