@@ -4,6 +4,7 @@ const ora = require('ora');
44const YAML = require ( 'json2yaml' ) ;
55const Path = require ( 'path' ) ;
66const { js_beautify : beautify } = require ( 'js-beautify' ) ;
7+ const requireFromUrl = require ( 'require-from-url/sync' ) ;
78require ( 'require-yaml' ) ;
89
910/**
@@ -176,6 +177,8 @@ function requireConfig(filepath) {
176177 return false ;
177178 }
178179
180+ process . stdout . write ( chalk . cyan ( `Getting gren config from local file ${ filepath } \n` ) ) ;
181+
179182 if ( getFileNameFromPath ( filepath ) . match ( / \. / g) . length === 1 ) {
180183 return JSON . parse ( fs . readFileSync ( filepath , 'utf8' ) ) ;
181184 }
@@ -187,7 +190,7 @@ function requireConfig(filepath) {
187190 * Get configuration from the one of the config files
188191 *
189192 * @since 0.6.0
190- * @public
193+ * @private
191194 *
192195 * @param {string } path Path where to look for config files
193196 * @return {Object } The configuration from the first found file or empty object
@@ -292,6 +295,71 @@ function cleanConfig(confirm, path = process.cwd()) {
292295 } ) ;
293296}
294297
298+ /**
299+ * judge whether to get config from remote uri
300+ * @since 0.18.0
301+ * @private
302+ *
303+ * @param {string } path
304+ *
305+ * @returns {string }
306+ */
307+ function getRemoteUrl ( path ) {
308+ const pkgPath = Path . join ( path , 'package.json' ) ;
309+ const pkgExist = fs . existsSync ( pkgPath ) ;
310+ const { gren = '' } = pkgExist ? require ( pkgPath ) : { } ;
311+ return gren ;
312+ }
313+
314+ /**
315+ * get config from remote
316+ * @since 0.18.0
317+ * @private
318+ *
319+ * @param {string } path Path where to look for config files
320+ * @return {Object } The configuration from the first found file or empty object
321+ */
322+ function getConfigFromRemote ( url ) {
323+ if ( ! url ) return null ;
324+
325+ process . stdout . write ( chalk . cyan ( `Fetching gren config from remote: ${ url } \n` ) ) ;
326+
327+ let config = null ;
328+ try {
329+ config = requireFromUrl ( url ) ;
330+ } catch ( error ) {
331+ // console.error(error);
332+ process . stdout . write ( chalk . cyan ( `Fetched remote config fail: ${ url } \n` ) ) ;
333+ throw new Error ( error ) ;
334+ }
335+
336+ process . stdout . write ( chalk . cyan ( `Fetched remote config succeed` ) ) ;
337+
338+ return config ;
339+ }
340+
341+ /**
342+ * combine getConfigFromRemote & getGrenConfig
343+ * @since 0.18.0
344+ * @public
345+ *
346+ * @param {string } path Path where to look for config files
347+ * @return {Object } The configuration from the first found file or empty object
348+ */
349+ function getGrenConfig ( path , ...args ) {
350+ const remoteUrl = getRemoteUrl ( path ) ;
351+ let config ;
352+ if ( remoteUrl ) {
353+ config = getConfigFromRemote ( remoteUrl ) ;
354+ }
355+
356+ if ( ! config ) {
357+ config = getConfigFromFile ( path , ...args ) ;
358+ }
359+
360+ return config ;
361+ }
362+
295363/**
296364 * Just a noop function
297365 */
@@ -305,6 +373,9 @@ module.exports = {
305373 dashToCamelCase,
306374 formatDate,
307375 getConfigFromFile,
376+ getRemoteUrl,
377+ getConfigFromRemote,
378+ getGrenConfig,
308379 getFileExtension,
309380 getFileNameFromPath,
310381 getFileTypes,
0 commit comments