import { ensureDirSync, readdirSync, readFileSync, removeSync, renameSync, statSync, writeFileSync, copySync } from 'fs-extra'; import { dirname } from 'path'; import { tmpProjPath } from './paths'; /** * Copies module folders from the working directory to the e2e directory * @param modules a list of module names or scopes to copy */ export function copyNodeModules(modules: string[]) { modules.forEach(module => { removeSync(`${tmpProjPath()}/node_modules/${module}`); copySync( `./node_modules/${module}`, `${tmpProjPath()}/node_modules/${module}` ); }); } /** * Assert output from a asynchronous CLI command * @param output: Output from an asynchronous command */ export function expectTestsPass(v: { stdout: string; stderr: string }) { expect(v.stderr).toContain('Ran all test suites'); expect(v.stderr).not.toContain('fail'); } export function updateFile(f: string, content: string | Function): void { ensureDirSync(dirname(tmpProjPath(f))); if (typeof content === 'string') { writeFileSync(tmpProjPath(f), content); } else { writeFileSync( tmpProjPath(f), content(readFileSync(tmpProjPath(f)).toString()) ); } } export function renameFile(f: string, newPath: string): void { ensureDirSync(dirname(tmpProjPath(newPath))); renameSync(tmpProjPath(f), tmpProjPath(newPath)); } export function checkFilesExist(...expectedFiles: string[]) { expectedFiles.forEach(f => { const ff = f.startsWith('/') ? f : tmpProjPath(f); if (!exists(ff)) { throw new Error(`File '${ff}' does not exist`); } }); } export function listFiles(dirName: string) { return readdirSync(tmpProjPath(dirName)); } export function readJson(f: string): any { return JSON.parse(readFile(f)); } export function readFile(f: string) { const ff = f.startsWith('/') ? f : tmpProjPath(f); return readFileSync(ff).toString(); } export function cleanup() { removeSync(tmpProjPath()); } export function rmDist() { removeSync(`${tmpProjPath()}/dist`); } export function getCwd(): string { return process.cwd(); } export function directoryExists(filePath: string): boolean { try { return statSync(filePath).isDirectory(); } catch (err) { return false; } } export function fileExists(filePath: string): boolean { try { return statSync(filePath).isFile(); } catch (err) { return false; } } export function exists(filePath: string): boolean { return directoryExists(filePath) || fileExists(filePath); } export function getSize(filePath: string): number { return statSync(filePath).size; }