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

support home dir tilda sign #81

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions workflow-steps/cache/hashing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ function hash(input: string) {
return crypto.createHash('sha256').update(input).digest('hex');
}

export function buildCachePaths(inputPaths: string) {
export function buildCachePaths(
inputPaths: string,
warnInvalidPaths: boolean = true,
) {
const directories = Array.from(
new Set(
inputPaths
.split('\n')
.filter((p) => p)
.map((p) => p.replace(/^~/, '..'))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this now considers changes to the paths as well into the cache - if you change the paths, it should invalidate the cache as well

.reduce(
(allPaths, currPath) => [...allPaths, ...expandPath(currPath)],
[],
Expand All @@ -62,7 +66,7 @@ export function buildCachePaths(inputPaths: string) {
);

const invalidDirectories = directories.filter((dir) => !fs.existsSync(dir));
if (invalidDirectories.length > 0) {
if (invalidDirectories.length > 0 && warnInvalidPaths) {
console.warn(
`The following paths are not valid or empty:\n${invalidDirectories.join(
'\n',
Expand Down
14 changes: 9 additions & 5 deletions workflow-steps/cache/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { createPromiseClient } from '@bufbuild/connect';
import { createConnectTransport } from '@bufbuild/connect-web';
import { CacheService } from './generated_protos/cache_connect';
import { RestoreRequest, RestoreResponse } from './generated_protos/cache_pb';
import { hashKey } from './hashing-utils';
import { buildCachePaths, hashKey } from './hashing-utils';
import { appendFileSync, writeFileSync, existsSync } from 'fs';

const input_key = process.env.NX_CLOUD_INPUT_key;
const inputKey = process.env.NX_CLOUD_INPUT_key;
const inputPaths = process.env.NX_CLOUD_INPUT_paths;
const baseBranch =
process.env.NX_CLOUD_INPUT_base_branch ||
process.env['NX_CLOUD_INPUT_base-branch'];
Expand All @@ -19,10 +20,13 @@ export const cacheClient = createPromiseClient(

const currentBranch = process.env.NX_BRANCH;

if (!input_key) {
throw new Error('No cache restore key provided.');
if (!inputKey || !inputPaths) {
throw new Error('No cache restore key or paths provided.');
}
const key = `${hashKey(input_key)}`;

const paths = buildCachePaths(inputPaths, false);
const stringifiedPaths = paths.join(',');
const key = hashKey(`${inputKey}|${stringifiedPaths}`);
const currentBranchKeys = [key].map((k) => `${currentBranch}-${k}`);
const baseBranchKeys = baseBranch ? [key].map((k) => `${baseBranch}-${k}`) : [];

Expand Down
38 changes: 34 additions & 4 deletions workflow-steps/cache/output/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5987,10 +5987,38 @@ function hashKey(key2) {
function hash(input) {
return crypto.createHash("sha256").update(input).digest("hex");
}
function buildCachePaths(inputPaths2, warnInvalidPaths = true) {
const directories = Array.from(
new Set(
inputPaths2.split("\n").filter((p) => p).map((p) => p.replace(/^~/, "..")).reduce(
(allPaths, currPath) => [...allPaths, ...expandPath(currPath)],
[]
)
)
);
const invalidDirectories = directories.filter((dir) => !fs.existsSync(dir));
if (invalidDirectories.length > 0 && warnInvalidPaths) {
console.warn(
`The following paths are not valid or empty:
${invalidDirectories.join(
"\n"
)}`
);
}
return directories;
}
function expandPath(pattern) {
const globExpandedPaths = import_glob.glob.sync(pattern);
if (globExpandedPaths.length == 0) {
return [pattern];
}
return globExpandedPaths;
}

// main.ts
var import_fs = require("fs");
var input_key = process.env.NX_CLOUD_INPUT_key;
var inputKey = process.env.NX_CLOUD_INPUT_key;
var inputPaths = process.env.NX_CLOUD_INPUT_paths;
var baseBranch = process.env.NX_CLOUD_INPUT_base_branch || process.env["NX_CLOUD_INPUT_base-branch"];
var cacheClient = createPromiseClient(
CacheService,
Expand All @@ -5999,10 +6027,12 @@ var cacheClient = createPromiseClient(
})
);
var currentBranch = process.env.NX_BRANCH;
if (!input_key) {
throw new Error("No cache restore key provided.");
if (!inputKey || !inputPaths) {
throw new Error("No cache restore key or paths provided.");
}
var key = `${hashKey(input_key)}`;
var paths = buildCachePaths(inputPaths, false);
var stringifiedPaths = paths.join(",");
var key = hashKey(`${inputKey}|${stringifiedPaths}`);
var currentBranchKeys = [key].map((k) => `${currentBranch}-${k}`);
var baseBranchKeys = baseBranch ? [key].map((k) => `${baseBranch}-${k}`) : [];
cacheClient.restore(
Expand Down
20 changes: 10 additions & 10 deletions workflow-steps/cache/output/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5975,17 +5975,17 @@ function hashKey(key) {
function hash(input) {
return crypto.createHash("sha256").update(input).digest("hex");
}
function buildCachePaths(inputPaths) {
function buildCachePaths(inputPaths2, warnInvalidPaths = true) {
const directories = Array.from(
new Set(
inputPaths.split("\n").filter((p) => p).reduce(
inputPaths2.split("\n").filter((p) => p).map((p) => p.replace(/^~/, "..")).reduce(
(allPaths, currPath) => [...allPaths, ...expandPath(currPath)],
[]
)
)
);
const invalidDirectories = directories.filter((dir) => !fs.existsSync(dir));
if (invalidDirectories.length > 0) {
if (invalidDirectories.length > 0 && warnInvalidPaths) {
console.warn(
`The following paths are not valid or empty:
${invalidDirectories.join(
Expand All @@ -6004,8 +6004,8 @@ function expandPath(pattern) {
}

// post.ts
var input_key = process.env.NX_CLOUD_INPUT_key;
var input_paths = process.env.NX_CLOUD_INPUT_paths;
var inputKey = process.env.NX_CLOUD_INPUT_key;
var inputPaths = process.env.NX_CLOUD_INPUT_paths;
var stepGroupId = process.env.NX_STEP_GROUP_ID ? process.env.NX_STEP_GROUP_ID.replace(/-/g, "_") : "";
var cacheWasHit = process.env[`NX_CACHE_STEP_WAS_SUCCESSFUL_HIT_${stepGroupId}`] === "true";
if (!!cacheWasHit) {
Expand All @@ -6017,12 +6017,12 @@ if (!!cacheWasHit) {
baseUrl: "http://127.0.0.1:9000"
})
);
if (!input_key || !input_paths) {
throw new Error("No cache restore key or paths provided.");
}
const key = hashKey(input_key);
const paths = buildCachePaths(input_paths);
const paths = buildCachePaths(inputPaths);
const stringifiedPaths = paths.join(",");
const key = hashKey(`${inputKey}|${stringifiedPaths}`);
console.log("Storing the following directories..\n" + paths.join("\n"));
console.log(`
Using key..${key}`);
cacheClient.storeV2(
new StoreRequest({
key,
Expand Down
13 changes: 6 additions & 7 deletions workflow-steps/cache/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { CacheService } from './generated_protos/cache_connect';
import { StoreRequest, StoreResponse } from './generated_protos/cache_pb';
import { buildCachePaths, hashKey } from './hashing-utils';

const input_key = process.env.NX_CLOUD_INPUT_key;
const input_paths = process.env.NX_CLOUD_INPUT_paths;
const inputKey = process.env.NX_CLOUD_INPUT_key;
const inputPaths = process.env.NX_CLOUD_INPUT_paths;

const stepGroupId = process.env.NX_STEP_GROUP_ID
? process.env.NX_STEP_GROUP_ID.replace(/-/g, '_')
Expand All @@ -22,13 +22,12 @@ if (!!cacheWasHit) {
}),
);

if (!input_key || !input_paths) {
throw new Error('No cache restore key or paths provided.');
}
const key = hashKey(input_key);
const paths = buildCachePaths(input_paths);
const paths = buildCachePaths(inputPaths);
const stringifiedPaths = paths.join(',');
const key = hashKey(`${inputKey}|${stringifiedPaths}`);

console.log('Storing the following directories..\n' + paths.join('\n'));
console.log(`\nUsing key..${key}`);

cacheClient
.storeV2(
Expand Down
3 changes: 2 additions & 1 deletion workflow-steps/install-node-modules/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { execSync } = require('child_process');
const { existsSync, readFileSync, writeFileSync } = require('fs');
const { platform } = require('os');

async function main() {
const command = getInstallCommand();
Expand Down Expand Up @@ -27,7 +28,7 @@ async function runCommandWithRetries(command, maxRetries) {

while (retryCount < maxRetries) {
try {
execSync(command);
execSync(command, { stdio: 'inherit' });
patchJest();
console.log('Installed dependencies successfully!');
break;
Expand Down
Loading