Skip to content

feat: support fetch remote rc file #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ coverage
dist
bin
test/**.js.xml
.vscode
2 changes: 1 addition & 1 deletion lib/gren-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ init()
return;
}

const currentConfig = utils.getConfigFromFile(process.cwd());
const currentConfig = utils.getGrenConfig(process.cwd());
const fileContent = utils.writeConfigToFile(fileType, ObjectAssignDeep({}, currentConfig, data));

fs.writeFileSync(fileType, fileContent);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/Program.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import GitHubInfo from './GitHubInfo';
import program from 'commander';
import { getConfigFromFile } from './_utils.js';
import { getGrenConfig } from './_utils.js';

/** Class creating a Commander program, managing the options passed via bash and config file. */
class Program {
Expand All @@ -18,7 +18,7 @@ class Program {

this.options = Object.assign(
{},
getConfigFromFile(props.cwd, program.config),
getGrenConfig(props.cwd, program.config),
this._getOptionsFromObject(this.program, this.defaults)
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ const getQuestions = async() => {
value: 'merge',
name: 'Merge these settings over existing ones'
}],
when: () => Object.keys(utils.getConfigFromFile(process.cwd())).length > 0
when: () => Object.keys(utils.getGrenConfig(process.cwd())).length > 0
},
{
name: 'fileType',
Expand Down
73 changes: 72 additions & 1 deletion lib/src/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ora = require('ora');
const YAML = require('json2yaml');
const Path = require('path');
const { js_beautify: beautify } = require('js-beautify');
const requireFromUrl = require('require-from-url/sync');
require('require-yaml');

/**
Expand Down Expand Up @@ -176,6 +177,8 @@ function requireConfig(filepath) {
return false;
}

process.stdout.write(chalk.cyan(`Getting gren config from local file ${filepath}\n`));

if (getFileNameFromPath(filepath).match(/\./g).length === 1) {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
}
Expand All @@ -187,7 +190,7 @@ function requireConfig(filepath) {
* Get configuration from the one of the config files
*
* @since 0.6.0
* @public
* @private
*
* @param {string} path Path where to look for config files
* @return {Object} The configuration from the first found file or empty object
Expand Down Expand Up @@ -292,6 +295,71 @@ function cleanConfig(confirm, path = process.cwd()) {
});
}

/**
* judge whether to get config from remote uri
* @since 0.18.0
* @private
*
* @param {string} path
*
* @returns {string}
*/
function getRemoteUrl(path) {
const pkgPath = Path.join(path, 'package.json');
const pkgExist = fs.existsSync(pkgPath);
const { gren = '' } = pkgExist ? require(pkgPath) : {};
return gren;
}

/**
* get config from remote
* @since 0.18.0
* @private
*
* @param {string} path Path where to look for config files
* @return {Object} The configuration from the first found file or empty object
*/
function getConfigFromRemote(url) {
if (!url) return null;

process.stdout.write(chalk.cyan(`Fetching gren config from remote: ${url}\n`));

let config = null;
try {
config = requireFromUrl(url);
} catch (error) {
// console.error(error);
process.stdout.write(chalk.cyan(`Fetched remote config fail: ${url}\n`));
throw new Error(error);
}

process.stdout.write(chalk.cyan(`Fetched remote config succeed`));

return config;
}

/**
* combine getConfigFromRemote & getGrenConfig
* @since 0.18.0
* @public
*
* @param {string} path Path where to look for config files
* @return {Object} The configuration from the first found file or empty object
*/
function getGrenConfig(path, ...args) {
const remoteUrl = getRemoteUrl(path);
let config;
if (remoteUrl) {
config = getConfigFromRemote(remoteUrl);
}

if (!config) {
config = getConfigFromFile(path, ...args);
}

return config;
}

/**
* Just a noop function
*/
Expand All @@ -305,6 +373,9 @@ module.exports = {
dashToCamelCase,
formatDate,
getConfigFromFile,
getRemoteUrl,
getConfigFromRemote,
getGrenConfig,
getFileExtension,
getFileNameFromPath,
getFileTypes,
Expand Down
57 changes: 43 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy