Skip to content

Allow cloud code file to be an array of files or a folder #6962

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,46 @@ describe('Cloud Code', () => {
);
});

it('can load array of code files', async done => {
await reconfigureServer({
cloud: [
`${__dirname}/cloud/cloudCodeArray.js`,
`${__dirname}/cloud/cloudCodeArray2.js`,
],
});
const promises = [
Parse.Cloud.run('cloudCodeArray', {}),
Parse.Cloud.run('cloudCodeArray2', {}),
];
const result = await Promise.all(promises);
expect(result[0]).toEqual(
'It is possible to define cloud code as an array of files.'
);
expect(result[1]).toEqual(
'It is possible to define cloud code as an array of files two.'
);
done();
});

it('can load folder of code files', async done => {
await reconfigureServer({
cloud: `${__dirname}/cloud/functions`,
});

const promises = [
Parse.Cloud.run('cloudCodeSubfolder', {}),
Parse.Cloud.run('cloudCodeSubfolder2', {}),
];
const result = await Promise.all(promises);
expect(result[0]).toEqual(
'It is possible to define cloud code as a folder of files.'
);
expect(result[1]).toEqual(
'It is possible to define cloud code as a folder of files two.'
);
done();
});

it('can create functions', done => {
Parse.Cloud.define('hello', () => {
return 'Hello world!';
Expand Down
3 changes: 3 additions & 0 deletions spec/cloud/cloudCodeArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse.Cloud.define('cloudCodeArray', () => {
return 'It is possible to define cloud code as an array of files.';
});
3 changes: 3 additions & 0 deletions spec/cloud/cloudCodeArray2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse.Cloud.define('cloudCodeArray2', () => {
return 'It is possible to define cloud code as an array of files two.';
});
3 changes: 3 additions & 0 deletions spec/cloud/functions/cloudCodeSubfolderFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse.Cloud.define('cloudCodeSubfolder', () => {
return 'It is possible to define cloud code as a folder of files.';
});
3 changes: 3 additions & 0 deletions spec/cloud/functions/cloudCodeSubfolderFile2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse.Cloud.define('cloudCodeSubfolder2', () => {
return 'It is possible to define cloud code as a folder of files two.';
});
33 changes: 30 additions & 3 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,37 @@ class ParseServer {
addParseCloud();
if (typeof cloud === 'function') {
cloud(Parse);
} else if (typeof cloud === 'string') {
require(path.resolve(process.cwd(), cloud));
return;
}
let cloudFiles = [];
if (typeof cloud === 'string') {
cloudFiles.push(cloud);
} else if (cloud instanceof Array) {
cloudFiles = cloud;
} else {
throw "argument 'cloud' must either be a string or a function";
throw "argument 'cloud' must either be a string, array, or function";
}
for (const cloudFileName of cloudFiles) {
const cloudDir = fs.lstatSync(cloudFileName);
const filePath = path.resolve(process.cwd(), cloudFileName);
const requireFiles = files => {
const ext = path.extname(files) || '';
if (ext != '.js') {
console.log(
`Could not import cloud file ${files}. Error: Invalid format.`
);
} else {
require(files);
}
};
if (cloudDir.isDirectory()) {
const files = fs.readdirSync(filePath);
for (const file of files) {
requireFiles(path.join(filePath, file));
}
} else {
requireFiles(filePath);
}
}
}
}
Expand Down