Skip to content

Commit

Permalink
feat: support typescript.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Aug 22, 2023
1 parent 4fc289d commit 6fa592d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package-lock.json
.cache
.DS_Store
.rdoc-dist
*.tsbuildinfo

*.bak
*.tem
Expand Down
21 changes: 21 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export = whereis;
/**
* @typedef {Object} Options
* @property {'buffer' | null} encoding `encoding` means stdout/stderr are definitely `Buffer`.
*
* @typedef {import("child_process").ExecOptions} ExecOptions
* @param {string} command
* @param {ExecOptions & Options} options
* @returns {Promise<string>}
*/
declare function whereis(command: string, options: ExecOptions & Options): Promise<string>;
declare namespace whereis {
export { Options, ExecOptions };
}
type ExecOptions = import("child_process").ExecOptions;
type Options = {
/**
* `encoding` means stdout/stderr are definitely `Buffer`.
*/
encoding: 'buffer' | null;
};
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const { exec } = require('child_process');

module.exports = function whereis(command, options) {
/**
* @typedef {Object} Options
* @property {'buffer' | null} encoding `encoding` means stdout/stderr are definitely `Buffer`.
*
* @typedef {import("child_process").ExecOptions} ExecOptions
* @param {string} command
* @param {ExecOptions & Options} options
* @returns {Promise<string>}
*/
function whereis(command, options) {
return new Promise((resolve, reject) => {
if (!command) {
return reject('No command name is passed!');
Expand All @@ -11,11 +20,13 @@ module.exports = function whereis(command, options) {
reject(`Could not find ${command} on your system; ${error.message ? error.message : ''}`);
return;
}
stdout = stdout.split('\n')[0];
if (stdout === '' || stdout.charAt(0) !== '/') {
const str = stdout.toString().split('\n')[0];
if (str === '' || str.charAt(0) !== '/') {
reject(new Error(`Could not find ${command} on your system;`));
}
resolve(stdout, stderr);
resolve(str);
});
});
}
}

module.exports = whereis;
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"main": "index.js",
"scripts": {
"tsc": "tsc --build",
"start": "idoc --watch",
"build": "idoc",
"test": "jest",
Expand All @@ -23,8 +24,9 @@
"author": "kenny wong <wowohoo@qq.com>",
"license": "MIT",
"devDependencies": {
"idoc": "^1.21.4",
"jest": "^28.1.2"
"idoc": "^1.26.6",
"jest": "^29.6.3",
"typescript": "^5.1.6"
},
"jest": {
"coverageReporters": [
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"strict": true,
"declaration": true,
"checkJs": true,
"emitDeclarationOnly": true
}
}

0 comments on commit 6fa592d

Please sign in to comment.