Skip to content

Commit

Permalink
tools: Add options absolute/root to ls
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jun 17, 2024
1 parent 52c0483 commit 4cf7b76
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function ls() {
, out = []
, paths = {}
, reEscRe = /[*.+^=:${}()|\/\\]/g
, opts = { cwd: process.cwd(), dir: true, dot: false, file: true, stat: false }
, opts = { absolute: false, cwd: process.cwd(), dir: true, dot: false, file: true, root: "", stat: false }
for (; i > 0; ) {
key = arr[--i]
if (isObj(key)) Object.assign(opts, key)
Expand All @@ -114,8 +114,9 @@ function ls() {
var stat = fs.statSync(name)
if (outRe.test(name)) {
if (stat.isDirectory() ? opts.dir : opts.file) out.push(
opts.stat ? stat :
path.relative(opts.cwd, name)
opts.stat ? Object.assign(stat, { name: opts.absolute ? name : opts.root + path.relative(opts.cwd, name) }) :
opts.absolute ? name :
opts.root + path.relative(opts.cwd, name)
)
}
if (stat.isDirectory() && dirRe.test(name)) {
Expand Down
8 changes: 8 additions & 0 deletions test/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
describe("tools", function() {
var fs = require("fs")
, child = require("child_process")
, path = require("path")
, cli = require("..")

it ("should check command exists", [
Expand Down Expand Up @@ -59,7 +60,14 @@ describe("tools", function() {
// Assert options
assert.equal(cli.ls(".github/*", { dir: false }).join(" "), ".github/jshint.json .github/litejs.json")
assert.equal(cli.ls("*", { cwd: ".github", dir: false }).join(" "), "jshint.json litejs.json")
assert.own(cli.ls("*", { cwd: ".github", stat: true }), [
{ size: 225, name: "jshint.json" },
{ size: 4, name: "litejs.json" },
{ size: 4096, name: "workflows" }
])
assert.equal(cli.ls(".github/*", { file: false }).join(" "), ".github/workflows")
assert.equal(cli.ls(".github/*", { file: false, root: "/www/" }).join(" "), "/www/.github/workflows")
assert.equal(cli.ls(".github/*", { file: false, absolute: true }).join(" "), path.join(process.cwd(), ".github/workflows"))
assert.equal(cli.ls(".github/*", { dot: true }).join(" "), ".github/.dot .github/.dot2 .github/jshint.json .github/litejs.json .github/workflows")
cli.rmrf(".github/.dot")
cli.rmrf(".github/.dot2")
Expand Down

0 comments on commit 4cf7b76

Please sign in to comment.