Skip to content

Commit

Permalink
feat: 🎸 add new template identity
Browse files Browse the repository at this point in the history
  • Loading branch information
lenconda committed Sep 15, 2021
1 parent df5f24f commit ebf56c7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
6 changes: 4 additions & 2 deletions packages/dollie-core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const TEMPLATE_CACHE_PATHNAME_PREFIX = '/template';
const EXTEND_TEMPLATE_LABEL_PREFIX = 'extend:';
const EXTEND_TEMPLATE_PATHNAME_PREFIX = '/extends';
const MAIN_TEMPLATE_PATHNAME_PREFIX = '/main';
const TEMPLATE_FILE_IDENTITY = '__template.';
const TEMPLATE_FILE_IDENTITY_LEGACY = '__template.';
const TEMPLATE_FILE_IDENTITY = '.dollie';
const TEMPLATE_CONFIG_FILE_NAMES = [
'dollie.js',
'.dollie.js',
Expand All @@ -16,7 +17,8 @@ export {
EXTEND_TEMPLATE_LABEL_PREFIX,
EXTEND_TEMPLATE_PATHNAME_PREFIX,
MAIN_TEMPLATE_PATHNAME_PREFIX,
TEMPLATE_FILE_IDENTITY,
TEMPLATE_FILE_IDENTITY_LEGACY,
TEMPLATE_CONFIG_FILE_NAMES,
EXTEND_TEMPLATE_PREFIX,
TEMPLATE_FILE_IDENTITY,
};
8 changes: 0 additions & 8 deletions packages/dollie-core/src/generator.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,6 @@ abstract class Generator {
}
}

private checkFile(pathname: string): boolean {
const absolutePathname = path.resolve(TEMPLATE_CACHE_PATHNAME_PREFIX, pathname);
return (
this.volume.existsSync(absolutePathname) &&
this.volume.statSync(absolutePathname).isFile()
);
}

private readTemplateFileBuffer(pathname: string): Buffer {
return this.volume.readFileSync(path.resolve(
TEMPLATE_CACHE_PATHNAME_PREFIX,
Expand Down
16 changes: 14 additions & 2 deletions packages/dollie-core/src/utils/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import fs from 'fs';
import {
TEMPLATE_CACHE_PATHNAME_PREFIX,
TEMPLATE_FILE_IDENTITY_LEGACY,
TEMPLATE_FILE_IDENTITY,
} from '../constants';
import {
Expand Down Expand Up @@ -98,9 +99,20 @@ const readTemplateEntities = (
let relativePathname = initialRelativePathname;
let absolutePathname = initialAbsolutePathname;

if (initialEntityName.startsWith(TEMPLATE_FILE_IDENTITY)) {
if (
initialEntityName.startsWith(TEMPLATE_FILE_IDENTITY_LEGACY) ||
initialEntityName.endsWith(TEMPLATE_FILE_IDENTITY)
) {
isTemplateFile = true;
entityName = initialEntityName.slice(TEMPLATE_FILE_IDENTITY.length);
}

if (initialEntityName.startsWith(TEMPLATE_FILE_IDENTITY_LEGACY)) {
entityName = initialEntityName.slice(TEMPLATE_FILE_IDENTITY_LEGACY.length);
} else if (initialEntityName.endsWith(TEMPLATE_FILE_IDENTITY)) {
entityName = initialEntityName.slice(0, initialEntityName.length - TEMPLATE_FILE_IDENTITY.length);
}

if (isTemplateFile) {
relativePathname = `${relativeDirectoryPathname ? `${relativeDirectoryPathname}/` : ''}${entityName}`;
absolutePathname = `${absoluteDirectoryPathname ? `${absoluteDirectoryPathname}/` : ''}${entityName}`;
}
Expand Down

0 comments on commit ebf56c7

Please sign in to comment.