UNPKG

functionalscript

Version:

FunctionalScript is a purely functional subset of JavaScript

64 lines (63 loc) 2.39 kB
import { updateVersion } from "./version/module.f.js"; export const todo = () => { throw 'not implemented'; }; const cmp = ([a], [b]) => a < b ? -1 : a > b ? 1 : 0; export const env = ({ process: { env } }) => a => { const r = Object.getOwnPropertyDescriptor(env, a); return r === undefined ? undefined : typeof r.get === 'function' ? r.get() : r.value; }; export const allFiles = (io) => (s) => { const { fs: { promises: { readdir } }, process: { cwd } } = io; const load = async (p) => { let result = []; for (const i of await readdir(p, { withFileTypes: true })) { const { name } = i; if (name.startsWith('.')) { continue; } const file = `${p}/${name}`; if (i.isDirectory()) { if (name === 'node_modules') { continue; } result = [...result, ...await load(file)]; continue; } if (name.endsWith('.js') || name.endsWith('.ts')) { result = [...result, file]; } } return result; }; return load(s); }; export const loadModuleMap = async (io) => { const { fs: { existsSync }, asyncImport } = io; let map = []; const initCwd = env(io)('INIT_CWD'); const s = initCwd === undefined ? '.' : `${initCwd.replaceAll('\\', '/')}`; for (const f of await allFiles(io)(s)) { if (f.endsWith('.f.js') || (f.endsWith('.f.ts') && !existsSync(f.substring(0, f.length - 3) + '.js'))) { const source = await asyncImport(f); map = [...map, [f, source]]; } } return Object.fromEntries(map.toSorted(cmp)); }; export const index = async (io) => { updateVersion(io); const jj = './deno.json'; const jsr_json = JSON.parse(await io.fs.promises.readFile(jj, 'utf8')); const list = (await allFiles(io)('.')).filter(v => v.endsWith('/module.f.ts') || v.endsWith('/module.ts')); //console.log(list) const exportsA = list.map(v => [v, `./${v.substring(2)}`]); // console.log(exportsA) const exports = Object.fromEntries(exportsA); // console.log(exports) const json = JSON.stringify({ ...jsr_json, exports }, null, 2); // console.log(json) await io.fs.promises.writeFile(jj, json, 'utf8'); return 0; };