From e993ddc71b78819bf5b296728c272fd4d70fb8a0 Mon Sep 17 00:00:00 2001 From: Salem Ghoweri Date: Sun, 31 Mar 2019 08:47:09 -0400 Subject: [PATCH 1/3] feat: add preliminary support for .js configs --- packages/cli/bin/resolve-config.js | 2 +- packages/core/package.json | 1 + packages/core/patternlab-config.js | 122 ++++++++++++++++ packages/core/patternlab-config.json | 95 ------------ packages/core/src/index.js | 2 +- packages/core/src/lib/loaduikits.js | 4 +- packages/core/src/lib/patternlab.js | 3 +- packages/edition-twig/package.json | 7 +- .../edition-twig/patternlab-config.default.js | 122 ++++++++++++++++ packages/edition-twig/patternlab-config.js | 122 ++++++++++++++++ packages/edition-twig/patternlab-config.json | 137 ------------------ 11 files changed, 376 insertions(+), 241 deletions(-) create mode 100644 packages/core/patternlab-config.js delete mode 100644 packages/core/patternlab-config.json create mode 100644 packages/edition-twig/patternlab-config.default.js create mode 100644 packages/edition-twig/patternlab-config.js delete mode 100644 packages/edition-twig/patternlab-config.json diff --git a/packages/cli/bin/resolve-config.js b/packages/cli/bin/resolve-config.js index f34c5e083..e68a1afaa 100644 --- a/packages/cli/bin/resolve-config.js +++ b/packages/cli/bin/resolve-config.js @@ -31,7 +31,7 @@ function resolveConfig(configPath) { */ try { const absoluteConfigPath = path.resolve(configPath); // 1 - return yield readJsonAsync(absoluteConfigPath); // 2 + return yield require(absoluteConfigPath); } catch (err) { error( 'resolveConfig: Got an error during parsing your Pattern Lab config. Please make sure your config file exists.' diff --git a/packages/core/package.json b/packages/core/package.json index 38ea422ac..5819bbd02 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -21,6 +21,7 @@ "update-notifier": "2.2.0" }, "devDependencies": { + "resolve-pkg": "^1.0.0", "eslint": "4.18.2", "eslint-config-prettier": "2.9.0", "eslint-plugin-prettier": "2.6.0", diff --git a/packages/core/patternlab-config.js b/packages/core/patternlab-config.js new file mode 100644 index 000000000..a82d5c5b2 --- /dev/null +++ b/packages/core/patternlab-config.js @@ -0,0 +1,122 @@ +const resolvePkg = require('resolve-pkg'); + +module.exports = { + engines: { + twig: { + namespaces: [ + { + id: 'uikit', + recursive: true, + paths: [resolvePkg(`@pattern-lab/uikit-workshop/views-twig`)], + }, + { + id: 'atoms', + recursive: true, + paths: ['source/_patterns/00-atoms'], + }, + { + id: 'molecules', + recursive: true, + paths: ['source/_patterns/01-molecules'], + }, + ], + alterTwigEnv: [ + { + file: 'alter-twig.php', + functions: ['addCustomExtension'], + }, + ], + }, + }, + cacheBust: true, + cleanPublic: true, + defaultPattern: 'all', + defaultShowPatternInfo: false, + ishControlsHide: { + s: false, + m: false, + l: false, + full: false, + random: false, + disco: false, + hay: true, + mqs: false, + find: false, + 'views-all': false, + 'views-annotations': false, + 'views-code': false, + 'views-new': false, + 'tools-all': false, + 'tools-docs': false, + }, + ishViewportRange: { + s: [240, 500], + m: [500, 800], + l: [800, 2600], + }, + logLevel: 'info', + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only', + }, + paths: { + source: { + root: './source/', + patterns: './source/_patterns/', + data: './source/_data/', + meta: './source/_meta/', + annotations: './source/_annotations/', + styleguide: 'dist/', + patternlabFiles: { + 'general-header': + '../uikit-workshop/views/partials/general-header.mustache', + 'general-footer': + '../uikit-workshop/views/partials/general-footer.mustache', + patternSection: + '../uikit-workshop/views/partials/patternSection.mustache', + patternSectionSubtype: + '../uikit-workshop/views/partials/patternSectionSubtype.mustache', + viewall: '../uikit-workshop/views/viewall.mustache', + }, + js: './source/js', + images: './source/images', + fonts: './source/fonts', + css: './source/css', + }, + public: { + root: 'public/', + patterns: 'public/patterns/', + data: 'public/styleguide/data/', + annotations: 'public/annotations/', + styleguide: 'public/styleguide/', + js: 'public/js', + images: 'public/images', + fonts: 'public/fonts', + css: 'public/css', + }, + }, + patternExtension: 'twig', + patternStateCascade: ['inprogress', 'inreview', 'complete'], + patternExportDirectory: './pattern_exports/', + patternExportPatternPartials: [], + serverOptions: { + wait: 1000, + }, + starterkitSubDir: 'dist', + styleGuideExcludes: [], + theme: { + color: 'light', + density: 'compact', + layout: 'horizontal', + }, + uikits: [ + { + name: 'uikit-workshop', + outputDir: '', + enabled: true, + excludedPatternStates: [], + excludedTags: [], + }, + ], +}; diff --git a/packages/core/patternlab-config.json b/packages/core/patternlab-config.json deleted file mode 100644 index 80669ea5f..000000000 --- a/packages/core/patternlab-config.json +++ /dev/null @@ -1,95 +0,0 @@ -{ - "cacheBust": true, - "cleanPublic" : true, - "defaultPattern": "all", - "defaultShowPatternInfo": false, - "ishControlsHide": { - "s": false, - "m": false, - "l": false, - "full": false, - "random": false, - "disco": false, - "hay": true, - "mqs": false, - "find": false, - "views-all": false, - "views-annotations": false, - "views-code": false, - "views-new": false, - "tools-all": false, - "tools-docs": false - }, - "ishViewportRange": { - "s": [240, 500], - "m": [500, 800], - "l": [800, 2600] - }, - "logLevel": "info", - "outputFileSuffixes": { - "rendered": ".rendered", - "rawTemplate": "", - "markupOnly": ".markup-only" - }, - "paths" : { - "source": { - "root": "./source/", - "patterns": "./source/_patterns/", - "data": "./source/_data/", - "meta": "./source/_meta/", - "annotations": "./source/_annotations/", - "styleguide": "dist/", - "patternlabFiles": { - "general-header": - "views/partials/general-header.mustache", - "general-footer": - "views/partials/general-footer.mustache", - "patternSection": - "views/partials/patternSection.mustache", - "patternSectionSubtype": - "views/partials/patternSectionSubtype.mustache", - "viewall": - "views/viewall.mustache" - }, - "js": "./source/js", - "images": "./source/images", - "fonts": "./source/fonts", - "css": "./source/css" - }, - "public": { - "root": "public/", - "patterns": "public/patterns/", - "data": "public/styleguide/data/", - "annotations": "public/annotations/", - "styleguide": "public/styleguide/", - "js": "public/js", - "images": "public/images", - "fonts": "public/fonts", - "css": "public/css" - } - }, - "patternExtension": "mustache", - "patternStateCascade": ["inprogress", "inreview", "complete"], - "patternExportDirectory": "./pattern_exports/", - "patternExportPatternPartials": [], - "serverOptions": { - "wait": 1000 - }, - "starterkitSubDir": "dist", - "styleGuideExcludes": [ - ], - "theme": { - "color": "dark", - "density": "compact", - "layout": "horizontal" - }, - "uikits": [ - { - "name": "uikit-workshop", - "outputDir": "", - "enabled": true, - "excludedPatternStates": [], - "excludedTags": [] - } - ] -} diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 653a9f731..615ff5c29 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -19,7 +19,7 @@ const events = require('./lib/events'); const pe = require('./lib/pattern_exporter'); const pm = require('./lib/plugin_manager'); -const defaultConfig = require('../patternlab-config.json'); +const defaultConfig = require('../patternlab-config.js'); let buildPatterns = require('./lib/buildPatterns'); // eslint-disable-line let logger = require('./lib/log'); // eslint-disable-line diff --git a/packages/core/src/lib/loaduikits.js b/packages/core/src/lib/loaduikits.js index 1f97564a2..7ac215118 100644 --- a/packages/core/src/lib/loaduikits.js +++ b/packages/core/src/lib/loaduikits.js @@ -37,7 +37,7 @@ const readModuleFile = (kit, subPath) => { /** * Loads uikits, connecting configuration and installed modules * [1] Looks in node_modules for uikits. - * [2] Only continue if uikit is enabled in patternlab-config.json + * [2] Only continue if uikit is enabled in patternlab-config.js * [3] Reads files from uikit that apply to every template * @param {object} patternlab */ @@ -55,7 +55,7 @@ module.exports = patternlab => { logger.warning( `Could not find uikit with name uikit-${ kit.name - } defined within patternlab-config.json, or it is not enabled.` + } defined within a patternlab-config.js file, or it is not enabled.` ); return; } diff --git a/packages/core/src/lib/patternlab.js b/packages/core/src/lib/patternlab.js index 2b8cc3d43..50dbda15f 100644 --- a/packages/core/src/lib/patternlab.js +++ b/packages/core/src/lib/patternlab.js @@ -36,8 +36,7 @@ module.exports = class PatternLab { constructor(config) { // Either use the config we were passed, or load one up from the config file ourselves this.config = - config || - fs.readJSONSync(path.resolve(__dirname, '../../patternlab-config.json')); + config || require(path.resolve(__dirname, '../../patternlab-config.js')); //register our log events this.registerLogger(config.logLevel); diff --git a/packages/edition-twig/package.json b/packages/edition-twig/package.json index 43c23c1f0..010c6a96c 100644 --- a/packages/edition-twig/package.json +++ b/packages/edition-twig/package.json @@ -13,14 +13,15 @@ ], "main": "patternlab-config.json", "scripts": { - "build": "patternlab build --config ./patternlab-config.json", + "build": "patternlab build --config ./patternlab-config.js", "help": "patternlab --help", - "install": "patternlab install --config ./patternlab-config.json", - "serve": "patternlab serve --config ./patternlab-config.json", + "install": "patternlab install --config ./patternlab-config.js", + "serve": "patternlab serve --config ./patternlab-config.js", "start": "npm run serve", "version": "patternlab --version" }, "dependencies": { + "resolve-pkg": "^1.0.0", "@pattern-lab/cli": "^0.0.2-alpha.0", "@pattern-lab/core": "^3.0.1-alpha.0", "@pattern-lab/engine-twig-php": "^3.0.0", diff --git a/packages/edition-twig/patternlab-config.default.js b/packages/edition-twig/patternlab-config.default.js new file mode 100644 index 000000000..a82d5c5b2 --- /dev/null +++ b/packages/edition-twig/patternlab-config.default.js @@ -0,0 +1,122 @@ +const resolvePkg = require('resolve-pkg'); + +module.exports = { + engines: { + twig: { + namespaces: [ + { + id: 'uikit', + recursive: true, + paths: [resolvePkg(`@pattern-lab/uikit-workshop/views-twig`)], + }, + { + id: 'atoms', + recursive: true, + paths: ['source/_patterns/00-atoms'], + }, + { + id: 'molecules', + recursive: true, + paths: ['source/_patterns/01-molecules'], + }, + ], + alterTwigEnv: [ + { + file: 'alter-twig.php', + functions: ['addCustomExtension'], + }, + ], + }, + }, + cacheBust: true, + cleanPublic: true, + defaultPattern: 'all', + defaultShowPatternInfo: false, + ishControlsHide: { + s: false, + m: false, + l: false, + full: false, + random: false, + disco: false, + hay: true, + mqs: false, + find: false, + 'views-all': false, + 'views-annotations': false, + 'views-code': false, + 'views-new': false, + 'tools-all': false, + 'tools-docs': false, + }, + ishViewportRange: { + s: [240, 500], + m: [500, 800], + l: [800, 2600], + }, + logLevel: 'info', + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only', + }, + paths: { + source: { + root: './source/', + patterns: './source/_patterns/', + data: './source/_data/', + meta: './source/_meta/', + annotations: './source/_annotations/', + styleguide: 'dist/', + patternlabFiles: { + 'general-header': + '../uikit-workshop/views/partials/general-header.mustache', + 'general-footer': + '../uikit-workshop/views/partials/general-footer.mustache', + patternSection: + '../uikit-workshop/views/partials/patternSection.mustache', + patternSectionSubtype: + '../uikit-workshop/views/partials/patternSectionSubtype.mustache', + viewall: '../uikit-workshop/views/viewall.mustache', + }, + js: './source/js', + images: './source/images', + fonts: './source/fonts', + css: './source/css', + }, + public: { + root: 'public/', + patterns: 'public/patterns/', + data: 'public/styleguide/data/', + annotations: 'public/annotations/', + styleguide: 'public/styleguide/', + js: 'public/js', + images: 'public/images', + fonts: 'public/fonts', + css: 'public/css', + }, + }, + patternExtension: 'twig', + patternStateCascade: ['inprogress', 'inreview', 'complete'], + patternExportDirectory: './pattern_exports/', + patternExportPatternPartials: [], + serverOptions: { + wait: 1000, + }, + starterkitSubDir: 'dist', + styleGuideExcludes: [], + theme: { + color: 'light', + density: 'compact', + layout: 'horizontal', + }, + uikits: [ + { + name: 'uikit-workshop', + outputDir: '', + enabled: true, + excludedPatternStates: [], + excludedTags: [], + }, + ], +}; diff --git a/packages/edition-twig/patternlab-config.js b/packages/edition-twig/patternlab-config.js new file mode 100644 index 000000000..a82d5c5b2 --- /dev/null +++ b/packages/edition-twig/patternlab-config.js @@ -0,0 +1,122 @@ +const resolvePkg = require('resolve-pkg'); + +module.exports = { + engines: { + twig: { + namespaces: [ + { + id: 'uikit', + recursive: true, + paths: [resolvePkg(`@pattern-lab/uikit-workshop/views-twig`)], + }, + { + id: 'atoms', + recursive: true, + paths: ['source/_patterns/00-atoms'], + }, + { + id: 'molecules', + recursive: true, + paths: ['source/_patterns/01-molecules'], + }, + ], + alterTwigEnv: [ + { + file: 'alter-twig.php', + functions: ['addCustomExtension'], + }, + ], + }, + }, + cacheBust: true, + cleanPublic: true, + defaultPattern: 'all', + defaultShowPatternInfo: false, + ishControlsHide: { + s: false, + m: false, + l: false, + full: false, + random: false, + disco: false, + hay: true, + mqs: false, + find: false, + 'views-all': false, + 'views-annotations': false, + 'views-code': false, + 'views-new': false, + 'tools-all': false, + 'tools-docs': false, + }, + ishViewportRange: { + s: [240, 500], + m: [500, 800], + l: [800, 2600], + }, + logLevel: 'info', + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only', + }, + paths: { + source: { + root: './source/', + patterns: './source/_patterns/', + data: './source/_data/', + meta: './source/_meta/', + annotations: './source/_annotations/', + styleguide: 'dist/', + patternlabFiles: { + 'general-header': + '../uikit-workshop/views/partials/general-header.mustache', + 'general-footer': + '../uikit-workshop/views/partials/general-footer.mustache', + patternSection: + '../uikit-workshop/views/partials/patternSection.mustache', + patternSectionSubtype: + '../uikit-workshop/views/partials/patternSectionSubtype.mustache', + viewall: '../uikit-workshop/views/viewall.mustache', + }, + js: './source/js', + images: './source/images', + fonts: './source/fonts', + css: './source/css', + }, + public: { + root: 'public/', + patterns: 'public/patterns/', + data: 'public/styleguide/data/', + annotations: 'public/annotations/', + styleguide: 'public/styleguide/', + js: 'public/js', + images: 'public/images', + fonts: 'public/fonts', + css: 'public/css', + }, + }, + patternExtension: 'twig', + patternStateCascade: ['inprogress', 'inreview', 'complete'], + patternExportDirectory: './pattern_exports/', + patternExportPatternPartials: [], + serverOptions: { + wait: 1000, + }, + starterkitSubDir: 'dist', + styleGuideExcludes: [], + theme: { + color: 'light', + density: 'compact', + layout: 'horizontal', + }, + uikits: [ + { + name: 'uikit-workshop', + outputDir: '', + enabled: true, + excludedPatternStates: [], + excludedTags: [], + }, + ], +}; diff --git a/packages/edition-twig/patternlab-config.json b/packages/edition-twig/patternlab-config.json deleted file mode 100644 index 8612b1633..000000000 --- a/packages/edition-twig/patternlab-config.json +++ /dev/null @@ -1,137 +0,0 @@ -{ - "engines": { - "twig": { - "namespaces": [ - { - "id": "uikit", - "recursive": true, - "paths": [ - "../uikit-workshop/views-twig" - ] - }, - { - "id": "atoms", - "recursive": true, - "paths": [ - "source/_patterns/00-atoms" - ] - }, - { - "id": "molecules", - "recursive": true, - "paths": [ - "source/_patterns/01-molecules" - ] - } - ], - "alterTwigEnv": [ - { - "file": "alter-twig.php", - "functions": [ - "addCustomExtension" - ] - } - ] - } - }, - "cacheBust": true, - "cleanPublic": true, - "defaultPattern": "all", - "defaultShowPatternInfo": false, - "ishControlsHide": { - "s": false, - "m": false, - "l": false, - "full": false, - "random": false, - "disco": false, - "hay": true, - "mqs": false, - "find": false, - "views-all": false, - "views-annotations": false, - "views-code": false, - "views-new": false, - "tools-all": false, - "tools-docs": false - }, - "ishViewportRange": { - "s": [ - 240, - 500 - ], - "m": [ - 500, - 800 - ], - "l": [ - 800, - 2600 - ] - }, - "logLevel": "info", - "outputFileSuffixes": { - "rendered": ".rendered", - "rawTemplate": "", - "markupOnly": ".markup-only" - }, - "paths": { - "source": { - "root": "./source/", - "patterns": "./source/_patterns/", - "data": "./source/_data/", - "meta": "./source/_meta/", - "annotations": "./source/_annotations/", - "styleguide": "dist/", - "patternlabFiles": { - "general-header": "../uikit-workshop/views/partials/general-header.mustache", - "general-footer": "../uikit-workshop/views/partials/general-footer.mustache", - "patternSection": "../uikit-workshop/views/partials/patternSection.mustache", - "patternSectionSubtype": "../uikit-workshop/views/partials/patternSectionSubtype.mustache", - "viewall": "../uikit-workshop/views/viewall.mustache" - }, - "js": "./source/js", - "images": "./source/images", - "fonts": "./source/fonts", - "css": "./source/css" - }, - "public": { - "root": "public/", - "patterns": "public/patterns/", - "data": "public/styleguide/data/", - "annotations": "public/annotations/", - "styleguide": "public/styleguide/", - "js": "public/js", - "images": "public/images", - "fonts": "public/fonts", - "css": "public/css" - } - }, - "patternExtension": "twig", - "patternStateCascade": [ - "inprogress", - "inreview", - "complete" - ], - "patternExportDirectory": "./pattern_exports/", - "patternExportPatternPartials": [], - "serverOptions": { - "wait": 1000 - }, - "starterkitSubDir": "dist", - "styleGuideExcludes": [], - "theme": { - "color": "light", - "density": "compact", - "layout": "horizontal" - }, - "uikits": [ - { - "name": "uikit-workshop", - "outputDir": "", - "enabled": true, - "excludedPatternStates": [], - "excludedTags": [] - } - ] -} From f1b6f2ade6ea88ae485415179291a1576add28ba Mon Sep 17 00:00:00 2001 From: Salem Ghoweri Date: Thu, 11 Apr 2019 14:52:48 -0700 Subject: [PATCH 2/3] fix: update eslint config to allow async functions, etc --- .eslintrc.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintrc.json b/.eslintrc.json index d5dd0f115..777cc3c69 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -5,7 +5,7 @@ "es6": true }, "parserOptions": { - "ecmaVersion": 6, + "ecmaVersion": 2017, "sourceType": "module" }, "globals": {}, From 9959b98cf21233be06d4ee49394ced006e73d7c4 Mon Sep 17 00:00:00 2001 From: Salem Ghoweri Date: Thu, 11 Apr 2019 14:54:38 -0700 Subject: [PATCH 3/3] refactor: auto-resolve config paths + support .js and .json formats --- packages/cli/bin/resolve-config.js | 11 +++- packages/core/package.json | 1 + packages/core/src/lib/patternlab.js | 21 ++++--- .../core/src/lib/utils/find-config-file.js | 57 +++++++++++++++++++ packages/edition-twig/package.json | 6 +- 5 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 packages/core/src/lib/utils/find-config-file.js diff --git a/packages/cli/bin/resolve-config.js b/packages/cli/bin/resolve-config.js index e68a1afaa..f29918baa 100644 --- a/packages/cli/bin/resolve-config.js +++ b/packages/cli/bin/resolve-config.js @@ -4,6 +4,9 @@ const path = require('path'); const error = require('./utils').error; const readJsonAsync = require('./utils').readJsonAsync; const wrapAsync = require('./utils').wrapAsync; +const { + findPatternLabConfig, +} = require('@pattern-lab/core/src/lib/utils/find-config-file'); /** * @func resolveConfig @@ -30,8 +33,12 @@ function resolveConfig(configPath) { * 2. Read the config file */ try { - const absoluteConfigPath = path.resolve(configPath); // 1 - return yield require(absoluteConfigPath); + if (configPath) { + const absoluteConfigPath = path.resolve(configPath); // 1 + return yield findPatternLabConfig(absoluteConfigPath); + } else { + return yield findPatternLabConfig(); + } } catch (err) { error( 'resolveConfig: Got an error during parsing your Pattern Lab config. Please make sure your config file exists.' diff --git a/packages/core/package.json b/packages/core/package.json index 5819bbd02..e472809ec 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -4,6 +4,7 @@ "version": "3.0.1-alpha.0", "main": "./src/index.js", "dependencies": { + "cosmiconfig": "^5.2.0", "@pattern-lab/engine-mustache": "^2.0.1-alpha.0", "@pattern-lab/live-server": "^1.3.3-beta.1", "chalk": "1.1.3", diff --git a/packages/core/src/lib/patternlab.js b/packages/core/src/lib/patternlab.js index 50dbda15f..c9262bec2 100644 --- a/packages/core/src/lib/patternlab.js +++ b/packages/core/src/lib/patternlab.js @@ -4,7 +4,6 @@ const dive = require('dive'); const _ = require('lodash'); const path = require('path'); const cleanHtml = require('js-beautify').html; - const inherits = require('util').inherits; const pm = require('./plugin_manager'); const plugin_manager = new pm(); @@ -16,7 +15,7 @@ const loaduikits = require('./loaduikits'); const logger = require('./log'); const processIterative = require('./processIterative'); const processRecursive = require('./processRecursive'); - +const { findPatternLabConfig } = require('./utils/find-config-file'); const loadPattern = require('./loadPattern'); const sm = require('./starterkit_manager'); @@ -32,20 +31,22 @@ function PatternLabEventEmitter() { } inherits(PatternLabEventEmitter, EventEmitter); -module.exports = class PatternLab { +class PatternLab { constructor(config) { - // Either use the config we were passed, or load one up from the config file ourselves - this.config = - config || require(path.resolve(__dirname, '../../patternlab-config.js')); + // Use the already-resolved config passed in OR auto load one up from the config + const getConfigAsync = async () => { + this.config = config || (await Promise.resolve(findPatternLabConfig())); + }; + getConfigAsync(); //register our log events - this.registerLogger(config.logLevel); + this.registerLogger(this.config.logLevel); logger.info(`Pattern Lab Node v${packageInfo.version}`); // Load up engines please this.engines = patternEngines; - this.engines.loadAllEngines(config); + this.engines.loadAllEngines(this.config); // // INITIALIZE EMPTY GLOBAL DATA STRUCTURES @@ -361,4 +362,6 @@ module.exports = class PatternLab { }); return promiseAllPatternFiles; } -}; +} + +module.exports = PatternLab; diff --git a/packages/core/src/lib/utils/find-config-file.js b/packages/core/src/lib/utils/find-config-file.js new file mode 100644 index 000000000..c41f12c60 --- /dev/null +++ b/packages/core/src/lib/utils/find-config-file.js @@ -0,0 +1,57 @@ +const cosmiconfig = require('cosmiconfig'); + +// find and load up the pattern lab configuration +async function findPatternLabConfig(configPath) { + return new Promise(resolve => { + const moduleName = 'patternlab'; + const explorer = cosmiconfig(moduleName, { + searchPlaces: [ + 'package.json', + `.${moduleName}rc`, + `.${moduleName}rc.json`, + `.${moduleName}rc.yaml`, + `.${moduleName}rc.yml`, + `.${moduleName}rc.js`, + `${moduleName}.config.js`, + `${moduleName}.config.json`, + ], + }); + + // if a config path is provided, try to load that up. otherwise recursively search + if (configPath) { + return explorer + .load(configPath) + .then(result => { + const config = result.config; + config.filepath = result.filepath; + resolve(config); + // result.config is the parsed configuration object. + // result.filepath is the path to the config file that was found. + // result.isEmpty is true if there was nothing to parse in the config file. + }) + .catch(error => { + console.log(error); + // Do something constructive. + }); + } else { + return explorer + .search() + .then(result => { + const config = result.config; + config.filepath = result.filepath; + resolve(config); + // result.config is the parsed configuration object. + // result.filepath is the path to the config file that was found. + // result.isEmpty is true if there was nothing to parse in the config file. + }) + .catch(error => { + console.log(error); + // Do something constructive. + }); + } + }); +} + +module.exports = { + findPatternLabConfig, +}; diff --git a/packages/edition-twig/package.json b/packages/edition-twig/package.json index a45997654..b5ac8805d 100644 --- a/packages/edition-twig/package.json +++ b/packages/edition-twig/package.json @@ -13,10 +13,10 @@ ], "main": "patternlab-config.json", "scripts": { - "build": "patternlab build --config ./patternlab-config.json", + "build": "patternlab build --config ./patternlab-config.js", "help": "patternlab --help", - "install": "patternlab install --config ./patternlab-config.json", - "serve": "patternlab serve --config ./patternlab-config.json", + "install": "patternlab install --config ./patternlab-config.js", + "serve": "patternlab serve --config ./patternlab-config.js", "start": "npm run serve", "version": "patternlab --version" },