Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port the source code to JavaScript #1

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*.json text
*.md text diff=markdown
*.mjs text
*.ps1 text
*.svg text
*.ts text
*.txt text
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**/.DS_Store
/.idea/
/bin/*.map
/docs/api/
/etc/http-client.private.env.json
/lib/
Expand Down
44 changes: 42 additions & 2 deletions bin/which.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#!/usr/bin/env node
import c from"node:console";import{EOL as x}from"node:os";import{exit as w}from"node:process";import{parseArgs as b}from"node:util";var p={bugs:"https://github.com/cedx/which.js/issues",description:"Find the instances of an executable in the system path. Like the `which` Linux command.",homepage:"https://docs.belin.io/which.js",license:"MIT",name:"@cedx/which",repository:"cedx/which.js",type:"module",version:"8.1.0",author:{email:"cedric@belin.io",name:"C\xE9dric Belin",url:"https://belin.io"},bin:{which:"./bin/which.js"},devDependencies:{"@types/eslint__js":"^8.42.3","@types/gulp":"^4.0.17","@types/node":"^20.12.7",del:"^7.1.0",esbuild:"^0.20.2",eslint:"^8.57.0",execa:"^8.0.1",gulp:"^5.0.0",typedoc:"^0.25.13",typescript:"^5.4.5","typescript-eslint":"^7.7.0"},engines:{node:">=20.0.0"},exports:{types:"./lib/index.d.ts",import:"./lib/index.js"},files:["lib/","src/"],keywords:["find","path","system","utility","which"],scripts:{prepack:"gulp",start:"gulp build && node bin/which.js --help",test:"gulp build && node --test --test-reporter=spec"}};import{stat as m}from"node:fs/promises";import{delimiter as u,extname as g,resolve as y}from"node:path";import o from"node:process";var r=class s{extensions;paths;constructor(t={}){let{extensions:e=[],paths:n=[]}=t;if(!e.length){let i=o.env.PATHEXT??"";e=i?i.split(";"):[".exe",".cmd",".bat",".com"]}if(!n.length){let i=o.env.PATH??"";n=i?i.split(s.isWindows?";":u):[]}this.extensions=e.map(i=>i.toLowerCase()),this.paths=n.map(i=>i.replace(/^"|"$/g,"")).filter(i=>i.length)}static get isWindows(){return o.platform=="win32"||["cygwin","msys"].includes(o.env.OSTYPE??"")}async*find(t){for(let e of this.paths)yield*this.#i(e,t)}async isExecutable(t){try{let e=await m(t);return e.isFile()&&(s.isWindows?this.#t(t):this.#e(e))}catch{return!1}}#t(t){return this.extensions.includes(g(t).toLowerCase())}#e(t){let e=t.mode;if(e&1)return!0;let n=typeof o.getgid=="function"?o.getgid():-1;if(e&8)return n==t.gid;let i=typeof o.getuid=="function"?o.getuid():-1;return e&64?i==t.uid:e&72?i==0:!1}async*#i(t,e){for(let n of["",...s.isWindows?this.extensions:[]]){let i=y(t,`${e}${n}`);await this.isExecutable(i)&&(yield i)}}};import{delimiter as d}from"node:path";var a=class{#t;#e;constructor(t,e){this.#t=t,this.#e=e}async all(){let t=[];for await(let e of this.stream())t.includes(e)||t.push(e);if(t.length)return t;throw Error(`No "${this.#t}" in (${this.#e.paths.join(r.isWindows?";":d)}).`)}async first(){let{value:t}=await this.stream().next();if(t)return t;throw Error(`No "${this.#t}" in (${this.#e.paths.join(r.isWindows?";":d)}).`)}stream(){return this.#e.find(this.#t)}};function l(s,t={}){return new a(s,new r(t))}var h=!1,v=`
import console from "node:console";
import {EOL} from "node:os";
import {exit} from "node:process";
import {parseArgs} from "node:util";
import pkg from "../package.json" with {type: "json"};
import which from "../src/index.js";

// The usage information.
const usage = `
Find the instances of an executable in the system path.

Usage:
Expand All @@ -13,4 +21,36 @@ Options:
-s, --silent Silence the output, just return the exit code (0 if any executable is found, otherwise 1).
-h, --help Display this help.
-v, --version Output the version number.
`;async function j(){let{positionals:s,values:t}=b({allowPositionals:!0,options:{all:{short:"a",type:"boolean",default:!1},help:{short:"h",type:"boolean",default:!1},silent:{short:"s",type:"boolean",default:!1},version:{short:"v",type:"boolean",default:!1}}});if(t.help||t.version)return c.log(t.version?p.version:v.trim());if(!s.length)throw Error("You must provide the name of a command to find.");h=t.silent??!1;let e=l(s[0]),n=await(t.all?e.all():e.first());h||c.log(Array.isArray(n)?n.join(x):n)}j().catch(s=>{h||c.error(s instanceof Error?s.message:s),w(1)});
`;

// Value indicating whether to silence the output.
let silent = false;

try {
// Parse the command line arguments.
const {positionals, values} = parseArgs({allowPositionals: true, options: {
all: {short: "a", type: "boolean", default: false},
help: {short: "h", type: "boolean", default: false},
silent: {short: "s", type: "boolean", default: false},
version: {short: "v", type: "boolean", default: false}
}});

// Print the usage.
if (values.help || values.version) {
console.log(values.version ? pkg.version : usage.trim());
exit(0);
}

// Check the requirements.
if (!positionals.length) throw Error("You must provide the name of a command to find.");

// Find the instances of the provided executable.
silent = values.silent ?? false;
const finder = which(positionals[0]);
const paths = await (values.all ? finder.all() : finder.first());
if (!silent) console.log(Array.isArray(paths) ? paths.join(EOL) : paths);
}
catch(error) {
if (!silent) console.error(error instanceof Error ? error.message : error);
exit(1);
}
3 changes: 3 additions & 0 deletions bin/which.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env pwsh
Set-StrictMode -Version Latest
node "$PSScriptRoot/which.js" @args
24 changes: 0 additions & 24 deletions etc/esbuild.js

This file was deleted.

143 changes: 47 additions & 96 deletions etc/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import {join} from "node:path";
import babelParser from "@babel/eslint-parser";
import js from "@eslint/js";
import ts from "typescript-eslint";

export default ts.config(
export default [
js.configs.recommended,
...ts.configs.strictTypeChecked,
...ts.configs.stylisticTypeChecked,
{
languageOptions: {
parser: babelParser,
parserOptions: {
project: join(import.meta.dirname, "../tsconfig.json")
requireConfigFile: false,
babelOptions: {
babelrc: false,
configFile: false,
plugins: ["@babel/plugin-syntax-import-attributes"]
}
}
},
rules: {
"array-callback-return": "error",
"no-await-in-loop": "off",
"no-constant-binary-expression": "off",
"no-constructor-return": "error",
"no-duplicate-imports": "error",
"no-new-native-nonconstructor": "error",
"no-promise-executor-return": "off",
"no-inner-declarations": "error",
"no-promise-executor-return": "error",
"no-self-compare": "error",
"no-template-curly-in-string": "error",
"no-unmodified-loop-condition": "error",
"no-unreachable-loop": "error",
"no-unused-private-class-members": "error",
"no-use-before-define": "off",
"no-use-before-define": ["error", {functions: false}],
// "no-useless-assignment": "error",
"require-atomic-updates": ["error", {allowProperties: true}],

"accessor-pairs": "error",
"arrow-body-style": "error",
"block-scoped-var": "error",
"camelcase": "off",
"camelcase": "error",
"capitalized-comments": "error",
"class-methods-use-this": "off",
"complexity": ["error", {max: 50}],
Expand All @@ -44,78 +46,78 @@ export default ts.config(
"dot-notation": "error",
"eqeqeq": "off",
"func-name-matching": "error",
"func-names": "off",
"func-names": "error",
"func-style": ["error", "declaration", {allowArrowFunctions: true}],
"grouped-accessor-pairs": "error",
"guard-for-in": "error",
"id-denylist": "off",
"id-denylist": "error",
"id-length": ["error", {exceptions: ["_", "x", "y"]}],
"id-match": "error",
"init-declarations": "off",
"max-classes-per-file": "off",
"init-declarations": "error",
"logical-assignment-operators": "error",
"max-classes-per-file": "error",
"max-depth": "error",
"max-lines": ["error", {max: 500}],
"max-lines-per-function": ["error", {max: 100}],
"max-nested-callbacks": "error",
"max-params": "off",
"max-params": "error",
"max-statements": ["error", {max: 25}],
"multiline-comment-style": ["error", "separate-lines"],
"new-cap": ["error", {capIsNewExceptions: ["RangeError", "SyntaxError", "TypeError"]}],
"no-alert": "error",
"no-array-constructor": "error",
"no-bitwise": "off",
"no-caller": "error",
"no-confusing-arrow": "off",
"no-console": "off",
"no-continue": "off",
"no-console": "error",
"no-continue": "error",
"no-div-regex": "error",
"no-else-return": "error",
"no-empty-function": "error",
"no-empty-static-block": "error",
"no-eq-null": "off",
"no-eq-null": "error",
"no-eval": "error",
"no-extend-native": "error",
"no-extra-bind": "error",
"no-extra-label": "error",
"no-implicit-coercion": "error",
"no-implicit-globals": "error",
"no-implied-eval": "error",
"no-inline-comments": "off",
"no-invalid-this": "off",
"no-inline-comments": "error",
"no-invalid-this": "error",
"no-iterator": "error",
"no-label-var": "error",
"no-labels": "error",
"no-lone-blocks": "error",
"no-lonely-if": "error",
"no-loop-func": "off",
"no-loop-func": "error",
"no-magic-numbers": "off",
"no-multi-assign": ["error", {ignoreNonDeclaration: true}],
"no-multi-str": "error",
"no-negated-condition": "off",
"no-nested-ternary": "off",
"no-negated-condition": "error",
"no-nested-ternary": "error",
"no-new": "error",
"no-new-func": "error",
"no-new-wrappers": "error",
"no-object-constructor": "error",
"no-octal-escape": "error",
"no-param-reassign": "off",
"no-plusplus": "off",
"no-param-reassign": "error",
"no-plusplus": "error",
"no-proto": "error",
"no-restricted-exports": "off",
"no-restricted-globals": "off",
"no-restricted-imports": "off",
"no-restricted-properties": "off",
"no-restricted-syntax": "off",
"no-return-assign": "off",
"no-restricted-exports": "error",
"no-restricted-globals": "error",
"no-restricted-imports": "error",
"no-restricted-properties": "error",
"no-restricted-syntax": "error",
"no-return-assign": "error",
"no-script-url": "error",
"no-sequences": "error",
"no-shadow": "off",
"no-shadow": "error",
"no-ternary": "off",
"no-throw-literal": "error",
"no-undef-init": "error",
"no-undefined": "error",
"no-underscore-dangle": "error",
"no-unneeded-ternary": "error",
"no-unused-expressions": "off",
"no-unused-expressions": "error",
"no-useless-call": "error",
"no-useless-computed-key": "error",
"no-useless-concat": "error",
Expand All @@ -128,17 +130,20 @@ export default ts.config(
"one-var": ["error", "never"],
"operator-assignment": "error",
"prefer-arrow-callback": "error",
"prefer-destructuring": "off",
"prefer-const": "error",
"prefer-destructuring": "error",
"prefer-exponentiation-operator": "error",
"prefer-named-capture-group": "off",
"prefer-named-capture-group": "error",
"prefer-numeric-literals": "error",
"prefer-object-has-own": "error",
"prefer-object-spread": "error",
"prefer-promise-reject-errors": "error",
"prefer-regex-literals": "error",
"prefer-rest-params": "error",
"prefer-spread": "error",
"prefer-template": "error",
"radix": ["error", "as-needed"],
"require-await": "off",
"require-await": "error",
"require-unicode-regexp": "off",
"sort-imports": "off",
"sort-keys": "off",
Expand All @@ -149,61 +154,7 @@ export default ts.config(
"yoda": "error",

"line-comment-position": "error",
"unicode-bom": "error",

"@typescript-eslint/class-literal-property-style": "off",
"@typescript-eslint/class-methods-use-this": "off",
"@typescript-eslint/consistent-type-exports": "error",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/default-param-last": "error",
"@typescript-eslint/explicit-function-return-type": ["error", {allowExpressions: true}],
"@typescript-eslint/explicit-member-accessibility": ["error", {accessibility: "no-public"}],
"@typescript-eslint/explicit-module-boundary-types": "error",
"@typescript-eslint/init-declarations": "error",
"@typescript-eslint/max-params": ["error", {max: 4}],
"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/method-signature-style": "error",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-confusing-void-expression": "off",
"@typescript-eslint/no-dupe-class-members": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-import-type-side-effects": "error",
"@typescript-eslint/no-invalid-this": "error",
"@typescript-eslint/no-loop-func": "error",
"@typescript-eslint/no-magic-numbers": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-redeclare": "error",
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/no-restricted-imports": "error",
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unsafe-unary-minus": "error",
"@typescript-eslint/no-unused-expressions": ["error", {allowTaggedTemplates: true, allowTernary: true}],
"@typescript-eslint/no-use-before-define": ["error", {functions: false}],
"@typescript-eslint/no-useless-empty-export": "error",
"@typescript-eslint/parameter-properties": "error",
"@typescript-eslint/prefer-destructuring": "error",
"@typescript-eslint/prefer-enum-initializers": "off",
"@typescript-eslint/prefer-readonly": "error",
"@typescript-eslint/prefer-readonly-parameter-types": "off",
"@typescript-eslint/prefer-regexp-exec": "error",
"@typescript-eslint/promise-function-async": "off",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/return-await": "error",
"@typescript-eslint/sort-type-constituents": "error",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/switch-exhaustiveness-check": "error",
"@typescript-eslint/typedef": "error"
}
},
{
files: ["gulpfile.js", "etc/*.js", "test/**/*.js"],
rules: {
"prefer-arrow-callback": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-floating-promises": "off"
"unicode-bom": "error"
}
}
);
];
5 changes: 3 additions & 2 deletions etc/typedoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* @type {Partial<import("typedoc").TypeDocOptions>}
*/
export default {
entryPoints: ["../src/index.ts"],
entryPoints: ["../src/index.js"],
excludePrivate: true,
gitRevision: "main",
hideGenerator: true,
name: "Which for JS",
out: "../docs/api",
readme: "none"
readme: "none",
tsconfig: "../src/jsconfig.json"
};
Loading
Loading