chore: make config load from state explicit

Make the loading of config from state explicit by splitting into a
dedicated static method.
This commit is contained in:
Steven Hartland 2023-05-17 21:42:24 +01:00
parent 68377a4bda
commit c7502cd809
4 changed files with 62 additions and 27 deletions

27
dist/restore/index.js vendored
View File

@ -60056,14 +60056,6 @@ class CacheConfig {
*/
static async new() {
const self = new CacheConfig();
const source = lib_core.getState(STATE_CONFIG);
if (source !== "") {
// Post action, use what we calculated in the main action ensuring consistency.
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w) => new Workspace(w.root, w.target));
return self;
}
// Construct key prefix:
// This uses either the `shared-key` input,
// or the `key` input combined with the `job` key.
@ -60157,6 +60149,25 @@ class CacheConfig {
self.cargoBins = Array.from(bins.values());
return self;
}
/**
* Reads and returns the cache config from the action `state`.
*
* @throws {Error} if the state is not present.
* @returns {CacheConfig} the configuration.
* @see {@link CacheConfig#saveState}
* @see {@link CacheConfig#new}
*/
static fromState() {
const source = lib_core.getState(STATE_CONFIG);
if (!source) {
throw new Error("Cache configuration not found in state");
}
const self = new CacheConfig();
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w) => new Workspace(w.root, w.target));
return self;
}
/**
* Prints the configuration to the action log.
*/

29
dist/save/index.js vendored
View File

@ -60056,14 +60056,6 @@ class CacheConfig {
*/
static async new() {
const self = new CacheConfig();
const source = core.getState(STATE_CONFIG);
if (source !== "") {
// Post action, use what we calculated in the main action ensuring consistency.
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w) => new Workspace(w.root, w.target));
return self;
}
// Construct key prefix:
// This uses either the `shared-key` input,
// or the `key` input combined with the `job` key.
@ -60157,6 +60149,25 @@ class CacheConfig {
self.cargoBins = Array.from(bins.values());
return self;
}
/**
* Reads and returns the cache config from the action `state`.
*
* @throws {Error} if the state is not present.
* @returns {CacheConfig} the configuration.
* @see {@link CacheConfig#saveState}
* @see {@link CacheConfig#new}
*/
static fromState() {
const source = core.getState(STATE_CONFIG);
if (!source) {
throw new Error("Cache configuration not found in state");
}
const self = new CacheConfig();
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w) => new Workspace(w.root, w.target));
return self;
}
/**
* Prints the configuration to the action log.
*/
@ -60470,7 +60481,7 @@ async function run() {
core.info(`Cache up-to-date.`);
return;
}
const config = await CacheConfig["new"]();
const config = CacheConfig.fromState();
config.printInfo();
core.info("");
// TODO: remove this once https://github.com/actions/toolkit/pull/553 lands

View File

@ -46,15 +46,6 @@ export class CacheConfig {
*/
static async new(): Promise<CacheConfig> {
const self = new CacheConfig();
const source = core.getState(STATE_CONFIG);
if (source !== "") {
// Post action, use what we calculated in the main action ensuring consistency.
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w: any) => new Workspace(w.root, w.target));
return self;
}
// Construct key prefix:
// This uses either the `shared-key` input,
@ -177,6 +168,28 @@ export class CacheConfig {
return self;
}
/**
* Reads and returns the cache config from the action `state`.
*
* @throws {Error} if the state is not present.
* @returns {CacheConfig} the configuration.
* @see {@link CacheConfig#saveState}
* @see {@link CacheConfig#new}
*/
static fromState(): CacheConfig {
const source = core.getState(STATE_CONFIG);
if (!source) {
throw new Error("Cache configuration not found in state");
}
const self = new CacheConfig();
Object.assign(self, JSON.parse(source));
self.workspaces = self.workspaces
.map((w: any) => new Workspace(w.root, w.target));
return self;
}
/**
* Prints the configuration to the action log.
*/

View File

@ -25,7 +25,7 @@ async function run() {
return;
}
const config = await CacheConfig.new();
const config = CacheConfig.fromState();
config.printInfo();
core.info("");