Skip to content
This repository was archived by the owner on Feb 3, 2022. It is now read-only.

Commit 12b0f58

Browse files
committed
Allow for nested templates of functions/assets
1 parent 8b5dd47 commit 12b0f58

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

src/create-twilio-function/create-files.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const open = promisify(fs.open);
66
const write = promisify(fs.write);
77
const readdir = promisify(fs.readdir);
88
const copyFile = promisify(fs.copyFile);
9+
const { COPYFILE_EXCL } = fs.constants;
10+
const stat = promisify(fs.stat);
911

1012
function createDirectory(path, dirName) {
1113
return mkdir(path + '/' + dirName);
@@ -39,22 +41,26 @@ function createPackageJSON(path, name) {
3941
return createFile(fullPath, packageJSON);
4042
}
4143

42-
function createExampleFromTemplates(path) {
43-
return readdir('./templates').then(dirs =>
44-
Promise.all(
45-
dirs.map(dir =>
46-
mkdir(`${path}/${dir}`)
47-
.then(() => readdir(`./templates/${dir}`))
48-
.then(files =>
49-
Promise.all(
50-
files.map(file =>
51-
copyFile(`./templates/${dir}/${file}`, `${path}/${dir}/${file}`)
52-
)
53-
)
54-
)
44+
function copyRecursively(src, dest) {
45+
return readdir(src).then(children => {
46+
return Promise.all(
47+
children.map(child =>
48+
copyFile(`./${src}/${child}`, `${dest}/${child}`, COPYFILE_EXCL).then(
49+
() => {
50+
return stat(`${src}/${child}`).then(stat => {
51+
if (stat.isDirectory()) {
52+
return copyRecursively(`${src}/${child}`, `${dest}/${child}`);
53+
}
54+
});
55+
}
56+
)
5557
)
56-
)
57-
);
58+
);
59+
});
60+
}
61+
62+
function createExampleFromTemplates(path) {
63+
return copyRecursively('./templates', path);
5864
}
5965

6066
function createEnvFile(path, { accountSid, authToken }) {

templates/assets/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h2>Assets</h2>
3434
browser, but you can load it as part of a function by finding its path
3535
using <code>Runtime.getAssets()</code> and then requiring the file.
3636
There is an example of this in
37-
<code>/functions/private-message.protected.js</code>.
37+
<code>/functions/private-message.js</code>.
3838
</p>
3939

4040
<h2>Functions</h2>
@@ -56,7 +56,7 @@ <h2>Functions</h2>
5656
<a href="https://www.twilio.com/docs/usage/security#validating-requests"
5757
>validating requests from Twilio in the documentation</a
5858
>. There is an example of a protected function in
59-
<code>/functions/private-message.protected.js</code>
59+
<code>/functions/sms/reply.protected.js</code>
6060
</p>
6161

6262
<h2>twilio-run</h2>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exports.handler = function(context, event, callback) {
2+
const twiml = new Twilio.twiml.MessagingResponse();
3+
twiml.message('Hello World!');
4+
callback(null, twiml);
5+
};

0 commit comments

Comments
 (0)