refactor: use namespace as blueprint for folder structure

pull/5246/head
Arman Ozak 5 years ago
parent 19a676a569
commit ebaba63a88

@ -18,13 +18,12 @@ export function generateBarrelFromPath(tree: Tree, indexPath: string) {
const saveFile = createFileSaver(tree); const saveFile = createFileSaver(tree);
const dir = tree.getDir(indexPath); const dir = tree.getDir(indexPath);
let _exports = ''; const _exports: string[] = [];
dir.subfiles.forEach(fragment => { dir.subfiles.forEach(fragment => {
if (!fragment.endsWith('.ts')) return; if (!fragment.endsWith('.ts')) return;
_exports += `export * from './${fragment.replace(/\.ts$/, '')}'; _exports.push(`export * from './${fragment.replace(/\.ts$/, '')}';`);
`;
}); });
dir.subdirs.forEach(fragment => { dir.subdirs.forEach(fragment => {
@ -34,10 +33,16 @@ export function generateBarrelFromPath(tree: Tree, indexPath: string) {
subDir.visit(() => (hasFiles = true)); subDir.visit(() => (hasFiles = true));
if (!hasFiles) return; if (!hasFiles) return;
_exports += `export * as ${strings.classify(fragment)} from './${fragment}'; _exports.push(`export * as ${strings.classify(fragment)} from './${fragment}';`);
`;
generateBarrelFromPath(tree, subDirPath); generateBarrelFromPath(tree, subDirPath);
}); });
if (_exports) saveFile(indexPath + '/index.ts', _exports); _exports.sort();
if (_exports.length)
saveFile(
indexPath + '/index.ts',
_exports.join(`
`),
);
} }

@ -13,7 +13,7 @@ export interface EnumGeneratorParams {
} }
export function isEnumImport(path: string) { export function isEnumImport(path: string) {
return path.includes('/enums/'); return path.endsWith('.enum');
} }
export function getEnumNamesFromImports(serviceImports: Record<string, string[]>) { export function getEnumNamesFromImports(serviceImports: Record<string, string[]>) {

@ -1,15 +1,33 @@
import { dir, kebab } from './text'; import { strings } from '@angular-devkit/core';
import { kebab } from './text';
export function relativePathToEnum(namespace: string, enumNamespace: string, enumName: string) { export function relativePathToEnum(namespace: string, enumNamespace: string, enumName: string) {
const repeats = namespace ? namespace.split('.').length : 0; const path = calculateRelativePath(namespace, enumNamespace);
const path = '..' + '/..'.repeat(repeats) + '/enums/' + dir(enumNamespace); return removeDoubleSlash(path + `/${kebab(enumName)}.enum`);
return removeDoubleSlash(path + '/' + kebab(enumName));
} }
export function relativePathToModel(namespace: string, modelNamespace: string) { export function relativePathToModel(namespace: string, modelNamespace: string) {
const repeats = namespace ? namespace.split('.').length : 0; const path = calculateRelativePath(namespace, modelNamespace);
const path = '..' + '/..'.repeat(repeats) + '/models/' + dir(modelNamespace); return removeTrailingSlash(path + '/models');
return removeTrailingSlash(path); }
function calculateRelativePath(ns1: string, ns2: string) {
if (ns1 === ns2) return '.';
const parts1 = ns1 ? ns1.split('.') : [];
const parts2 = ns2 ? ns2.split('.') : [];
while (parts1.length && parts2.length) {
if (parts1[0] !== parts2[0]) break;
parts1.shift();
parts2.shift();
}
const up = '../'.repeat(parts1.length) || '.';
const down = parts2.reduce((acc, p) => acc + '/' + strings.dasherize(p), '');
return removeTrailingSlash(removeDoubleSlash(up + down));
} }
function removeDoubleSlash(path: string) { function removeDoubleSlash(path: string) {

Loading…
Cancel
Save