@angular-devkit/core#Path TypeScript Examples

The following examples show how to use @angular-devkit/core#Path. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: find-module.ts    From router with MIT License 6 votes vote down vote up
/**
 * Find the module referred by a set of options passed to the schematics.
 */
export function findModuleFromOptions(
  host: Tree,
  options: ModuleOptions
): Path | undefined {
  if (options.hasOwnProperty('skipImport') && options.skipImport) {
    return undefined;
  }

  if (!options.module) {
    const pathToCheck =
      (options.path || '') +
      (options.flat ? '' : '/' + strings.dasherize(options.name as string));

    return normalize(findModule(host, pathToCheck));
  } else {
    const modulePath = normalize('/' + options.path + '/' + options.module);
    const moduleBaseName = normalize(modulePath).split('/').pop();

    if (host.exists(modulePath)) {
      return normalize(modulePath);
    } else if (host.exists(modulePath + '.ts')) {
      return normalize(modulePath + '.ts');
    } else if (host.exists(modulePath + '.module.ts')) {
      return normalize(modulePath + '.module.ts');
    } else if (host.exists(modulePath + '/' + moduleBaseName + '.module.ts')) {
      return normalize(modulePath + '/' + moduleBaseName + '.module.ts');
    } else {
      throw new Error(`Specified module path ${modulePath} does not exist`);
    }
  }
}
Example #2
Source File: find-module.ts    From router with MIT License 6 votes vote down vote up
/**
 * Function to find the "closest" module to a generated file's path.
 */
export function findModule(host: Tree, generateDir: string): Path {
  let dir: DirEntry | null = host.getDir('/' + generateDir);

  const moduleRe = /\.module\.ts$/;
  const routingModuleRe = /-routing\.module\.ts/;

  while (dir) {
    const matches = dir.subfiles.filter(
      (p) => moduleRe.test(p) && !routingModuleRe.test(p)
    );

    if (matches.length === 1) {
      return join(dir.path, matches[0]);
    } else if (matches.length > 1) {
      throw new Error(
        'More than one module matches. Use skip-import option to skip importing ' +
          'the component into the closest module.'
      );
    }

    dir = dir.parent;
  }

  throw new Error(
    'Could not find an NgModule. Use the skip-import ' +
      'option to skip importing in NgModule.'
  );
}
Example #3
Source File: find-module.ts    From fab-menu with MIT License 6 votes vote down vote up
/**
 * Function to find the "closest" module to a generated file's path.
 */
export function findModule(
  host: Tree,
  generateDir: string,
  moduleExt = MODULE_EXT,
  routingModuleExt = ROUTING_MODULE_EXT
): Path {
  return originalFindModule(host, generateDir, moduleExt, routingModuleExt);
}
Example #4
Source File: parsers.ts    From form-schematic with MIT License 6 votes vote down vote up
parseName = (
	path: string,
	name: string
): { name: string; path: Path } => {
	const nameWithoutPath = basename(name as Path);
	const namePath = dirname((path + '/' + name) as Path);

	return {
		name: dasherize(nameWithoutPath),
		path: normalize('/' + namePath)
	};
}
Example #5
Source File: find-module.ts    From edit-in-place with MIT License 6 votes vote down vote up
/**
 * Function to find the "closest" module to a generated file's path.
 */
export function findModule(
  host: Tree,
  generateDir: string,
  moduleExt = MODULE_EXT,
  routingModuleExt = ROUTING_MODULE_EXT
): Path {
  let dir: DirEntry | null = host.getDir('/' + generateDir);
  let foundRoutingModule = false;

  while (dir) {
    const allMatches = dir.subfiles.filter(p => p.endsWith(moduleExt));
    const filteredMatches = allMatches.filter(p => !p.endsWith(routingModuleExt));

    foundRoutingModule = foundRoutingModule || allMatches.length !== filteredMatches.length;

    if (filteredMatches.length === 1) {
      return join(dir.path, filteredMatches[0]);
    } else if (filteredMatches.length > 1) {
      throw new Error(
        'More than one module matches. Use skip-import option to skip importing ' +
          'the component into the closest module.'
      );
    }

    dir = dir.parent;
  }

  const errorMsg = foundRoutingModule
    ? 'Could not find a non Routing NgModule.' +
      `\nModules with suffix '${routingModuleExt}' are strictly reserved for routing.` +
      '\nUse the skip-import option to skip importing in NgModule.'
    : 'Could not find an NgModule. Use the skip-import option to skip importing in NgModule.';
  throw new Error(errorMsg);
}
Example #6
Source File: dynamic-watch-entry.plugin.ts    From angular-miniprogram with MIT License 6 votes vote down vote up
constructor(
    private options: {
      pages: AssetPattern[];
      components: AssetPattern[];
      workspaceRoot: Path;
      context: BuilderContext;
      config: webpack.Configuration;
    },
    private buildPlatform: BuildPlatform
  ) {}
Example #7
Source File: index.ts    From angular-miniprogram with MIT License 6 votes vote down vote up
async getFileList(dirPath: Path): Promise<string[]> {
    const fileList: string[] = [];
    const list = await this.list(dirPath).toPromise();
    for (let i = 0; i < list.length; i++) {
      const element = list[i];
      const filePath = join(dirPath, element);
      if (await this.isDirectory(filePath).toPromise()) {
        fileList.push(...(await this.getFileList(filePath)));
      } else {
        fileList.push(filePath);
      }
    }
    return fileList;
  }
Example #8
Source File: find-module.ts    From fab-menu with MIT License 5 votes vote down vote up
/**
 * Find the module referred by a set of options passed to the schematics.
 */
export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path | undefined {
  return originalFindModuleFromOptions(host, options);
}
Example #9
Source File: find-module.ts    From edit-in-place with MIT License 5 votes vote down vote up
/**
 * Find the module referred by a set of options passed to the schematics.
 */
export function findModuleFromOptions(host: Tree, options: any, projectPath: any): Path | undefined {
  if (options.hasOwnProperty('skipImport') && options.skipImport) {
    return undefined;
  }

  const moduleExt = options.moduleExt || MODULE_EXT;
  const routingModuleExt = options.routingModuleExt || ROUTING_MODULE_EXT;

  if (!options.module) {
    const pathToCheck = (projectPath || '') + '/' + options.name;
    const module = findModule(host, pathToCheck, moduleExt, routingModuleExt);
    return module ? normalize(module) : undefined;
  } else {
    const modulePath = normalize(`/${projectPath}/${options.module}`);
    const componentPath = normalize(`/${projectPath}/${options.name}`);
    const moduleBaseName = normalize(modulePath)
      .split('/')
      .pop();

    const candidateSet = new Set<Path>([normalize(projectPath || '/')]);

    for (let dir = modulePath; dir !== NormalizedRoot; dir = dirname(dir)) {
      candidateSet.add(dir);
    }
    for (let dir = componentPath; dir !== NormalizedRoot; dir = dirname(dir)) {
      candidateSet.add(dir);
    }

    const candidatesDirs = [...candidateSet].sort((a, b) => b.length - a.length);
    for (const c of candidatesDirs) {
      const candidateFiles = ['', `${moduleBaseName}.ts`, `${moduleBaseName}${moduleExt}`].map(x => join(c, x));

      for (const sc of candidateFiles) {
        if (host.exists(sc)) {
          return normalize(sc);
        }
      }
    }
    throw new Error(
      `Specified module '${options.module}' does not exist.\n` +
        `Looked in the following directories:\n    ${candidatesDirs.join('\n    ')}`
    );
  }
}
Example #10
Source File: dynamic-watch-entry.plugin.ts    From angular-miniprogram with MIT License 5 votes vote down vote up
absoluteProjectRoot!: Path;
Example #11
Source File: dynamic-watch-entry.plugin.ts    From angular-miniprogram with MIT License 5 votes vote down vote up
absoluteProjectSourceRoot!: Path;