Skip to content

Commit

Permalink
Merge pull request #22 from guardian/nicl/sources-empty
Browse files Browse the repository at this point in the history
Error if contentDirectories invalid
  • Loading branch information
nicl authored Dec 1, 2022
2 parents d8a0117 + 1cfdd25 commit 49c3b20
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
40 changes: 29 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38295,22 +38295,40 @@ var getProjectName = ({ stacks }) => {
);
}
};
var isSources = (obj) => {
if (typeof obj === "object") {
return Object.values(obj).every(
(source) => Array.isArray(source)
);
}
return false;
};
var getDeployments = () => {
const input = getInput2("contentDirectories", { required: true });
const contentDirectoriesInput = input ? load(input) : {};
const deployments = Object.entries(contentDirectoriesInput).map(
([name, sources]) => ({ name, sources })
);
const totalDeployments = deployments.reduce(
(acc, { sources }) => acc + sources.length,
0
);
if (totalDeployments === 0) {
if (!isSources(contentDirectoriesInput)) {
throw new Error(
"Not configured with any deployment sources, no files will be uploaded to Riff-Raff."
`Invalid contentDirectories. Each value must be a list of sources, but got: ${input ?? ""}`
);
}
return deployments;
if (isSources(contentDirectoriesInput)) {
const deployments = Object.entries(
contentDirectoriesInput
).map(([name, sources]) => ({ name, sources }));
const totalDeployments = deployments.reduce(
(acc, { sources }) => acc + sources.length,
0
);
if (totalDeployments === 0) {
throw new Error(
"Not configured with any deployment sources, no files will be uploaded to Riff-Raff."
);
}
return deployments;
}
throw new Error(
`Invalid contentDirectories. Each value must be a list of sources, but got: ${input ?? ""}`
);
};
var getRiffRaffYaml = () => {
const configInput = getInput2("config");
Expand Down Expand Up @@ -38438,7 +38456,7 @@ var main = async () => {
["Project name", projectName],
["Build number", buildNumber]
]).write();
core4.info("writting rr yaml...");
core4.info("writing rr yaml...");
write(`${stagingDir}/riff-raff.yaml`, dump(riffRaffYaml));
deployments.forEach((deployment) => {
cp(deployment.sources, `${stagingDir}/${deployment.name}`);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"test": "jest",
"tsc": "tsc --noEmit",
"lint": "eslint src --ext .ts --no-error-on-unmatched-pattern",
"format": "prettier --check \"src/**/*.ts\""
"format": "prettier --check \"src/**/*.ts\"",
"format-fix": "prettier --write \"src/**/*.ts\""
},
"repository": {
"type": "git",
Expand Down
54 changes: 40 additions & 14 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,55 @@ const getProjectName = ({ stacks }: RiffraffYaml): string => {
}
};

type Sources = Record<string, string[]>;

const isSources = (obj: unknown): obj is Sources => {
if (typeof obj === 'object') {
return Object.values(obj as object).every((source) =>
Array.isArray(source),
);
}

return false;
};

const getDeployments = (): Deployment[] => {
const input = getInput('contentDirectories', { required: true });

const contentDirectoriesInput = input
? (yaml.load(input) as Record<string, string[]>)
: {};
const contentDirectoriesInput = input ? yaml.load(input) : {};

const deployments: Deployment[] = Object.entries(contentDirectoriesInput).map(
([name, sources]) => ({ name, sources }),
);
if (!isSources(contentDirectoriesInput)) {
throw new Error(
`Invalid contentDirectories. Each value must be a list of sources, but got: ${
input ?? ''
}`,
);
}

const totalDeployments: number = deployments.reduce(
(acc, { sources }) => acc + sources.length,
0,
);
if (isSources(contentDirectoriesInput)) {
const deployments: Deployment[] = Object.entries(
contentDirectoriesInput,
).map(([name, sources]) => ({ name, sources }));

if (totalDeployments === 0) {
throw new Error(
'Not configured with any deployment sources, no files will be uploaded to Riff-Raff.',
const totalDeployments: number = deployments.reduce(
(acc, { sources }) => acc + sources.length,
0,
);

if (totalDeployments === 0) {
throw new Error(
'Not configured with any deployment sources, no files will be uploaded to Riff-Raff.',
);
}

return deployments;
}

return deployments;
throw new Error(
`Invalid contentDirectories. Each value must be a list of sources, but got: ${
input ?? ''
}`,
);
};

const getRiffRaffYaml = (): RiffraffYaml => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const main = async (): Promise<void> => {
])
.write();

core.info('writting rr yaml...');
core.info('writing rr yaml...');
write(`${stagingDir}/riff-raff.yaml`, yaml.dump(riffRaffYaml));

deployments.forEach((deployment: Deployment) => {
Expand Down

0 comments on commit 49c3b20

Please sign in to comment.