Skip to content

Commit

Permalink
Merge pull request #4 from CloudCannon/fix/update-types
Browse files Browse the repository at this point in the history
Update types
  • Loading branch information
rphillips-cc authored Aug 2, 2024
2 parents 45a5399 + f125687 commit 5914495
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 148 deletions.
57 changes: 27 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
107 changes: 30 additions & 77 deletions src/index.js
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(),
};
}
Loading

0 comments on commit 5914495

Please sign in to comment.