Skip to content

Commit

Permalink
Adding includes and excludes (#14)
Browse files Browse the repository at this point in the history
* Adding includes and excludes
* Updating workflows
  • Loading branch information
DaanV2 authored Mar 15, 2024
1 parent 40e2874 commit 84a7cb1
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 119 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
5 changes: 3 additions & 2 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: 📋 PR

on:
pull_request: {}
workflow_dispatch: {}

push:
branches:
- main
jobs:
build:
name: 📦🔍 Build Check
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# This is a basic workflow to help you get started with Actions

name: Test script

# Controls when the action will run.
on:
pull_request:

pull_request: {}
workflow_dispatch: {}
push:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
branches:
- main

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ inputs:
description: The content of to add to the index file, default to '.content.md'
required: false
default: '.content.md'
include:
description: The files to additionaly include in the index file, follows grep pattern on the filenames, supports multiple patterns via multi-line string
required: false
default: ''
exclude:
description: The files to exclude from the index file, follows grep pattern on the filenames, supports multiple patterns via multi-line string
required: false
default: ''
runs:
using: 'node20'
main: 'dist/index.js'
24 changes: 18 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@
"devDependencies": {
"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@vercel/ncc": "^0.38.1",
"tslint": "^6.1.3",
"typescript": "^5.3.3",
"@vercel/ncc": "^0.38.1"
"typescript": "^5.3.3"
},
"dependencies": {
"@actions/core": "^1.10.1"
"@actions/core": "^1.10.1",
"picomatch": "^4.0.1"
}
}
39 changes: 24 additions & 15 deletions src/action.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import { existsSync } from "fs";
import { createFolder } from "./traverse";
import { getInput, setFailed } from "@actions/core";
import { Processor } from "./traverse";
import {
debug,
error,
getInput,
getMultilineInput,
setFailed,
} from "@actions/core";
import { Arguments } from "./arguments";
import { info } from "console";

//Start code
try {
// This should be a token with access to your repository scoped in as a secret.
// The YML workflow will need to set myToken with the GitHub Secret Token
// token: ${{ secrets.GITHUB_TOKEN }}
const folder = getInput("folder");
const filename = getInput("filename");
const content = getInput("content");
var result = false;

console.log("starting on: " + folder);
const args: Arguments = {
contentFilename: getInput("content"),
excludes: getMultilineInput("exclude"),
includes: getMultilineInput("include"),
indexFilename: getInput("filename"),
startFolder: getInput("folder"),
};
Arguments.sanitize(args);

if (existsSync(folder)) {
result = createFolder(folder, { content, filename });
} else {
throw { message: "Couldn't not find folder: " + folder };
}
info("starting on: " + args.startFolder);
debug(`arguments ${JSON.stringify(args, undefined, 2)}`);
const processor = new Processor(args);
const result = processor.createFolder(args.startFolder);

if (result) {
console.log("success");
info("success");
} else {
console.log("failure");
error("failure");
setFailed("no pages were created");
}
} catch (error) {
Expand Down
46 changes: 46 additions & 0 deletions src/arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { debug } from "@actions/core";
import { existsSync } from "fs";

export class Arguments {
/** The folder to start the process of */
startFolder: string;
/** The filename of the index files */
indexFilename: string;
/** The content filename to add to indexes */
contentFilename: string;
/** The patterns to include, addeds *.md by default */
includes: string[];
/** The patterns to exclude */
excludes: string[];
}

export namespace Arguments {
export function sanitize(args: Arguments): void {
// Checks
if (args.startFolder === "") {
throw new Error("No folder specified");
}
if (!existsSync(args.startFolder)) {
throw new Error("Folder does not exist: " + args.startFolder);
}

if (args.indexFilename === "") {
throw new Error("No filename specified for the indexes");
}

if (!Array.isArray(args.includes)) {
args.includes = [];
}
if (!Array.isArray(args.excludes)) {
args.excludes = [];
}

// Add the content filename to the excludes
if (args.contentFilename !== "") {
args.excludes.push(args.contentFilename);
}

args.includes.push("*.md");
args.excludes.push(args.indexFilename, ".git", "node_modules");
}
}
52 changes: 52 additions & 0 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { PicomatchOptions, isMatch } from "picomatch";

export class FileFilter {
private include: string[];
private exclude: string[];
private opts: PicomatchOptions;

/**
* Creates a new FileFilter.
* @param include The patterns to include, addeds *.md by default.
* @param exclude The patterns to exclude.
*/
constructor(include: string[], exclude: string[]) {
this.include = include;
this.exclude = exclude;

this.opts = {
contains: true,
};
}

/**
* Checks if the file is a match, and not excluded
* @param filename The filename to check
* @returns true if the file is a match, false otherwise
*/
public isMatch(filename: string): boolean {
return this.included(filename) && !this.excluded(filename);
}

/**
* Checks if the file is included
* @param filename The filepath to check
* @returns true if the file is included, false otherwise
*/
included(filename: string): boolean {
return this.include.some((pattern) =>
isMatch(filename, pattern, this.opts)
);
}

/**
* Checks if the file is excluded
* @param filename The filepath to check
* @returns true if the file is excluded, false otherwise
*/
excluded(filename: string): boolean {
return this.exclude.some((pattern) =>
isMatch(filename, pattern, this.opts)
);
}
}
Loading

0 comments on commit 84a7cb1

Please sign in to comment.