Skip to content

Commit

Permalink
queued operations and ts (#9)
Browse files Browse the repository at this point in the history
Queues async operations instead of blocking on them.

I've never seen `ncc` before but it said it supports TS out of the box 🤷

I haven't ran this because I don't know how to run actions locally, but
theoretically it will function :D pls test
  • Loading branch information
tbillington authored Oct 31, 2024
1 parent 5896ecd commit ed82140
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 134 deletions.
88 changes: 71 additions & 17 deletions dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27533,37 +27533,91 @@ module.exports = parseParams
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __nccwpck_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => (module['default']) :
/******/ () => (module);
/******/ __nccwpck_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __nccwpck_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __nccwpck_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};
const core = __nccwpck_require__(4708);

async function main() {
// Find the current time since the UNIX epoch.
const stamp = Date.now();

core.info(`Creating timestamp at ${new Date(stamp)}.`);
core.info("All files in the `target` folder not accessed between now and the end of the run will be purged.")
// This entry need to be wrapped in an IIFE because it need to be in strict mode.
(() => {
"use strict";
__nccwpck_require__.r(__webpack_exports__);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(4708);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};

// Save the timestamp so that it can be accessed in the post step.
core.saveState("timestamp", stamp);
function main() {
return __awaiter(this, void 0, void 0, function* () {
// Find the current time since the UNIX epoch.
const stamp = Date.now();
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Creating timestamp at ${new Date(stamp)}.`);
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info("All files in the `target` folder not accessed between now and the end of the run will be purged.");
// Save the timestamp so that it can be accessed in the post step.
_actions_core__WEBPACK_IMPORTED_MODULE_0__.saveState("timestamp", stamp);
});
}

try {
main();

core.saveState("failed", "false");
} catch (err) {
core.setFailed(`Action failed with error: ${err}`);

_actions_core__WEBPACK_IMPORTED_MODULE_0__.saveState("failed", "false");
}
catch (err) {
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(`Action failed with error: ${err}`);
// The post action will, by default, always run. This signals that an error occurred and it
// should not proceed.
core.saveState("failed", "true");
_actions_core__WEBPACK_IMPORTED_MODULE_0__.saveState("failed", "true");
}

})();

module.exports = __webpack_exports__;
/******/ })()
;
172 changes: 89 additions & 83 deletions dist/post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25707,14 +25707,6 @@ module.exports = require("fs");

/***/ }),

/***/ 1943:
/***/ ((module) => {

"use strict";
module.exports = require("fs/promises");

/***/ }),

/***/ 8611:
/***/ ((module) => {

Expand Down Expand Up @@ -27547,12 +27539,34 @@ module.exports = parseParams
/******/
/************************************************************************/
var __webpack_exports__ = {};
const core = __nccwpck_require__(4708);
const exec = __nccwpck_require__(9365);
// This entry need to be wrapped in an IIFE because it need to be in strict mode.
(() => {
"use strict";

// EXTERNAL MODULE: ./node_modules/.pnpm/@[email protected]/node_modules/@actions/core/lib/core.js
var core = __nccwpck_require__(4708);
// EXTERNAL MODULE: ./node_modules/.pnpm/@[email protected]/node_modules/@actions/exec/lib/exec.js
var exec = __nccwpck_require__(9365);
;// CONCATENATED MODULE: external "fs/promises"
const promises_namespaceObject = require("fs/promises");
// EXTERNAL MODULE: external "path"
var external_path_ = __nccwpck_require__(6928);
// EXTERNAL MODULE: external "stream"
var external_stream_ = __nccwpck_require__(2203);
;// CONCATENATED MODULE: ./src/post.ts
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};




const fs = __nccwpck_require__(1943);
const path = __nccwpck_require__(6928);
const stream = __nccwpck_require__(2203);

// A list of files, relative to the `target` directory, that will never be deleted. For more
// information on the `target` folder's file structure, please see
Expand All @@ -27563,98 +27577,90 @@ const SKIPPED_FILES = [
"CACHEDIR.TAG",
".rustc_info.json",
];

/**
* Returns the path to the `target` directory of the current Cargo project.
*
* @param {string} manifestPath;
* @returns {string}
*/
async function locateTarget(manifestPath) {
// An array of strings, where each string is a line outputted by `cargo locate-project`. Note
// that `exec.exec()` doesn't guarantee that each written string will be a line (separated by
// `\n`), so this should be considered a hack and may break in the future.
const lines = [];

const outStream = new stream.Writable({
write(chunk, _encoding, callback) {
lines.push(chunk.toString("utf-8"));
callback();
}
});

// Locate the absolute path to `Cargo.toml`.
await exec.exec(
"cargo locate-project",
[
function locateTarget(manifestPath) {
return __awaiter(this, void 0, void 0, function* () {
// An array of strings, where each string is a line outputted by `cargo locate-project`. Note
// that `exec.exec()` doesn't guarantee that each written string will be a line (separated by
// `\n`), so this should be considered a hack and may break in the future.
const lines = [];
const outStream = new external_stream_.Writable({
write(chunk, _encoding, callback) {
lines.push(chunk.toString("utf-8"));
callback();
}
});
// Locate the absolute path to `Cargo.toml`.
yield exec.exec("cargo locate-project", [
`--manifest-path=${manifestPath}`,
"--workspace",
"--message-format=plain",
"--color=never",
],
{ outStream },
);

// Destroy the stream, just in case it wasn't done so already.
outStream.destroy();

core.debug(`Executing \`cargo locate-project\` resulted in the following \`stdout\`: ${lines}`);

// From the path to `Cargo.toml`, return the path to `target`.
return path.join(lines[1], "../", "target");
], { outStream });
// Destroy the stream, just in case it wasn't done so already.
outStream.destroy();
core.debug(`Executing \`cargo locate-project\` resulted in the following \`stdout\`: ${lines}`);
// From the path to `Cargo.toml`, return the path to `target`.
return external_path_.join(lines[1], "../", "target");
});
}

async function main() {
const stamp = Number(core.getState("timestamp"));
core.info(`Using timestamp: ${new Date(stamp)}.`);

const manifestPath = core.getInput("manifest-path", { required: true });
core.info(`Locating \`target\` folder from ${manifestPath}.`);

// Find `target` folder.
const targetPath = await locateTarget(manifestPath);
core.info(`Sweeping files from ${targetPath}.`);

// Iterate recursively over all files in `target`.
for (const file of await fs.readdir(targetPath, { recursive: true })) {
const filePath = path.join(targetPath, file);
const stat = await fs.stat(filePath);

// Skip over folders, since they cannot be deleted with `fs.rm()` and take up a minimal
// amount of space. Additionally, skip certain whitelisted files where it wouldn't make
// sense to delete them.
if (stat.isDirectory() || SKIPPED_FILES.includes(file)) {
core.debug(`Skipped ${filePath} because it is a directory or is whitelisted.`);
continue;
}

// If the file's last access time is older than the timestamp, delete it.
if (stat.atime.getTime() < stamp) {
if (core.isDebug()) {
core.info(`Deleting ${filePath} with \`atime\` of ${stat.atime}.`);
} else {
core.info(`Deleting ${filePath}.`);
}

await fs.rm(filePath);
} else {
core.debug(`Skipped ${filePath} because it was accessed after timestamp.`);
function main() {
return __awaiter(this, void 0, void 0, function* () {
const stamp = Number(core.getState("timestamp"));
core.info(`Using timestamp: ${new Date(stamp)}.`);
const manifestPath = core.getInput("manifest-path", { required: true });
core.info(`Locating \`target\` folder from ${manifestPath}.`);
// Find `target` folder.
const targetPath = yield locateTarget(manifestPath);
core.info(`Sweeping files from ${targetPath}.`);
const operationHandles = [];
// Iterate recursively over all files in `target`.
for (const file of yield promises_namespaceObject.readdir(targetPath, { recursive: true })) {
const filePath = external_path_.join(targetPath, file);
operationHandles.push(promises_namespaceObject.stat(filePath).then((stat) => __awaiter(this, void 0, void 0, function* () {
// Skip over folders, since they cannot be deleted with `fs.rm()` and take up a minimal
// amount of space. Additionally, skip certain whitelisted files where it wouldn't make
// sense to delete them.
if (stat.isDirectory() || SKIPPED_FILES.includes(file)) {
core.debug(`Skipped ${filePath} because it is a directory or is whitelisted.`);
return;
}
// If the file's last access time is older than the timestamp, delete it.
if (stat.atime.getTime() < stamp) {
if (core.isDebug()) {
core.info(`Deleting ${filePath} with \`atime\` of ${stat.atime}.`);
}
else {
core.info(`Deleting ${filePath}.`);
}
yield promises_namespaceObject.rm(filePath);
}
else {
core.debug(`Skipped ${filePath} because it was accessed after timestamp.`);
}
})));
}
}
yield Promise.all(operationHandles);
});
}

try {
const failed = core.getState("failed");

if (failed == "true") {
throw new Error("Main action failed, skipping post action.");
}

main();
} catch (err) {
}
catch (err) {
core.setFailed(`Action failed with error: ${err}`);
}

})();

module.exports = __webpack_exports__;
/******/ })()
;
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
},
"scripts": {
"build": "pnpm buildMain ; pnpm buildPost",
"buildMain": "ncc build src/main.js --out dist/main --license licenses.txt",
"buildPost": "ncc build src/post.js --out dist/post --license licenses.txt"
"buildMain": "ncc build src/main.ts --out dist/main --license licenses.txt",
"buildPost": "ncc build src/post.ts --out dist/post --license licenses.txt"
},
"dependencies": {
"@actions/core": "^1.11.1",
"@actions/exec": "^1.1.1"
},
"devDependencies": {
"@vercel/ncc": "^0.38.2"
"@types/node": "^22.8.4",
"@vercel/ncc": "^0.38.2",
"typescript": "^5.6.3"
}
}
}
Loading

0 comments on commit ed82140

Please sign in to comment.