@angular-devkit/core#workspaces TypeScript Examples

The following examples show how to use @angular-devkit/core#workspaces. 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: utility.ts    From ngx-electronify with MIT License 7 votes vote down vote up
function createHost(tree: Tree): workspaces.WorkspaceHost {
  return {
    async readFile(path: string): Promise<string> {
      const data = tree.read(path);
      if (!data) {
        throw new Error('File not found.');
      }

      return virtualFs.fileBufferToString(data);
    },
    async writeFile(path: string, data: string): Promise<void> {
      return tree.overwrite(path, data);
    },
    async isDirectory(path: string): Promise<boolean> {
      // approximate a directory check
      return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
    },
    async isFile(path: string): Promise<boolean> {
      return tree.exists(path);
    }
  };
}
Example #2
Source File: utils.ts    From elements with MIT License 7 votes vote down vote up
function createHost(tree: Tree): workspaces.WorkspaceHost {
  return {
    async readFile(path: string): Promise<string> {
      const data = tree.read(path);
      if (!data) {
        throw new SchematicsException('File not found.');
      }
      return virtualFs.fileBufferToString(data);
    },
    async writeFile(path: string, data: string): Promise<void> {
      return tree.overwrite(path, data);
    },
    async isDirectory(path: string): Promise<boolean> {
      return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
    },
    async isFile(path: string): Promise<boolean> {
      return tree.exists(path);
    },
  };
}
Example #3
Source File: index.ts    From ngx-electronify with MIT License 6 votes vote down vote up
ngAdd =
  (options: NgAddOptions) => async (tree: Tree, _context: SchematicContext) => {
    const { host, workspace } = await getWorkspace(tree);
    const project = workspace.projects.get(options.project);

    if (!project) {
      throw new SchematicsException(
        'The specified Angular project is not defined in this workspace.'
      );
    }

    if (project.extensions.projectType !== 'application') {
      throw new SchematicsException(
        `ngx-electronify requires an Angular project type of "application" in angular.json.`
      );
    }

    // add a new architect entry in the angular.json file of the current project
    project.targets.add({
      name: 'desktop',
      builder: 'ngx-electronify:electron',
      options: {}
    });

    await workspaces.writeWorkspace(workspace, host);
    return tree;
  }
Example #4
Source File: utility.ts    From ngx-electronify with MIT License 6 votes vote down vote up
export async function getWorkspace(tree: Tree, path = '/') {
  const host = createHost(tree);

  const { workspace } = await workspaces.readWorkspace(path, host);

  return { host, workspace };
}
Example #5
Source File: utils.ts    From elements with MIT License 6 votes vote down vote up
export async function getProjectName(tree: Tree) {
  const host = createHost(tree);
  const { workspace } = await workspaces.readWorkspace('/', host);
  return workspace.extensions.defaultProject as string;
}
Example #6
Source File: workspace.ts    From source-map-analyzer with MIT License 6 votes vote down vote up
/* Below code reference is taken from Angular CLI project. 
  https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/workspace.ts

  These methods are not part of public APIs so we should not be referencing those methods.
  that's why added below method here.
*/

function createHost(tree: Tree): workspaces.WorkspaceHost {
    return {
        async readFile(path: string): Promise<string> {
            const data = tree.read(path);
            if (!data) {
                throw new SchematicsException('File not found.');
            }
            return virtualFs.fileBufferToString(data);
        },
        async writeFile(path: string, data: string): Promise<void> {
            return tree.overwrite(path, data);
        },
        async isDirectory(path: string): Promise<boolean> {
            return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
        },
        async isFile(path: string): Promise<boolean> {
            return tree.exists(path);
        },
    };
}
Example #7
Source File: workspace.ts    From source-map-analyzer with MIT License 6 votes vote down vote up
export function updateWorkspace(
    updaterOrWorkspace:
      | workspaces.WorkspaceDefinition
      | ((workspace: workspaces.WorkspaceDefinition) => void | Rule | PromiseLike<void | Rule>),
  ): Rule {
    return async (tree: Tree) => {
      const host = createHost(tree);
  
      if (typeof updaterOrWorkspace === 'function') {
        const { workspace } = await workspaces.readWorkspace('/', host);
  
        const result = await updaterOrWorkspace(workspace);
  
        await workspaces.writeWorkspace(workspace, host);
  
        return result || noop;
      } else {
        await workspaces.writeWorkspace(updaterOrWorkspace, host);
  
        return noop;
      }
    };
  }
Example #8
Source File: workspace.ts    From source-map-analyzer with MIT License 5 votes vote down vote up
export async function getWorkspace(tree: Tree, path = '/') : Promise<workspaces.WorkspaceDefinition> {
    const host = createHost(tree);
    const { workspace } = await workspaces.readWorkspace(path, host);
    return workspace;
}