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

Commit a7a3942

Browse files
author
sw-yx
committed
add netlify dev chalk brand and reuse accessToken for speed
1 parent 970b695 commit a7a3942

File tree

8 files changed

+47
-33
lines changed

8 files changed

+47
-33
lines changed

src/commands/dev/exec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ExecCommand extends Command {
1111
console.log(
1212
`${NETLIFYDEV} Checking your site's environment variables...`
1313
);
14-
const accessToken = await this.authenticate();
14+
const accessToken = api.accessToken;
1515
const { addEnvVariables } = require("../../utils/dev");
1616
await addEnvVariables(api, site, accessToken);
1717
} else {

src/commands/dev/index.js

Lines changed: 3 additions & 3 deletions
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

Lines changed: 9 additions & 5 deletions
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

Lines changed: 22 additions & 18 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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)