caffeine-mc
Version:
Select, configure and extend your to-JavaScript compiler, with arbitrary code, on a per file bases from within the file.
5 lines (4 loc) • 3.65 kB
JavaScript
let Caf = require('caffeine-script-runtime');
Caf.defMod(module, () => {return Caf.importInvoke(["merge", "isString", "Error", "formattedInspect", "mockTempDir", "mockCwdFunction", "unmockCwdFunction", "unmockTempDir", "Promise", "isPlainObject", "path", "log", "Object"], [global, require('art-standard-lib'), require('../MockableCwd'), require('../MockableTempDir'), {path: require('path')}], (merge, isString, Error, formattedInspect, mockTempDir, mockCwdFunction, unmockCwdFunction, unmockTempDir, Promise, isPlainObject, path, log, Object) => {let actualFs, mockFileStructure, cwd, resolvePath, mockMethods, mockedMethods, existsSync, exists, statSync, readFileSync, writeFileSync; actualFs = require("fs-extra"); mockFileStructure = {}; cwd = "/"; resolvePath = function(filePath) {let currentFolder, folders; currentFolder = cwd; if (!isString(filePath)) {throw new Error(`filePath must be a string got: ${Caf.toString(formattedInspect(filePath))}`);}; folders = filePath.split("/"); if (folders[0] === "") {folders.shift(); currentFolder = mockFileStructure;} else {throw new Error("cwd not supported");}; while (folders.length > 0) {if (!(currentFolder = currentFolder[folders[0]])) {return;}; folders.shift();}; return currentFolder;}; mockMethods = {setMockFileStructure: function(newMockFileStructure) {cwd = "/"; mockTempDir("/tmp"); mockCwdFunction(() => cwd); return mockFileStructure = merge({tmp: {}}, newMockFileStructure);}, isMocked: function() {return mockFileStructure != null;}, getMockFileStructure: function() {return mockFileStructure;}, unmockFs: function() {unmockCwdFunction(); unmockTempDir(); return mockFileStructure = null;}}; mockedMethods = {existsSync: existsSync = (filePath) => !!resolvePath(filePath), exists: exists = (filePath) => Promise.then(() => existsSync(filePath)), statSync: statSync = (filePath) => {let file; file = resolvePath(filePath); if (!(file != null)) {throw new Error(`Could not find file: ${Caf.toString(filePath)}`);}; return {isDirectory: () => isPlainObject(file), isFile: () => isString(file)};}, readFileSync: readFileSync = function(filePath) {let temp; return ((temp = resolvePath(filePath)) != null ? temp : (() => {throw new Error(`Could not find file: ${Caf.toString(filePath)}`);})());}, readFile: function(filePath) {return Promise.then(() => readFileSync(filePath));}, stat: function(filePath) {return Promise.then(() => statSync(filePath));}, writeFileSync: writeFileSync = function(filePath, contents) {let folderName, fileName, folder; folderName = path.dirname(filePath); fileName = path.basename(filePath); folder = resolvePath(folderName); if (!folder) {throw new Error(`Could not find folder: ${Caf.toString(folderName)}`);}; if (isPlainObject(folder[fileName])) {throw new Error(`File already exists as a folder: ${Caf.toString(folderName)}/${Caf.toString(fileName)}`);}; return folder[fileName] = contents;}, writeFile: function(filePath, contents) {return Promise.then(() => writeFileSync(filePath, contents));}, readdirSync: function(path, options) {let folder; folder = resolvePath(path); if (!isPlainObject(folder)) {log({readdirSync: {path, options, mockFileStructure}}); throw new Error(`Not a folder: ${Caf.toString(path)}`);}; return Object.keys(folder);}}; return merge(mockMethods, Caf.object(actualFs, (v, k) => function(...args) {let mock; return mockFileStructure ? (mock = mockedMethods[k]) ? mock.apply(null, args) : (log.warn(`fs-extra mocked method not found: ${Caf.toString(k)}`), (() => {throw new Error(`fs-extra mocked method not found: ${Caf.toString(k)}`);})()) : v.apply(null, args);}));});});
//# sourceMappingURL=fs-extra.js.map