• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bcomnes / siteup / 6608581122

23 Oct 2023 04:06AM UTC coverage: 90.351% (+1.8%) from 88.526%
6608581122

push

github

bcomnes
Refactors and type

More types

# Conflicts:
#	package.json

# Conflicts:
#	package.json

185 of 216 branches covered (0.0%)

Branch coverage included in aggregate %.

420 of 420 new or added lines in 11 files covered. (100.0%)

1566 of 1722 relevant lines covered (90.94%)

9.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.4
/lib/builder.js
1
import { mkdir } from 'fs/promises'
1✔
2
import { join } from 'path'
1✔
3
import { keyBy } from './helpers/key-by.js'
1✔
4

1✔
5
import { buildPages } from './build-pages/index.js'
1✔
6
import { identifyPages } from './identify-pages.js'
1✔
7
import { buildStatic } from './build-static/index.js'
1✔
8
import { buildEsbuild } from './build-esbuild/index.js'
1✔
9

1✔
10
/**
1✔
11
 * @typedef {import('esbuild').Message} EsbuildMessage
1✔
12
 */
1✔
13

1✔
14
/**
1✔
15
 * @typedef BuildStepResult
1✔
16
 * @property {string} type - Identifier for the type of build step.
1✔
17
 * @property {Error[] | EsbuildMessage[]} [errors] - Any errors that occurred during the build step.
1✔
18
 * @property {Error[] | EsbuildMessage[]} [warnings] - Any warnings that occurred during the build step.
1✔
19
 * @property {object[]} [success]
1✔
20
 * @property {object} [buildOpts] - Options used during the build step.
1✔
21
 */
1✔
22

1✔
23
/**
1✔
24
 * @callback BuildStep
1✔
25
 * A function that represents a step in the build process. All build steps should
1✔
26
 * conform to this interface for consistency.
1✔
27
 *
1✔
28
 * @param {string} src - The source directory from which the site should be built.
1✔
29
 * @param {string} dest - The destination directory where the built site should be placed.
1✔
30
 * @param {SiteData} siteData - Data related to the site being built.
1✔
31
 * @param {SiteupOpts?} opts - Additional options for the build step.
1✔
32
 * @returns {Promise<BuildStepResult>} - The results of the build step.
1✔
33
 */
1✔
34

1✔
35
/**
1✔
36
 * @typedef SiteupOpts
1✔
37
 * @property {string[]?} [ignore] - Array of file/folder patterns to ignore.
1✔
38
 * @property {boolean?} [static=true] - Disable static file processing
1✔
39
 * @property {string[]?} [ignore=[]] - Array of ignore strings
1✔
40
 */
1✔
41

1✔
42
/**
1✔
43
 * The data generated about the site generate dby identifyPages
1✔
44
 * @typedef {Awaited<ReturnType<identifyPages>>} SiteData
1✔
45
 */
1✔
46

1✔
47
/**
1✔
48
 * Builds a siteup site from src to dest with a few options.
1✔
49
 *
1✔
50
 *
1✔
51
 * @async
1✔
52
 * @function
1✔
53
 * @export
1✔
54
 * @param {string} src - The source directory from which the site should be built.
1✔
55
 * @param {string} dest - The destination directory where the built site should be placed.
1✔
56
 * @param {SiteupOpts} opts - Options for the build process.
1✔
57
 *
1✔
58
 *
1✔
59
 * @throws {AggregateError|Error} Throws an error if there were any issues during the prebuild or build process.
1✔
60
 *                          This error contains the results of the build up to the point of failure.
1✔
61
 *
1✔
62
 * @example
1✔
63
 *
1✔
64
 * const buildOptions = {
1✔
65
 *   static: true
1✔
66
 * };
1✔
67
 *
1✔
68
 * try {
1✔
69
 *   const buildResults = await builder('./src', './dist', { static: true })
1✔
70
 *   console.log(buildResults)
1✔
71
 * } catch (error) {
1✔
72
 *   console.error(error)
1✔
73
 * }
1✔
74
 */
1✔
75
export async function builder (src, dest, opts) {
1✔
76
  /** @type {SiteData} */
6✔
77
  const siteData = await identifyPages(src, opts)
6✔
78

6✔
79
  await ensureDest(dest, siteData)
6✔
80

6✔
81
  /**
6✔
82
  * @type {BuildStep[]}
6✔
83
  */
6✔
84
  const steps = [
6✔
85
    buildEsbuild
6✔
86
  ]
6✔
87

6✔
88
  if (opts.static === false) {
6!
89
    steps.push(buildStatic)
×
90
  }
×
91

6✔
92
  const reports = await Promise.all(
6✔
93
    steps.map(
6✔
94
      step => step(src, dest, siteData, opts)
6✔
95
    )
6✔
96
  )
6✔
97

6✔
98
  const stepErrors = collectKeys('errors', reports)
6✔
99

6✔
100
  if (stepErrors.length > 0) {
6✔
101
    const preBuildError = new AggregateError(stepErrors, 'Prebuild finished but there were errors.')
1✔
102
    preBuildError.results = reports
1✔
103
    throw preBuildError
1✔
104
  }
1✔
105

5✔
106
  const pageBuildReport = await buildPages(src, dest, siteData, opts)
5✔
107

5✔
108
  const reportsAndSiteData = [siteData, pageBuildReport, ...reports]
5✔
109

5✔
110
  const errors = collectKeys('errors', reportsAndSiteData)
5✔
111
  const warnings = collectKeys('warnings', reportsAndSiteData)
5✔
112

5✔
113
  const results = {
5✔
114
    ...keyBy(reportsAndSiteData, result => result.type),
5✔
115
    warnings
5✔
116
  }
5✔
117

5✔
118
  if (errors.length > 0) {
5✔
119
    const buildError = new AggregateError(errors, 'Build finished but there were errors.')
2✔
120
    buildError.results = results
2✔
121
    throw buildError
2✔
122
  } else {
5✔
123
    return results
3✔
124
  }
3✔
125
}
6✔
126

1✔
127
/**
1✔
128
 * Create folders for each page.
1✔
129
 *
1✔
130
 * @async
1✔
131
 * @function
1✔
132
 * @param {string} dest - The destination directory where folders will be created.
1✔
133
 * @param {SiteData} siteData - An object containing an array of pages, each having a path where a folder should be created.
1✔
134
 * @returns {Promise<void>} - A promise that resolves when all directories have been created.
1✔
135
 */
1✔
136
async function ensureDest (dest, siteData) {
6✔
137
  await mkdir(dest, { recursive: true })
6✔
138

6✔
139
  for (const page of siteData.pages) {
6✔
140
    await mkdir(join(dest, page.path), { recursive: true })
30✔
141
  }
30✔
142
}
6✔
143

1✔
144
/**
1✔
145
 * Collect keys like errors and warnings and remove them from the result.
1✔
146
 *
1✔
147
 * @param {string} key - The key to collect from each result object.
1✔
148
 * @param {Array<Record<string, any>>} arrayOfResults - An array of result objects to extract the specified key from.
1✔
149
 * @returns {Array<any>} - A flattened array containing values associated with the specified key from each result object.
1✔
150
 */
1✔
151
function collectKeys (key, arrayOfResults) {
16✔
152
  const collection = []
16✔
153

16✔
154
  for (const result of arrayOfResults) {
16✔
155
    const value = result[key]
36✔
156
    if (value) collection.push(value)
36✔
157
    delete result[key]
36✔
158
  }
36✔
159

16✔
160
  return collection.flat()
16✔
161
}
16✔
162

1✔
163
// filewatching brainstorming
1✔
164
//
1✔
165
// build everything
1✔
166
// start a cpx watcher
1✔
167
// start a esbuild watcher
1✔
168
// start a chokidar watcher
1✔
169
//   - if js, md, or html file changes, rebuild it
1✔
170
//   - if a a vars file changes, rebuild the page
1✔
171
//   - if a page css file change, rebuild all pages
1✔
172
//   - if a global or root layout changes, rebuild all pages
1✔
173
//   - if a page is deleted, unregister any js entry points
1✔
174
//   - if a page client is deleted, unregister
1✔
175
//   - TODO full register unregister lifecycle
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc