Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit abac9f9

Browse files
authored
Merge pull request #81 from netlify/wait-for-dep-install
Make sure to wait for dependency install
2 parents 5c3eae9 + f923443 commit abac9f9

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/commands/functions/create.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async function getNameFromArgs(args, flags, defaultName) {
8181
message: "name your function: ",
8282
default: defaultName,
8383
type: "input",
84-
validate: val => !!val && /^[\w\-.]+$/i.test(val)
84+
validate: val => Boolean(val) && /^[\w\-.]+$/i.test(val)
8585
// make sure it is not undefined and is a valid filename.
8686
// this has some nuance i have ignored, eg crossenv and i18n concerns
8787
}
@@ -137,16 +137,15 @@ async function pickTemplate() {
137137
// ...goreg
138138
...specialCommands
139139
];
140-
} else {
141-
// only show filtered results sorted by score
142-
let ans = [
143-
...filterRegistry(jsreg, input),
144-
// ...filterRegistry(tsreg, input),
145-
// ...filterRegistry(goreg, input)
146-
...specialCommands
147-
].sort((a, b) => b.score - a.score);
148-
return ans;
149140
}
141+
// only show filtered results sorted by score
142+
let ans = [
143+
...filterRegistry(jsreg, input),
144+
// ...filterRegistry(tsreg, input),
145+
// ...filterRegistry(goreg, input)
146+
...specialCommands
147+
].sort((a, b) => b.score - a.score);
148+
return ans;
150149
}
151150
});
152151
return chosentemplate;
@@ -265,6 +264,14 @@ async function downloadFromURL(flags, args, functionsDir) {
265264
}
266265
}
267266

267+
async function installDeps(functionPath) {
268+
return new Promise((resolve, reject) => {
269+
cp.exec("npm i", { cwd: path.join(functionPath) }, () => {
270+
resolve();
271+
});
272+
});
273+
}
274+
268275
// no --url flag specified, pick from a provided template
269276
async function scaffoldFromTemplate(flags, args, functionsDir) {
270277
const chosentemplate = await pickTemplate(); // pull the rest of the metadata from the template
@@ -274,7 +281,7 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
274281
name: "chosenurl",
275282
message: "URL to clone: ",
276283
type: "input",
277-
validate: val => !!validateRepoURL(val)
284+
validate: val => Boolean(validateRepoURL(val))
278285
// make sure it is not undefined and is a valid filename.
279286
// this has some nuance i have ignored, eg crossenv and i18n concerns
280287
}
@@ -322,7 +329,7 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
322329
// this.log('from ', pathToTemplate, ' to ', functionPath)
323330
const vars = { NETLIFY_STUFF_TO_REPLACE: "REPLACEMENT" }; // SWYX: TODO
324331
let hasPackageJSON = false;
325-
copy(pathToTemplate, functionPath, vars, (err, createdFiles) => {
332+
copy(pathToTemplate, functionPath, vars, async (err, createdFiles) => {
326333
if (err) throw err;
327334
createdFiles.forEach(filePath => {
328335
this.log(`Created ${filePath}`);
@@ -340,10 +347,10 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
340347
// npm install
341348
if (hasPackageJSON) {
342349
this.log(`installing dependencies for ${name}...`);
343-
cp.exec("npm i", { cwd: path.join(functionPath) }, () => {
344-
this.log(`installing dependencies for ${name} complete `);
345-
});
350+
await installDeps(functionPath);
351+
this.log(`installing dependencies for ${name} complete `);
346352
}
353+
347354
installAddons.call(this, addons, path.resolve(functionPath));
348355
if (onComplete) onComplete(); // do whatever the template wants to do after it is scaffolded
349356
});

0 commit comments

Comments
 (0)