From c73c325e029b595bd51f7e83b09798aec2581762 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Sat, 5 Jun 2021 14:32:47 +0200 Subject: [PATCH] Add support for user-configurable workflows (#113) Co-authored-by: Sindre Sorhus --- index.js | 3 +++ init.js | 5 +++++ package.json | 1 + readme.md | 15 +++++++++++++++ test/_utils.js | 1 + test/fixtures/config/user-config.json | 7 +++++++ test/user-config.js | 14 ++++++++++++++ 7 files changed, 46 insertions(+) create mode 100644 test/fixtures/config/user-config.json create mode 100644 test/user-config.js diff --git a/index.js b/index.js index bfd5a7c..072dc59 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const loudRejection = require('loud-rejection'); const cleanStack = require('clean-stack'); const dotProp = require('dot-prop'); const CacheConf = require('cache-conf'); +const AlfredConfig = require('alfred-config'); const updateNotification = require('./lib/update-notification'); const alfy = module.exports; @@ -99,6 +100,8 @@ alfy.config = new Conf({ cwd: alfy.alfred.data }); +alfy.userConfig = new AlfredConfig(); + alfy.cache = new CacheConf({ configName: 'cache', cwd: alfy.alfred.cache, diff --git a/init.js b/init.js index 9aeb4b6..b0c683c 100755 --- a/init.js +++ b/init.js @@ -8,6 +8,11 @@ const execa = require('execa'); preferLocal: true, localDir: __dirname }); + + await execa('alfred-config', { + preferLocal: true, + localDir: __dirname + }); } catch (error) { console.error(error); process.exit(1); diff --git a/package.json b/package.json index 6478956..e763178 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "mac" ], "dependencies": { + "alfred-config": "^0.2.2", "alfred-link": "^0.3.1", "alfred-notifier": "^0.2.0", "cache-conf": "^0.6.0", diff --git a/readme.md b/readme.md index 9de1e52..d235cfa 100644 --- a/readme.md +++ b/readme.md @@ -393,6 +393,21 @@ alfy.config.get('unicorn'); //=> '🦄' ``` +#### userConfig + +Type: `Map` + +Exports a [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) with the user workflow configuration. A workflow configuration allows your users to provide configuration information for the workflow. For instance, if you are developing a GitHub workflow, you could let your users provide their own API tokens. + +See [`alfred-config`](https://github.com/SamVerschueren/alfred-config#workflow-configuration) for more details. + +Example: + +```js +alfy.userConfig.get('apiKey'); +//=> '16811cad1b8547478b3e53eae2e0f083' +``` + #### cache Type: `object` diff --git a/test/_utils.js b/test/_utils.js index 3119680..16b037a 100644 --- a/test/_utils.js +++ b/test/_utils.js @@ -6,6 +6,7 @@ const tempfile = require('tempfile'); exports.alfy = (options = {}) => { delete require.cache[path.resolve(__dirname, '../index.js')]; + process.env.alfred_workflow_data = options.data || tempfile(); process.env.alfred_workflow_cache = options.cache || tempfile(); process.env.alfred_workflow_version = options.version || '1.0.0'; return require('..'); diff --git a/test/fixtures/config/user-config.json b/test/fixtures/config/user-config.json new file mode 100644 index 0000000..78dab12 --- /dev/null +++ b/test/fixtures/config/user-config.json @@ -0,0 +1,7 @@ +{ + // 🦄 Unicorns 🦄 + "unicorn": "🦄", + + // 🌈 Rainbows 🌈 + "rainbow": "🌈" +} diff --git a/test/user-config.js b/test/user-config.js new file mode 100644 index 0000000..62ea284 --- /dev/null +++ b/test/user-config.js @@ -0,0 +1,14 @@ +import path from 'path'; +import test from 'ava'; +import {alfy as createAlfy} from './_utils'; + +test('read user config', t => { + const alfy = createAlfy({ + data: path.join(__dirname, 'fixtures/config') + }); + + t.is(alfy.userConfig.size, 2); + t.is(alfy.userConfig.get('unicorn'), '🦄'); + t.is(alfy.userConfig.get('rainbow'), '🌈'); + t.false(alfy.userConfig.has('foo')); +});