fs-extra#stat TypeScript Examples

The following examples show how to use fs-extra#stat. 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: utils.ts    From cli with MIT License 7 votes vote down vote up
removeUselessFiles = async (target: string) => {
  const nm = join(target, 'node_modules');
  const list = await globby(uselessFilesMatch, {
    cwd: nm,
    deep: 10,
  });
  console.log('  - Useless files Count', list.length);
  let size = 0;
  for (const file of list) {
    const path = join(nm, file);
    if (existsSync(path)) {
      const stats = await stat(path);
      size += stats.size;
      await unlink(path);
    }
  }
  console.log(
    `  - Remove Useless file ${Number(size / (2 << 19)).toFixed(2)} MB`
  );
}
Example #2
Source File: log.ts    From backstage with Apache License 2.0 6 votes vote down vote up
export async function recursiveReadDir(dir: string): Promise<string[]> {
  const subdirs = await readdir(dir);
  const files = await Promise.all(
    subdirs.map(async subdir => {
      const res = join(dir, subdir);
      return (await stat(res)).isDirectory() ? recursiveReadDir(res) : [res];
    }),
  );
  return files.reduce((a, f) => a.concat(f), []);
}
Example #3
Source File: to-node.ts    From mordred with MIT License 6 votes vote down vote up
export async function fileToNode(filename: string, cwd: string, getType: typeof GetType): Promise<FileNode> {
  const absolutePath = join(cwd, filename)
  const content = await readFile(absolutePath, 'utf8')
  const { ctime, mtime } = await stat(absolutePath)
  return {
    id: getFileNodeId(filename),
    type: FILE_NODE_TYPE,
    mime: getType(extname(filename)),
    createdAt: ctime,
    updatedAt: mtime,    
    content,
    relativePath: filename,
    absolutePath,
    slug: filename.replace(/\.[a-zA-Z0-9]+$/, '')
  }
}
Example #4
Source File: io.ts    From DefinitelyTyped-tools with MIT License 6 votes vote down vote up
/**
 * Work around a bug where directories bundled on Windows do not have executable permission when extracted on Linux.
 * https://github.com/npm/node-tar/issues/7#issuecomment-17572926
 */
function addDirectoryExecutablePermission(_: string, stat: Stats): boolean {
  if (stat.isDirectory()) {
    stat.mode = addExecutePermissionsFromReadPermissions(stat.mode);
  }
  return true;
}
Example #5
Source File: index.ts    From DefinitelyTyped-tools with MIT License 5 votes vote down vote up
async function runTests(
  dirPath: string,
  onlyTestTsNext: boolean,
  expectOnly: boolean,
  tsLocal: string | undefined
): Promise<void> {
  const isOlderVersion = /^v(0\.)?\d+$/.test(basename(dirPath));

  const indexText = await readFile(joinPaths(dirPath, "index.d.ts"), "utf-8");
  // If this *is* on DefinitelyTyped, types-publisher will fail if it can't parse the header.
  const dt = indexText.includes("// Type definitions for");
  if (dt) {
    // Someone may have copied text from DefinitelyTyped to their type definition and included a header,
    // so assert that we're really on DefinitelyTyped.
    const dtRoot = findDTRoot(dirPath);
    const packageName = basename(dirPath);
    assertPathIsInDefinitelyTyped(dirPath, dtRoot);
    assertPathIsNotBanned(packageName);
    assertPackageIsNotDeprecated(packageName, await readFile(joinPaths(dtRoot, "notNeededPackages.json"), "utf-8"));
  }

  const typesVersions = await mapDefinedAsync(await readdir(dirPath), async (name) => {
    if (name === "tsconfig.json" || name === "tslint.json" || name === "tsutils") {
      return undefined;
    }
    const version = withoutPrefix(name, "ts");
    if (version === undefined || !(await stat(joinPaths(dirPath, name))).isDirectory()) {
      return undefined;
    }

    if (!TypeScriptVersion.isTypeScriptVersion(version)) {
      throw new Error(`There is an entry named ${name}, but ${version} is not a valid TypeScript version.`);
    }
    if (!TypeScriptVersion.isRedirectable(version)) {
      throw new Error(`At ${dirPath}/${name}: TypeScript version directories only available starting with ts3.1.`);
    }
    return version;
  });

  if (dt) {
    await checkPackageJson(dirPath, typesVersions);
  }

  const minVersion = maxVersion(
    getMinimumTypeScriptVersionFromComment(indexText),
    TypeScriptVersion.lowest
  ) as TypeScriptVersion;
  if (onlyTestTsNext || tsLocal) {
    const tsVersion = tsLocal ? "local" : TypeScriptVersion.latest;
    await testTypesVersion(dirPath, tsVersion, tsVersion, isOlderVersion, dt, expectOnly, tsLocal, /*isLatest*/ true);
  } else {
    // For example, typesVersions of [3.2, 3.5, 3.6] will have
    // associated ts3.2, ts3.5, ts3.6 directories, for
    // <=3.2, <=3.5, <=3.6 respectively; the root level is for 3.7 and above.
    // so this code needs to generate ranges [lowest-3.2, 3.3-3.5, 3.6-3.6, 3.7-latest]
    const lows = [TypeScriptVersion.lowest, ...typesVersions.map(next)];
    const his = [...typesVersions, TypeScriptVersion.latest];
    assert.strictEqual(lows.length, his.length);
    for (let i = 0; i < lows.length; i++) {
      const low = maxVersion(minVersion, lows[i]);
      const hi = his[i];
      assert(
        parseFloat(hi) >= parseFloat(low),
        `'// Minimum TypeScript Version: ${minVersion}' in header skips ts${hi} folder.`
      );
      const isLatest = hi === TypeScriptVersion.latest;
      const versionPath = isLatest ? dirPath : joinPaths(dirPath, `ts${hi}`);
      if (lows.length > 1) {
        console.log("testing from", low, "to", hi, "in", versionPath);
      }
      await testTypesVersion(versionPath, low, hi, isOlderVersion, dt, expectOnly, undefined, isLatest);
    }
  }
}
Example #6
Source File: io.ts    From DefinitelyTyped-tools with MIT License 5 votes vote down vote up
export async function isDirectory(path: string): Promise<boolean> {
  return (await stat(path)).isDirectory();
}
Example #7
Source File: MacUpdater.ts    From electron-differential-updater with MIT License 4 votes vote down vote up
protected doDownloadUpdate(
    downloadUpdateOptions: DownloadUpdateOptions
  ): Promise<Array<string>> {
    this.updateInfoForPendingUpdateDownloadedEvent = null;
    const provider = downloadUpdateOptions.updateInfoAndProvider.provider;
    const files = downloadUpdateOptions.updateInfoAndProvider.provider.resolveFiles(
      downloadUpdateOptions.updateInfoAndProvider.info
    );
    const zipFileInfo = findFile(files, "zip", ["pkg", "dmg"]);
    if (zipFileInfo == null) {
      throw newError(
        `ZIP file not provided: ${safeStringifyJson(files)}`,
        "ERR_UPDATER_ZIP_FILE_NOT_FOUND"
      );
    }
    const server = createServer();
    server.on("close", () => {
      this._logger.info(
        `Proxy server for native Squirrel.Mac is closed (was started to download ${zipFileInfo.url.href})`
      );
    });
    function getServerUrl(): string {
      const address = server.address() as AddressInfo;
      return `http://127.0.0.1:${address.port}`;
    }
    return this.executeDownload({
      fileExtension: "zip",
      fileInfo: zipFileInfo,
      downloadUpdateOptions,
      task: async (destinationFile, downloadOptions) => {
        try {
          if (
            await this.differentialDownloadInstaller(
              zipFileInfo,
              downloadUpdateOptions,
              destinationFile,
              provider
            )
          ) {
            await this.httpExecutor.download(
              zipFileInfo.url,
              destinationFile,
              downloadOptions
            );
          }
        } catch (e) {
          console.log(e);
        }
      },
      done: async event => {
        const downloadedFile = event.downloadedFile;
        this.updateInfoForPendingUpdateDownloadedEvent = event;
        let updateFileSize = zipFileInfo.info.size;
        if (updateFileSize == null) {
          updateFileSize = (await stat(downloadedFile)).size;
        }

        return await new Promise<Array<string>>((resolve, reject) => {
          // insecure random is ok
          const fileUrl =
            "/" + Date.now() + "-" + Math.floor(Math.random() * 9999) + ".zip";
          server.on(
            "request",
            (request: IncomingMessage, response: ServerResponse) => {
              const requestUrl = request.url!!;
              this._logger.info(`${requestUrl} requested`);
              if (requestUrl === "/") {
                const data = Buffer.from(
                  `{ "url": "${getServerUrl()}${fileUrl}" }`
                );
                response.writeHead(200, {
                  "Content-Type": "application/json",
                  "Content-Length": data.length
                });
                response.end(data);
                return;
              }

              if (!requestUrl.startsWith(fileUrl)) {
                this._logger.warn(`${requestUrl} requested, but not supported`);
                response.writeHead(404);
                response.end();
                return;
              }

              this._logger.info(
                `${fileUrl} requested by Squirrel.Mac, pipe ${downloadedFile}`
              );

              let errorOccurred = false;
              response.on("finish", () => {
                try {
                  setImmediate(() => server.close());
                } finally {
                  if (!errorOccurred) {
                    this.nativeUpdater.removeListener("error", reject);
                    resolve([]);
                  }
                }
              });

              const readStream = createReadStream(downloadedFile);
              readStream.on("error", error => {
                try {
                  response.end();
                } catch (e) {
                  this._logger.warn(`cannot end response: ${e}`);
                }
                errorOccurred = true;
                this.nativeUpdater.removeListener("error", reject);
                reject(new Error(`Cannot pipe "${downloadedFile}": ${error}`));
              });

              response.writeHead(200, {
                "Content-Type": "application/zip",
                "Content-Length": updateFileSize
              });
              readStream.pipe(response);
            }
          );
          server.listen(0, "127.0.0.1", () => {
            this.nativeUpdater.setFeedURL({
              url: getServerUrl(),
              headers: { "Cache-Control": "no-cache" }
            });

            this.nativeUpdater.once("error", reject);
            this.nativeUpdater.checkForUpdates();
          });
        });
      }
    });
  }
Example #8
Source File: integration.test.ts    From cardano-launcher with Apache License 2.0 4 votes vote down vote up
describe('Starting cardano-wallet (and its node)', () => {
  beforeEach(before);
  afterEach(after);

  // eslint-disable-next-line jest/expect-expect
  it(
    'cardano-wallet responds to requests',
    () =>
      launcherTest(stateDir => {
        return {
          stateDir,
          networkName: 'testnet',
          nodeConfig: {
            kind: 'shelley',
            configurationDir: getShelleyConfigDir('testnet'),
            network: cardanoNode.networks.testnet,
          },
        };
      }),
    longTestTimeoutMs
  );

  it(
    'emits one and only one exit event',
    async () => {
      const launcher = await setupTestLauncher(stateDir => {
        return {
          stateDir,
          networkName: 'testnet',
          nodeConfig: {
            kind: 'shelley',
            configurationDir: getShelleyConfigDir('testnet'),
            network: cardanoNode.networks.testnet,
          },
        };
      });

      const events: ExitStatus[] = [];
      launcher.walletBackend.events.on('exit', st => events.push(st));

      await launcher.start();
      await Promise.all([
        launcher.stop(testsStopTimeout),
        launcher.stop(testsStopTimeout),
        launcher.stop(testsStopTimeout),
      ]);
      await launcher.stop(testsStopTimeout);

      expect(events).toHaveLength(1);
    },
    veryLongTestTimeoutMs
  );

  it(
    'accepts WriteStreams to pipe each child process stdout and stderr streams',
    () =>
      withMainnetConfigDir(async configurationDir => {
        const walletLogFile = await tmp.file();
        const nodeLogFile = await tmp.file();
        const launcher = new Launcher({
          stateDir: (
            await tmp.dir({
              unsafeCleanup: true,
              prefix: 'launcher-integration-test-',
            })
          ).path,
          networkName: 'testnet',
          nodeConfig: {
            kind: 'shelley',
            configurationDir,
            network: cardanoNode.networks.testnet,
          },
          childProcessLogWriteStreams: {
            node: fs.createWriteStream(nodeLogFile.path, {
              fd: nodeLogFile.fd,
            }),
            wallet: fs.createWriteStream(walletLogFile.path, {
              fd: walletLogFile.fd,
            }),
          },
        }, loggers.app);
        await launcher.start();
        await launcher.stop(testsStopTimeout);
        const nodeLogFileStats = await stat(nodeLogFile.path);
        const walletLogFileStats = await stat(walletLogFile.path);
        expect(nodeLogFileStats.size).toBeGreaterThan(0);
        expect(walletLogFileStats.size).toBeGreaterThan(0);
      }),
    veryLongTestTimeoutMs
  );

  it(
    'accepts the same WriteStream for both the wallet and node to produce a combined stream',
    async () =>
      await withMainnetConfigDir(async configurationDir => {
        const logFile = await tmp.file();
        const writeStream = fs.createWriteStream(logFile.path, {
          fd: logFile.fd,
        });
        const launcher = new Launcher({
          stateDir: (
            await tmp.dir({
              unsafeCleanup: true,
              prefix: 'launcher-integration-test-',
            })
          ).path,
          networkName: 'mainnet',
          nodeConfig: {
            kind: 'shelley',
            configurationDir,
            network: cardanoNode.networks.testnet,
          },
          childProcessLogWriteStreams: {
            node: writeStream,
            wallet: writeStream,
          },
        }, loggers.app);
        await launcher.start();
        const logFileStats = await stat(writeStream.path);
        expect(logFileStats.size).toBeGreaterThan(0);
        await launcher.stop(testsStopTimeout);
      }),
    veryLongTestTimeoutMs
  );

  // eslint-disable-next-line jest/expect-expect
  it(
    'can configure the cardano-wallet to serve the API with TLS',
    async () =>
      launcherTest(stateDir => {
        return {
          stateDir,
          networkName: 'testnet',
          nodeConfig: {
            kind: 'shelley',
            configurationDir: getShelleyConfigDir('testnet'),
            network: cardanoNode.networks.testnet,
          },
          tlsConfiguration: {
            caCert: path.join(tlsDir, 'ca.crt'),
            svCert: path.join(tlsDir, 'server', 'server.crt'),
            svKey: path.join(tlsDir, 'server', 'server.key'),
          },
        };
      }, true),
    veryLongTestTimeoutMs
  );

  it(
    'handles case where cardano-node fails during initialisation',
    async () => {
      expect.assertions(5);
      let chainDir: string;
      await withMainnetConfigDir(async configurationDir => {
        const launcher = await setupTestLauncher(stateDir => {
          // cardano-node will expect this to be a directory, and exit with an error
          chainDir = path.join(stateDir, 'chain');
          fs.writeFileSync(chainDir, 'bomb');

          return {
            stateDir,
            networkName: 'testnet',
            nodeConfig: {
              kind: 'shelley',
              configurationDir,
              network: cardanoNode.networks.testnet,
            },
          };
        });

        await launcher.start().catch(passthroughErrorLogger);
        expect((await fs.promises.stat(chainDir)).isFile()).toBe(true);

        const expectations = new Promise<void>((done, fail) =>
          launcher.walletBackend.events.on('exit', (status: ExitStatus) => {
            try {
              expect(status.wallet.code).toBe(0);
              expect(status.node.code).not.toBe(0);
              // cardano-node is sometimes not exiting properly on both linux
              // and windows.
              // fixme: This assertion is disabled until the node is fixed.
              if (status.node.signal !== null) {
                loggers.test.error("Flaky test - cardano-node did not exit properly.", status.node.signal);
              }
              // expect(status.node.signal).toBeNull();
              // Maintain same number of assertions...
              expect(status.node).not.toBeNull();
            } catch (e) {
              fail(e);
            }
            done();
          })
        );

        await launcher.stop(testsStopTimeout);

        await expectations;
      });
    },
    veryLongTestTimeoutMs
  );

  it(
    'services listen only on a private address',
    async () => {
      const launcher = await setupTestLauncher(stateDir => {
        return {
          stateDir,
          networkName: 'testnet',
          nodeConfig: {
            kind: 'shelley',
            configurationDir: getShelleyConfigDir('testnet'),
            network: cardanoNode.networks.testnet,
          },
        };
      });

      await launcher.start();
      const walletApi = launcher.walletBackend.api;
      const nodeConfig =
        launcher.nodeService.getConfig() as cardanoNode.NodeStartService;
      for (const host of listExternalAddresses()) {
        loggers.test.log(`Testing ${host}`);
        expect(
          await testPort(host, walletApi.requestParams.port, loggers.test)
        ).toBe(false);
        expect(await testPort(host, nodeConfig.listenPort, loggers.test)).toBe(
          false
        );
      }

      await launcher.stop(testsStopTimeout);
    },
    veryLongTestTimeoutMs
  );

  it(
    'applies RTS options to cardano-node',
    async () => {
      let hp = 'cardano-node.hp';
      const launcher = await setupTestLauncher(stateDir => {
        hp = path.join(stateDir, hp);
        return {
          stateDir,
          networkName: 'testnet',
          nodeConfig: {
            kind: 'shelley',
            configurationDir: getShelleyConfigDir('testnet'),
            network: cardanoNode.networks.testnet,
            rtsOpts: ['-h'], // generates a basic heap profile
          },
        };
      });

      await launcher.start();
      expect(await pathExists(hp)).toBe(true);
      await launcher.stop(testsStopTimeout);
    },
    veryLongTestTimeoutMs
  );
});
Example #9
Source File: ResourceApi.test.ts    From joplin-utils with MIT License 4 votes vote down vote up
describe('test ResourceApi', () => {
  let id: string
  beforeAll(async () => {
    id = (await createTestResource()).id
  })
  afterAll(async () => {
    await resourceApi.remove(id)
  })
  const tempPath = path.resolve(__dirname, '.temp')
  beforeEach(async () => {
    await remove(tempPath)
    await mkdirp(tempPath)
  })
  it('test list', async () => {
    const res = await resourceApi.list({ fields: ['id', 'title'] })
    console.log(res)
    expect(res.items.length).toBeGreaterThan(0)
  })
  it('test get', async () => {
    const res = await resourceApi.get(id)
    console.log(res)
    expect(res.id).toBe(id)
  })
  /**
   * TODO 一个官方未修复的 bug,参考:https://github.com/laurent22/joplin/issues/4575
   */
  it.skip('test get filename', async () => {
    const res = await resourceApi.get(id, ['id', 'filename'])
    console.log(res)
    expect(res.filename).not.toBe('')
  })

  describe('diff fetch and axios', () => {
    const path = resolve(__dirname, '../resource/resourcesByFileId.png')
    it('test create', async () => {
      const title = 'image title'
      const json = await resourceApi.create({
        title,
        data: createReadStream(path),
      })
      expect(json.title).toBe(title)
    })
  })
  describe('test update', () => {
    it('basic example', async () => {
      const title = `new title ${Date.now()}`
      const updateRes = await resourceApi.update({ id, title })
      expect(updateRes.title).toBe(title)
    })
    it('update file', async () => {
      const content = 'test'
      const txtPath = path.resolve(tempPath, 'test.txt')
      await writeFile(txtPath, content)
      await resourceApi.update({ id, data: createReadStream(txtPath) })
      const res = await resourceApi.fileByResourceId(id)
      expect(res.toString()).toBe(content)
    })
    it('update properties and file', async () => {
      const title = `new title ${Date.now()}`
      const content = 'test'
      const txtPath = path.resolve(tempPath, 'test.txt')
      await writeFile(txtPath, content)
      const updateRes = await resourceApi.update({ id, title, data: createReadStream(txtPath) })
      expect(updateRes.title).toBe(title)
      const res = await resourceApi.fileByResourceId(id)
      expect(res.toString()).toBe(content)
    })
    it('update file only', async () => {
      const content = 'test'
      const txtPath = path.resolve(tempPath, 'test.txt')
      await writeFile(txtPath, content)
      const { title } = await resourceApi.get(id)
      await resourceApi.update({ id, data: createReadStream(txtPath) })
      const res = await resourceApi.fileByResourceId(id)
      expect(res.toString()).toBe(content)
      expect((await resourceApi.get(id)).title).toBe(title)
    })
  })
  /**
   * 已知错误
   * https://discourse.joplinapp.org/t/pre-release-v2-8-is-now-available-updated-14-april/25158/10?u=rxliuli
   */
  it.skip('test remove ', async () => {
    const id = (await createTestResource()).id
    await resourceApi.remove(id)
    await expect(resourceApi.get(id)).rejects.toThrowError()
  })
  it('test fileByResourceId', async () => {
    const res = await resourceApi.fileByResourceId(id)
    console.log(typeof res)
    const path = resolve(tempPath, 'resourcesByFileId.png')
    await writeFile(path, res)
    expect(pathExistsSync(path)).toBeTruthy()
  })
  it('test to get the size of the attachment resource', async () => {
    const id = (await createTestResource()).id
    const res = await resourceApi.get(id, ['id', 'title', 'size'])
    const stats = await stat(path.resolve(__dirname, '../resource/resourcesByFileId.png'))
    expect(res.size).toEqual(stats.size)
  })
})