Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add .ts files to prettier config #6204

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"config": {
"eslint": "--cache --format=codeframe --max-warnings=0 \"{src,scripts,tests,.github}/**/*.{mjs,cjs,js,md,html}\" \"*.{mjs,cjs,js,md,html}\" \".*.{mjs,cjs,js,md,html}\"",
"prettier": "--ignore-path .eslintignore --loglevel=warn \"{src,tools,scripts,tests,.github}/**/*.{mjs,cjs,js,mts,md,yml,json,html}\" \"*.{mjs,cjs,js,mts,yml,json,html}\" \".*.{mjs,cjs,js,yml,json,html}\" \"!CHANGELOG.md\" \"!**/*/package-lock.json\" \"!.github/**/*.md\""
"prettier": "--ignore-path .eslintignore --loglevel=warn \"{src,tools,scripts,tests,.github}/**/*.{mjs,cjs,js,mts,md,yml,json,html,ts}\" \"*.{mjs,cjs,js,mts,yml,json,html,ts}\" \".*.{mjs,cjs,js,yml,json,html,ts}\" \"!CHANGELOG.md\" \"!**/*/package-lock.json\" \"!.github/**/*.md\""
},
"dependencies": {
"@bugsnag/js": "7.20.2",
Expand Down
1 change: 0 additions & 1 deletion src/commands/blobs/blobs-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface Options extends OptionValues {
input?: string
}


export const blobsSet = async (
storeName: string,
key: string,
Expand Down
193 changes: 95 additions & 98 deletions src/commands/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,110 +230,107 @@ export const dev = async (options: OptionValues, command: BaseCommand) => {
printBanner({ url })
}


export const createDevCommand = (program: BaseCommand) => {
createDevExecCommand(program)

return (
program
.command('dev')
.alias('develop')
.description(
`Local dev server\nThe dev command will run a local dev server with Netlify's proxy and redirect rules`,
)
.option('-c ,--command <command>', 'command to run')
.option(
'--context <context>',
'Specify a deploy context or branch for environment variables (contexts: "production", "deploy-preview", "branch-deploy", "dev")',
normalizeContext,
)
.option('-p ,--port <port>', 'port of netlify dev', (value) => Number.parseInt(value))
.addOption(
new Option('--targetPort <port>', 'Old, prefer --target-port. Port of target app server')
.argParser((value) => Number.parseInt(value))
.hideHelp(true),
)
.addOption(new Option('--no-open', 'disables the automatic opening of a browser window'))
.option('--target-port <port>', 'port of target app server', (value) => Number.parseInt(value))
.option('--framework <name>', 'framework to use. Defaults to #auto which automatically detects a framework')
.option('-d ,--dir <path>', 'dir with static files')
.option('-f ,--functions <folder>', 'specify a functions folder to serve')
.option('-o ,--offline', 'disables any features that require network access')
.option(
'-l, --live [subdomain]',
'start a public live session; optionally, supply a subdomain to generate a custom URL',
false,
)
.addOption(
new Option('--functionsPort <port>', 'Old, prefer --functions-port. Port of functions server')
.argParser((value) => Number.parseInt(value))
.hideHelp(true),
)
.option('--functions-port <port>', 'port of functions server', (value) => Number.parseInt(value))
.addOption(
new Option(
'--geo <mode>',
'force geolocation data to be updated, use cached data from the last 24h if found, or use a mock location',
)
.choices(['cache', 'mock', 'update'])
.default('cache'),
)
.addOption(
new Option(
'--country <geoCountry>',
'Two-letter country code (https://ntl.fyi/country-codes) to use as mock geolocation (enables --geo=mock automatically)',
).argParser(getGeoCountryArgParser('netlify dev --geo=mock --country=FR')),
)
.addOption(
new Option('--staticServerPort <port>', 'port of the static app server used when no framework is detected')
.argParser((value) => Number.parseInt(value))
.hideHelp(),
return program
.command('dev')
.alias('develop')
.description(
`Local dev server\nThe dev command will run a local dev server with Netlify's proxy and redirect rules`,
)
.option('-c ,--command <command>', 'command to run')
.option(
'--context <context>',
'Specify a deploy context or branch for environment variables (contexts: "production", "deploy-preview", "branch-deploy", "dev")',
normalizeContext,
)
.option('-p ,--port <port>', 'port of netlify dev', (value) => Number.parseInt(value))
.addOption(
new Option('--targetPort <port>', 'Old, prefer --target-port. Port of target app server')
.argParser((value) => Number.parseInt(value))
.hideHelp(true),
)
.addOption(new Option('--no-open', 'disables the automatic opening of a browser window'))
.option('--target-port <port>', 'port of target app server', (value) => Number.parseInt(value))
.option('--framework <name>', 'framework to use. Defaults to #auto which automatically detects a framework')
.option('-d ,--dir <path>', 'dir with static files')
.option('-f ,--functions <folder>', 'specify a functions folder to serve')
.option('-o ,--offline', 'disables any features that require network access')
.option(
'-l, --live [subdomain]',
'start a public live session; optionally, supply a subdomain to generate a custom URL',
false,
)
.addOption(
new Option('--functionsPort <port>', 'Old, prefer --functions-port. Port of functions server')
.argParser((value) => Number.parseInt(value))
.hideHelp(true),
)
.option('--functions-port <port>', 'port of functions server', (value) => Number.parseInt(value))
.addOption(
new Option(
'--geo <mode>',
'force geolocation data to be updated, use cached data from the last 24h if found, or use a mock location',
)
.addOption(
new Option(
'-e, --edgeInspect [address]',
'Old, prefer --edge-inspect. Enable the V8 Inspector Protocol for Edge Functions, with an optional address in the host:port format',
)
.conflicts('edgeInspectBrk')
.argParser(validateShortFlagArgs)
.hideHelp(true),
.choices(['cache', 'mock', 'update'])
.default('cache'),
)
.addOption(
new Option(
'--country <geoCountry>',
'Two-letter country code (https://ntl.fyi/country-codes) to use as mock geolocation (enables --geo=mock automatically)',
).argParser(getGeoCountryArgParser('netlify dev --geo=mock --country=FR')),
)
.addOption(
new Option('--staticServerPort <port>', 'port of the static app server used when no framework is detected')
.argParser((value) => Number.parseInt(value))
.hideHelp(),
)
.addOption(
new Option(
'-e, --edgeInspect [address]',
'Old, prefer --edge-inspect. Enable the V8 Inspector Protocol for Edge Functions, with an optional address in the host:port format',
)
.addOption(
new Option(
'-e, --edge-inspect [address]',
'enable the V8 Inspector Protocol for Edge Functions, with an optional address in the host:port format',
)
.conflicts('edgeInspectBrk')
.argParser(validateShortFlagArgs),
.conflicts('edgeInspectBrk')
.argParser(validateShortFlagArgs)
.hideHelp(true),
)
.addOption(
new Option(
'-e, --edge-inspect [address]',
'enable the V8 Inspector Protocol for Edge Functions, with an optional address in the host:port format',
)
.addOption(
new Option(
'-E, --edgeInspectBrk [address]',
'Old, prefer --edge-inspect-brk. Enable the V8 Inspector Protocol for Edge Functions and pause execution on the first line of code, with an optional address in the host:port format',
)
.conflicts('edgeInspect')
.hideHelp(true)
.argParser(validateShortFlagArgs),
.conflicts('edgeInspectBrk')
.argParser(validateShortFlagArgs),
)
.addOption(
new Option(
'-E, --edgeInspectBrk [address]',
'Old, prefer --edge-inspect-brk. Enable the V8 Inspector Protocol for Edge Functions and pause execution on the first line of code, with an optional address in the host:port format',
)
.addOption(
new Option(
'-E, --edge-inspect-brk [address]',
'enable the V8 Inspector Protocol for Edge Functions and pause execution on the first line of code, with an optional address in the host:port format',
)
.conflicts('edgeInspect')
.argParser(validateShortFlagArgs),
.conflicts('edgeInspect')
.hideHelp(true)
.argParser(validateShortFlagArgs),
)
.addOption(
new Option(
'-E, --edge-inspect-brk [address]',
'enable the V8 Inspector Protocol for Edge Functions and pause execution on the first line of code, with an optional address in the host:port format',
)
.addExamples([
'netlify dev',
'netlify dev -d public',
'netlify dev -c "hugo server -w" --target-port 1313',
'netlify dev --context production',
'netlify dev --edge-inspect',
'netlify dev --edge-inspect=127.0.0.1:9229',
'netlify dev --edge-inspect-brk',
'netlify dev --edge-inspect-brk=127.0.0.1:9229',
'BROWSER=none netlify dev # disable browser auto opening',
])
.action(dev)
)
.conflicts('edgeInspect')
.argParser(validateShortFlagArgs),
)
.addExamples([
'netlify dev',
'netlify dev -d public',
'netlify dev -c "hugo server -w" --target-port 1313',
'netlify dev --context production',
'netlify dev --edge-inspect',
'netlify dev --edge-inspect=127.0.0.1:9229',
'netlify dev --edge-inspect-brk',
'netlify dev --edge-inspect-brk=127.0.0.1:9229',
'BROWSER=none netlify dev # disable browser auto opening',
])
.action(dev)
}
1 change: 0 additions & 1 deletion src/commands/logs/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type BaseCommand from '../base-command.js'

import { CLI_LOG_LEVEL_CHOICES_STRING, LOG_LEVELS, LOG_LEVELS_LIST } from './log-levels.js'


function getLog(logData: { level: string; message: string }) {
let logString = ''
switch (logData.level) {
Expand Down
20 changes: 10 additions & 10 deletions src/functions-templates/typescript/abtest/{{name}}.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import type { Context } from "https://edge.netlify.com";
import type { Context } from 'https://edge.netlify.com'

export default async (request: Request, context: Context) => {
// look for existing "test_bucket" cookie
const bucketName = "test_bucket";
const bucket = context.cookies.get(bucketName);
const bucketName = 'test_bucket'
const bucket = context.cookies.get(bucketName)

// return here if we find a cookie
if (bucket) {
return new Response(`Welcome back! You were assigned ${bucketName} **${bucket}** when you last visited the site!`);
return new Response(`Welcome back! You were assigned ${bucketName} **${bucket}** when you last visited the site!`)
}

// if no "test_bucket" cookie is found, assign the user to a bucket
// in this example we're using two buckets (a, b) with an equal weighting of 50/50
const weighting = 0.5;
const weighting = 0.5

// get a random number between (0-1)
// this is a basic example and you may want to experiment
const random = Math.random();
const newBucketValue = random <= weighting ? "a" : "b";
const random = Math.random()
const newBucketValue = random <= weighting ? 'a' : 'b'

// set the new "test_bucket" cookie
context.cookies.set({
name: bucketName,
value: newBucketValue,
});
})

return new Response(
`Congratulations! You have been assigned ${bucketName} **${newBucketValue}**. View your browser cookies to check it out!`,
);
};
)
}
11 changes: 5 additions & 6 deletions src/functions-templates/typescript/geolocation/{{name}}.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from "https://edge.netlify.com";
import { Context } from 'https://edge.netlify.com'

export default async (request: Request, context: Context) => {
export default async (request: Request, context: Context) =>
// Here's what's available on context.geo

// context: {
Expand All @@ -17,8 +17,7 @@ export default async (request: Request, context: Context) => {
// }
// }

return Response.json({
Response.json({
geo: context.geo,
header: request.headers.get("x-nf-geo"),
});
};
header: request.headers.get('x-nf-geo'),
})
7 changes: 3 additions & 4 deletions src/functions-templates/typescript/json/{{name}}.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Context } from "https://edge.netlify.com";
import type { Context } from 'https://edge.netlify.com'

export default async (request: Request, context: Context) => {
return Response.json({ hello: "world", location: context.geo.city });
};
export default async (request: Request, context: Context) =>
Response.json({ hello: 'world', location: context.geo.city })
12 changes: 6 additions & 6 deletions src/functions-templates/typescript/log/{{name}}.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Context } from "https://edge.netlify.com";
import type { Context } from 'https://edge.netlify.com'

export default async (request: Request, context: Context) => {
console.log(`There was a request from ${context.geo.city} to ${request.url}`);
console.log(`There was a request from ${context.geo.city} to ${request.url}`)

return new Response("The request to this URL was logged", {
headers: { "content-type": "text/html" },
});
};
return new Response('The request to this URL was logged', {
headers: { 'content-type': 'text/html' },
})
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { schedule } from '@netlify/functions';
import { schedule } from '@netlify/functions'

// To learn about scheduled functions and supported cron extensions,
// To learn about scheduled functions and supported cron extensions,
// see: https://ntl.fyi/sched-func
export const handler = schedule("@hourly", async (event) => {
const eventBody = JSON.parse(event.body);
console.log(`Next function run at ${eventBody.next_run}.`);
export const handler = schedule('@hourly', async (event) => {
const eventBody = JSON.parse(event.body)
console.log(`Next function run at ${eventBody.next_run}.`)

return {
statusCode: 200
};
});
return {
statusCode: 200,
}
})
30 changes: 15 additions & 15 deletions src/functions-templates/typescript/set-cookies/{{name}}.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import type { Context } from "https://edge.netlify.com";
import type { Context } from 'https://edge.netlify.com'

export default async (request: Request, context: Context) => {
const url = new URL(request.url);
const url = new URL(request.url)

switch (url.searchParams.get("action")) {
case "set":
switch (url.searchParams.get('action')) {
case 'set':
context.cookies.set({
name: "action",
value: "hello",
});
name: 'action',
value: 'hello',
})

return new Response('Cookie value has been set. Reload this page without the "action" parameter to see it.');
return new Response('Cookie value has been set. Reload this page without the "action" parameter to see it.')

case "clear":
context.cookies.delete("action");
case 'clear':
context.cookies.delete('action')

return new Response(
'Cookie value has been cleared. Reload this page without the "action" parameter to see the new state.',
);
)
}

const value = context.cookies.get("action");
const value = context.cookies.get('action')
const message = value
? `Cookie value is "${value}". You can clear it by using "?action=clear".`
: 'Cookie has not been set. You can do so by adding "?action=set" to the URL.';
: 'Cookie has not been set. You can do so by adding "?action=set" to the URL.'

return new Response(message);
};
return new Response(message)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context } from "https://edge.netlify.com";
import type { Context } from 'https://edge.netlify.com'

export default async (request: Request, context: Context) => {
request.headers.set("X-Your-Custom-Header", "Your custom header value");
};
request.headers.set('X-Your-Custom-Header', 'Your custom header value')
}
Loading
Loading