|
| 1 | +const inquirerTablePrompt = require("inquirer-table-prompt"); |
| 2 | +const pkg = require("./package.json"); |
| 3 | +const slugify = require("@sindresorhus/slugify"); |
| 4 | +const { getSetupForData, getSetupForPage } = require("./lib/setup"); |
| 5 | + |
| 6 | +module.exports.name = pkg.name; |
| 7 | + |
| 8 | +module.exports.transform = ({ data, log, options }) => { |
| 9 | + if (typeof options.writeFile !== "function") { |
| 10 | + return data; |
| 11 | + } |
| 12 | + |
| 13 | + const utils = { |
| 14 | + slugify |
| 15 | + }; |
| 16 | + const files = data.objects.reduce((result, object) => { |
| 17 | + let processedObject = object; |
| 18 | + |
| 19 | + // Unless `options.fullAssetObjects` is true, we reduce any asset objects |
| 20 | + // down to a string containing just the URL. |
| 21 | + if (!options.fullAssetObjects) { |
| 22 | + processedObject = Object.keys(object).reduce((result, fieldName) => { |
| 23 | + const value = |
| 24 | + object[fieldName].__metadata && |
| 25 | + object[fieldName].__metadata.modelName === "__asset" |
| 26 | + ? object[fieldName].url |
| 27 | + : object[fieldName]; |
| 28 | + |
| 29 | + return { |
| 30 | + ...result, |
| 31 | + [fieldName]: value |
| 32 | + }; |
| 33 | + }, {}); |
| 34 | + } |
| 35 | + |
| 36 | + const writer = options.writeFile(processedObject, utils); |
| 37 | + |
| 38 | + if (!writer) return result; |
| 39 | + |
| 40 | + return result.concat(writer); |
| 41 | + }, []); |
| 42 | + |
| 43 | + return { |
| 44 | + ...data, |
| 45 | + files: (data.files || []).concat(files) |
| 46 | + }; |
| 47 | +}; |
| 48 | + |
| 49 | +module.exports.getOptionsFromSetup = ({ answers, debug }) => { |
| 50 | + const { data: dataObjects = [], pages = [] } = answers; |
| 51 | + const conditions = []; |
| 52 | + |
| 53 | + pages.forEach(page => { |
| 54 | + const { modelName, projectId, source } = page.__model; |
| 55 | + |
| 56 | + let location = ""; |
| 57 | + |
| 58 | + if (page.location.fileName) { |
| 59 | + location = `'${page.location.fileName}'`; |
| 60 | + } else { |
| 61 | + const { directory, fileNameField, useDate } = page.location; |
| 62 | + const locationParts = []; |
| 63 | + |
| 64 | + if (directory) { |
| 65 | + locationParts.push(`'${directory}/'`); |
| 66 | + } |
| 67 | + |
| 68 | + if (useDate) { |
| 69 | + locationParts.push(`createdAt.substring(0, 10) + '-'`); |
| 70 | + } |
| 71 | + |
| 72 | + locationParts.push(`utils.slugify(fields['${fileNameField}']) + '.md'`); |
| 73 | + location = locationParts.join(" + "); |
| 74 | + } |
| 75 | + |
| 76 | + const contentField = page.contentField |
| 77 | + ? `fields['${page.contentField}']` |
| 78 | + : "{}"; |
| 79 | + const layout = |
| 80 | + page.layoutSource === "static" |
| 81 | + ? `'${page.layout}'` |
| 82 | + : `fields['${page.layout}']`; |
| 83 | + const extractedProperties = [ |
| 84 | + "__metadata", |
| 85 | + page.contentField ? `'${page.contentField}': content` : null, |
| 86 | + page.layoutSource ? "layout" : null, |
| 87 | + "...frontmatterFields" |
| 88 | + ]; |
| 89 | + |
| 90 | + conditions.push( |
| 91 | + `if (modelName === '${modelName}' && projectId === '${projectId}' && source === '${source}') {`, |
| 92 | + ` const { ${extractedProperties.filter(Boolean).join(", ")} } = entry;`, |
| 93 | + ``, |
| 94 | + ` return {`, |
| 95 | + ` content: {`, |
| 96 | + ` body: ${contentField},`, |
| 97 | + ` frontmatter: ${ |
| 98 | + page.layoutSource |
| 99 | + ? `{ ...frontmatterFields, layout: ${layout} }` |
| 100 | + : "frontmatterFields" |
| 101 | + },`, |
| 102 | + ` },`, |
| 103 | + ` format: 'frontmatter-md',`, |
| 104 | + ` path: ${location}`, |
| 105 | + ` };`, |
| 106 | + `}\n` |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + dataObjects.forEach(dataObject => { |
| 111 | + const { modelName, projectId, source } = dataObject.__model; |
| 112 | + const { format, isMultiple } = dataObject; |
| 113 | + const location = dataObject.location.fileName |
| 114 | + ? `'${dataObject.location.fileName}'` |
| 115 | + : `fields['${dataObject.location.fileNameField}']`; |
| 116 | + |
| 117 | + conditions.push( |
| 118 | + `if (modelName === '${modelName}' && projectId === '${projectId}' && source === '${source}') {`, |
| 119 | + ` const { __metadata, ...fields } = entry;`, |
| 120 | + ``, |
| 121 | + ` return {`, |
| 122 | + ` append: ${isMultiple},`, |
| 123 | + ` content: fields,`, |
| 124 | + ` format: '${format}',`, |
| 125 | + ` path: ${location}`, |
| 126 | + ` };`, |
| 127 | + `}\n` |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + const functionBody = ` |
| 132 | +// This function is invoked for each entry and its return value determines |
| 133 | +// whether the entry will be written to a file. When an object with \`content\`, |
| 134 | +// \`format\` and \`path\` properties is returned, a file will be written with |
| 135 | +// those parameters. If a falsy value is returned, no file will be created. |
| 136 | +const { __metadata: meta, ...fields } = entry; |
| 137 | +
|
| 138 | +if (!meta) return; |
| 139 | +
|
| 140 | +const { createdAt = '', modelName, projectId, source } = meta; |
| 141 | +
|
| 142 | +${conditions.join("\n")} |
| 143 | + `.trim(); |
| 144 | + |
| 145 | + debug("Function body: %s", functionBody); |
| 146 | + |
| 147 | + return { |
| 148 | + writeFile: new Function("entry", "utils", functionBody) |
| 149 | + }; |
| 150 | +}; |
| 151 | + |
| 152 | +module.exports.getSetup = ({ chalk, data, inquirer }) => { |
| 153 | + inquirer.registerPrompt("table", inquirerTablePrompt); |
| 154 | + |
| 155 | + return async () => { |
| 156 | + const { models: modelTypes } = await inquirer.prompt([ |
| 157 | + { |
| 158 | + type: "table", |
| 159 | + name: "models", |
| 160 | + message: "Choose a type for each of the following models:", |
| 161 | + pageSize: 7, |
| 162 | + rows: data.models.map((model, index) => ({ |
| 163 | + name: `${model.modelLabel || model.modelName}\n${chalk.dim( |
| 164 | + `└${model.source}` |
| 165 | + )}`, |
| 166 | + value: index |
| 167 | + })), |
| 168 | + columns: [ |
| 169 | + { |
| 170 | + name: "Page", |
| 171 | + value: "page" |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "Data", |
| 175 | + value: "data" |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "Skip", |
| 179 | + value: undefined |
| 180 | + } |
| 181 | + ] |
| 182 | + } |
| 183 | + ]); |
| 184 | + const dataModels = []; |
| 185 | + const pageModels = []; |
| 186 | + |
| 187 | + modelTypes.forEach((type, index) => { |
| 188 | + if (type === "data") { |
| 189 | + dataModels.push(data.models[index]); |
| 190 | + } else if (type === "page") { |
| 191 | + pageModels.push(data.models[index]); |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + let queue = Promise.resolve({ data: [], pages: [] }); |
| 196 | + |
| 197 | + pageModels.forEach((model, index) => { |
| 198 | + queue = queue.then(async setupData => { |
| 199 | + console.log( |
| 200 | + `\nConfiguring page: ${chalk.bold( |
| 201 | + model.modelLabel || model.modelName |
| 202 | + )} ${chalk.reset.italic.green( |
| 203 | + `(${index + 1} of ${pageModels.length}` |
| 204 | + )})` |
| 205 | + ); |
| 206 | + |
| 207 | + return getSetupForPage({ chalk, data, inquirer, model, setupData }); |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + dataModels.forEach((model, index) => { |
| 212 | + queue = queue.then(async setupData => { |
| 213 | + console.log( |
| 214 | + `\nConfiguring data object: ${chalk.bold( |
| 215 | + model.modelLabel || model.modelName |
| 216 | + )} ${chalk.reset.italic.green( |
| 217 | + `(${index + 1} of ${dataModels.length}` |
| 218 | + )})` |
| 219 | + ); |
| 220 | + |
| 221 | + return getSetupForData({ chalk, data, inquirer, model, setupData }); |
| 222 | + }); |
| 223 | + }); |
| 224 | + |
| 225 | + return queue; |
| 226 | + }; |
| 227 | +}; |
0 commit comments