fs#rmSync TypeScript Examples

The following examples show how to use fs#rmSync. 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: index.ts    From airnode with MIT License 6 votes vote down vote up
removeKey = (key: string) => {
  try {
    // Force will not throw an error if the file doesn't exist
    rmSync(join(CACHE_BASE_PATH, key), { force: true });
  } catch (e) {
    logger.error(`Unable to remove key from fs cache`);
    logger.error((e as Error).stack!);
  }
}
Example #2
Source File: index.ts    From airnode with MIT License 6 votes vote down vote up
addKey = (key: string, data: any, force = false) => {
  // To not break existing tests do nothing if we're executing in a test environment.
  if (isJest() && !force) {
    return;
  }

  const filePath = join(CACHE_BASE_PATH, key);
  try {
    const fileExists = existsSync(filePath);
    if (fileExists && !force) {
      logger.error(`Unable to overwrite key from fs cache as key exists and force is set to false`);
      logger.error(`Key: ${key}`);
      return;
    }

    if (fileExists) {
      rmSync(filePath, { force: true });
    }

    writeFileSync(filePath, JSON.stringify(data));
  } catch (e) {
    logger.error(`Unable to remove key from fs cache`);
    logger.error((e as Error).stack!);
  }
}
Example #3
Source File: project.ts    From cli with Apache License 2.0 6 votes vote down vote up
public async compressResources() {
    let cachedManifest;
    try {
      this.ensureProjectCompliance();
      cachedManifest = readJsonSync(this.resourceManifestPath, {
        throws: false,
      });
      rmSync(this.resourceManifestPath, {force: true});
      await new Promise<void>((resolve, reject) => {
        const outputStream = createWriteStream(this.temporaryZipPath);
        const archive = archiver('zip');

        outputStream.on('close', () => resolve());
        archive.on('error', (err) => reject(err));

        archive.pipe(outputStream);
        archive.directory(this.resourcePath, false);
        archive.finalize();
      });
      if (cachedManifest) {
        writeJsonSync(this.resourceManifestPath, cachedManifest);
      }
      return this.temporaryZipPath;
    } catch (error) {
      if (cachedManifest) {
        writeJsonSync(this.resourceManifestPath, cachedManifest);
      }
      CliUx.ux.error(error as string | Error);
    }
  }
Example #4
Source File: expandedPreviewer.ts    From cli with Apache License 2.0 6 votes vote down vote up
private deleteOldestPreviews() {
    const getFilePath = (fileDirent: Dirent) =>
      join(ExpandedPreviewer.previewDirectory, fileDirent.name);

    const getEpochFromSnapshotDir = (dir: Dirent): number =>
      parseInt(dir.name.match(/(?<=-)\d+$/)?.[0] ?? '0');

    const allFiles = readdirSync(ExpandedPreviewer.previewDirectory, {
      withFileTypes: true,
    });
    const dirs = allFiles
      .filter((potentialDir) => potentialDir.isDirectory())
      .sort(
        (dirA, dirB) =>
          getEpochFromSnapshotDir(dirA) - getEpochFromSnapshotDir(dirB)
      );

    while (dirs.length >= ExpandedPreviewer.previewHistorySize) {
      rmSync(getFilePath(dirs.shift()!), {
        recursive: true,
        force: true,
      });
    }
  }
Example #5
Source File: filesDiffProcessor.ts    From cli with Apache License 2.0 6 votes vote down vote up
export function recursiveDirectoryDiff(
  currentDir: string,
  nextDir: string,
  deleteMissingResources: boolean
) {
  const currentFilePaths = getAllFilesPath(currentDir);
  const nextFilePaths = getAllFilesPath(nextDir);

  nextFilePaths.forEach((filePath) => {
    const nextFileJson = readJsonSync(join(nextDir, filePath));
    let dataToWrite = nextFileJson;
    if (currentFilePaths.has(filePath)) {
      currentFilePaths.delete(filePath);
      const currentFileJSON = readJsonSync(join(currentDir, filePath));
      dataToWrite = buildDiffedJson(
        currentFileJSON,
        nextFileJson,
        deleteMissingResources
      );
    }
    writeJsonSync(join(currentDir, filePath), dataToWrite, defaultWriteOptions);
  });

  if (deleteMissingResources) {
    currentFilePaths.forEach((filePath) => rmSync(join(currentDir, filePath)));
  }
}
Example #6
Source File: index.ts    From airnode with MIT License 5 votes vote down vote up
sweep = () => {
  try {
    const now = Date.now();

    const stattedFiles = readdirSync(CACHE_BASE_PATH).map((file) => ({
      ...statSync(join(CACHE_BASE_PATH, file)),
      file,
    }));

    const deletedFiles = stattedFiles
      .filter((fileDetails) => now - fileDetails.mtimeMs > CACHE_MAX_FILESYSTEM_AGE_MINUTES * 60 * 1000)
      .map((fileDetails) => {
        rmSync(fileDetails.file, { force: true });
        return fileDetails;
      });

    const remainingFiles = stattedFiles.filter((file) => !deletedFiles.includes(file));

    if (remainingFiles.length < CACHE_MAX_FILES) {
      return;
    }

    // Delete oldest cache entries first
    type statFile = { file: string; mtimeMs: number };
    const sortFn = (a: statFile, b: statFile) => {
      if (a.mtimeMs > b.mtimeMs) {
        return -1;
      }

      if (a.mtimeMs < b.mtimeMs) {
        return 1;
      }

      return 0;
    };

    remainingFiles
      .sort(sortFn)
      .slice(CACHE_MAX_FILES)
      .forEach((fileDetails) => {
        rmSync(fileDetails.file, { force: true });
      });
  } catch (e) {
    logger.error(`Unable to sweep old files from fs cache`);
    logger.error((e as Error).stack!);
  }
}
Example #7
Source File: project.spec.ts    From cli with Apache License 2.0 5 votes vote down vote up
mockedRmSync = jest.mocked(rmSync)
Example #8
Source File: project.ts    From cli with Apache License 2.0 5 votes vote down vote up
public reset() {
    if (this.isResourcesProject) {
      rmSync(Project.resourceFolderName, {recursive: true, force: true});
    }
  }
Example #9
Source File: filesDiffProcessor.spec.ts    From cli with Apache License 2.0 5 votes vote down vote up
mockedRm = jest.mocked(rmSync)
Example #10
Source File: build.test.ts    From foca with MIT License 5 votes vote down vote up
test('ESM with type=commonjs', async () => {
  rmSync('dist/esm/package.json');
  await testFile('dist/esm/index.js', 1);
});
Example #11
Source File: expandedPreviewer.spec.ts    From cli with Apache License 2.0 4 votes vote down vote up
describe('ExpandedPreviewer', () => {
  const Blob = jest.fn();
  const fakeBlob: Blob = new Blob();

  const mockedRecursiveDirectoryDiff = jest.mocked(recursiveDirectoryDiff);
  const mockedExistsSync = jest.mocked(existsSync);
  const mockedReaddirSync = jest.mocked(readdirSync);
  const mockedRmSync = jest.mocked(rmSync);
  const mockedMkdirSync = jest.mocked(mkdirSync);
  const mockedSpawnProcess = jest.mocked(spawnProcess);
  const mockedSpawnProcessOutput = jest.mocked(spawnProcessOutput);
  const mockedProject = jest.mocked(Project);
  const mockedProjectRefresh = jest.fn();
  const mockedSnapshotFactory = jest.mocked(SnapshotFactory, true);
  const mockedSnapshotDownload = jest.fn().mockReturnValue(fakeBlob);
  const mockedSnapshotDelete = jest.fn();

  let nbOfExistingPreview: number;
  const mockExistingPreviews = () => {
    const dirs = new Array<Dirent>();
    for (let i = 0; i < nbOfExistingPreview; i++) {
      dirs.push(getDirectory(`someOrgId-${i}`));
    }
    mockedReaddirSync.mockReturnValueOnce(dirs);
  };

  const mockExistsSync = () => {
    mockedExistsSync.mockReturnValue(true);
  };

  const mockProject = () => {
    mockedProject.mockImplementation(
      (path: string) =>
        ({
          pathToProject: path,
          resourcePath: resolve(join(path, 'resources')),
          refresh: mockedProjectRefresh,
        } as unknown as Project)
    );
  };

  const mockSnapshotFactory = async () => {
    mockedSnapshotFactory.createFromOrg.mockReturnValue(
      Promise.resolve({
        download: mockedSnapshotDownload,
        delete: mockedSnapshotDelete,
      } as unknown as Snapshot)
    );
  };

  const mockSpawnProcess = () => {
    mockedSpawnProcessOutput.mockResolvedValue({
      exitCode: 'ENOENT',
      stderr: '',
      stdout: '',
    });
  };

  const defaultMocks = () => {
    mockExistsSync();
    mockExistingPreviews();
    mockProject();
    mockSnapshotFactory();
    mockSpawnProcess();
    jest.spyOn(Date, 'now').mockImplementation(() => 42);
  };

  beforeAll(() => {
    nbOfExistingPreview = 4;
  });

  beforeEach(() => {
    defaultMocks();
  });

  afterEach(() => {
    mockedReaddirSync.mockReset();
    mockedRmSync.mockReset();
  });

  afterAll(() => {
    jest.restoreAllMocks();
  });

  describe('when there are 5 expanded preview stored or more', () => {
    beforeAll(() => {
      nbOfExistingPreview = 8;
    });

    afterAll(() => {
      nbOfExistingPreview = 4;
    });

    fancyIt()('should delete the exceeding preview directories', async () => {
      const expandedPreviewer = new ExpandedPreviewer(
        getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
        'someorg',
        new Project('my/awesome/path'),
        false
      );
      await expandedPreviewer.preview();

      expect(mockedReaddirSync).toBeCalledWith(join('.coveo/preview'), {
        withFileTypes: true,
      });
      expect(mockedRmSync).toHaveBeenCalledTimes(4);
      for (let index = 0; index < 4; index++) {
        expect(mockedRmSync).toHaveBeenNthCalledWith(
          index + 1,
          join('.coveo/preview', `someOrgId-${index}`),
          expect.anything()
        );
      }
    });
  });

  describe('when no preview has been done yet', () => {
    fancyIt()('should not delete any preview directories', async () => {
      mockedExistsSync.mockReturnValueOnce(false);
      const expandedPreviewer = new ExpandedPreviewer(
        getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
        'someorg',
        new Project('my/awesome/path'),
        false
      );
      await expandedPreviewer.preview();

      expect(mockedExistsSync).toBeCalledWith(join('.coveo/preview'));
      expect(mockedReaddirSync).not.toBeCalled();
    });
  });

  describe('when there are less than 5 expanded preview stored', () => {
    beforeAll(() => {
      nbOfExistingPreview = 4;
    });

    afterAll(() => {
      nbOfExistingPreview = 4;
    });

    fancyIt()('should not delete any preview directories', async () => {
      const expandedPreviewer = new ExpandedPreviewer(
        getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
        'someorg',
        new Project('my/awesome/path'),
        false
      );
      await expandedPreviewer.preview();

      expect(mockedReaddirSync).toBeCalledWith(join('.coveo/preview'), {
        withFileTypes: true,
      });
      expect(mockedRmSync).not.toHaveBeenCalled();
    });
  });

  describe('when shouldDelete is false', () => {
    fancyIt()(
      'should call the fillDiffProcessor with the proper options',
      async () => {
        const expandedPreviewer = new ExpandedPreviewer(
          getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
          'someorg',
          new Project('my/awesome/path'),
          false
        );
        await expandedPreviewer.preview();

        expect(mockedRecursiveDirectoryDiff).toBeCalledWith(
          expect.anything(),
          expect.anything(),
          false
        );
      }
    );
  });

  describe('when shouldDelete is true', () => {
    fancyIt()(
      'should call the fillDiffProcessor with the proper options',
      async () => {
        const expandedPreviewer = new ExpandedPreviewer(
          getSuccessReport('some-id', ResourceSnapshotsReportType.DryRun),
          'someorg',
          new Project('my/awesome/path'),
          true
        );
        await expandedPreviewer.preview();

        expect(mockedRecursiveDirectoryDiff).toBeCalledWith(
          expect.anything(),
          expect.anything(),
          true
        );
      }
    );
  });

  describe('when calling #preview', () => {
    let fakeReport: ResourceSnapshotsReportModel;
    let expandedPreviewer: ExpandedPreviewer;
    let previewPath: string;

    beforeEach(async () => {
      stderr.start();
      stdout.start();

      previewPath = join('.coveo/preview', 'someorg-42');
      fakeReport = getSuccessReport(
        'some-id',
        ResourceSnapshotsReportType.DryRun
      );
      expandedPreviewer = new ExpandedPreviewer(
        fakeReport,
        'someorg',
        new Project('my/awesome/path'),
        false
      );
      await expandedPreviewer.preview();

      stderr.stop();
      stdout.stop();
    });

    fancyIt()('should get a snapshot of the target org', async () => {
      const previewPath = join('.coveo', 'preview', 'someorg-42');
      expect(mockedMkdirSync).toHaveBeenCalledWith(previewPath, {
        recursive: true,
      });

      expect(mockedProject).toHaveBeenCalledWith(resolve(previewPath));
      expect(mockedSnapshotFactory.createFromOrg).toHaveBeenCalledWith(
        fakeReport.resourceOperationResults,
        'someorg'
      );
      expect(mockedSnapshotDownload).toHaveBeenCalled();
      expect(mockedProjectRefresh).toHaveBeenCalledWith(fakeBlob);
    });

    fancyIt()('should commit the snapshot of the target org', async () => {
      expect(mockedSpawnProcess).toHaveBeenNthCalledWith(1, 'git', ['init'], {
        cwd: previewPath,
        stdio: 'ignore',
      });
      expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
        2,
        'git',
        ['add', '.'],
        {
          cwd: previewPath,
          stdio: 'ignore',
        }
      );
      expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
        3,
        'git',
        ['commit', '--message=someorg currently'],
        {
          cwd: previewPath,
          stdio: 'ignore',
        }
      );
    });

    fancyIt()('should get the commit hash', async () => {
      expect(mockedSpawnProcessOutput).toHaveBeenCalledWith(
        'git',
        ['rev-parse', '--short', 'HEAD'],
        {
          cwd: previewPath,
        }
      );
    });

    fancyIt()(
      'should write the diff between the snapshot of the target org and the snapshot on file',
      async () => {
        expect(mockedRecursiveDirectoryDiff).toBeCalledWith(
          join('.coveo/preview', 'someorg-42', 'resources'),
          join('my/awesome/path', 'resources'),
          expect.anything()
        );
      }
    );

    fancyIt()('should commit the diff', () => {
      expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
        4,
        'git',
        ['add', '.'],
        {
          cwd: previewPath,
          stdio: 'ignore',
        }
      );
      expect(mockedSpawnProcess).toHaveBeenNthCalledWith(
        5,
        'git',
        ['commit', '--message=someorg after snapshot application'],
        {
          cwd: previewPath,
          stdio: 'ignore',
        }
      );
    });

    fancyIt()('should delete the snapshot created in the target org', () => {
      expect(mockedSnapshotDelete).toHaveBeenCalled();
    });
  });
});