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

Commit

Permalink
Fixes templates path.
Browse files Browse the repository at this point in the history
Uses path.join instead of naiive string concatenation and __dirname to get the templates directory correct when run as a module.
  • Loading branch information
philnash committed Jul 15, 2019
1 parent 2b3f682 commit af3031d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
36 changes: 20 additions & 16 deletions src/create-twilio-function/create-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ const readdir = promisify(fs.readdir);
const copyFile = promisify(fs.copyFile);
const { COPYFILE_EXCL } = fs.constants;
const stat = promisify(fs.stat);
const path = require('path');

function createDirectory(path, dirName) {
return mkdir(path + '/' + dirName);
function createDirectory(pathName, dirName) {
return mkdir(path.join(pathName, dirName));
}

function createFile(fullPath, content) {
Expand All @@ -19,8 +20,8 @@ function createFile(fullPath, content) {
});
}

function createPackageJSON(path, name) {
const fullPath = `${path}/package.json`;
function createPackageJSON(pathName, name) {
const fullPath = path.join(pathName, 'package.json');
const packageJSON = JSON.stringify(
{
name: name,
Expand All @@ -45,37 +46,40 @@ function copyRecursively(src, dest) {
return readdir(src).then(children => {
return Promise.all(
children.map(child =>
stat(`${src}/${child}`).then(stat => {
stat(path.join(src, child)).then(stat => {
if (stat.isDirectory()) {
return mkdir(`${dest}/${child}`).then(() =>
copyRecursively(`${src}/${child}`, `${dest}/${child}`)
return mkdir(path.join(dest, child)).then(() =>
copyRecursively(path.join(src, child), path.join(dest, child))
);
} else {
return copyFile(
`./${src}/${child}`,
`${dest}/${child}`,
path.join(src, child),
path.join(dest, child),
COPYFILE_EXCL
);
).catch(console.error);
}
})
)
);
});
}

function createExampleFromTemplates(path) {
return copyRecursively('./templates', path);
function createExampleFromTemplates(pathName) {
return copyRecursively(
path.join(__dirname, '..', '..', 'templates'),
pathName
);
}

function createEnvFile(path, { accountSid, authToken }) {
const fullPath = `${path}/.env`;
function createEnvFile(pathName, { accountSid, authToken }) {
const fullPath = path.join(pathName, '.env');
const content = `ACCOUNT_SID=${accountSid}
AUTH_TOKEN=${authToken}`;
return createFile(fullPath, content);
}

function createNvmrcFile(path) {
const fullPath = `${path}/.nvmrc`;
function createNvmrcFile(pathName) {
const fullPath = path.join(pathName, '.nvmrc');
const content = versions.node;
return createFile(fullPath, content);
}
Expand Down
3 changes: 2 additions & 1 deletion src/create-twilio-function/create-gitignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const fs = require('fs');
const { promisify } = require('util');
const writeGitignore = promisify(require('gitignore').writeFile);
const open = promisify(fs.open);
const path = require('path');

function createGitignore(dirPath) {
const fullPath = `${dirPath}/.gitignore`;
const fullPath = path.join(dirPath, '.gitignore');
return open(fullPath, 'wx').then(fd => {
const stream = fs.createWriteStream(null, { fd: fd });
return writeGitignore({ type: 'Node', file: stream });
Expand Down

0 comments on commit af3031d

Please sign in to comment.