1
0
Fork 0
mirror of https://github.com/dilllxd/gitfolio.git synced 2024-08-14 22:28:09 +00:00
gitfolio/utils.js
Rohit Gohri e62e4c05b3 Cli feature (#29)
* Move all cli options to single bin file, export fns from each file instead

* Make username/title required in cli itself

* fix username not being parsed, set desc separately

* read outDir from env var, defaults to dist

* set outDir env var in cli to cwd

* Move default configs to default dir

* handle blog.json not existing

* fix assets path

* make build function async

* update clean command

* added defaults to CLI
2019-05-23 10:30:49 +05:30

39 lines
1.1 KiB
JavaScript

const path = require('path');
const bluebird = require('bluebird');
const fs = bluebird.promisifyAll(require('fs'));
const outDir = path.resolve(process.env.OUT_DIR || './dist/');
const configPath = path.join(outDir, 'config.json');
const blogPath = path.join(outDir, 'blog.json');
const defaultConfigPath = path.resolve(`${__dirname}/default/config.json`);
const defaultBlogPath = path.resolve(`${__dirname}/default/blog.json`);
/**
* Tries to read file from out dir,
* if not present returns default file contents
*/
async function getFileWithDefaults(file, defaultFile) {
try {
await fs.accessAsync(file, fs.constants.F_OK);
} catch (err) {
const defaultData = await fs.readFileAsync(defaultFile);
return JSON.parse(defaultData);
}
const data = await fs.readFileAsync(file);
return JSON.parse(data);
}
async function getConfig() {
return getFileWithDefaults(configPath, defaultConfigPath);
}
async function getBlog() {
return getFileWithDefaults(blogPath, defaultBlogPath);
}
module.exports = {
outDir,
getConfig,
getBlog,
};