nav-new-publication-cli
Version:
Add new publication to the navigator web project
89 lines (78 loc) • 1.82 kB
text/typescript
import { readJson, writeJson } from 'fs-extra'
// import { resolve } from 'path';
import { logError, logSuccess } from './utils';
interface Msgs {
error: string | null,
success: string | null,
};
interface Publication {
title: string,
stackAlias?: string,
export: {
vr: number,
functional: number,
}
domain: string,
}
interface PublicationList {
[key: string]: Publication[];
nationals: Publication[];
ronionals: Publication[];
regionals: Publication[];
visionals: Publication[];
}
export interface PublicationData {
[key: string]: string | number | undefined | {
dev: string,
qa: string,
preprod: string,
prod: string,
} | {
min: number,
max: number,
};
platform: string;
title: string;
stackAlias?: string;
vr: number;
func: number;
domain: string;
arn: {
dev: string,
qa: string,
preprod: string,
prod: string,
};
autoScaling: {
min: number,
max: number,
};
};
export const loadJson = async (path: string, msgs: Msgs) => {
try {
const jsonObj = await readJson(path);
if(msgs.success) logSuccess(msgs.success);
return jsonObj;
} catch (err) {
if(msgs.error) logError(msgs.error, err);
return null;
}
}
export const writePublicationJson = async (path: string, publications:PublicationList, data: PublicationData, msgs: Msgs) => {
let newPublication: Publication = {
title: data.title,
export: {
vr: data.vr,
functional: data.func,
},
domain: data.domain,
};
if(data.stackAlias) newPublication.stackAlias = data.stackAlias;
publications[data.platform].push(newPublication);
try {
await writeJson(path, publications);
if(msgs.success) logSuccess(msgs.success);
} catch (err) {
if(msgs.error) logError(msgs.error, err);
}
}