Skip to content

Commit

Permalink
feat: implemented locale based folder/file scanning & version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
gokceno committed Apr 2, 2024
1 parent 2a1e0e6 commit 0eece16
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 25 deletions.
6 changes: 3 additions & 3 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"pino-pretty": "10.0.0",
"@gokceno/crux-graphql-schema": "1.0.6",
"@gokceno/crux-locales": "0.0.2",
"@gokceno/crux-bucket": "1.1.6",
"@gokceno/crux-bucket": "1.2.0",
"@gokceno/crux-bucket-cache-libsql": "0.3.10",
"@gokceno/crux-bucket-source-filesystem": "0.0.9",
"@gokceno/crux-bucket-source-github": "0.0.9"
"@gokceno/crux-bucket-source-filesystem": "0.1.0",
"@gokceno/crux-bucket-source-github": "0.1.0"
}
}
6 changes: 6 additions & 0 deletions packages/bucket-source-filesystem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @gokceno/crux-bucket-source-filesystem

## 0.1.0

### Minor Changes

- Implemented locale based folder/file scanning.

## 0.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/bucket-source-filesystem/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gokceno/crux-bucket-source-filesystem",
"repository": "git+https://github.com/gokceno/crux.md.git",
"version": "0.0.9",
"version": "0.1.0",
"main": "./src/Filesystem.js",
"type": "module",
"publishConfig": {
Expand Down
45 changes: 36 additions & 9 deletions packages/bucket-source-filesystem/src/Filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import slugify from '@sindresorhus/slugify'

export const FileSystem = ({ bucketPath }) => {
const _defaultFileExtension = 'md';
const _root = [(bucketPath || './')];
const _root = bucketPath || './';
const _slugifyReplacements = [
['ü', 'u'],
['Ü', 'u'],
Expand All @@ -19,9 +19,9 @@ export const FileSystem = ({ bucketPath }) => {
['Ç', 'c'],
['&', ''],
];
const open = async({ filename }) => {
const open = async(filename) => {
try {
return await fs.readFile(path.join(..._root, filename), 'utf8');
return await fs.readFile(path.join(_root, filename), 'utf8');
}
catch(e) {
console.error(e);
Expand All @@ -30,14 +30,18 @@ export const FileSystem = ({ bucketPath }) => {
}
const list = async ({ collection, locale, omitBody = true }) => {
try {
const filenames = await fs.readdir(path.join(..._root, (locale ?? ''), 'collections', collection));
const filenames = await fs.readdir(
_constructPath({ root: _root, collection, locale })
);
const filteredFiles = filenames.filter(filename => filename.split('.')[1] === _defaultFileExtension);
const filePromises = filteredFiles.map(async (filename) => {
const file = await open({ filename: path.join((locale ?? ''), 'collections', collection, filename) });
const frontMatter = _extractFrontMatter(file);
const file = await open(
_constructPath({ locale, collection, filename })
);
if(file === undefined) {
throw new Error('Failed to get file contents or types got mixed up.');
}
const frontMatter = _extractFrontMatter(file);
return {
_id: slugify(filename.replace('.' + _defaultFileExtension, ''), { customReplacements: _slugifyReplacements, decamelize: false }),
_slug: slugify(frontMatter.title || '', { customReplacements: _slugifyReplacements, decamelize: false }),
Expand All @@ -52,16 +56,39 @@ export const FileSystem = ({ bucketPath }) => {
throw new Error('One or more paths not found.');
}
}
const get = async({ filename, locale }) => {
let file = await open({ filename: path.join((locale ?? ''), 'singles', [filename, _defaultFileExtension].join('.')) });
const get = async({ single, locale }) => {
let file = await open(
_constructPath({ locale, single })
);
const frontMatter = _extractFrontMatter(file);
return {
_id: slugify(filename.replace('.' + _defaultFileExtension, ''), { customReplacements: _slugifyReplacements, decamelize: false }),
_id: slugify(single, { customReplacements: _slugifyReplacements, decamelize: false }),
_slug: slugify(frontMatter.title || '', { customReplacements: _slugifyReplacements, decamelize: false }),
...frontMatter,
..._extractBody(file),
}
}
const _constructPath = ({ root, collection, single, filename, locale }) => {
let language, country;
let fragments = [];
if(root !== undefined) fragments.push(root);
if(locale !== undefined) {
// eslint-disable-next-line no-unused-vars
[language, country] = locale.split('-');
fragments.push(language);
}
if(collection !== undefined) {
fragments.push('collections', collection);
if (filename !== undefined) fragments.push(filename);
}
else if(single !== undefined) {
fragments.push('singles', [single, _defaultFileExtension].join('.'));
}
else {
throw new Error('Misformed file path.')
}
return path.join(...fragments);
}
const _extractBody = (file) => {
const body = file.split('---')[2];
if(body === undefined) throw new Error('Can not extract body, file may be formatted incorrectly.');
Expand Down
6 changes: 6 additions & 0 deletions packages/bucket-source-github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @gokceno/crux-bucket-source-filesystem

## 0.1.0

### Minor Changes

- Implemented locale based folder/file scanning.

## 0.0.9

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/bucket-source-github/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gokceno/crux-bucket-source-github",
"repository": "git+https://github.com/gokceno/crux.md.git",
"version": "0.0.9",
"version": "0.1.0",
"main": "./src/GitHub.js",
"type": "module",
"publishConfig": {
Expand Down
12 changes: 6 additions & 6 deletions packages/bucket-source-github/src/GitHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const GitHub = ({ owner, repo, basePath = '', auth, headers = { 'X-GitHub
['Ç', 'c'],
['&', ''],
];
const open = async({ filename }) => {
const open = async(filename) => {
try {
const response = await _octokit.request(`GET /repos/${owner}/${repo}/contents/${path.join(basePath, filename)}`, {
owner,
Expand Down Expand Up @@ -47,7 +47,7 @@ export const GitHub = ({ owner, repo, basePath = '', auth, headers = { 'X-GitHub
headers
});
const promises = response?.data?.filter(d => d.name.split('.')[1] === _defaultFileExtension).map(async (file) => {
const fileContents = await open({ filename: path.join((locale ?? ''), 'collections', collection, file.name) });
const fileContents = await open(path.join((locale ?? ''), 'collections', collection, file.name));
const frontMatter = _extractFrontMatter(fileContents);
return {
_id: slugify(file.name.replace('.' + _defaultFileExtension, ''), { customReplacements: _slugifyReplacements, decamelize: false }),
Expand All @@ -64,13 +64,13 @@ export const GitHub = ({ owner, repo, basePath = '', auth, headers = { 'X-GitHub
throw new Error('Generic error.');
}
}
const get = async({ filename, locale }) => {
let finalPath = ['singles', [filename, _defaultFileExtension].join('.')];
const get = async({ single, locale }) => {
let finalPath = ['singles', [single, _defaultFileExtension].join('.')];
if(locale !== undefined) finalPath.unshift(locale);
const fileContents = await open({ filename: path.join(...finalPath) });
const fileContents = await open(path.join(...finalPath));
const frontMatter = _extractFrontMatter(fileContents);
return {
_id: slugify(filename.replace('.' + _defaultFileExtension, ''), { customReplacements: _slugifyReplacements, decamelize: false }),
_id: slugify(single, { customReplacements: _slugifyReplacements, decamelize: false }),
_slug: slugify(frontMatter.title || '', { customReplacements: _slugifyReplacements, decamelize: false }),
...frontMatter,
..._extractBody(fileContents),
Expand Down
6 changes: 6 additions & 0 deletions packages/bucket/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @gokceno/crux-bucket

## 1.2.0

### Minor Changes

- Implemented locale based folder/file scanning.

## 1.1.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/bucket/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@gokceno/crux-bucket",
"repository": "git+https://github.com/gokceno/crux.md.git",
"version": "1.1.6",
"version": "1.2.0",
"main": "./src/Bucket.js",
"type": "module",
"publishConfig": {
Expand Down
8 changes: 4 additions & 4 deletions packages/bucket/src/Bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ export const Bucket = () => {
const fetch = async (params) => {
_cache.setManifest(params.manifest);
if(_collection !== undefined) return _fetchCollection({ cache: _cache, source: _source, collection: _collection, order: _order, locale: _locale, filters: _filters, expansions: _expansions, manifest: params.manifest, ...params.limit });
if(_single !== undefined) return _fetchSingle({ cache: _cache, source: _source, single: _single, expansions: _expansions, ...params });
if(_single !== undefined) return _fetchSingle({ cache: _cache, source: _source, single: _single, expansions: _expansions, locale: _locale, ...params });
throw new Error('Select failed.');
}
const manifest = async() => {
// TODO: handle parse errors
if(_cache === undefined) return YAML.parse(await _source.open({ filename: 'manifest.yml' }));
if(_cache === undefined) return YAML.parse(await _source.open('manifest.yml'));
if(!_cache.isCached({ isManifest: true })) {
return _cache.populate({ isManifest: true, data: YAML.parse(await _source.open({ filename: 'manifest.yml' }))});
return _cache.populate({ isManifest: true, data: YAML.parse(await _source.open('manifest.yml'))});
}
return _cache.get({ isManifest: true });
}
Expand Down Expand Up @@ -115,7 +115,7 @@ export const Bucket = () => {
const _fetchSingle = async (params) => {
const { manifest, cache, source, single, locale, expansions } = params;
if (!cache.isCached({ single, locale })) {
let sourceData = await source.get({ locale, filename: single });
let sourceData = await source.get({ locale, single });
const [ data ] = await Promise.all(expansions.map(async (expansion) => { // FIXME: What if more than one expansion??
if(typeof Object.values(expansion)[0] === 'string') {
return await _handleStringExpansion(expansion, sourceData, manifest, cache);
Expand Down

0 comments on commit 0eece16

Please sign in to comment.