Skip to content

Commit

Permalink
Merge pull request #9 from bmacnaughton/windows
Browse files Browse the repository at this point in the history
add windows support
  • Loading branch information
bmacnaughton authored Nov 29, 2023
2 parents 1d5d2ab + e9356bb commit 4c2a964
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 146 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/commit-test.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
name: 'commit-test'

on: [push]
on:
push:
branches-ignore:
- test/**
pull_request:
branches-ignore:
- test/**

jobs:
basic-tests:
linux-test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
node-version: [14.x, 16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v4
- name: Testing node v${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test

other-os-tests:
if: github.event_name == 'pull_request'
needs: linux-test
runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [18.x, 20.x, 21.x]
os: [windows-latest, macos-latest]

steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
Minimal utility to walk directory trees performing actions on each directory
entry. `action-walk` has no external production dependencies and has only one
strong opinion - don't presume anything about why the directory tree is being
walked. Oh, and another strong opinion - not supporting node versions prior to
v12.12.0.
walked.

No presumptions means that this does little more than walk the tree. There
are two options to facilitate implementing your code on top of `action-walk`.
Expand All @@ -17,6 +16,10 @@ object.

### usage

`action-walk` should run on any version of node that supports the `node:` prefix
when requiring built-in modules. It is tested on even-numbered versions of node
starting with 14 on both Linux and Windows.

`npm install action-walk`

### examples
Expand Down Expand Up @@ -101,4 +104,3 @@ asynchronous work but only the value of `dirAction` is meaningful.
### todo

- add error handling
- add windows support
7 changes: 4 additions & 3 deletions action-walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

const fsp = require('fs').promises;
const path = require('path');
const { sep } = path;

async function walk (dir, options = {}) {
const noop = async () => undefined;
Expand All @@ -24,7 +25,7 @@ async function walk (dir, options = {}) {
let linkAction;
let otherAction;
let stat;
if (options.stat === 'stat' || options.stat === 'lstat') {
if (options.stat === 'lstat') {
stat = options.stat;
} else if (options.stat) {
stat = 'stat';
Expand Down Expand Up @@ -52,8 +53,8 @@ async function walk (dir, options = {}) {
for await (const dirent of await fsp.opendir(dir)) {
let entry = path.join(dir, dirent.name);
// path.join refuses to start a path with '.'
if (dir === '.' || dir.startsWith('./')) {
entry = './' + entry;
if (dir === '.' || dir.startsWith(`.${sep}`)) {
entry = `.${sep}${entry}`;
}
const ctx = {dirent, stack};
if (options.own) {
Expand Down
20 changes: 10 additions & 10 deletions examples/clean-repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@

const fs = require('fs');
const fsp = require('fs/promises');
const { sep } = require('node:path');

// replace with @bmacnaughton/action-walk to used in your own project.
const walk = require('../action-walk.js');

function dirAction(path, context) {
const { dirent, own } = context;
const { dirent, own, stack } = context;

// we want to go into node_modules directories but only to find a
// node_modules/.cache directory.
if (path.endsWith(`node_modules/${dirent.name}`)) {
// if it's not cache, skip it.
if (dirent.name !== '.cache') {
if (stack.at(-2) === 'node_modules') {
if (stack.at(-1) !== '.cache') {
return 'skip';
}

// the directory is named .cache, so delete it if requested. (action-walk
// doesn't create a cache, but this is just an example.)
const cachePath = `${path}/@bmacnaughton/action-walk`;
// doesn't create a cache; this is just an example.)
const cachePath = `${path}${sep}@bmacnaughton${sep}action-walk`;
if (!own.deleteCache) {
console.log(`[DRY RUN: deleting ${cachePath}]`);
return 'skip';
}
// it is node_modules/.cache, so asynchronously delete @contrast/rasp-v3
// recursively and return skip without waiting for the delete to finish.
// this allows action-walk to continue traversing the directory tree while
// the delete is in progress.
// recursively (ignoring ENOENT errors) and return skip without waiting
// for the delete to finish. this allows action-walk to continue traversing
// the directory tree while the delete is in progress.
deleteDirectoryTree(cachePath);

return 'skip';
Expand Down Expand Up @@ -98,7 +98,7 @@ const options = {
}

if (packageDotJson.name !== '@bmacnaughton/action-walk') {
console.log('? this script must be run from the mono-repo root');
console.log('? this script must be run from the repo root');
process.exit(1);
}

Expand Down
Loading

0 comments on commit 4c2a964

Please sign in to comment.