How to use the @yarnpkg/fslib.xfs.readFilePromise function in @yarnpkg/fslib

To help you get started, we’ve selected a few @yarnpkg/fslib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yarnpkg / berry / packages / yarnpkg-check / sources / cli.ts View on Github external
async function buildJsonNode(p: PortablePath, accesses: Array) {
  const content = await xfs.readFilePromise(p, `utf8`);

  // Just to be sure that it's valid JSON
  JSON.parse(content);

  const sourceFile = await ts.createSourceFile(
    npath.fromPortablePath(p),
    `(${content})`,
    ts.ScriptTarget.ES2015,
    /*setParentNodes */ true,
  );

  // @ts-ignore
  let node: ts.Node = sourceFile.statements[0].expression;
  if (!node)
    throw new Error(`Invalid source tree`);
github yarnpkg / berry / packages / plugin-pack / sources / packUtils.ts View on Github external
const cb = (error: any) => {
        if (error) {
          rejectFn(error);
        } else {
          resolveFn();
        }
      };

      if (stat.isFile()) {
        let content: Buffer;

        // The root package.json supports replacement fields in publishConfig
        if (file === `package.json`)
          content = Buffer.from(JSON.stringify(await genPackageManifest(workspace), null, 2));
        else
          content = await xfs.readFilePromise(source);

        pack.entry({...opts, type: `file`}, content, cb);
      } else if (stat.isSymbolicLink()) {
        pack.entry({...opts, type: `symlink`, linkname: await xfs.readlinkPromise(source)}, cb);
      }

      await awaitTarget;
    }

    pack.finalize();
  });
github yarnpkg / berry / packages / yarnpkg-core / sources / Project.ts View on Github external
private async setupResolutions() {
    this.storedResolutions = new Map();

    this.storedDescriptors = new Map();
    this.storedPackages = new Map();

    const lockfilePath = ppath.join(this.cwd, this.configuration.get(`lockfileFilename`));
    const defaultLanguageName = this.configuration.get(`defaultLanguageName`);

    if (xfs.existsSync(lockfilePath)) {
      const content = await xfs.readFilePromise(lockfilePath, `utf8`);
      const parsed: any = parseSyml(content);

      // Protects against v1 lockfiles
      if (parsed.__metadata) {
        const lockfileVersion = parsed.__metadata.version;

        for (const key of Object.keys(parsed)) {
          if (key === `__metadata`)
            continue;

          const data = parsed[key];
          const locator = structUtils.parseLocator(data.resolution, true);

          const manifest = new Manifest();
          manifest.load(data);
github yarnpkg / berry / packages / yarnpkg-json-proxy / sources / makeUpdater.ts View on Github external
export async function makeUpdater(filename: PortablePath) {
  let indent = `  `;
  let obj;

  if (xfs.existsSync(filename)) {
    const content = await xfs.readFilePromise(filename, `utf8`);
    const indentMatch = content.match(/^[ \t]+/m);

    if (indentMatch)
      indent = indentMatch[0];

    obj = JSON.parse(content || `{}`);
  }

  if (!obj)
    obj = {};

  const tracker = makeTracker(obj);
  const initial = tracker.immutable;

  return {
    open(cb: (value: Object) => void) {
github yarnpkg / berry / packages / yarnpkg-core / sources / Configuration.ts View on Github external
static async findRcFiles(startingCwd: PortablePath) {
    const rcFilename = getRcFilename();
    const rcFiles = [];

    let nextCwd = startingCwd;
    let currentCwd = null;

    while (nextCwd !== currentCwd) {
      currentCwd = nextCwd;

      const rcPath = ppath.join(currentCwd, rcFilename as PortablePath);

      if (xfs.existsSync(rcPath)) {
        const content = await xfs.readFilePromise(rcPath, `utf8`);

        let data;
        try {
          data = parseSyml(content) as any;
        } catch (error) {
          let tip = ``;

          if (content.match(/^\s+(?!-)[^:]+\s+\S+/m))
            tip = ` (in particular, make sure you list the colons after each key name)`;

          throw new UsageError(`Parse error when loading ${rcPath}; please check it's proper Yaml${tip}`);
        }

        rcFiles.push({path: rcPath, cwd: currentCwd, data});
      }
github yarnpkg / berry / packages / plugin-git / sources / GitFetcher.ts View on Github external
async cloneFromRemote(locator: Locator, opts: FetchOptions) {
    const cloneTarget = await gitUtils.clone(locator.reference, opts.project.configuration);

    const packagePath = ppath.join(cloneTarget, `package.tgz` as PortablePath);
    await scriptUtils.prepareExternalProject(cloneTarget, packagePath, {
      configuration: opts.project.configuration,
      report: opts.report,
    });

    const sourceBuffer = await xfs.readFilePromise(packagePath);

    return await miscUtils.releaseAfterUseAsync(async () => {
      return await tgzUtils.convertToZip(sourceBuffer, {
        stripComponents: 1,
        prefixPath: structUtils.getIdentVendorPath(locator),
      });
    });
  }
}
github yarnpkg / berry / packages / yarnpkg-check / sources / cli.ts View on Github external
async function parseFile(p: PortablePath) {
  const content = await xfs.readFilePromise(p, `utf8`);
  if (probablyMinified(content))
    return null;

  return await ts.createSourceFile(
    npath.fromPortablePath(p),
    content,
    ts.ScriptTarget.ES2015,
    /*setParentNodes */ true,
  );
}
github yarnpkg / berry / packages / plugin-patch / sources / patchUtils.ts View on Github external
onAbsolute: async () => {
        return await xfs.readFilePromise(patchPath, `utf8`);
      },