Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function buildRelativePath(from: string, to: string): string {
from = normalize(from);
to = normalize(to);
// Convert to arrays.
const fromParts = from.split('/');
const toParts = to.split('/');
// Remove file names (preserving destination)
fromParts.pop();
const toFileName = toParts.pop();
const relativePath = relative(normalize(fromParts.join('/') || '/'),
normalize(toParts.join('/') || '/'));
let pathPrefix = '';
// Set the path prefix for same dir or child dir, parent dir starts with `..`
if (!relativePath) {
pathPrefix = '.';
} else if (!relativePath.startsWith('.')) {
pathPrefix = `./`;
}
if (pathPrefix && !pathPrefix.endsWith('/')) {
pathPrefix += '/';
}
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
}
export function buildRelativePath(from: string, to: string): string {
from = normalize(from);
to = normalize(to);
// Convert to arrays.
const fromParts = from.split('/');
const toParts = to.split('/');
// Remove file names (preserving destination)
fromParts.pop();
const toFileName = toParts.pop();
const relativePath = relative(normalize(fromParts.join('/')), normalize(toParts.join('/')));
let pathPrefix = '';
// Set the path prefix for same dir or child dir, parent dir starts with `..`
if (!relativePath) {
pathPrefix = '.';
} else if (!relativePath.startsWith('.')) {
pathPrefix = `./`;
}
if (pathPrefix && !pathPrefix.endsWith('/')) {
pathPrefix += '/';
}
return pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName;
}
concatMap(from => {
const to = join(this.root(), relative(this._templateRoot, from));
return this.read(from).pipe(
concatMap(buffer => this.write(to, buffer)),
);
}),
map(() => { }),
styleext: options.style,
viewEncapsulation: options.viewEncapsulation,
};
const workspace = config_1.getWorkspace(host);
let newProjectRoot = workspace.newProjectRoot;
let appDir = `${newProjectRoot}/${options.name}`;
let sourceRoot = `${appDir}/src`;
let sourceDir = `${sourceRoot}/app`;
let relativePathToWorkspaceRoot = appDir.split('/').map(x => '..').join('/');
const rootInSrc = options.projectRoot !== undefined;
if (options.projectRoot !== undefined) {
newProjectRoot = options.projectRoot;
appDir = `${newProjectRoot}/src`;
sourceRoot = appDir;
sourceDir = `${sourceRoot}/app`;
relativePathToWorkspaceRoot = core_1.relative(core_1.normalize('/' + sourceRoot), core_1.normalize('/'));
if (relativePathToWorkspaceRoot === '') {
relativePathToWorkspaceRoot = '.';
}
}
const tsLintRoot = appDir;
const e2eOptions = {
name: `${options.name}-e2e`,
relatedAppName: options.name,
rootSelector: appRootSelector,
};
if (options.projectRoot !== undefined) {
e2eOptions.projectRoot = 'e2e';
}
return schematics_1.chain([
addAppToWorkspaceFile(options, workspace),
options.skipPackageJson ? schematics_1.noop() : addDependenciesToPackageJson(),
directory.visit((path, entry) => {
if (entry) {
const rel = core.relative(directory.path, path).replace(dir, renamed);
const originalComponentName = componentName(dir);
const renamedComponentName = componentName(renamed);
const content = entry.content
.toString('utf8')
.replace(new RegExp(dir, 'g'), renamed)
.replace(originalComponentName, renamedComponentName);
const target = core.join(targetDir.path, renamed, rel);
if (tree.exists(target)) {
tree.overwrite(target, content);
}
else {
tree.create(target, content);
moduleContent = moduleContent
.replace(`${dir}/${dir}.component`, `${renamed}/${renamed}.component`)
.replace(new RegExp(originalComponentName, 'g'), renamedComponentName);
}
private async _recursiveList(path: Path, items: string[]): Promise {
const fragments = await this._host.list(path).toPromise();
for (const fragment of fragments) {
const item = join(path, fragment);
if (await this._host.isDirectory(item).toPromise()) {
await this._recursiveList(item, items);
} else {
items.push('/' + relative(normalize(this.base), item));
}
}
return items;
}
}
function buildRelativePath(from, to) {
const { path: fromPath, filename: fromFileName, directory: fromDirectory, } = parsePath(from);
const { path: toPath, filename: toFileName, directory: toDirectory, } = parsePath(to);
const relativePath = core_1.relative(fromDirectory, toDirectory);
const fixedRelativePath = relativePath.startsWith('.')
? relativePath
: `./${relativePath}`;
return !toFileName || toFileName === 'index.ts'
? fixedRelativePath
: `${fixedRelativePath.endsWith('/')
? fixedRelativePath
: fixedRelativePath + '/'}${convertToTypeScriptFileName(toFileName)}`;
}
exports.buildRelativePath = buildRelativePath;
return (host) => {
if (!options.module) {
return host;
}
const modulePath = core_1.normalize('/' + options.module);
const text = host.read(modulePath);
if (text === null) {
throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
}
const sourceText = text.toString('utf-8');
const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
const importModulePath = core_1.normalize(`/${options.sourceDir}/${options.path}/`
+ (options.flat ? '' : stringUtils.dasherize(options.name) + '/')
+ stringUtils.dasherize(options.name)
+ '.module');
const relativeDir = core_1.relative(core_1.dirname(modulePath), core_1.dirname(importModulePath));
const relativePath = (relativeDir.startsWith('.') ? relativeDir : './' + relativeDir)
+ '/' + core_1.basename(importModulePath);
const changes = ast_utils_1.addImportToModule(source, modulePath, stringUtils.classify(`${options.name}Module`), relativePath);
const recorder = host.beginUpdate(modulePath);
for (const change of changes) {
if (change instanceof change_1.InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(recorder);
return host;
};
}
export function importPath(from: Path, to: Path): string {
const relativePath = relative(dirname(from), dirname(to));
if (relativePath.startsWith('.')) {
return relativePath;
}
if (!relativePath) {
return generateCurrentDirImport(basename(to));
}
return generateCurrentDirImport(join(relativePath, basename(to)));
}
public relative(from: Path, to: Path): string {
const placeholder = '/placeholder';
const relativeDir = relative(
dirname((placeholder + from) as Path),
dirname((placeholder + to) as Path),
);
return (relativeDir.startsWith('.')
? relativeDir
: './' + relativeDir
).concat(relativeDir.length === 0 ? basename(to) : '/' + basename(to));
}
}