|
2 | 2 | * @module launchpad-content/content-options
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -import { SourceOptions } from './content-sources/content-source.js'; |
| 5 | +export const DOWNLOAD_PATH_TOKEN = '%DOWNLOAD_PATH%'; |
| 6 | + |
| 7 | +export const TIMESTAMP_TOKEN = '%TIMESTAMP%'; |
| 8 | + |
| 9 | +/** |
| 10 | + * @typedef { import('./content-sources/airtable-source.js').AirtableOptions |
| 11 | + * | import('./content-sources/contentful-source.js').ContentfulOptions |
| 12 | + * | import('./content-sources/json-source.js').JsonOptions |
| 13 | + * | import('./content-sources/sanity-source.js').SanityOptions |
| 14 | + * | import('./content-sources/strapi-source.js').StrapiOptions |
| 15 | + * } AllSourceOptions |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * @typedef ContentOptions |
| 20 | + * @property {AllSourceOptions[]} [sources] A list of content source options. This defines which content is downloaded from where. |
| 21 | + * @property {Array<Object<string, number>>} [imageTransforms] A list of image transforms to apply to a copy of each downloaded image. |
| 22 | + * @property {Object<string, string>} [contentTransforms] A list of content transforms to apply to all donwloaded content. |
| 23 | + * @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'. |
| 25 | + * @property {string} [tempPath] Temp file directory path. Defaults to '%DOWNLOAD_PATH%/.tmp/'. |
| 26 | + * @property {string} [backupPath] Temp directory path where all downloaded content will be backed up before removal. Defaults to '%TIMESTAMP%/.tmp-backup/'. |
| 27 | + * @property {string} [keep] Which files to keep in `dest` if `clearOldFilesOnSuccess` or `clearOldFilesOnStart` are `true`. E.g. `'*.json|*.csv|*.xml|*.git*'` |
| 28 | + * @property {string} [strip] Strips this string from all media file paths when saving them locally |
| 29 | + * @property {boolean} [backupAndRestore] Back up files before downloading and restore originals for all sources on failure of any single source. Defaults to true. |
| 30 | + * @property {number} [maxConcurrent] Max concurrent downloads. Defaults to 4. |
| 31 | + * @property {number} [maxTimeout] Max request timeout in ms. Defaults to 30000. |
| 32 | + * @property {boolean} [clearOldFilesOnSuccess] Remove all existing files in dest dir when downloads succeed. Ignores files that match `keep`. Defaults to true. |
| 33 | + * @property {boolean} [clearOldFilesOnStart] Will remove all existing files _before_ downloads starts. `false` will ensure that existing files are only deleted after a download succeeds. Defaults to false. |
| 34 | + * @property {boolean} [ignoreCache] Will always download files regardless of whether they've been cached. Defaults to false. |
| 35 | + * @property {boolean} [enableIfModifiedSinceCheck] Enables the HTTP if-modified-since check. Disabling this will assume that the local file is the same as the remote file if it already exists. Defaults to true. |
| 36 | + * @property {boolean} [enableContentLengthCheck] Compares the HTTP header content-length with the local file size. Disabling this will assume that the local file is the same as the remote file if it already exists. Defaults to true. |
| 37 | + * @property {boolean} [abortOnError] If set to `true`, errors will cause syncing to abort all remaining tasks immediately. Defaults to true. |
| 38 | + * @property {boolean} [ignoreImageTransformCache] Set to `true` to always re-generate transformed images, even if cached versions of the original and transformed image already exist. Defaults to false. |
| 39 | + * @property {boolean} [ignoreImageTransformErrors] Set to `false` if you want to abort a content source from downloading if any of the image transforms fail. Leaving this to `true` will allow for non-image files to fail quietly. Defaults to true. |
| 40 | + * @property {boolean} [forceClearTempFiles] Set to `false` if you want to keep all contents of the tempPath dir before downloading. Defaults to true. |
| 41 | + */ |
| 42 | + |
| 43 | +/** |
| 44 | + * @type {Required<ContentOptions>} |
| 45 | + */ |
| 46 | +export const CONTENT_OPTION_DEFAULTS = { |
| 47 | + sources: [], |
| 48 | + imageTransforms: [], |
| 49 | + contentTransforms: {}, |
| 50 | + downloadPath: '.downloads/', |
| 51 | + credentialsPath: '.credentials.json', |
| 52 | + tempPath: '%DOWNLOAD_PATH%/.tmp/', |
| 53 | + backupPath: '%TIMESTAMP%/.tmp-backup/', |
| 54 | + keep: '', |
| 55 | + strip: '', |
| 56 | + backupAndRestore: true, |
| 57 | + maxConcurrent: 4, |
| 58 | + maxTimeout: 30000, |
| 59 | + clearOldFilesOnSuccess: true, |
| 60 | + clearOldFilesOnStart: false, |
| 61 | + ignoreCache: false, |
| 62 | + enableIfModifiedSinceCheck: true, |
| 63 | + enableContentLengthCheck: true, |
| 64 | + abortOnError: true, |
| 65 | + ignoreImageTransformCache: false, |
| 66 | + ignoreImageTransformErrors: true, |
| 67 | + forceClearTempFiles: true |
| 68 | +}; |
6 | 69 |
|
7 | 70 | /**
|
8 |
| - * Options for all content and media downloads. Each of these settings can also be configured per `ContentSource`. |
| 71 | + * Apply defaults to passed content config |
| 72 | + * @param {ContentOptions} [config] |
9 | 73 | */
|
10 |
| -export class ContentOptions { |
11 |
| - static get DOWNLOAD_PATH_TOKEN() { |
12 |
| - return '%DOWNLOAD_PATH%'; |
13 |
| - } |
14 |
| - |
15 |
| - static get TIMESTAMP_TOKEN() { |
16 |
| - return '%TIMESTAMP%'; |
17 |
| - } |
18 |
| - |
19 |
| - /** |
20 |
| - * @param {any} options |
21 |
| - */ |
22 |
| - constructor({ |
23 |
| - sources = [], |
24 |
| - imageTransforms = [], |
25 |
| - contentTransforms = {}, |
26 |
| - downloadPath = '.downloads/', |
27 |
| - credentialsPath = '.credentials.json', |
28 |
| - tempPath = `${ContentOptions.DOWNLOAD_PATH_TOKEN}/.tmp/`, |
29 |
| - backupPath = `${ContentOptions.DOWNLOAD_PATH_TOKEN}/.tmp-backup/`, |
30 |
| - keep = '', |
31 |
| - strip = '', |
32 |
| - backupAndRestore = true, |
33 |
| - maxConcurrent = 4, |
34 |
| - maxTimeout = 30000, |
35 |
| - clearOldFilesOnStart = false, |
36 |
| - clearOldFilesOnSuccess = true, |
37 |
| - ignoreCache = false, |
38 |
| - enableIfModifiedSinceCheck = true, |
39 |
| - enableContentLengthCheck = true, |
40 |
| - abortOnError = true, |
41 |
| - ignoreImageTransformErrors = true, |
42 |
| - ignoreImageTransformCache = false, |
43 |
| - forceClearTempFiles = true, |
44 |
| - ...rest // Any additional custom arguments |
45 |
| - } = {}) { |
46 |
| - /** |
47 |
| - * A list of content source options. This defines which content is downloaded from where. |
48 |
| - * @type {Array<SourceOptions>} |
49 |
| - * @default [] |
50 |
| - */ |
51 |
| - this.sources = sources; |
52 |
| - |
53 |
| - /** |
54 |
| - * A list of image transforms to apply to a copy of each downloaded image. |
55 |
| - * @type {Array<Object<string, number>>} |
56 |
| - * @default [] |
57 |
| - */ |
58 |
| - this.imageTransforms = imageTransforms; |
59 |
| - |
60 |
| - /** |
61 |
| - * A list of content transforms to apply to all donwloaded content. |
62 |
| - * @type {Object<string, string>} |
63 |
| - * @default {} |
64 |
| - */ |
65 |
| - this.contentTransforms = contentTransforms; |
66 |
| - |
67 |
| - /** |
68 |
| - * The path at which to store all downloaded files. |
69 |
| - * @type {string} |
70 |
| - * @default '.downloads/' |
71 |
| - */ |
72 |
| - this.downloadPath = downloadPath; |
73 |
| - |
74 |
| - /** |
75 |
| - * The path to the json containing credentials for all content sources. |
76 |
| - * @type {string} |
77 |
| - * @default '.credentials.json' |
78 |
| - */ |
79 |
| - this.credentialsPath = credentialsPath; |
80 |
| - |
81 |
| - /** |
82 |
| - * Temp file directory path. |
83 |
| - * @type {string} |
84 |
| - * @default '%DOWNLOAD_PATH%/.tmp/' |
85 |
| - */ |
86 |
| - this.tempPath = tempPath; |
87 |
| - |
88 |
| - /** |
89 |
| - * Temp directory path where all downloaded content will be backed up before removal. |
90 |
| - * @type {string} |
91 |
| - * @default '%DOWNLOAD_PATH%/.backups/' |
92 |
| - */ |
93 |
| - this.backupPath = backupPath; |
94 |
| - |
95 |
| - /** |
96 |
| - * Which files to keep in `dest` if `clearOldFilesOnSuccess` or `clearOldFilesOnStart` are `true`. E.g. `'*.json|*.csv|*.xml|*.git*'` |
97 |
| - * @type {string} |
98 |
| - * @default '' |
99 |
| - */ |
100 |
| - this.keep = keep; |
101 |
| - |
102 |
| - /** |
103 |
| - * Strips this string from all media file paths when saving them locally |
104 |
| - * @type {string} |
105 |
| - * @default '' |
106 |
| - */ |
107 |
| - this.strip = strip; |
108 |
| - |
109 |
| - /** |
110 |
| - * Back up files before downloading and restore originals for all sources on failure of any single source. |
111 |
| - * @type {boolean} |
112 |
| - * @default true |
113 |
| - */ |
114 |
| - this.backupAndRestore = backupAndRestore; |
115 |
| - |
116 |
| - /** |
117 |
| - * Max concurrent downloads. |
118 |
| - * @type {number} |
119 |
| - * @default 4 |
120 |
| - */ |
121 |
| - this.maxConcurrent = maxConcurrent; |
122 |
| - |
123 |
| - /** |
124 |
| - * Max request timeout in ms. |
125 |
| - * @type {number} |
126 |
| - * @default 30000 |
127 |
| - */ |
128 |
| - this.maxTimeout = maxTimeout; |
129 |
| - |
130 |
| - /** |
131 |
| - * Remove all existing files in dest dir when downloads succeed. Ignores files that match `keep` |
132 |
| - * @type {boolean} |
133 |
| - * @default true |
134 |
| - */ |
135 |
| - this.clearOldFilesOnSuccess = clearOldFilesOnSuccess; |
136 |
| - |
137 |
| - /** |
138 |
| - * Will remove all existing files _before_ downloads starts. `false` will ensure that existing files are only deleted after a download succeeds. |
139 |
| - * @type {boolean} |
140 |
| - * @default false |
141 |
| - */ |
142 |
| - this.clearOldFilesOnStart = clearOldFilesOnStart; |
143 |
| - |
144 |
| - /** |
145 |
| - * Will always download files regardless of whether they've been cached |
146 |
| - * @type {boolean} |
147 |
| - * @default false |
148 |
| - */ |
149 |
| - this.ignoreCache = ignoreCache; |
150 |
| - |
151 |
| - /** |
152 |
| - * Enables the HTTP if-modified-since check. Disabling this will assume that the local file is the same as the remote file if it already exists. |
153 |
| - * @type {boolean} |
154 |
| - * @default true |
155 |
| - */ |
156 |
| - this.enableIfModifiedSinceCheck = enableIfModifiedSinceCheck; |
157 |
| - |
158 |
| - /** |
159 |
| - * Compares the HTTP header content-length with the local file size. Disabling this will assume that the local file is the same as the remote file if it already exists. |
160 |
| - * @type {boolean} |
161 |
| - * @default true |
162 |
| - */ |
163 |
| - this.enableContentLengthCheck = enableContentLengthCheck; |
164 |
| - |
165 |
| - /** |
166 |
| - * If set to `true`, errors will cause syncing to abort all remaining tasks immediately |
167 |
| - * @type {boolean} |
168 |
| - * @default true |
169 |
| - */ |
170 |
| - this.abortOnError = abortOnError; |
171 |
| - |
172 |
| - /** |
173 |
| - * Set to `true` to always re-generate transformed images, even if cached versions of the original and transformed image already exist. |
174 |
| - * @type {boolean} |
175 |
| - * @default false |
176 |
| - */ |
177 |
| - this.ignoreImageTransformCache = ignoreImageTransformCache; |
178 |
| - |
179 |
| - /** |
180 |
| - * Set to `false` if you want to abort a content source from downloading if any of the image transforms fail. Leaving this to `true` will allow for non-image files to fail quietly. |
181 |
| - * @type {boolean} |
182 |
| - * @default true |
183 |
| - */ |
184 |
| - this.ignoreImageTransformErrors = ignoreImageTransformErrors; |
185 |
| - |
186 |
| - /** |
187 |
| - * Set to `false` if you want to keep all contents of the tempPath dir before downloading |
188 |
| - * @type {boolean} |
189 |
| - * @default true |
190 |
| - */ |
191 |
| - this.forceClearTempFiles = forceClearTempFiles; |
192 |
| - |
193 |
| - // Allows for additional properties to be inherited |
194 |
| - Object.assign(this, rest); |
195 |
| - } |
| 74 | +export function resolveContentOptions(config) { |
| 75 | + return { |
| 76 | + ...CONTENT_OPTION_DEFAULTS, |
| 77 | + ...config |
| 78 | + }; |
196 | 79 | }
|
197 | 80 |
|
198 |
| -export default ContentOptions; |
| 81 | +/** |
| 82 | + * @typedef {ReturnType<typeof resolveContentOptions>} ResolvedContentOptions |
| 83 | + */ |
| 84 | + |
| 85 | +/** |
| 86 | + * @param {ContentOptions} config |
| 87 | + * @returns {ContentOptions} |
| 88 | + */ |
| 89 | +export function defineContentConfig(config) { |
| 90 | + return config; |
| 91 | +} |
0 commit comments