DenReg/cli/global.ts
User user e5829d9a4f
All checks were successful
continuous-integration/drone/push Build is passing
Add script running abilities
2021-04-28 11:47:52 +02:00

93 lines
2.2 KiB
TypeScript

import { Ini, FS, Colors } from "./deps.ts";
import setupCMD from "./commands/setup.ts";
export interface IMeta {
name: string;
version: string;
description?: string;
author?: string;
contributors?: string[];
deprecated?: boolean;
files: string[];
root?: string;
hooks?: {
prepublish?: string | string[];
postpublish?: string | string[];
};
scripts?: {
[key: string]: string | string[];
};
}
let verbose = false;
export function log(...args: any) {
if (verbose) console.log(...args);
}
let config: any = {};
let configLocation = "";
let configInitialized = false;
export function getConfig(name: string) {
if (!configInitialized) throw new Error("Not initialized!");
return config[name];
}
export async function setConfig(name: string, value: string) {
if (!configInitialized) throw new Error("Not initialized!");
config[name] = value;
const data = Ini.encode(config);
await Deno.writeFile(configLocation, new TextEncoder().encode(data), {
create: true,
});
}
const readJson = (name: string) => Deno.readTextFile(name).then(JSON.parse);
const writeJson = (name: string, value: any, spaces?: string) =>
Deno.writeTextFile(name, JSON.stringify(value, null, spaces));
export async function getMeta() {
log("Reading meta.json");
return (await readJson("meta.json")) as IMeta;
}
export async function setMeta(meta: IMeta): Promise<void> {
log("Saving meta.json");
return writeJson("meta.json", meta, " ");
}
let interactive = true;
export function isInteractive() {
return interactive;
}
export async function init(globalOptions: any) {
configLocation = globalOptions.config;
interactive = globalOptions.interactive;
verbose = globalOptions.verbose;
try {
const data = Deno.readFileSync(configLocation);
config = Ini.decode(new TextDecoder().decode(data));
} catch (err) {
log("Error loading config:");
log(err);
}
configInitialized = true;
if (!config.username || !config.registry || !config.password) {
if (!isInteractive()) {
console.error(
Colors.red("Run setup or set necessary value in " + configLocation)
);
} else {
await setupCMD();
}
}
}