fix: module name resolve error

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-24 00:19:39 +08:00
parent 548c76f154
commit 996a63ba2d

View File

@ -38,7 +38,6 @@
var BufferedInputStream = Java.type('java.io.BufferedInputStream'); var BufferedInputStream = Java.type('java.io.BufferedInputStream');
var URL = Java.type('java.net.URL') var URL = Java.type('java.net.URL')
var separatorChar = File.separatorChar; var separatorChar = File.separatorChar;
function __assign(t) { function __assign(t) {
@ -55,7 +54,6 @@
* 判断是否为一个文件 * 判断是否为一个文件
* @param file * @param file
* @returns {*} * @returns {*}
* @private
*/ */
function _isFile(file) { function _isFile(file) {
return file.isFile && file.isFile(); return file.isFile && file.isFile();
@ -65,7 +63,6 @@
* 获得文件规范路径 * 获得文件规范路径
* @param file * @param file
* @returns {*} * @returns {*}
* @private
*/ */
function _canonical(file) { function _canonical(file) {
return file.canonicalPath; return file.canonicalPath;
@ -75,7 +72,6 @@
* 获得文件绝对路径 * 获得文件绝对路径
* @param file * @param file
* @returns {*} * @returns {*}
* @private
*/ */
function _absolute(file) { function _absolute(file) {
return file.absolutePath; return file.absolutePath;
@ -106,6 +102,8 @@
/** /**
* 解析文件 * 解析文件
* @param file 文件
* @param dir 目录
* @returns {*} * @returns {*}
*/ */
function resolveAsFile(file, dir) { function resolveAsFile(file, dir) {
@ -128,6 +126,8 @@
/** /**
* 解析目录 * 解析目录
* @param file 文件
* @param dir 目录
* @returns {*} * @returns {*}
*/ */
function resolveAsDirectory(file, dir) { function resolveAsDirectory(file, dir) {
@ -215,7 +215,6 @@
* 预编译Json * 预编译Json
* @param module Json模块 * @param module Json模块
* @param file Json 文件 * @param file Json 文件
* @param optional 附加选项
* @returns {Object} * @returns {Object}
*/ */
function compileJson(module, file) { function compileJson(module, file) {
@ -225,23 +224,25 @@
/** /**
* 尝试从网络下载依赖包 * 尝试从网络下载依赖包
* @param name 包名称
*/ */
function download(name) { function download(name) {
// handle name es6-map/implement => es6-map @ms/common/dist/reflect => @ms/common // handle name es6-map/implement => es6-map @ms/common/dist/reflect => @ms/common
var names = name.split('/'); var name_arr = name.split('/');
name = name.startsWith('@') ? names[0] + '/' + names[1] : names[0]; var module_name = name.startsWith('@') ? name_arr[0] + '/' + name_arr[1] : name_arr[0];
var tempFile = Files.createTempDirectory('MiaoScript').resolve(java.util.UUID.randomUUID().toString() + '.http'); var tempFile = Files.createTempDirectory('MiaoScript').resolve(module_name + '.tgz');
Files.copy(new URL('https://repo.yumc.pw/repository/npm/' + name).openStream(), tempFile) Files.copy(new URL('https://repo.yumc.pw/repository/npm/' + module_name).openStream(), tempFile, StandardCopyOption.REPLACE_EXISTING)
var info = JSON.parse(new java.lang.String(Files.readAllBytes(tempFile), 'UTF-8')); var info = JSON.parse(new java.lang.String(Files.readAllBytes(tempFile), 'UTF-8'));
var url = info.versions[info['dist-tags']['latest']].dist.tarball; var url = info.versions[info['dist-tags']['latest']].dist.tarball;
console.log('local module ' + name + ' not found but exist at internet ' + url + ' downloading...') console.log('node_module ' + module_name + ' not found at local but exist at internet ' + url + ' downloading...')
var tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new URL(url).openStream()))); var tis = new TarInputStream(new BufferedInputStream(new GZIPInputStream(new URL(url).openStream())));
var entry; var target = root + separatorChar + 'node_modules' + separatorChar; var entry; var target = root + separatorChar + 'node_modules' + separatorChar + module_name;
while ((entry = tis.getNextEntry()) != null) { while ((entry = tis.getNextEntry()) != null) {
var targetPath = Paths.get(target + entry.getName().substring('package/'.length)); var targetPath = Paths.get(target + separatorChar + entry.getName().substring('package/'.length));
targetPath.toFile().getParentFile().mkdirs(); targetPath.toFile().getParentFile().mkdirs();
Files.copy(tis, targetPath, StandardCopyOption.REPLACE_EXISTING); Files.copy(tis, targetPath, StandardCopyOption.REPLACE_EXISTING);
} }
return name;
} }
/** /**
@ -250,19 +251,19 @@
* @param path 路径 * @param path 路径
* @param optional 附加选项 * @param optional 附加选项
* @returns {*} * @returns {*}
* @private
*/ */
function _require(name, path, optional) { function _require(name, path, optional) {
var file = new File(name); var file = new File(name);
file = _isFile(file) ? file : resolve(name, path); file = _isFile(file) ? file : resolve(name, path);
optional = __assign({ cache: true, recursive: 0 }, optional); optional = __assign({ cache: true }, optional);
if (optional.recursive > 1) { throw new Error('Recursive call error!') }
if (file === undefined) { if (file === undefined) {
try { try {
if (notFoundModules[name]) { throw new Error("can't found module in network!") } if (optional.recursive || notFoundModules[name]) {
download(name); console.log(name, path, optional, notFoundModules[name])
optional.recursive++ throw new Error("Can't found module " + name + ' in network!')
return _require(name, path, optional); }
optional.recursive = true;
return _require(download(name), path, optional);
} catch (ex) { } catch (ex) {
notFoundModules[name] = true; notFoundModules[name] = true;
throw new FileNotFoundException("Can't found module " + name + ' in directory ' + path + ' ERROR: ' + ex) throw new FileNotFoundException("Can't found module " + name + ' in directory ' + path + ' ERROR: ' + ex)