mirror of https://github.com/actions/cache.git
Use @actions/glob for pattern matching
parent
1e233443e8
commit
db235cfc56
@ -0,0 +1,34 @@
|
|||||||
|
name: Cache
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
# Build and unit test
|
||||||
|
build:
|
||||||
|
runs-on: self-hosted
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v1
|
||||||
|
with:
|
||||||
|
node-version: '12.x'
|
||||||
|
- run: npm install
|
||||||
|
- run: npm run build
|
||||||
|
- name: Restore npm cache
|
||||||
|
uses: ./
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
~/Desktop/cache-me
|
||||||
|
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
||||||
|
- name: Prettier Format Check
|
||||||
|
run: npm run format-check
|
||||||
|
- name: ESLint Check
|
||||||
|
run: npm run lint
|
||||||
|
- name: Build & Test
|
||||||
|
run: npm run test
|
@ -1,42 +0,0 @@
|
|||||||
import * as path from "path";
|
|
||||||
import * as os from "os";
|
|
||||||
import * as pathUtils from "../src/utils/pathUtils";
|
|
||||||
|
|
||||||
jest.mock("@actions/core");
|
|
||||||
jest.mock("os");
|
|
||||||
|
|
||||||
test("expandPaths with no ~ in path", () => {
|
|
||||||
const filePath = ".cache/yarn";
|
|
||||||
|
|
||||||
const resolvedPath = pathUtils.expandPaths([filePath]);
|
|
||||||
|
|
||||||
const expectedPath = [path.resolve(filePath)];
|
|
||||||
expect(resolvedPath).toStrictEqual(expectedPath);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("expandPaths with ~ in path", () => {
|
|
||||||
const filePath = "~/.cache/yarn";
|
|
||||||
|
|
||||||
const homedir = jest.requireActual("os").homedir();
|
|
||||||
const homedirMock = jest.spyOn(os, "homedir");
|
|
||||||
homedirMock.mockImplementation(() => {
|
|
||||||
return homedir;
|
|
||||||
});
|
|
||||||
|
|
||||||
const resolvedPath = pathUtils.expandPaths([filePath]);
|
|
||||||
|
|
||||||
const expectedPath = [path.join(homedir, ".cache/yarn")];
|
|
||||||
expect(resolvedPath).toStrictEqual(expectedPath);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("expandPaths with home not found", () => {
|
|
||||||
const filePath = "~/.cache/yarn";
|
|
||||||
const homedirMock = jest.spyOn(os, "homedir");
|
|
||||||
homedirMock.mockImplementation(() => {
|
|
||||||
return "";
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(() => pathUtils.expandPaths([filePath])).toThrow(
|
|
||||||
"Unable to resolve `~` to HOME"
|
|
||||||
);
|
|
||||||
});
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,63 +0,0 @@
|
|||||||
import * as os from "os";
|
|
||||||
import * as path from "path";
|
|
||||||
import { readdirSync } from "fs";
|
|
||||||
import { IOptions, Minimatch } from "minimatch";
|
|
||||||
|
|
||||||
const globCharacters: string[] = ["*", "?", "[", "]"];
|
|
||||||
const options: IOptions = {
|
|
||||||
nocase: true,
|
|
||||||
dot: true,
|
|
||||||
nobrace: true
|
|
||||||
};
|
|
||||||
|
|
||||||
// export function resolvePath(filePath: string): string {
|
|
||||||
// if (filePath[0] === "~") {
|
|
||||||
// const home = os.homedir();
|
|
||||||
// if (!home) {
|
|
||||||
// throw new Error("Unable to resolve `~` to HOME");
|
|
||||||
// }
|
|
||||||
// return path.join(home, filePath.slice(1));
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return path.resolve(filePath);
|
|
||||||
// }
|
|
||||||
|
|
||||||
export function isMinimatchPattern(pattern: string): boolean {
|
|
||||||
if (globCharacters.some(x => pattern.includes(x))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function matchDirectories(pattern: string, workspace: string): string[] {
|
|
||||||
const directories = readdirSync(workspace, { withFileTypes: true })
|
|
||||||
.filter(dirent => dirent.isDirectory())
|
|
||||||
.map(dirent => dirent.name);
|
|
||||||
|
|
||||||
const minimatch = new Minimatch(pattern, options);
|
|
||||||
const matches = directories.filter(x => minimatch.match(x));
|
|
||||||
|
|
||||||
return matches;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function expandPaths(filePaths: string[]): string[] {
|
|
||||||
const paths: string[] = [];
|
|
||||||
const workspace = process.env["GITHUB_WORKSPACE"] ?? process.cwd();
|
|
||||||
|
|
||||||
for (const filePath of filePaths) {
|
|
||||||
if (isMinimatchPattern(filePath)) {
|
|
||||||
paths.push(...matchDirectories(filePath, workspace));
|
|
||||||
} else if (filePath[0] === "~") {
|
|
||||||
const home = os.homedir();
|
|
||||||
if (!home) {
|
|
||||||
throw new Error("Unable to resolve `~` to HOME");
|
|
||||||
}
|
|
||||||
paths.push(path.join(home, filePath.slice(1)));
|
|
||||||
} else {
|
|
||||||
paths.push(path.resolve(filePath));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return paths;
|
|
||||||
}
|
|
Loading…
Reference in New Issue