UNPKG

static-fs

Version:

A static filesystem to bundle files and read them using Node.js

111 lines (82 loc) 4.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateStaticFsVolume = void 0; var _path = require("path"); var _filesystem = require("../filesystem"); var _common = require("../common"); // Creates a Static-Fs runtime file in the target const createStaticFsRuntimeFile = async outDir => { const sourceFile = require.resolve(`../runtime`); const outFile = (0, _path.resolve)(outDir, 'static_fs_runtime.js'); await (0, _common.copyFile)(sourceFile, outFile); return outFile; }; // Patches target node app entry points in order // to make the server code able to read files // from the static fs const patchEntryPoints = async (entryPoints, staticFSRuntimeFile, staticFsVolumeFile) => { for (const entryPoint of entryPoints) { const isEntryPointAFile = await (0, _common.isFile)(entryPoint); if (isEntryPointAFile) { let loaderPath = (0, _path.relative)((0, _path.dirname)(entryPoint), staticFSRuntimeFile).replace(/\\/g, '/'); if (loaderPath.charAt(0) !== '.') { loaderPath = `./${loaderPath}`; } let fsPath = (0, _path.relative)((0, _path.dirname)(entryPoint), staticFsVolumeFile).replace(/\\/g, '/'); fsPath = `\${__dirname }/${fsPath}`; let content = await (0, _common.readFile)(entryPoint, { encoding: 'utf8' }); const staticFsPatch = `require('${loaderPath}')\n.load(require.resolve(\`${fsPath}\`));\n`; let shebangPrefix = ''; let useStrictPrefix = ''; if (content.indexOf(staticFsPatch) === -1) { // #! shebang const shebangRx = /^#!.*$/gm.exec(content.toString()); if (shebangRx && shebangRx.index === 0) { shebangPrefix = `${shebangRx[0]}\n`; // remove shebangPrefix content = content.replace(shebangPrefix, ''); } // use strict instruction const useStrictRx = /^["']use strict["'];$/gm.exec(content.toString()); if (useStrictRx && useStrictRx.index === 0) { useStrictPrefix = `${useStrictRx[0]}\n`; // remove useStrictPrefix content = content.replace(useStrictPrefix, ''); } // remove previous loader content = content.replace(/^require.*static_fs_runtime.js.*$/gm, ''); content = content.replace(/\/\/ load static_fs_volume: .*$/gm, ''); content = content.trim(); const additionalNLSeparator = shebangPrefix || useStrictPrefix ? '\n' : ''; content = `${shebangPrefix}${useStrictPrefix}${additionalNLSeparator}// load static_fs_volume: ${fsPath}\n${staticFsPatch}\n${content}`; await (0, _common.writeFile)(entryPoint, content); } } } }; // adds folders and files to the static filesystem volume const addFoldersToStaticFsVolume = async (mountRootDir, foldersToAdd, exclusions) => { const sfs = new _filesystem.WritableStaticVolume(mountRootDir); for (const folderToAdd of foldersToAdd) { await sfs.addFolder(folderToAdd, exclusions); } await sfs.write(); // returning all the files and base folders added to that created volume return sfs.getAddedFilesAndFolders(); }; const generateStaticFsVolume = async (mountRootDir, foldersToAdd, appEntryPointsToPatch, exclusions = []) => { const sanitizedMountRootDir = (0, _path.resolve)(mountRootDir); const sanitizedOutputDir = (0, _path.resolve)(sanitizedMountRootDir, '.static_fs'); const sanitizedFoldersToAdd = foldersToAdd.map(p => (0, _path.resolve)(p)); const sanitizedAppEntryPointsToPatch = appEntryPointsToPatch.map(p => (0, _path.resolve)(p)); const sanitizedExclusions = exclusions.reduce((accum, val) => { const resolvedVal = (0, _path.resolve)(val); if (!resolvedVal.includes(sanitizedMountRootDir)) { throw new Error(`All exclusions should has mountRoot has parent: ${sanitizedMountRootDir}`); } accum[resolvedVal] = true; return accum; }, {}); const filesAddedToVolume = await addFoldersToStaticFsVolume(sanitizedMountRootDir, sanitizedFoldersToAdd, sanitizedExclusions); const staticFSRuntimeFile = await createStaticFsRuntimeFile(sanitizedOutputDir); await patchEntryPoints(sanitizedAppEntryPointsToPatch, staticFSRuntimeFile, (0, _path.resolve)(sanitizedOutputDir, 'static_fs_volume.sfsv'), sanitizedMountRootDir); return filesAddedToVolume; }; exports.generateStaticFsVolume = generateStaticFsVolume;