Skip to content

Commit 2b84c3b

Browse files
authored
Merge pull request #133 from bluecadet/feat/dotenv
Load .env files when launching from CLI
2 parents b9eb6c7 + 4a84ec1 commit 2b84c3b

File tree

20 files changed

+351
-300
lines changed

20 files changed

+351
-300
lines changed

.changeset/nasty-cheetahs-care.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@bluecadet/launchpad-utils": minor
3+
"@bluecadet/launchpad-content": minor
4+
"@bluecadet/launchpad-monitor": minor
5+
"@bluecadet/launchpad": minor
6+
---
7+
8+
Load dotenv files when launching from CLI

.docs/generate.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import jsdoc2md from 'jsdoc-to-markdown';
22
import fs from 'fs-extra';
33
import path from 'path';
44
import chalk from 'chalk';
5-
import { ConfigManager, LogManager } from '@bluecadet/launchpad-utils';
5+
import { loadConfigFromFile, LogManager } from '@bluecadet/launchpad-utils';
6+
import { findConfigFile } from 'typescript';
67

7-
const config = ConfigManager.getInstance().getConfig();
8+
const configFile = findConfigFile();
9+
const config = configFile ? (await loadConfigFromFile(configFile)) : {};
810
const logger = LogManager.getInstance(config).getLogger('docs');
911

1012
/**

packages/content/README.md

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,54 +45,55 @@ Currently supported sources are:
4545

4646
Some content sources require credentials to access their APIs.
4747

48-
These can all be stored in a local `.credentials.json` file which maps content-source IDs to their credentials. For example:
48+
These can all be stored in a `.env` or `.env.local` file which will be automatically loaded by launchpad.
4949

50-
### `.credentials.json`
50+
### `.env.local`
5151

52-
```json
53-
{
54-
"airtable-cms": {
55-
"apiKey": "<YOUR_AIRTABLE_API_KEY>"
56-
},
57-
"contentful-cms": {
58-
"previewToken": "<YOUR_CONTENTFUL_PREVIEW_TOKEN>",
59-
"deliveryToken": "<YOUR_CONTENTFUL_DELIVERY_TOKEN>",
60-
"usePreviewApi": false
61-
},
62-
"sanity-cms": {
63-
"apiToken": "<YOUR_API_TOKEN>"
64-
},
65-
"strapi-cms": {
66-
"identifier": "<YOUR_API_USER>",
67-
"password": "<YOUR_API_PASS>"
68-
}
69-
}
52+
```sh
53+
AIRTABLE_API_KEY=<YOUR_AIRTABLE_API_KEY>
54+
55+
CONTENTFUL_PREVIEW_TOKEN=<YOUR_CONTENTFUL_PREVIEW_TOKEN>
56+
CONTENTFUL_DELIVERY_TOKEN=<YOUR_CONTENTFUL_DELIVERY_TOKEN>
57+
CONTENTFUL_USE_PREVIEW_API=false
58+
59+
SANITY_API_TOKEN=<YOUR_API_TOKEN>
60+
61+
STRAPI_IDENTIFIER=<YOUR_API_USER>
62+
STRAPI_PASSWORD=<YOUR_API_PASS>
7063
```
7164

72-
### `launchpad.json`
65+
### `launchpad.config.js`
7366

7467
```js
75-
{
76-
"content": {
77-
"sources": [{
78-
"id": "airtable-cms",
79-
"type": "airtable",
80-
//...
81-
}, {
82-
"id": "contentful-cms",
83-
"type": "contentful",
84-
//...
85-
}, {
86-
"id": "sanity-cms",
87-
"type": "sanity",
88-
//...
89-
}, {
90-
"id": "strapi-cms",
91-
"type": "strapi",
92-
//...
93-
}]
94-
}
95-
}
68+
export default defineConfig({
69+
content: {
70+
sources: [
71+
{
72+
id: "airtable-cms",
73+
type: "airtable",
74+
apiKey: process.env.AIRTABLE_API_KEY,
75+
},
76+
{
77+
id: "contentful-cms",
78+
type: "contentful",
79+
previewToken: process.env.CONTENTFUL_PREVIEW_TOKEN,
80+
deliveryToken: process.env.CONTENTFUL_DELIVERY_TOKEN,
81+
usePreviewApi: false,
82+
},
83+
{
84+
id: "sanity-cms",
85+
type: "sanity",
86+
apiToken: process.env.SANITY_API_TOKEN,
87+
},
88+
{
89+
id: "strapi-cms",
90+
type: "strapi",
91+
identifier: process.env.STRAPI_IDENTIFIER,
92+
},
93+
],
94+
},
95+
});
96+
9697
```
9798

9899
## Post Processing

packages/content/docs/contentful-source.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Also supports all fields of the Contentful SDK's config.
99
See: 'Configuration' under https://contentful.github.io/contentful.js/contentful/9.1.7/
1010
| Property | Type | Default | Description |
1111
| - | - | - | - |
12-
| <a name="module_contentful-source.ContentfulOptions+space">`space`</a> | <code>string</code>| <code>''</code> | Your Contentful space ID. Note that credentials.json will require an accessToken in addition to this |
12+
| <a name="module_contentful-source.ContentfulOptions+space">`space`</a> | <code>string</code>| <code>''</code> | Your Contentful space ID. Note that an accessToken will be required in addition to this |
1313
| <a name="module_contentful-source.ContentfulOptions+locale">`locale`</a> | <code>string</code>| <code>'en-US'</code> | Optional. Used to pull localized images. |
1414
| <a name="module_contentful-source.ContentfulOptions+filename">`filename`</a> | <code>string</code>| <code>'content.json'</code> | Optional. The filename you want to use for where all content (entries and assets metadata) will be stored. |
1515
| <a name="module_contentful-source.ContentfulOptions+contentTypes">`contentTypes`</a> | <code>Array.&lt;string&gt;</code>| | Optionally limit queries to these content types.<br>This will also apply to linked assets.<br>Types that link to other types will include up to 10 levels of child content.<br>E.g. filtering by Story, might also include Chapters and Images.<br>Uses `searchParams['sys.contentType.sys.id[in]']` under the hood. |

packages/content/docs/strapi-source.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ Options for StrapiSource
1313
| <a name="module_strapi-source.StrapiOptions+limit">`limit`</a> | <code>number</code>| <code>100</code> | Max number of entries per page. |
1414
| <a name="module_strapi-source.StrapiOptions+maxNumPages">`maxNumPages`</a> | <code>number</code>| <code>-1</code> | Max number of pages. Use the default of `-1` for all pages |
1515
| <a name="module_strapi-source.StrapiOptions+pageNumZeroPad">`pageNumZeroPad`</a> | <code>number</code>| <code>0</code> | How many zeros to pad each json filename index with. |
16-
| <a name="module_strapi-source.StrapiOptions+identifier">`identifier`</a> | <code>string</code>| | Username or email. Should be configured via `./credentials.json` |
17-
| <a name="module_strapi-source.StrapiOptions+password">`password`</a> | <code>string</code>| | Should be configured via `./credentials.json` |
16+
| <a name="module_strapi-source.StrapiOptions+identifier">`identifier`</a> | <code>string</code>| | Username or email. Should be configured via `./.env.local` |
17+
| <a name="module_strapi-source.StrapiOptions+password">`password`</a> | <code>string</code>| | Should be configured via `./.env.local` |
1818
| <a name="module_strapi-source.StrapiOptions+token">`token`</a> | <code>string</code>| | Can be used instead of identifer/password if you previously generated one. Otherwise this will be automatically generated using the identifier or password. |

packages/content/lib/content-options.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const TIMESTAMP_TOKEN = '%TIMESTAMP%';
2121
* @property {Array<Object<string, number>>} [imageTransforms] A list of image transforms to apply to a copy of each downloaded image.
2222
* @property {Object<string, string>} [contentTransforms] A list of content transforms to apply to all donwloaded content.
2323
* @property {string} [downloadPath] The path at which to store all downloaded files. Defaults to '.downloads/'.
24-
* @property {string} [credentialsPath] The path to the json containing credentials for all content sources. Defaults to '.credentials.json'.
24+
* @property {string} [credentialsPath] The path to the json containing credentials for all content sources. Defaults to '.credentials.json'. Deprecated in favor of `.env`/`.env.local`.
2525
* @property {string} [tempPath] Temp file directory path. Defaults to '%DOWNLOAD_PATH%/.tmp/'.
2626
* @property {string} [backupPath] Temp directory path where all downloaded content will be backed up before removal. Defaults to '%TIMESTAMP%/.tmp-backup/'.
2727
* @property {string} [keep] Which files to keep in `dest` if `clearOldFilesOnSuccess` or `clearOldFilesOnStart` are `true`. E.g. `'*.json|*.csv|*.xml|*.git*'`
@@ -41,14 +41,13 @@ export const TIMESTAMP_TOKEN = '%TIMESTAMP%';
4141
*/
4242

4343
/**
44-
* @type {Required<ContentOptions>}
44+
* @satisfies {ContentOptions}
4545
*/
4646
export const CONTENT_OPTION_DEFAULTS = {
4747
sources: [],
4848
imageTransforms: [],
4949
contentTransforms: {},
5050
downloadPath: '.downloads/',
51-
credentialsPath: '.credentials.json',
5251
tempPath: '%DOWNLOAD_PATH%/.tmp/',
5352
backupPath: '%TIMESTAMP%/.tmp-backup/',
5453
keep: '',

packages/content/lib/content-sources/contentful-source.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { Logger } from '@bluecadet/launchpad-utils';
3030

3131
/**
3232
* @typedef BaseContentfulOptions
33-
* @property {string} space Your Contentful space ID. Note that credentials.json will require an accessToken in addition to this
33+
* @property {string} space Your Contentful space ID. Note that an accessToken is required in addition to this
3434
* @property {string} [locale] Optional. Used to pull localized images.
3535
* @property {string} [filename] Optional. The filename you want to use for where all content (entries and assets metadata) will be stored. Defaults to 'content.json'
3636
* @property {string} [protocol] Optional. Defaults to 'https'

packages/content/lib/content-sources/strapi-source.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import { Logger } from '@bluecadet/launchpad-utils';
2626

2727
/**
2828
* @typedef StrapiLoginCredentials
29-
* @property {string} identifier Username or email. Should be configured via `./credentials.json`
30-
* @property {string} password Should be configured via `./credentials.json`
29+
* @property {string} identifier Username or email. Should be configured via `./.env.local`
30+
* @property {string} password Should be configured via `./.env.local`
3131
*
3232
* @typedef StrapiTokenCredentials
3333
* @property {string} token Can be used instead of identifer/password if you previously generated one. Otherwise this will be automatically generated using the identifier or password.

packages/content/lib/credentials.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,34 @@ import { Logger } from '@bluecadet/launchpad-utils';
77
*/
88
let creds = {};
99

10+
/**
11+
* @deprecated Use .env for managing sensitive data instead
12+
*/
1013
class Credentials {
1114
/** @type {Logger | Console} */
1215
static logger = console;
1316

1417
/**
15-
* @param {string} credentialsPath
18+
* @param {string} [credentialsPath]
1619
* @param {Logger | Console} logger
20+
* @deprecated Use .env for managing sensitive data instead
1721
*/
1822
static init(credentialsPath, logger = console) {
1923
this.logger = logger;
24+
25+
if (!credentialsPath) {
26+
return;
27+
}
28+
29+
this.logger.warn(`${chalk.white('credentialsPath')} option is deprecated. Please use ${chalk.white('.env')}/${chalk.white('.env.local')} instead.`);
30+
2031
try {
21-
if (credentialsPath && fsx.existsSync(credentialsPath)) {
32+
if (fsx.existsSync(credentialsPath)) {
2233
this.logger.info(chalk.gray(`Loading credentials from '${chalk.white(credentialsPath)}'`));
2334
const rawdata = fsx.readFileSync(credentialsPath);
2435
creds = JSON.parse(rawdata.toString());
25-
} else if (credentialsPath) {
26-
this.logger.warn(`No credentials file found at '${credentialsPath}'`);
2736
} else {
28-
this.logger.warn(chalk.yellow('No credentials path specified'));
37+
this.logger.warn(`No credentials file found at '${credentialsPath}'`);
2938
}
3039
} catch (err) {
3140
if (err instanceof Error) {
@@ -36,6 +45,7 @@ class Credentials {
3645

3746
/**
3847
* @param {string} id
48+
* @deprecated Use .env for managing sensitive data instead
3949
*/
4050
static getCredentials(id) {
4151
if (id in creds) {

packages/content/lib/launchpad-content.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class LaunchpadContent {
6060
}
6161
}
6262

63-
/** @type {Required<import('./content-options.js').ResolvedContentOptions>} */
63+
/** @type {import('./content-options.js').ResolvedContentOptions} */
6464
_config;
6565

6666
/** @type {Logger} */

0 commit comments

Comments
 (0)