-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from CloudCannon/fix/update-types
Update types
- Loading branch information
Showing
9 changed files
with
239 additions
and
148 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,11 +36,11 @@ | |
}, | ||
"author": "CloudCannon <[email protected]>", | ||
"devDependencies": { | ||
"@cloudcannon/configuration-types": "^0.0.7", | ||
"@types/node": "^20.14.12", | ||
"@cloudcannon/configuration-types": "^0.0.8", | ||
"@types/node": "^22.0.2", | ||
"ava": "^6.1.3", | ||
"c8": "^10.1.2", | ||
"eslint": "^9.7.0", | ||
"eslint": "^9.8.0", | ||
"prettier": "^3.3.3", | ||
"typescript": "^5.5.4" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,58 @@ | ||
import { guessSsg, ssgs } from './ssgs/ssgs.js'; | ||
import { last, stripTopPath } from './utility.js'; | ||
import { getCollectionPaths, processCollectionPaths } from './collections.js'; | ||
import { stripTopPath } from './utility.js'; | ||
import { processCollectionPaths } from './collections.js'; | ||
|
||
export { ssgs } from './ssgs/ssgs.js'; | ||
|
||
/** | ||
* Provides a summary of a file at this path. | ||
* Filters out file paths not in the provided source. | ||
* | ||
* @param filePath {string} The input file path. | ||
* @param ssg {import('./ssgs/ssg').default} The associated SSG. | ||
* @returns {import('./types').ParsedFile} Summary of the file. | ||
*/ | ||
function parseFile(filePath, ssg) { | ||
const type = ssg.getFileType(filePath); | ||
|
||
return { | ||
filePath, | ||
type, | ||
}; | ||
} | ||
|
||
/** | ||
* Provides a summary of files. | ||
* | ||
* @param filePaths {string[]} The input file path. | ||
* @param ssg {import('./ssgs/ssg').default} The associated SSG. | ||
* @param source {string} The site's source path. | ||
* @returns {import('./types').ParsedFiles} The file summaries grouped by type. | ||
* @param filePaths {string[]} List of input file paths. | ||
* @param source {string | undefined} The source path. | ||
*/ | ||
function parseFiles(filePaths, ssg) { | ||
/** @type {Record<string, number>} */ | ||
const collectionPathCounts = {}; | ||
|
||
/** @type {Record<import('./types').FileType, import('./types').ParsedFile[]>} */ | ||
const groups = { | ||
config: [], | ||
content: [], | ||
partial: [], | ||
other: [], | ||
template: [], | ||
ignored: [], | ||
}; | ||
|
||
for (let i = 0; i < filePaths.length; i++) { | ||
const file = parseFile(filePaths[i], ssg); | ||
|
||
if (file.type === 'content') { | ||
const lastPath = last(getCollectionPaths(filePaths[i])); | ||
if (lastPath || lastPath === '') { | ||
collectionPathCounts[lastPath] = collectionPathCounts[lastPath] || 0; | ||
collectionPathCounts[lastPath] += 1; | ||
} | ||
} | ||
function filterPaths(filePaths, source) { | ||
source = `${source || ''}/`.replace(/\/+/, '/').replace(/^\//, ''); | ||
source = source === '/' ? '' : source; | ||
|
||
if (file.type !== 'ignored') { | ||
groups[file.type].push(file); | ||
} | ||
if (!source) { | ||
return filePaths; | ||
} | ||
|
||
return { collectionPathCounts, groups }; | ||
} | ||
|
||
/** | ||
* Attempts to find the current timezone. | ||
* | ||
* @returns {import('@cloudcannon/configuration-types').Timezone | undefined} | ||
*/ | ||
function getTimezone() { | ||
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
return /** @type {import('@cloudcannon/configuration-types').Timezone | undefined} */ (timezone); | ||
return filePaths.filter((filePath) => filePath.startsWith(source)); | ||
} | ||
|
||
/** | ||
* Generates a baseline CloudCannon configuration based on the file path provided. | ||
* | ||
* @param filePaths {string[]} List of input file paths. | ||
* @param options {import('./types').GenerateOptions=} Options to aid generation. | ||
* @returns {Promise<import('@cloudcannon/configuration-types').Configuration>} | ||
* @returns {Promise<import('./types').GenerateResult>} | ||
*/ | ||
export async function generate(filePaths, options) { | ||
const ssgKey = options?.config?.ssg || options?.buildConfig?.ssg; | ||
const ssg = ssgKey ? ssgs[ssgKey] : guessSsg(filePaths); | ||
const files = parseFiles(filePaths, ssg); | ||
const collectionPaths = processCollectionPaths(files.collectionPathCounts); | ||
const ssg = options?.buildConfig?.ssg | ||
? ssgs[options.buildConfig.ssg] | ||
: guessSsg(filterPaths(filePaths, options?.config?.source)); | ||
|
||
const source = | ||
options?.config?.source ?? options?.buildConfig?.source ?? ssg.getSource(files, filePaths); | ||
const source = options?.config?.source ?? ssg.getSource(filePaths); | ||
filePaths = filterPaths(filePaths, source); | ||
|
||
const files = ssg.groupFiles(filePaths); | ||
const collectionPaths = processCollectionPaths(files.collectionPathCounts); | ||
const collectionsConfig = | ||
options?.config?.collections_config || ssg.generateCollectionsConfig(collectionPaths, source); | ||
|
||
return { | ||
ssg: ssg?.key, | ||
source, | ||
collections_config: collectionsConfig, | ||
paths: { | ||
collections: source | ||
? stripTopPath(collectionPaths.basePath, source) | ||
: collectionPaths.basePath, | ||
...options?.config?.paths, | ||
ssg: ssg.key, | ||
config: { | ||
source, | ||
collections_config: collectionsConfig, | ||
paths: { | ||
collections: source | ||
? stripTopPath(collectionPaths.basePath, source) | ||
: collectionPaths.basePath, | ||
...options?.config?.paths, | ||
}, | ||
timezone: options?.config?.timezone ?? ssg.getTimezone(), | ||
}, | ||
timezone: options?.config?.timezone ?? getTimezone(), | ||
}; | ||
} |
Oops, something went wrong.