Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add readScripts method for loading scripts from dir #123

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/methods/a-simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
({
field: 'value',

add(a, b) {
return a + b;
},

sub: (a, b) => a - b,
});
9 changes: 9 additions & 0 deletions examples/methods/b-simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
({
field: 'value',

add(a, b) {
return a + b;
},

sub: (a, b) => a - b,
});
5 changes: 5 additions & 0 deletions metavm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ export function readScript(
filePath: string,
options?: BaseOptions,
): Promise<MetaScript>;

export function readScripts(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better naming: readDirectory

dir: string,
options?: BaseOptions,
): Promise<Record<string, MetaScript>>;
23 changes: 18 additions & 5 deletions metavm.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,28 @@ class MetaScript {

const createScript = (name, src, options) => new MetaScript(name, src, options);

const readScript = async (filePath, options) => {
const readScript = async (filePath, options = {}) => {
const src = await fsp.readFile(filePath, 'utf8');
if (src === '') throw new SyntaxError(`File ${filePath} is empty`);
const name = options?.filename
? options.filename
: path.basename(filePath, '.js');
const script = new MetaScript(name, src, options);
const name = options.filename ?? path.basename(filePath, '.js');
const script = createScript(name, src, options);
return script;
};

const readScripts = async (dir, options) => {
const files = await fsp.readdir(dir, { withFileTypes: true });
const container = {};
for (const file of files) {
const { name } = file;
if (file.isFile() && !name.endsWith('.js')) continue;
const location = path.join(dir, name);
const key = path.basename(name, '.js');
const loader = file.isFile() ? readScript : readScripts;
container[key] = await loader(location, options);
}
return container;
};

module.exports = {
createContext,
MetaScript,
Expand All @@ -186,4 +198,5 @@ module.exports = {
NODE_CONTEXT,
MODULE_TYPE,
readScript,
readScripts,
};
27 changes: 27 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ test('Load script', async () => {
assert.strictEqual(ms.exports.add(2, 3), 5);
});

test('Load scripts', async () => {
const dir = path.join(examples, 'methods');
const metaScripts = await metavm.readScripts(dir);

assert.strictEqual(typeof metaScripts, 'object');
assert.ok('a-simple' in metaScripts);
assert.ok('b-simple' in metaScripts);

const scripts = Object.values(metaScripts);

assert.strictEqual(scripts.length, 2);

for (const ms of scripts) {
assert.strictEqual(typeof ms.exports, 'object');

const fields = Object.keys(ms);
assert.deepEqual(fields, SCRIPT_FIELDS);

const keys = Object.keys(ms.exports);
assert.deepEqual(keys, ['field', 'add', 'sub']);

assert.strictEqual(ms.exports.field, 'value');
assert.strictEqual(ms.exports.sub(2, 3), -1);
assert.strictEqual(ms.exports.add(2, 3), 5);
}
});

test('Load empty script', async () => {
try {
const filePath = path.join(examples, 'simple');
Expand Down