Skip to content

Commit

Permalink
JSON-like file and specified file object creator
Browse files Browse the repository at this point in the history
  • Loading branch information
vilicvane committed Nov 4, 2023
1 parent ffdc626 commit 90a72fe
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
61 changes: 61 additions & 0 deletions packages/core/src/library/composables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
YAMLFile,
YAMLFileOptions,
} from './files/index.js';
import {JSONLikeFile} from './files/index.js';

export function text(
path: string,
Expand Down Expand Up @@ -88,6 +89,66 @@ export function json(...args: any[]): Composable<JSONFile<unknown>> {
};
}

export type ObjectModuleOptions = {
type: 'module' | 'commonjs';
comment?: string;
};

export function objectModule<TContent>(
path: string,
value: TContent | ComposeFunction<JSONLikeFile<TContent>>,
options: ObjectModuleOptions,
): Composable<JSONLikeFile<TContent>>;
export function objectModule<TContent>(
value: TContent | ComposeFunction<JSONLikeFile<TContent>>,
options: ObjectModuleOptions,
): Composable<JSONLikeFile<TContent>>;
export function objectModule(
...args: any[]
): Composable<JSONLikeFile<unknown>> {
let path: string | undefined;
let value: unknown;
let options: ObjectModuleOptions;

if (typeof args[0] === 'string') {
[path, value, options] = args;
} else {
[value, options] = args;
}

const {type, comment} = options;

let composer: ComposeFunction<JSONLikeFile<unknown>>;

if (typeof value === 'function') {
composer = value as ComposeFunction<JSONLikeFile<unknown>>;
} else {
composer = () => value;
}

let composableType: string;
let before = comment ? `${comment}\n` : '';

if (type === 'module') {
composableType = 'esm-object-module';
before += 'export default ';
} else {
composableType = 'cjs-object-module';
before += 'module.exports = ';
}

return {
type: composableType,
file: (path, context) => new JSONLikeFile(composableType, path, context),
path,
compose: composer,
options: {
before,
after: ';',
},
};
}

export function yaml<TContent>(
path: string,
value: TContent | ComposeFunction<YAMLFile<TContent>>,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/library/file/composable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type {FileObjectCreator} from '../space/index.js';

import type {File, FileGenerics} from './file.js';

export type Composable<TFile extends FileGenerics = FileGenerics> = {
type?: string;
path?: string;
file?: FileObjectCreator;
compose: ComposeFunction<TFile>;
options?: TFile['TComposeOptions'];
};
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/library/files/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './structured-file.js';
export * from './text-file.js';
export * from './binary-file.js';
export * from './yaml-file.js';
export * from './json-like-file.js';
20 changes: 20 additions & 0 deletions packages/core/src/library/files/json-like-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type {JSONFileOptions} from './json-file.js';
import {StructuredFile} from './structured-file.js';

export type JSONLikeFileOptions = JSONFileOptions & {
before?: string;
after?: string;
};

export class JSONLikeFile<TContent> extends StructuredFile<
TContent | undefined,
JSONLikeFileOptions
> {
content: TContent | undefined;

protected stringify(content: TContent | undefined): string {
const {space = 2, before = '', after = ''} = this.options;

return `${before}${JSON.stringify(content, undefined, space)}${after}`;
}
}
1 change: 1 addition & 0 deletions packages/core/src/library/space/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class Context {
outputPath: Path.join(space.dir, Path.relative(this.dir, path)),
},
composable.type,
composable.file,
);

fileMap.set(path, file);
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/library/space/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,18 @@ export class Space {
createFileObject(
path: string,
context: FileContext,
type = this.extensionToFileTypeMap.get(Path.dirname(path)),
type: string | undefined,
creator: FileObjectCreator | undefined,
): File {
if (!type) {
if (type === undefined && !creator) {
throw new Error(
`Cannot infer composable file type from path ${JSON.stringify(path)}`,
);
}

const creator = this.fileObjectCreatorMap.get(type);
if (!creator) {
creator = this.fileObjectCreatorMap.get(type);
}

if (!creator) {
throw new Error(`Unknown file type ${JSON.stringify(type)}`);
Expand Down

0 comments on commit 90a72fe

Please sign in to comment.