Skip to content

Commit

Permalink
Merge pull request #16 from bvanjoi/next
Browse files Browse the repository at this point in the history
release: 0.0.11
  • Loading branch information
bvanjoi authored Jun 24, 2022
2 parents 3d720d9 + 672f8ed commit 0d16fd1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 32 deletions.
15 changes: 5 additions & 10 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import factory, { RawResolverOptions } from '../index'
test('sync function from native code', (t) => {
const resolver = factory.create({})
const result = factory.resolve(resolver, __dirname, './fixture/lib')
t.is(result.path, path.resolve(__dirname, './fixture/lib.js'))
t.is(result.status, true)
t.is(result, path.resolve(__dirname, './fixture/lib.js'))
})

test('resolve do not exist file', (t) => {
Expand All @@ -33,12 +32,10 @@ test('extensions options', (t) => {
}
const resolver = factory.create(resolverOptions)
const result = factory.resolve(resolver, __dirname, './fixture/lib')
t.is(result.path, path.resolve(__dirname, './fixture/lib.ts'))
t.is(result.status, true)
t.is(result, path.resolve(__dirname, './fixture/lib.ts'))
// with query and fragment
const result2 = factory.resolve(resolver, __dirname, './fixture/lib?query#fragment')
t.is(result2.path, path.resolve(__dirname, './fixture/lib.ts?query#fragment'))
t.is(result2.status, true)
t.is(result2, path.resolve(__dirname, './fixture/lib.ts?query#fragment'))
})


Expand All @@ -57,10 +54,8 @@ test('alias options', (t) => {
}
const resolver = factory.create(resolverOptions)
const result = factory.resolve(resolver, __dirname, '@alias/lib')
t.is(result.path, path.resolve(__dirname, './fixture/lib.js'))
t.is(result.status, true)
t.is(result, path.resolve(__dirname, './fixture/lib.js'))

const result2 = factory.resolve(resolver, __dirname, '@false/lib')
t.is(result2.path, undefined)
t.is(result2.status, false)
t.is(result2, "false")
})
6 changes: 1 addition & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,4 @@ export interface ResolverInternal {

}
export function create(options: RawResolverOptions): ExternalObject<ResolverInternal>
export interface ResolveResult {
status: boolean
path?: string
}
export function resolve(resolver: ExternalObject<ResolverInternal>, base_dir: string, id: string): {status: boolean, path?: string}
export function resolve(resolver: ExternalObject<ResolverInternal>, base_dir: string, id: string): string | false
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-resolver",
"version": "0.0.10",
"version": "0.0.11",
"description": "node binding for nodejs-resolver",
"main": "index.js",
"license": "MIT",
Expand Down
20 changes: 4 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,21 @@ pub fn create(options: RawResolverOptions) -> Result<External<Resolver>, napi::E
Ok(External::new(resolver))
}

#[napi(object)]
pub struct ResolveResult {
pub status: bool,
pub path: Option<String>,
}

#[napi(
ts_args_type = "resolver: ExternalObject<ResolverInternal>, base_dir: string, id: string",
ts_return_type = "{status: boolean, path?: string}"
ts_return_type = "string | false"
)]
pub fn resolve(
resolver: External<Resolver>,
base_dir: String,
id: String,
) -> Result<ResolveResult, napi::Error> {
) -> Result<String, napi::Error> {
match (*resolver).resolve(Path::new(&base_dir), &id) {
Ok(val) => {
if let nodejs_resolver::ResolverResult::Info(info) = val {
Ok(ResolveResult {
status: true,
path: Some(format!("{}{}{}", info.path.display(), &info.request.query, &info.request.fragment)),
})
Ok(format!("{}{}{}", info.path.display(), &info.request.query, &info.request.fragment))
} else {
Ok(ResolveResult {
status: false,
path: None,
})
Ok(String::from("false"))
}
}
Err(err) => Err(napi::Error::new(napi::Status::GenericFailure, err)),
Expand Down

0 comments on commit 0d16fd1

Please sign in to comment.