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

Commit 0c476d4

Browse files
authored
Merge pull request #91 from netlify/makeNetlifyDevFaster
Make netlify dev faster (reuse accessToken)
2 parents 5f6d81b + 7b23aef commit 0c476d4

File tree

9 files changed

+77
-38
lines changed

9 files changed

+77
-38
lines changed

README.md

+27-5
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,49 @@ Netlify CLI plugin for local dev experience.
44

55
## What is Netlify Dev?
66

7-
Netlify Dev brings the power of Netlify's Edge Logic layer, serverless functions and [add-on ecosystem](#using-add-ons) to your local laptop. It runs Netlify's production routing engine in a local dev server to make all redirects, proxy rules, function routes or add-on routes available locally and injects the correct environment variables from your site environment, installed add-ons or your netlify.toml file into your build and function environment.
7+
Netlify Dev brings the power of Netlify's Edge Logic layer, [serverless functions](#netlify-functions) and [add-on ecosystem](#using-add-ons) to your local machine. It runs Netlify's production routing engine in a local dev server to make all redirects, proxy rules, function routes or add-on routes available locally and injects the correct environment variables from your site environment, installed add-ons or your netlify.toml file into your build and function environment.
88

99
It automatically detects common tools like Gatsby, Hugo, React Static, Eleventy, and more, to give a zero config setup for your local dev server and can help scaffolding new functions as you work on them.
1010

11+
## Prerequisites
12+
13+
There are just two:
14+
15+
- You should be [logged in on Netlify CLI](https://www.netlify.com/docs/cli/#authentication)
16+
- Your project should be linked to a `siteID` on Netlify (using [netlify init](https://www.netlify.com/docs/cli/#continuous-deployment) or [netlify link](https://www.netlify.com/docs/cli/#linking-and-unlinking-sites)). You can confirm this has been done if you have a `.netlify` folder with a `state.json` file containing your `siteID`.
17+
18+
This is how we pull down your build environment variables and manage your addons on your local machine.
19+
1120
## Usage
1221

1322
- `netlify dev` start a local dev server for the build tool you're using
1423
- `netlify dev:exec <command>` runs a shell command within the netlify dev environment
1524
- `netlify functions:create` bootstrap a new function
1625

26+
As these commands are expected to be frequently used, it may be helpful to define aliases in your terminal (Mac: [bash](https://jonsuh.com/blog/bash-command-line-shortcuts/), [zsh](https://askubuntu.com/questions/758496/how-to-make-a-permanent-alias-in-oh-my-zsh), Windows: [doskey](https://stackoverflow.com/questions/20530996/aliases-in-windows-command-prompt), [registry](https://stackoverflow.com/questions/20530996/aliases-in-windows-command-prompt)) to your personal preference. For example:
27+
28+
```bash
29+
## ~/.zshrc
30+
alias ndeploy="netlify deploy --prod"
31+
alias nd="netlify dev"
32+
alias ndl="netlify dev --live"
33+
alias nfc="netlify functions:create"
34+
alias ndx="netlify dev:exec "
35+
```
36+
1737
## Using the beta
1838

1939
Currently the Netlify dev plugin is in private beta. You'll need to follow these steps to enable it:
2040

2141
Make sure Netlify CLI is installed and up to date:
2242

23-
```
43+
```bash
2444
npm install -g netlify-cli
2545
```
2646

2747
Then clone and activate the plugin:
2848

29-
```
49+
```bash
3050
git clone [email protected]:netlify/netlify-dev-plugin.git
3151
cd netlify-dev-plugin
3252
npm install
@@ -161,7 +181,7 @@ Add-ons are a way for Netlify users to extend the functionality of their Jamstac
161181
162182
To try out an add-on with Netlify dev, run the `netlify addons:create` command:
163183
164-
```
184+
```bash
165185
netlify addons:create fauna
166186
```
167187
@@ -175,8 +195,10 @@ After you have installed an add-on, it will be visible with the `netlify addons:
175195
176196
To share your ongoing dev session with a coworker, just run Netlify Dev with a `--live` flag:
177197
178-
```
198+
```bash
179199
netlify dev --live
180200
```
181201
182202
You will get a URL that looks like `https://clever-cray-2aa156-6639f3.netlify.live/`. This can be accessed by anyone as long as you keep your session open.
203+
204+
> Note: there are currently known issues with ending the live session alongside your webdevserver. We are working on fixing it. In the mean time you can run `ps aux | grep live-tunnel` and kill these sessions manually.

src/commands/dev/exec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ class ExecCommand extends Command {
88
async run() {
99
const { site, api } = this.netlify;
1010
if (site.id) {
11-
const accessToken = await this.authenticate();
11+
console.log(
12+
`${NETLIFYDEV} Checking your site's environment variables...`
13+
);
14+
const accessToken = api.accessToken;
1215
const { addEnvVariables } = require("../../utils/dev");
1316
await addEnvVariables(api, site, accessToken);
1417
} else {

src/commands/dev/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ function startDevServer(settings, log, error) {
170170

171171
class DevCommand extends Command {
172172
async run() {
173+
this.log(`${NETLIFYDEV} Starting Netlify Dev...`);
173174
const { flags, args } = this.parse(DevCommand);
174175
const { api, site, config } = this.netlify;
175176
const functionsDir =
@@ -180,7 +181,6 @@ class DevCommand extends Command {
180181

181182
let accessToken = api.accessToken;
182183
if (site.id && !flags.offline) {
183-
accessToken = await this.authenticate();
184184
const { addEnvVariables } = require("../../utils/dev");
185185
addonUrls = await addEnvVariables(api, site, accessToken);
186186
}
@@ -189,7 +189,7 @@ class DevCommand extends Command {
189189
let settings = serverSettings(config.dev);
190190
if (!(settings && settings.command)) {
191191
this.log(
192-
"[Netlify Dev] No dev server detected, using simple static server"
192+
`${NETLIFYDEV} No dev server detected, using simple static server`
193193
);
194194
const dist =
195195
(config.dev && config.dev.publish) ||
@@ -240,7 +240,7 @@ class DevCommand extends Command {
240240
live: flags.live || false
241241
});
242242

243-
const banner = chalk.bold(`Netlify Dev Server now ready on ${url}`);
243+
const banner = chalk.bold(`${NETLIFYDEV} Server now ready on ${url}`);
244244
this.log(
245245
boxen(banner, {
246246
padding: 1,

src/commands/functions/build.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const fs = require("fs");
22
const { flags } = require("@oclif/command");
33
const Command = require("@netlify/cli-utils");
44
const { zipFunctions } = require("@netlify/zip-it-and-ship-it");
5+
const chalk = require("chalk");
6+
const NETLIFYDEV = `[${chalk.cyan("Netlify Dev")}]`;
57

68
class FunctionsBuildCommand extends Command {
79
async run() {
@@ -12,27 +14,29 @@ class FunctionsBuildCommand extends Command {
1214
const dst = flags.functions || config.build.functions;
1315

1416
if (src === dst) {
15-
this.log("Source and destination for function build can't be the same");
17+
this.log(
18+
`${NETLIFYDEV} Source and destination for function build can't be the same`
19+
);
1620
process.exit(1);
1721
}
1822

1923
if (!src || !dst) {
2024
if (!src)
2125
this.log(
22-
"You must specify a source folder with a --src flag or a functionsSource field in your config"
26+
`${NETLIFYDEV} Error: You must specify a source folder with a --src flag or a functionsSource field in your config`
2327
);
2428
if (!dst)
2529
this.log(
26-
"You must specify a destination functions folder with a --functions flag or a functions field in your config"
30+
`${NETLIFYDEV} Error: You must specify a destination functions folder with a --functions flag or a functions field in your config`
2731
);
2832
process.exit(1);
2933
}
3034

3135
fs.mkdirSync(dst, { recursive: true });
3236

33-
this.log("Building functions");
37+
this.log(`${NETLIFYDEV} Building functions`);
3438
zipFunctions(src, dst, { skipGo: true });
35-
this.log("Functions built to ", dst);
39+
this.log(`${NETLIFYDEV} Functions built to `, dst);
3640
}
3741
}
3842

src/commands/functions/create.js

+22-18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const cp = require("child_process");
1212
const { createAddon } = require("netlify/src/addons");
1313
const ora = require("ora");
1414
const { track } = require("@netlify/cli-utils/src/utils/telemetry");
15+
const chalk = require("chalk");
16+
const NETLIFYDEV = `[${chalk.cyan("Netlify Dev")}]`;
1517

1618
const templatesDir = path.resolve(__dirname, "../../functions-templates");
1719

@@ -161,7 +163,6 @@ async function pickTemplate() {
161163
const filteredTemplateNames = filteredTemplates.map(x =>
162164
input ? x.string : x
163165
);
164-
// console.log({ filteredTemplateNames })
165166
return registry
166167
.filter(t => filteredTemplateNames.includes(t.name + t.description))
167168
.map(t => {
@@ -203,15 +204,17 @@ function ensureFunctionDirExists(flags, config) {
203204
const functionsDir =
204205
flags.functions || (config.build && config.build.functions);
205206
if (!functionsDir) {
206-
this.log("No functions folder specified in netlify.toml or as an argument");
207+
this.log(
208+
`${NETLIFYDEV} No functions folder specified in netlify.toml or as an argument`
209+
);
207210
process.exit(1);
208211
}
209212
if (!fs.existsSync(functionsDir)) {
210213
this.log(
211-
`functions folder ${functionsDir} specified in netlify.toml but folder not found, creating it...`
214+
`${NETLIFYDEV} functions folder ${functionsDir} specified in netlify.toml but folder not found, creating it...`
212215
);
213216
fs.mkdirSync(functionsDir);
214-
this.log(`functions folder ${functionsDir} created`);
217+
this.log(`${NETLIFYDEV} functions folder ${functionsDir} created`);
215218
}
216219
return functionsDir;
217220
}
@@ -227,7 +230,7 @@ async function downloadFromURL(flags, args, functionsDir) {
227230
fs.lstatSync(fnFolder + ".js").isFile()
228231
) {
229232
this.log(
230-
`A single file version of the function ${name} already exists at ${fnFolder}.js`
233+
`${NETLIFYDEV} Warning: A single file version of the function ${name} already exists at ${fnFolder}.js. Terminating without further action.`
231234
);
232235
process.exit(1);
233236
}
@@ -254,9 +257,11 @@ async function downloadFromURL(flags, args, functionsDir) {
254257
})
255258
);
256259

257-
this.log(`installing dependencies for ${nameToUse}...`);
260+
this.log(`${NETLIFYDEV} Installing dependencies for ${nameToUse}...`);
258261
cp.exec("npm i", { cwd: path.join(functionsDir, nameToUse) }, () => {
259-
this.log(`installing dependencies for ${nameToUse} complete `);
262+
this.log(
263+
`${NETLIFYDEV} Installing dependencies for ${nameToUse} complete `
264+
);
260265
});
261266

262267
// read, execute, and delete function template file if exists
@@ -296,16 +301,13 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
296301
try {
297302
await downloadFromURL.call(this, flags, args, functionsDir);
298303
} catch (err) {
299-
console.error("Error downloading from URL: " + flags.url);
304+
console.error(`${NETLIFYDEV} Error downloading from URL: ` + flags.url);
300305
console.error(err);
301306
process.exit(1);
302307
}
303308
} else if (chosentemplate === "report") {
304309
console.log(
305-
"opening in browser: https://github.com/netlify/netlify-dev-plugin/issues/new"
306-
);
307-
require("../../utils/openBrowser.js")(
308-
"https://github.com/netlify/netlify-dev-plugin/issues/new"
310+
`${NETLIFYDEV} Open in browser: https://github.com/netlify/netlify-dev-plugin/issues/new`
309311
);
310312
} else {
311313
const {
@@ -323,7 +325,7 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
323325
}
324326

325327
const name = await getNameFromArgs(args, flags, templateName);
326-
this.log(`Creating function ${name}`);
328+
this.log(`${NETLIFYDEV} Creating function ${name}`);
327329
const functionPath = ensureFunctionPathIsOk.call(
328330
this,
329331
functionsDir,
@@ -338,7 +340,7 @@ async function scaffoldFromTemplate(flags, args, functionsDir) {
338340
copy(pathToTemplate, functionPath, vars, async (err, createdFiles) => {
339341
if (err) throw err;
340342
createdFiles.forEach(filePath => {
341-
this.log(`Created ${filePath}`);
343+
this.log(`${NETLIFYDEV} Created ${filePath}`);
342344
require("fs").chmodSync(path.resolve(filePath), 0o777);
343345
if (filePath.includes("package.json")) hasPackageJSON = true;
344346
});
@@ -377,12 +379,12 @@ async function installAddons(addons = [], fnPath) {
377379
);
378380
return false;
379381
}
380-
console.log("checking Netlify APIs...");
382+
console.log(`${NETLIFYDEV} checking Netlify APIs...`);
381383

382384
return api.getSite({ siteId }).then(async siteData => {
383-
const accessToken = await this.authenticate();
385+
const accessToken = api.accessToken;
384386
const arr = addons.map(({ addonName, addonDidInstall }) => {
385-
console.log("installing addon: " + addonName);
387+
console.log(`${NETLIFYDEV} installing addon: ` + addonName);
386388
// will prompt for configs if not supplied - we do not yet allow for addon configs supplied by `netlify functions:create` command and may never do so
387389
return createSiteAddon(
388390
accessToken,
@@ -423,7 +425,9 @@ async function installAddons(addons = [], fnPath) {
423425
function ensureFunctionPathIsOk(functionsDir, flags, name) {
424426
const functionPath = path.join(functionsDir, name);
425427
if (fs.existsSync(functionPath)) {
426-
this.log(`Function ${functionPath} already exists, cancelling...`);
428+
this.log(
429+
`${NETLIFYDEV} Function ${functionPath} already exists, cancelling...`
430+
);
427431
process.exit(1);
428432
}
429433
return functionPath;

src/commands/functions/serve.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const { Command, flags } = require("@oclif/command");
2+
const chalk = require("chalk");
3+
const NETLIFYDEV = `[${chalk.cyan("Netlify Dev")}]`;
24

35
class FunctionsServeCommand extends Command {
46
async run() {
5-
this.log(`serve a function`);
7+
this.log(`${NETLIFYDEV} NOT IMPLEMENTED YET: serve a function`);
68
}
79
}
810

src/commands/functions/update.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const { Command, flags } = require("@oclif/command");
2+
const chalk = require("chalk");
3+
const NETLIFYDEV = `[${chalk.cyan("Netlify Dev")}]`;
24

35
class FunctionsUpdateCommand extends Command {
46
async run() {
5-
this.log(`update a function`);
7+
this.log(`${NETLIFYDEV} NOT IMPLEMENTED YET: update a function`);
68
}
79
}
810

src/live-tunnel.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const os = require("os");
44
const path = require("path");
55
const execa = require("execa");
66
const { fetchLatest, updateAvailable } = require("gh-release-fetch");
7+
const chalk = require("chalk");
8+
const NETLIFYDEV = `[${chalk.cyan("Netlify Dev")}]`;
79

810
async function createTunnel(siteId, netlifyApiToken, log) {
911
await installTunnelClient(log);
@@ -14,7 +16,7 @@ async function createTunnel(siteId, netlifyApiToken, log) {
1416
);
1517
process.exit(1);
1618
}
17-
log("Creating Live Tunnel for " + siteId);
19+
log(`${NETLIFYDEV} Creating Live Tunnel for ` + siteId);
1820
const url = `https://api.netlify.com/api/v1/live_sessions?site_id=${siteId}`;
1921

2022
const response = await fetch(url, {
@@ -71,7 +73,7 @@ async function installTunnelClient(log) {
7173
return;
7274
}
7375

74-
log("Installing Live Tunnel Client");
76+
log(`${NETLIFYDEV} Installing Live Tunnel Client`);
7577

7678
const win = isWindows();
7779
const platform = win ? "windows" : process.platform;

src/utils/dev.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const NETLIFYDEV = `[${chalk.cyan("Netlify Dev")}]`;
99
*
1010
* ```
1111
* // usage example
12-
* const { site } = this.netlify
12+
* const { site, api } = this.netlify
1313
* if (site.id) {
14-
* const accessToken = await this.authenticate()
14+
* const accessToken = api.accessToken
1515
* const addonUrls = await addEnvVariables(site, accessToken)
1616
* // addonUrls is only for startProxy in netlify dev:index
1717
* }

0 commit comments

Comments
 (0)