Comparing version
@@ -101,6 +101,18 @@ 'use strict' | ||
// make sure a file uri has correct espresso extension | ||
$void.appendExt = function (path) { | ||
return !path || typeof path !== 'string' ? path | ||
: path.endsWith('.es') ? path | ||
: path + '.es' | ||
$void.completeFile = function (path) { | ||
if (!path || typeof path !== 'string') { | ||
path = '' | ||
} else if (path.endsWith('/')) { | ||
while (path.endsWith('/')) { | ||
path = path.substring(0, path.length - 1) | ||
} | ||
if (path) { | ||
var offset = path.length - 2 | ||
while (offset >= 0 && path[offset] !== '/') { | ||
offset-- | ||
} | ||
path += '/' + (offset >= 0 ? path.substring(offset + 1) : path) | ||
} | ||
} | ||
return path.endsWith('.es') ? path : path + '.es' | ||
} | ||
@@ -107,0 +119,0 @@ |
@@ -11,3 +11,3 @@ 'use strict' | ||
var evaluate = $void.evaluate | ||
var appendExt = $void.appendExt | ||
var completeFile = $void.completeFile | ||
var sharedSymbolOf = $void.sharedSymbolOf | ||
@@ -45,3 +45,3 @@ var staticOperator = $void.staticOperator | ||
} | ||
source = appendExt(source) | ||
source = completeFile(source) | ||
if (!loader.isResolved(source)) { | ||
@@ -48,0 +48,0 @@ source = loader.resolve(source, dirs) |
@@ -13,3 +13,3 @@ 'use strict' | ||
var evaluate = $void.evaluate | ||
var appendExt = $void.appendExt | ||
var completeFile = $void.completeFile | ||
var sharedSymbolOf = $void.sharedSymbolOf | ||
@@ -80,9 +80,5 @@ var staticOperator = $void.staticOperator | ||
function importModule (space, appHome, moduleUri, source) { | ||
if (typeof source !== 'string') { | ||
if (source instanceof Symbol$) { | ||
source = source.key | ||
} else { | ||
warn('import', 'invalid module source:', source) | ||
return null | ||
} | ||
if (typeof source !== 'string' || !source) { | ||
warn('import', 'invalid module source:', source) | ||
return null | ||
} | ||
@@ -97,3 +93,3 @@ var type | ||
var uri = type ? source // native module | ||
: resolve(space, appHome, moduleUri, appendExt(source)) | ||
: resolve(space, appHome, moduleUri, source) | ||
if (!uri) { | ||
@@ -142,3 +138,3 @@ return null | ||
) | ||
var uri = loader.resolve(source, dirs) | ||
var uri = loader.resolve(completeFile(source), dirs) | ||
if (typeof uri === 'string') { | ||
@@ -148,6 +144,9 @@ return uri | ||
// try to load native Espresso modules. | ||
if ($void.require.resolve) { | ||
return $void.require.resolve(source, | ||
if ($void.require.resolve && !isRelative(source)) { | ||
uri = $void.require.resolve(source, appHome, | ||
space.local['-app-dir'], $void.$env('user-home'), $void | ||
) | ||
if (typeof uri === 'string') { | ||
return uri | ||
} // else, make sure to display both warnings. | ||
} | ||
@@ -158,5 +157,9 @@ warn('import', 'failed to resolve', source, 'in', dirs) | ||
function isRelative (source) { | ||
return source.startsWith('./') || source.startsWith('../') | ||
} | ||
function dirsOf (source, moduleDir, appDir, userDir, homeDir, runtimeDir) { | ||
return moduleDir | ||
? source.startsWith('./') || source.startsWith('../') | ||
? isRelative(source) | ||
? [ moduleDir ] | ||
@@ -163,0 +166,0 @@ : [ runtimeDir, appDir, userDir, homeDir ] |
@@ -10,3 +10,3 @@ 'use strict' | ||
var evaluate = $void.evaluate | ||
var appendExt = $void.appendExt | ||
var completeFile = $void.completeFile | ||
var sharedSymbolOf = $void.sharedSymbolOf | ||
@@ -40,3 +40,3 @@ var staticOperator = $void.staticOperator | ||
// try to locate the source uri | ||
var uri = resolve(appDir, moduleUri, appendExt(source)) | ||
var uri = resolve(appDir, moduleUri, completeFile(source)) | ||
if (typeof uri !== 'string') { | ||
@@ -43,0 +43,0 @@ return null |
@@ -10,3 +10,3 @@ 'use strict' | ||
var execute = $void.execute | ||
var appendExt = $void.appendExt | ||
var completeFile = $void.completeFile | ||
var atomicArrayOf = $void.atomicArrayOf | ||
@@ -27,3 +27,3 @@ | ||
var loader = $void.loader | ||
appSource = appendExt(appSource) | ||
appSource = completeFile(appSource) | ||
var uri = loader.resolve(appSource, [ | ||
@@ -30,0 +30,0 @@ appHome, $void.runtime('home') |
@@ -22,3 +22,3 @@ 'use strict' | ||
var loader = $void.loader | ||
var appendExt = $void.appendExt | ||
var completeFile = $void.completeFile | ||
@@ -65,19 +65,17 @@ // customize native runtime by a profile. | ||
} | ||
var space = prepareTestSpace() | ||
var target = completeFile(args.length > 0 ? args[0] : 'test.es') | ||
var file = loader.resolve(target, [PWD, PWD + '/test']) | ||
if (typeof file !== 'string') { // load test suites | ||
warn('app:runTest', 'missing test suite:', target, 'in', [PWD, PWD + '/test']) | ||
return null | ||
} | ||
var space = prepareTestSpace(file) | ||
space.bindOperators() | ||
var targets = args.length > 0 ? args : ['test.es'] | ||
for (var i = 0; i < targets.length; i++) { | ||
var target = appendExt(targets[i]) | ||
var file = loader.resolve(target, [PWD, PWD + '/test']) | ||
if (typeof file === 'string') { // load test suites | ||
space.$load(file) | ||
} else { | ||
warn('app:runTest', 'missing test suite:', target) | ||
} | ||
} | ||
space.$load(file) | ||
return space.local.test() | ||
} | ||
function prepareTestSpace () { | ||
var appSpace = $void.createBootstrapSpace(PWD + '/.') | ||
function prepareTestSpace (file) { | ||
var testHome = path.join(path.dirname(file), '@') | ||
var appSpace = $void.createBootstrapSpace(testHome) | ||
var test = appSpace.$import('test') | ||
@@ -88,3 +86,3 @@ for (var key in test) { | ||
// run tests in a module space. | ||
return $void.createModuleSpace(PWD + '/test', appSpace) | ||
return $void.createModuleSpace(testHome, appSpace) | ||
} | ||
@@ -91,0 +89,0 @@ |
@@ -95,18 +95,11 @@ 'use strict' | ||
// try to resolve a compatible Espresso NPM module. | ||
modules.resolve = function (source, appDir, userHome, $void) { | ||
// try to split package name and module (file) path. | ||
var offset = source.indexOf('/') | ||
var pack = offset > 0 ? source.substring(0, offset) : source | ||
var mod = (offset > 0 && source.substring(offset + 1)) || pack | ||
// a trailing folder separator indicates a folder. | ||
if (mod.endsWith('/')) { | ||
while (mod.endsWith('/')) { | ||
mod = mod.substring(0, mod.length - 1) | ||
} | ||
// the <default> module has the same name of its container folder. | ||
// or, when multiple trailing '/', falls back to package name again. | ||
mod = mod ? path.join(mod, path.basename(mod)) : pack | ||
} | ||
modules.resolve = function (source, appHome, appDir, userHome, $void) { | ||
// a package is always treated as a directory. | ||
var filePath = $void.completeFile(source.indexOf('/') > 0 ? source : source + '/') | ||
// split package name and module (file) path. | ||
var offset = filePath.indexOf('/') | ||
var pack = filePath.substring(0, offset) | ||
var mod = filePath.substring(offset + 1) | ||
// try to resolve the package by underlying require.resolve logic. | ||
var paths = pathsOf(appDir, userHome) | ||
var paths = pathsOf(appHome, appDir, userHome) | ||
var dir = tryToResolveDir(pack, paths) | ||
@@ -117,3 +110,4 @@ if (typeof dir !== 'string') { | ||
if (typeof dir !== 'string') { | ||
$void.$warn('modules:resolve', 'failed to resolve', moduleUri, 'for', dir) | ||
$void.$warn('modules:resolve', 'failed to resolve', source, | ||
'as', filePath, 'on either', pack, 'or', '@eslang/' + pack, 'in', paths) | ||
return null | ||
@@ -124,4 +118,4 @@ } | ||
var uri = path.join(dir, 'es', mod) | ||
if (!fs.existsSync(uri) || !fs.existsSync(path.join(dir, 'package.json'))) { | ||
$void.$warn('modules:resolve', 'failed to resolve', moduleUri, 'at', dir) | ||
if (!fs.existsSync(uri)) { | ||
$void.$warn('modules:resolve', 'failed to resolve', source, 'at', uri) | ||
return null | ||
@@ -135,3 +129,3 @@ } | ||
'an Espresso module without native dependency does not need to be ' + | ||
'published as a npm package.', [moduleUri, dir]) | ||
'published as a npm package.', [source, dir]) | ||
return null | ||
@@ -144,16 +138,31 @@ } | ||
function pathsOf (appDir, userHome) { | ||
return appDir.endsWith('/es') && fs.existsSync( | ||
path.resolve(appDir, '../package.json') | ||
) ? [ // for a (possibly) compatible Espresso npm package. | ||
path.resolve(appDir, '../node_modules'), | ||
path.join(userHome, '.es/node_modules') | ||
] : [ // for a single Espresso app file, only check shared modules. | ||
path.join(userHome, '.es/node_modules') | ||
] | ||
function isEspressoPackage (dir) { | ||
return fs.existsSync(path.join(dir, 'es')) && | ||
fs.existsSync(path.join(dir, 'package.json')) | ||
} | ||
function findPackage (dir) { | ||
var offset = dir.indexOf('/es/') | ||
if (offset === 0) { // es must be in a package so it must have a parent dir. | ||
offset = dir.indexOf(3) | ||
} | ||
dir = offset > 0 ? dir.substring(0, offset) | ||
: dir.endsWith('/es') ? path.resolve(dir, '..') : null | ||
return dir && isEspressoPackage(dir) ? dir : null | ||
} | ||
function pathsOf (appHome, appDir, userHome) { | ||
var pack = findPackage(appHome) | ||
var paths = pack ? [path.join(pack, 'node_modules')] : [] | ||
if (appDir !== appHome) { | ||
(pack = findPackage(appDir)) && paths.push(path.join(pack, 'node_modules')) | ||
} | ||
paths.push(path.join(userHome, '.es/node_modules')) | ||
return paths | ||
} | ||
function tryToResolveDir (pack, paths) { | ||
try { | ||
return path.dirname(require.resolve(pack, { paths: paths })) | ||
var dir = path.dirname(require.resolve(pack, { paths: paths })) | ||
return isEspressoPackage(dir) ? dir : null | ||
} catch (err) { | ||
@@ -160,0 +169,0 @@ return err |
@@ -12,2 +12,4 @@ 'use strict' | ||
// load native Espresso module extension. | ||
require('./modules') | ||
// load native io provider. | ||
@@ -14,0 +16,0 @@ require('./io')($void) |
{ | ||
"name": "eslang", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "email": "leevi@nirlstudio.com", |
Sorry, the diff of this file is not supported yet
1334814
0.06%11527
0.18%