summaryrefslogtreecommitdiff
path: root/date-fns/scripts/build/docs.js
blob: 85c99eb4e87cabc7c92f26e01bebc44a2928ffb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env node

/**
 * @file
 * The script generates docs.json used as the source of truth
 * for the source code generators (FP, typings, etc.).
 *
 * It's a part of the build process.
 */

const os = require('os')
const pLimit = require('p-limit')
const fs = require('fs/promises')
const path = require('path')
const cloneDeep = require('lodash.clonedeep')
const jsDocParser = require('jsdoc-to-markdown')
const listFns = require('../_lib/listFns')
const docsConfig = require('../../docs/index.js')

const docsPath = path.resolve(process.cwd(), 'tmp/docs.json')

generateDocsFromSource()
  .then(generatedDocsObj)
  .then(injectStaticDocsToDocsObj)
  .then(injectSharedDocsToDocsObj)
  .then(writeDocsFile)
  .catch(reportErrors)

/**
 * Generates docs object from a list of functions using extended JSDoc format.
 */
async function generateDocsFromSource() {
  const fns = await listFns()

  const limit = pLimit(os.cpus().length)

  const configFile = path.resolve(process.cwd(), 'jsdoc2md.json')

  const jobs = fns.map((fn) => {
    return limit(() =>
      jsDocParser
        .getTemplateData({
          files: fn.fullPath,
          'no-cache': true,
          configure: configFile,
        })
        .then((result) => result[0])
    )
  })

  const docsResult = await Promise.all(jobs)

  return docsResult
    .map((doc) => {
      const pureTag =
        doc.customTags && doc.customTags.find((t) => t.tag === 'pure')
      const pure = (pureTag && pureTag.value) !== 'false'
      return {
        type: 'jsdoc',
        kind: 'function',
        urlId: doc.name,
        category: doc.category,
        title: doc.name,
        description: doc.summary,
        content: doc,
        pure,
      }
    })
    .reduce(
      (array, doc) =>
        array
          .concat(generateFnDoc(doc))
          .concat(
            doc.pure
              ? [generateFPFnDoc(doc)].concat(
                  generateFPFnWithOptionsDoc(doc) || []
                )
              : []
          ),
      []
    )
}

/**
 * Generates docs object.
 */
function generatedDocsObj(docs) {
  return groupDocs(docs, docsConfig.groups)
}

/**
 * Injects static docs (markdown documents specified in the config file)
 * to docs object.
 */
function injectStaticDocsToDocsObj(docsFileObj) {
  return getListOfStaticDocs()
    .then((staticDocs) => {
      staticDocs.forEach((staticDoc) => {
        docsFileObj[staticDoc.category].push(staticDoc)
      })
      return docsFileObj
    })
    .catch(reportErrors)
}

/**
 * Injects shared docs to docs object.
 */
function injectSharedDocsToDocsObj(docsFileObj) {
  return generateSharedDocs()
    .then((sharedDocs) => {
      sharedDocs.forEach((sharedDoc) => {
        docsFileObj[sharedDoc.category].push(sharedDoc)
      })
      return docsFileObj
    })
    .catch(reportErrors)
}

/**
 * Prints an error and exits the process with 1 status code.
 */
function reportErrors(err) {
  console.error(err.stack)
  process.exit(1)
}

/**
 * Writes docs file.
 */
function writeDocsFile(docsFileObj) {
  return fs.writeFile(docsPath, JSON.stringify(docsFileObj))
}

/**
 * Groups passed docs list.
 */
function groupDocs(docs, groups) {
  return docs.reduce((acc, doc) => {
    ;(acc[doc.category] = acc[doc.category] || []).push(doc)
    return acc
  }, buildGroupsTemplate(groups))
}

/**
 * Builds an object where the key is a group name and the value is
 * an empty array. Pre-generated docs object allows to preserve the desired
 * groups order.
 */
function buildGroupsTemplate(groups) {
  return groups.reduce((acc, group) => {
    acc[group] = []
    return acc
  }, {})
}

/**
 * Returns promise to list of static docs with its contents.
 */
function getListOfStaticDocs() {
  return Promise.all(
    docsConfig.staticDocs.map((staticDoc) => {
      return fs
        .readFile(staticDoc.path)
        .then((docContent) => docContent.toString())
        .then((content) => Object.assign({ content }, staticDoc))
        .catch(reportErrors)
    })
  )
}

/**
 * Returns promise to list of shared docs with its contents.
 */
function generateSharedDocs() {
  const docs = docsConfig.sharedDocs
    .map(
      (fn) =>
        jsDocParser.getTemplateDataSync({
          files: fn.fullPath,
          'no-cache': true,
        })[0]
    )
    .map((doc) => ({
      type: 'jsdoc',
      kind: 'typedef',
      urlId: doc.name,
      category: doc.category,
      title: doc.name,
      description: doc.summary,
      content: doc,
      properties: paramsToTree(doc.properties),
    }))

  return Promise.resolve(docs)
}

function generateFnDoc(dirtyDoc) {
  const doc = cloneDeep(dirtyDoc)

  const isFPFn = false
  const { urlId, title } = doc
  const args = paramsToTree(doc.content.params)

  return Object.assign(doc, {
    isFPFn,
    args,
    relatedDocs: Object.assign(
      { default: urlId, fp: `fp/${urlId}` },
      withOptions(args) ? { fpWithOptions: `fp/${urlId}WithOptions` } : {}
    ),
    usage: generateUsage(title, isFPFn),
    usageTabs: generateUsageTabs(isFPFn),
    syntax: generateSyntaxString(title, args, isFPFn),
  })
}

function generateFPFnDoc(dirtyDoc) {
  const doc = cloneDeep(dirtyDoc)

  const isFPFn = true
  const { urlId, title } = doc
  const exceptions = doc.content.exceptions.filter(
    (exception) => !exception.description.includes('options.')
  )
  const params = doc.content.params
    .filter((param) => !param.name.startsWith('options'))
    .reverse()
  const args = paramsToTree(params)

  return Object.assign(doc, {
    isFPFn,
    args,
    generatedFrom: title,
    urlId: `fp/${urlId}`,
    relatedDocs: Object.assign(
      { default: urlId, fp: `fp/${urlId}` },
      withOptions(args) ? { fpWithOptions: `fp/${urlId}WithOptions` } : {}
    ),
    usage: generateUsage(title, isFPFn),
    usageTabs: generateUsageTabs(isFPFn),
    syntax: generateSyntaxString(title, args, isFPFn),

    content: Object.assign(doc.content, {
      exceptions,
      params,
      examples:
        'See [FP Guide](https://date-fns.org/docs/FP-Guide) for more information',
    }),
  })
}

function generateFPFnWithOptionsDoc(dirtyDoc) {
  const doc = cloneDeep(dirtyDoc)

  const isFPFn = true
  const { urlId, title } = doc
  const params = doc.content.params
    .map((param) => {
      if (!param.name.includes('.')) {
        param.optional = false
      }
      return param
    })
    .reverse()
  const args = paramsToTree(params)

  if (!withOptions(args)) return

  return Object.assign(doc, {
    isFPFn,
    args,
    generatedFrom: title,
    title: `${title}WithOptions`,
    urlId: `fp/${urlId}WithOptions`,
    relatedDocs: {
      default: urlId,
      fp: `fp/${urlId}`,
      fpWithOptions: `fp/${urlId}WithOptions`,
    },
    usage: generateUsage(title, isFPFn),
    usageTabs: generateUsageTabs(isFPFn),
    syntax: generateSyntaxString(title, args, isFPFn),

    content: Object.assign(doc.content, {
      params,
      id: `${doc.content.id}WithOptions`,
      longname: `${doc.content.longname}WithOptions`,
      name: `${doc.content.name}WithOptions`,
      examples:
        'See [FP Guide](https://date-fns.org/docs/FP-Guide) for more information',
    }),
  })
}

function withOptions(args) {
  return args && args[0].name === 'options'
}

function generateUsageTabs(isFPFn) {
  return isFPFn
    ? ['commonjs', 'es2015', 'esm']
    : ['commonjs', 'umd', 'es2015', 'esm']
}

function generateUsage(name, isFPFn) {
  const submodule = isFPFn ? '/fp' : ''

  let usage = {
    commonjs: {
      title: 'CommonJS',
      code: `var ${name} = require('date-fns${submodule}/${name}')`,
    },

    es2015: {
      title: 'ES 2015',
      code: `import ${name} from 'date-fns${submodule}/${name}'`,
    },

    esm: {
      title: 'ESM',
      code: `import { ${name} } from 'date-fns${
        submodule && `/esm/${submodule}`
      }'`,
      text:
        'See [ECMAScript Modules guide](https://date-fns.org/docs/ECMAScript-Modules) for more information',
    },
  }

  return usage
}

function paramsToTree(dirtyParams) {
  if (!dirtyParams) {
    return null
  }

  const params = cloneDeep(dirtyParams)

  const paramIndices = params.reduce((result, { name }, index) => {
    result[name] = index
    return result
  }, {})

  return params
    .map((param) => {
      const { name, isProperty } = param

      const indexOfDot = name.indexOf('.')

      if (indexOfDot >= 0 && !isProperty) {
        const parentIndex = paramIndices[name.substring(0, indexOfDot)]
        const parent = params[parentIndex]

        param.name = name.substring(indexOfDot + 1)
        param.isProperty = true
        if (!parent.props) {
          parent.props = [param]
        } else {
          parent.props.push(param)
        }
      }

      return param
    })
    .filter((param) => !param.isProperty)
}

function generateSyntaxString(name, args, isFPFn) {
  if (!args) {
    return undefined
  } else if (isFPFn) {
    return args.reduce((acc, arg) => acc.concat(`(${arg.name})`), name)
  } else {
    const argsString = args
      .map((arg) => (arg.optional ? `[${arg.name}]` : arg.name))
      .join(', ')
    return `${name}(${argsString})`
  }
}