UNPKG

@foal/cli

Version:

CLI tool for FoalTS

90 lines (89 loc) 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileSystemService = void 0; const fs_1 = require("fs"); const path_1 = require("path"); class FileSystemService { testDir = 'test-generators/subdir'; isTestingEnvironment() { return process.env.P1Z7kEbSUUPMxF8GqPwD8Gx_FOAL_CLI_TEST === 'true'; } setUp() { if (!this.isTestingEnvironment()) { throw new Error('Impossible to create the test directory in a non-testing environment.'); } (0, fs_1.mkdirSync)(this.testDir, { recursive: true }); } tearDown() { if (!this.isTestingEnvironment()) { throw new Error('Impossible to remove the test directory in a non-testing environment.'); } const [firstDir] = this.testDir.split('/'); (0, fs_1.rmSync)(firstDir, { recursive: true, force: true }); } computePath(path) { if (!this.isTestingEnvironment()) { return path; } if (!(0, fs_1.existsSync)(this.testDir)) { throw new Error('The testing environment is not set up.'); } return (0, path_1.join)(this.testDir, path); } mkdir(path) { (0, fs_1.mkdirSync)(this.computePath(path), { recursive: true }); } rmdir(path) { (0, fs_1.rmSync)(this.computePath(path), { recursive: true }); } writeFile(path, content) { (0, fs_1.writeFileSync)(this.computePath(path), content, 'utf8'); } readFile(path) { return (0, fs_1.readFileSync)(this.computePath(path), 'utf8'); } readBinaryFile(path) { return (0, fs_1.readFileSync)(this.computePath(path)); } readFileFromTemplates(path) { return (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../..', 'templates', path), 'utf8'); } readFileFromSpecs(path) { return (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../..', 'specs', path), 'utf8'); } readBinaryFileFromSpecs(path) { return (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../..', 'specs', path)); } deleteFile(path) { (0, fs_1.unlinkSync)(this.computePath(path)); } copyFile(src, dest) { (0, fs_1.copyFileSync)(this.computePath(src), this.computePath(dest)); } copyFileFromTemplates(src, dest) { (0, fs_1.copyFileSync)((0, path_1.join)(__dirname, '../../..', 'templates', src), this.computePath(dest)); } copyFileFromFixtures(src, dest) { (0, fs_1.copyFileSync)((0, path_1.join)(__dirname, '../../..', 'fixtures', src), this.computePath(dest)); } exists(path) { return (0, fs_1.existsSync)(this.computePath(path)); } existsTemplate(path) { return (0, fs_1.existsSync)((0, path_1.join)(__dirname, '../../..', 'templates', path)); } existsFixture(path) { return (0, fs_1.existsSync)((0, path_1.join)(__dirname, '../../..', 'fixtures', path)); } existsSpec(path) { return (0, fs_1.existsSync)((0, path_1.join)(__dirname, '../../..', 'specs', path)); } isDirectoryEmpty(path) { const dirPath = this.computePath(path); if (!(0, fs_1.existsSync)(dirPath)) { throw new Error(`Directory does not exist: ${path}`); } return (0, fs_1.readdirSync)(dirPath).length === 0; } } exports.FileSystemService = FileSystemService;